From 9506c24b9303d4a476e31711ad854cee006f3169 Mon Sep 17 00:00:00 2001 From: kdx Date: Sat, 25 Mar 2023 04:45:24 +0100 Subject: bump to C23 --- README.md | 2 +- compile_flags.txt | 2 +- headers/TZR.h | 1 + headers/TZR_render.h | 8 ++++++++ headers/TZR_resource.h | 4 ++++ main.c | 33 ++++++++++++++++++++------------- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 186c566..3b39710 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # introduction à TZR -TZR est un moteur de jeu minimaliste écrit en C99, conçu pour être une +TZR est un moteur de jeu minimaliste écrit en C23, conçu pour être une abstration simple et puissante à des concepts avancés de la SDL. ## création de projet diff --git a/compile_flags.txt b/compile_flags.txt index 7077ba2..28a6bfe 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,5 +1,5 @@ -Wall -Wextra -Wno-initializer-overrides --std=c99 +-std=c2x -Iheaders diff --git a/headers/TZR.h b/headers/TZR.h index 262b936..1af2f40 100644 --- a/headers/TZR.h +++ b/headers/TZR.h @@ -21,6 +21,7 @@ /* Should be called only once before usage of any other function unless * otherwise specified. On error this calls TZR_Quit and returns -1. * `config` will be duplicated internally and can be safely discarded. */ +[[nodiscard]] int _TZR_Init(const TZR_Config *config); /* Destroy and free everything created by TZR. */ diff --git a/headers/TZR_render.h b/headers/TZR_render.h index 5732770..b3f2ba3 100644 --- a/headers/TZR_render.h +++ b/headers/TZR_render.h @@ -5,28 +5,35 @@ /* All draw calls should happen between TZR_DrawBegin and TZR_DrawEnd. * Return -1 on error. */ +[[nodiscard]] int TZR_DrawBegin(void); /* Return -1 on error. */ +[[nodiscard]] int TZR_DrawEnd(void); /* Return -1 on error. */ #define TZR_DrawSetColor(...) _TZR_DrawSetColor(&(const TZR_Color){ \ .r=-1.0f, .g=-1.0f, .b=-1.0f, .a=-1.0f, ._=0, __VA_ARGS__ }) +[[nodiscard]] int _TZR_DrawSetColor(const TZR_Color *color); /* Return -1 on error. */ +[[nodiscard]] int TZR_DrawClear(void); /* Return -1 on error. Draw point on `x`;`y` in the framebuffer. */ +[[nodiscard]] int TZR_DrawPoint(int x, int y); /* Return -1 on error. Draw line between `x0`;`y0` and `x1`;`y1` in the * framebuffer. */ +[[nodiscard]] int TZR_DrawLine(int x0, int y0, int x1, int y1); /* Return -1 on error. Draw rectangle at `x`;`y` position of size `w`x`h` in the * framebuffer. */ +[[nodiscard]] int TZR_DrawRectangle(bool fill, int x, int y, int w, int h); /* Return -1 on error. Draw texture ressource `id` at `x`;`y` position of @@ -34,4 +41,5 @@ int TZR_DrawRectangle(bool fill, int x, int y, int w, int h); #define TZR_DrawImage(...) _TZR_DrawImage(&(const TZR_DrawImageArgs){ \ .x=0, .y=0, .ix=0, .iy=0, .w=INT_MIN, .h=INT_MIN, .r=0.0f, .sx=1.0f, \ .sy=1.0f, .center=false, ._=0, __VA_ARGS__ }) +[[nodiscard]] int _TZR_DrawImage(const TZR_DrawImageArgs *args); diff --git a/headers/TZR_resource.h b/headers/TZR_resource.h index d9a68c4..06f0763 100644 --- a/headers/TZR_resource.h +++ b/headers/TZR_resource.h @@ -2,10 +2,13 @@ #include "TZR_types.h" /* Return 0 on error. */ +[[nodiscard]] TZR_Uint TZR_LoadResourceFromMemory(TZR_ResourceType type, const void *data, int size); +[[nodiscard]] TZR_Uint TZR_LoadResourceTyped(TZR_ResourceType type, const char *path); /* Return 0 on error; try to autodetect resource type from file extension. */ +[[nodiscard]] TZR_Uint TZR_LoadResource(const char *path); /* Doesn't handle invalid ID. Pointer might get invalidated by resource @@ -35,5 +38,6 @@ int TZR_ReloadResource(TZR_Uint id); void TZR_ResourcesWatch(void); /* Used internaly. */ +[[nodiscard]] int TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size); void TZR_DestroyResource(TZR_Resource *res, int free_path); diff --git a/main.c b/main.c index bf5ff59..75c54cf 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* gcc TZR.c main.c -lSDL2 */ +/* gcc -std=c2x TZR.c main.c -lSDL2 -lSDL2_image */ #include "TZR.h" @@ -10,14 +10,20 @@ int main(int argc, char **argv) if (TZR_Init(.width=256, .height=256, .pixel_perfect=false)) return 1; + /* Call TZR_Quit on exit. */ + if (atexit(TZR_Quit)) { + perror("atexit(TZR_Quit)"); + return 1; + } + /* Load assets. */ const TZR_Uint id0 = TZR_LoadResourceTyped(TZR_RES_IMAGE, "res.bmp"); const TZR_Uint id1 = TZR_LoadResourceTyped(TZR_RES_RAW, "main.c"); - TZR_LoadResourceTyped(TZR_RES_IMAGE, "res.bmp"); - TZR_LoadResourceTyped(TZR_RES_RAW, "main.c"); + (void)TZR_LoadResourceTyped(TZR_RES_IMAGE, "res.bmp"); + (void)TZR_LoadResourceTyped(TZR_RES_RAW, "main.c"); const TZR_Uint id2 = TZR_LoadResource("smile.bmp"); if (id0 != 1 || id1 != 2 || id2 != 3) - return TZR_Quit(), 1; + return 1; /* Asset loading w/ #embed (C23 proposal). */ /* static const char res_bmp[] = @@ -38,14 +44,15 @@ int main(int argc, char **argv) x += TZR_IsKeyDown(SDL_SCANCODE_RIGHT); y -= TZR_IsKeyDown(SDL_SCANCODE_UP); y += TZR_IsKeyDown(SDL_SCANCODE_DOWN); - TZR_DrawBegin(); - TZR_DrawSetColor(0.0f, 0.0f, 0.0f); - TZR_DrawClear(); - TZR_DrawSetColor(1.0f, 1.0f, 1.0f, 0.2f); - TZR_DrawImage(id0, 128+x, 128+y, .sy=(float)x/10, .center=true); - TZR_DrawSetColor(.a=0.5f); - TZR_DrawImage(id2, y, x, .w=x*2); - TZR_DrawEnd(); + if (TZR_DrawBegin() + || TZR_DrawSetColor(0.0f, 0.0f, 0.0f) + || TZR_DrawClear() + || TZR_DrawSetColor(1.0f, 1.0f, 1.0f, 0.2f) + || TZR_DrawImage(id0, 128+x, 128+y, .sy=(float)x/10, .center=true) + || TZR_DrawSetColor(.a=0.5f) + || TZR_DrawImage(id2, y, x, .w=x*2) + || TZR_DrawEnd()) + return 1; } - return TZR_Quit(), 0; + return 0; } -- cgit v1.2.3