summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-10-26 10:00:20 +0200
committerkdx <kikoodx@paranoici.org>2023-10-26 10:00:20 +0200
commitf19696f98cc21b78d9f7e1d311e102632a26bbe1 (patch)
treec827ae9d4413a0490409a023359bccbbb07d9abd
parent37aa3281db338762909807a58dba616a908f43e3 (diff)
downloadcarrenoirsurfondblanc-f19696f98cc21b78d9f7e1d311e102632a26bbe1.tar.gz
init codeHEADmain
-rwxr-xr-xbuild.sh2
-rw-r--r--inc/_.h6
-rw-r--r--inc/r.h4
-rw-r--r--inc/ttf.h4
-rw-r--r--src/main.c9
-rw-r--r--src/r.c70
-rw-r--r--src/ttf.c18
7 files changed, 110 insertions, 3 deletions
diff --git a/build.sh b/build.sh
index 4a42936..535865a 100755
--- a/build.sh
+++ b/build.sh
@@ -4,4 +4,4 @@ gcc -std=c99 -Wall -Wextra \
-iquoteinc \
-include_.h \
src/*.c \
- -lSDL2 -lSDL2_mixer
+ -lSDL2 -lSDL2_ttf
diff --git a/inc/_.h b/inc/_.h
index 4a82eb8..0fbbb38 100644
--- a/inc/_.h
+++ b/inc/_.h
@@ -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())
diff --git a/inc/r.h b/inc/r.h
new file mode 100644
index 0000000..0cb7cc7
--- /dev/null
+++ b/inc/r.h
@@ -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);
diff --git a/src/main.c b/src/main.c
index 7c8bfdc..37db50d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}
diff --git a/src/r.c b/src/r.c
new file mode 100644
index 0000000..582c76d
--- /dev/null
+++ b/src/r.c
@@ -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();
+}