diff options
author | kdx <kikoodx@paranoici.org> | 2023-10-26 10:00:20 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-10-26 10:00:20 +0200 |
commit | f19696f98cc21b78d9f7e1d311e102632a26bbe1 (patch) | |
tree | c827ae9d4413a0490409a023359bccbbb07d9abd | |
parent | 37aa3281db338762909807a58dba616a908f43e3 (diff) | |
download | carrenoirsurfondblanc-main.tar.gz |
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | inc/_.h | 6 | ||||
-rw-r--r-- | inc/r.h | 4 | ||||
-rw-r--r-- | inc/ttf.h | 4 | ||||
-rw-r--r-- | src/main.c | 9 | ||||
-rw-r--r-- | src/r.c | 70 | ||||
-rw-r--r-- | src/ttf.c | 18 |
7 files changed, 110 insertions, 3 deletions
@@ -4,4 +4,4 @@ gcc -std=c99 -Wall -Wextra \ -iquoteinc \ -include_.h \ src/*.c \ - -lSDL2 -lSDL2_mixer + -lSDL2 -lSDL2_ttf @@ -1,11 +1,15 @@ #pragma once +#include "r.h" +#include "ttf.h" #include <stdio.h> #include <stdlib.h> +#include <unistd.h> -#define defer(x) do { if (atexit(X)) { X(); panic("defer"); } } while (0); +#define defer(X) do { if (atexit(X)) { X(); panic("defer"); } } while (0); #define auto __auto_type #define foreach(E, L) for (auto E = (L); E != NULL; E = E->next) #define forloop(I, F, T) for (auto I = (F); I < (T); I++) #define with(I, T) auto I = (T); if (I) #define plog(...) printf("\x1b[94mLOG %s:%s:%d\x1b[0m \t", __FILE_NAME__, __FUNCTION__, __LINE__), printf(__VA_ARGS__), putchar('\n') #define panic(...) plog("\x1b[31m"__VA_ARGS__), printf("\x1b[0m"), exit(EXIT_FAILURE) +#define sdlerr() plog("%s", SDL_GetError()) @@ -0,0 +1,4 @@ +#pragma once + +int r_init(const char *title, int width, int height); +void r_deinit(void); diff --git a/inc/ttf.h b/inc/ttf.h new file mode 100644 index 0000000..8e4c9af --- /dev/null +++ b/inc/ttf.h @@ -0,0 +1,4 @@ +#pragma once + +int ttf_init(void); +void ttf_deinit(void); @@ -1,6 +1,13 @@ int main(int argc, char **argv) { - panic("skill issue"); + if (r_init("carré noir sur fond blanc", 640, 480)) + panic("r_init failed"); + defer(r_deinit); + + if (ttf_init()) + panic("ttf_init failed"); + defer(ttf_deinit); + return 0; } @@ -0,0 +1,70 @@ +#include <SDL2/SDL.h> + +static SDL_Window *win; +static SDL_Renderer *ren; + +int +r_init(const char *title, int width, int height) +{ + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { + sdlerr(); + r_deinit(); + return -1; + } + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { + sdlerr(); + r_deinit(); + return -1; + } + if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) { + sdlerr(); + r_deinit(); + return -1; + } + + win = SDL_CreateWindow(title, + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + width, height, SDL_WINDOW_RESIZABLE); + if (win == NULL) { + sdlerr(); + r_deinit(); + return -1; + } + + ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + if (ren == NULL) { + sdlerr(); + r_deinit(); + return -1; + } + + char *const basepath = SDL_GetBasePath(); + if (basepath == NULL) { + sdlerr(); + r_deinit(); + return -1; + } + const int chdir_rv = chdir(basepath); + SDL_free(basepath); + if (chdir_rv < 0) { + plog("%m"); + r_deinit(); + return -1; + } + + return 0; +} + +void +r_deinit(void) +{ + if (ren != NULL) { + SDL_DestroyRenderer(ren); + ren = NULL; + } + if (win != NULL) { + SDL_DestroyWindow(win); + win = NULL; + } + SDL_Quit(); +} diff --git a/src/ttf.c b/src/ttf.c new file mode 100644 index 0000000..cf09a7c --- /dev/null +++ b/src/ttf.c @@ -0,0 +1,18 @@ +#include <SDL2/SDL_ttf.h> + +int +ttf_init(void) +{ + if (TTF_Init()) { + sdlerr(); + return -1; + } + + return 0; +} + +void +ttf_deinit(void) +{ + TTF_Quit(); +} |