diff --git a/CMakeLists.txt b/CMakeLists.txt index 98169d6..ecaed48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ set(ASSETS fxconv_declare_assets(${ASSETS} WITH_METADATA) add_executable(myaddin ${SOURCES} ${ASSETS} ${ASSETS_${FXSDK_PLATFORM}}) -target_compile_options(myaddin PRIVATE -Wall -Wextra -O2) +target_compile_options(myaddin PRIVATE -Wall -Wextra -O2 -std=c2x) target_link_libraries(myaddin Gint::Gint) generate_g3a(TARGET myaddin OUTPUT "hyperultra.g3a" NAME "" diff --git a/Makefile b/Makefile index 1a546ba..1913920 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC := gcc LD := $(CC) -CFLAGS := -Os -std=c99 -Wall -Wextra -Imap -MMD +CFLAGS := -Os -std=c2x -Wall -Wextra -Imap -MMD LDFLAGS := -lm -lSDL2 -lSDL2_image -lSDL2_mixer NAME := hyperultra EMBED := map/maps.h diff --git a/compile_flags.txt b/compile_flags.txt index bfdbe1a..86934b2 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,4 +1,4 @@ -Wall -Wextra --std=c99 +-std=c2x -Imap diff --git a/src/deathpart.c b/src/deathpart.c index 050cc54..b397133 100644 --- a/src/deathpart.c +++ b/src/deathpart.c @@ -16,10 +16,10 @@ IMPL_UPDATE() { IMPL_DRAW() { LZY_DrawSetColor(BLACK); if (this->width == 1) - LZY_DrawPoint(this->pos[0], this->pos[1]); + (void)LZY_DrawPoint(this->pos[0], this->pos[1]); else - LZY_DrawRect(this->pos[0], this->pos[1], - this->width, this->width); + (void)LZY_DrawRect(this->pos[0], this->pos[1], + this->width, this->width); } IMPL_END IMPL_INIT(deathpart) { diff --git a/src/lzy.h b/src/lzy.h index fabc11d..ea748d0 100644 --- a/src/lzy.h +++ b/src/lzy.h @@ -28,8 +28,6 @@ extern "C" { #include -#define LZY_UNUSED(x) (void)(x) - #ifdef FXCG50 #include typedef void LZY_Music; @@ -80,38 +78,42 @@ typedef struct LZY_Event { LZY_EventUnion u; } LZY_Event; -int LZY_Init(const char *title, int target_fps, const char *tset_path, - const char *font_path); +[[nodiscard]] int LZY_Init(const char *title, int target_fps, + const char *tset_path, const char *font_path); void LZY_Quit(void); -int LZY_DrawBegin(void); -int LZY_DrawEnd(void); +[[nodiscard]] int LZY_DrawBegin(void); +[[nodiscard]] int LZY_DrawEnd(void); void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b); void LZY_DrawSetColorNone(void); -int LZY_DrawClear(void); -int LZY_DrawPoint(int x, int y); -int LZY_DrawLine(int x0, int y0, int x1, int y1); -int LZY_DrawRect(int x, int y, int w, int h); -int LZY_DrawFillRect(int x, int y, int w, int h); -int LZY_DrawTile(unsigned int id, int x, int y); -int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, - unsigned int h); -int LZY_DrawChar(unsigned char chr, int x, int y); -int LZY_DrawText(int x, int y, const char *text); -int LZY_DrawTextF(int x, int y, const char *fmt, ...); -LZY_Music *LZY_MusicLoad(const char *path); -int LZY_MusicDestroy(LZY_Music *music); -int LZY_MusicPlay(LZY_Music *music, int loops); -LZY_Sound *LZY_SoundLoad(const char *path); -int LZY_SoundDestroy(LZY_Sound *sound); +[[nodiscard]] int LZY_DrawClear(void); +[[nodiscard]] int LZY_DrawPoint(int x, int y); +[[nodiscard]] int LZY_DrawLine(int x0, int y0, int x1, int y1); +[[nodiscard]] int LZY_DrawRect(int x, int y, int w, int h); +[[nodiscard]] int LZY_FillRect(int x, int y, int w, int h); +[[nodiscard]] int LZY_DrawTile(unsigned int id, int x, int y); +[[nodiscard]] int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, + unsigned int h); +[[nodiscard]] int LZY_DrawChar(unsigned char chr, int x, int y); +[[nodiscard]] int LZY_DrawText(int x, int y, const char *text); +[[nodiscard]] int LZY_DrawTextF(int x, int y, const char *fmt, ...); +[[nodiscard]] LZY_Music *LZY_MusicLoad(const char *path); +[[nodiscard]] int LZY_MusicDestroy(LZY_Music *music); +[[nodiscard]] int LZY_MusicPlay(LZY_Music *music, int loops); +[[nodiscard]] LZY_Sound *LZY_SoundLoad(const char *path); +[[nodiscard]] int LZY_SoundDestroy(LZY_Sound *sound); void LZY_SoundSetVolume(LZY_Sound *sound, float volume); -int LZY_SoundPlay(LZY_Sound *sound, int loops); +[[nodiscard]] int LZY_SoundPlay(LZY_Sound *sound, int loops); int LZY_PollEvent(LZY_Event *); void LZY_CycleEvents(void); int LZY_KeyDown(unsigned int key); int LZY_ShouldQuit(void); void LZY_Log(const char *fmt, ...); +int LZY_LogError(int rv); const char *LZY_GetError(void); +[[deprecated("renamed to LZY_FillRect")]] [[nodiscard]] int +LZY_DrawFillRect(int x, int y, int w, int h); + #ifdef __cplusplus } #endif @@ -193,6 +195,16 @@ int LZY_DrawTextF(int x, int y, const char *fmt, ...) { return LZY_DrawText(x, y, buf); } +int LZY_LogError(int rv) { + LZY_Log("%s", LZY_GetError()); + + return rv; +} + +int LZY_DrawFillRect(int x, int y, int w, int h) { + return LZY_FillRect(x, y, w, h); +} + #ifdef FXCG50 #include #include @@ -214,15 +226,12 @@ static int _LZY_Sign(int n) { return (n > 0) - (n < 0); } -int LZY_Init(const char *title, int target_fps, const char *tset_path, - const char *font_path) { +int LZY_Init([[maybe_unused]] const char *title, int target_fps, + [[maybe_unused]] const char *tset_path, + [[maybe_unused]] const char *font_path) { extern bopti_image_t LZY_GINT_TILESET; extern bopti_image_t LZY_GINT_FONT; - LZY_UNUSED(title); - LZY_UNUSED(tset_path); - LZY_UNUSED(font_path); - tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE; tset_height = LZY_GINT_TILESET.height / LZY_TILE_SIZE; @@ -291,7 +300,7 @@ int LZY_DrawRect(int x, int y, int w, int h) { return 0; } -int LZY_DrawFillRect(int x, int y, int w, int h) { +int LZY_FillRect(int x, int y, int w, int h) { w += w == 0; h += h == 0; x += draw_off_x; @@ -378,40 +387,32 @@ int LZY_DrawText(int x, int y, const char *text) { return err; } -LZY_Music *LZY_MusicLoad(const char *path) { - LZY_UNUSED(path); +LZY_Music *LZY_MusicLoad([[maybe_unused]] const char *path) { return NULL; } -int LZY_MusicDestroy(LZY_Music *music) { - LZY_UNUSED(music); +int LZY_MusicDestroy([[maybe_unused]] LZY_Music *music) { return -1; } -int LZY_MusicPlay(LZY_Music *music, int loops) { - LZY_UNUSED(music); - LZY_UNUSED(loops); +int LZY_MusicPlay([[maybe_unused]] LZY_Music *music, + [[maybe_unused]] int loops) { return -1; } -LZY_Sound *LZY_SoundLoad(const char *path) { - LZY_UNUSED(path); +LZY_Sound *LZY_SoundLoad([[maybe_unused]] const char *path) { return NULL; } -int LZY_SoundDestroy(LZY_Sound *sound) { - LZY_UNUSED(sound); +int LZY_SoundDestroy([[maybe_unused]] LZY_Sound *sound) { return -1; } -void LZY_SoundSetVolume(LZY_Sound *sound, float volume) { - LZY_UNUSED(sound); - LZY_UNUSED(volume); -} +void LZY_SoundSetVolume([[maybe_unused]] LZY_Sound *sound, + [[maybe_unused]] float volume) {} -int LZY_SoundPlay(LZY_Sound *sound, int loops) { - LZY_UNUSED(sound); - LZY_UNUSED(loops); +int LZY_SoundPlay([[maybe_unused]] LZY_Sound *sound, + [[maybe_unused]] int loops) { return -1; } @@ -451,9 +452,7 @@ int LZY_ShouldQuit(void) { return should_quit; } -void LZY_Log(const char *fmt, ...) { - LZY_UNUSED(fmt); -} +void LZY_Log([[maybe_unused]] const char *fmt, ...) {} const char *LZY_GetError(void) { return NULL; @@ -469,12 +468,9 @@ const char *LZY_GetError(void) { #include static const SDL_Scancode sc[LZYK_COUNT * 2] = { - SDL_SCANCODE_LEFT, SDL_SCANCODE_A, - SDL_SCANCODE_RIGHT, SDL_SCANCODE_D, - SDL_SCANCODE_UP, SDL_SCANCODE_W, - SDL_SCANCODE_DOWN, SDL_SCANCODE_S, - SDL_SCANCODE_LSHIFT,SDL_SCANCODE_J, - SDL_SCANCODE_X, SDL_SCANCODE_K, + SDL_SCANCODE_LEFT, SDL_SCANCODE_A, SDL_SCANCODE_RIGHT, SDL_SCANCODE_D, + SDL_SCANCODE_UP, SDL_SCANCODE_W, SDL_SCANCODE_DOWN, SDL_SCANCODE_S, + SDL_SCANCODE_Z, SDL_SCANCODE_J, SDL_SCANCODE_X, SDL_SCANCODE_K, }; static const SDL_Scancode fullscreen_sc = SDL_SCANCODE_F11; static const char *error = NULL; @@ -737,7 +733,7 @@ int LZY_DrawRect(int x, int y, int w, int h) { return 0; } -int LZY_DrawFillRect(int x, int y, int w, int h) { +int LZY_FillRect(int x, int y, int w, int h) { const SDL_Rect rect = {x, y, w + (w == 0), h + (h == 0)}; if (SDL_RenderFillRect(renderer, &rect) < 0) { @@ -955,10 +951,10 @@ int LZY_PollEvent(LZY_Event *e) { if (!sdl_e.key.repeat) { int i = LZYK_COUNT; while (i-- > 0) { - if (sc[i*2] == - sdl_e.key.keysym.scancode || - sc[i*2 + 1] == - sdl_e.key.keysym.scancode) { + if (sc[i * 2] == + sdl_e.key.keysym.scancode || + sc[i * 2 + 1] == + sdl_e.key.keysym.scancode) { e->type = LZY_KEYDOWN; e->u.key.scancode = i; input[i] = 1; @@ -979,8 +975,9 @@ int LZY_PollEvent(LZY_Event *e) { case SDL_KEYUP: { int i = LZYK_COUNT; while (i-- > 0) { - if (sc[i*2] == sdl_e.key.keysym.scancode || - sc[i*2 + 1] == sdl_e.key.keysym.scancode) { + if (sc[i * 2] == sdl_e.key.keysym.scancode || + sc[i * 2 + 1] == + sdl_e.key.keysym.scancode) { e->type = LZY_KEYUP; e->u.key.scancode = i; input[i] = 0; diff --git a/src/main.c b/src/main.c index 2a6e425..0ed1117 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,10 @@ #include #include +static Game *game = NULL; + +static void deinit(void); + int main(void) { if (LZY_Init("hyperultra!", 30, "res/tset.png", "res/font.png")) { @@ -15,10 +19,15 @@ int main(void) return 1; } - Game *const game = malloc(sizeof(Game)); + if (atexit(deinit)) { + perror("atexit(deinit)"); + deinit(); + return 1; + } + + game = malloc(sizeof(Game)); if (game == NULL) { LZY_Log("malloc failed"); - LZY_Quit(); return 1; } game_init(game); @@ -47,28 +56,26 @@ int main(void) hold = 0; } - LZY_DrawBegin(); + if (LZY_DrawBegin()) return LZY_LogError(1); LZY_DrawSetColor(WHITE); - LZY_DrawClear(); + (void)LZY_DrawClear(); LZY_DrawSetColor(BLACK); int y = -16; if (stage == 1) { - LZY_DrawText(48, y += 32, "WALKING is AUTOMATIC"); - LZY_DrawText(48, y += 32, "press SHIFT to jump"); - LZY_DrawText(48, y += 32, "hold UP to jump HIGHER"); - LZY_DrawText(48, y += 32, "hold DOWN to jump LOWER"); - } - else { - LZY_DrawText(48, y += 32, "HYPERULTRA"); - LZY_DrawText(48, y += 32, "a game made by KDX.re"); - LZY_DrawText(48, y += 32, "powered by GINT and SDL"); - y += 32; + (void)LZY_DrawText(48, y += 32, "WALKING is AUTOMATIC"); + (void)LZY_DrawText(48, y += 32, "press SHIFT to jump"); + (void)LZY_DrawText(48, y += 32, "hold UP to jump HIGHER"); + (void)LZY_DrawText(48, y += 32, "hold DOWN to jump LOWER"); + } else { + (void)LZY_DrawText(48, y += 32, "HYPERULTRA"); + (void)LZY_DrawText(48, y += 32, "a game made by KDX.re"); + (void)LZY_DrawText(48, y += 32, "powered by GINT and SDL"); } - LZY_DrawText(48, y += 48, "hold SHIFT to continue"); + (void)LZY_DrawText(48, y += 48, "hold SHIFT to continue"); if (hold) - LZY_DrawFillRect(0, DISPLAY_HEIGHT - 24, hold * 8, 24); + (void)LZY_FillRect(0, DISPLAY_HEIGHT - 24, hold * 8, 24); background_draw(); - LZY_DrawEnd(); + if (LZY_DrawEnd()) return LZY_LogError(1); } while (!LZY_ShouldQuit()) { @@ -77,16 +84,23 @@ int main(void) game_update(game); background_update(game); - LZY_DrawBegin(); + if (LZY_DrawBegin()) return LZY_LogError(1); LZY_DrawSetColor(WHITE); - LZY_DrawClear(); + if (LZY_DrawClear()) return LZY_LogError(1); game_draw(game); background_draw(); - LZY_DrawEnd(); + if (LZY_DrawClear()) return LZY_LogError(1); } - game_deinit(game); - free(game); - LZY_Quit(); return 0; } + +static void +deinit(void) +{ + if (game != NULL) { + game_deinit(game); + free(game); + } + LZY_Quit(); +} diff --git a/src/map.c b/src/map.c index 26b7b7c..ee432e9 100644 --- a/src/map.c +++ b/src/map.c @@ -66,10 +66,10 @@ draw_outline(int x, int y) const int down = (map_get(x, y + 1) == 1); x *= TSIZE; y *= TSIZE; - if (!left) LZY_DrawFillRect(x, y, 1, TSIZE); - if (!right) LZY_DrawFillRect(x + TSIZE - 1, y, 1, TSIZE); - if (!up) LZY_DrawFillRect(x, y, TSIZE, 1); - if (!down) LZY_DrawFillRect(x, y + TSIZE - 1, TSIZE, 1); + if (!left) (void)LZY_FillRect(x, y, 1, TSIZE); + if (!right) (void)LZY_FillRect(x + TSIZE - 1, y, 1, TSIZE); + if (!up) (void)LZY_FillRect(x, y, TSIZE, 1); + if (!down) (void)LZY_FillRect(x, y + TSIZE - 1, TSIZE, 1); } void @@ -88,5 +88,5 @@ map_draw_ui(void) const char *s = maps[map_id].name; const int x = (DISPLAY_WIDTH - CHR_WIDTH * strlen(s)) / 2; const int y = (DISPLAY_HEIGHT - CHR_HEIGHT) / 2; - LZY_DrawText(x, y, s); + (void)LZY_DrawText(x, y, s); } diff --git a/src/rotrect.c b/src/rotrect.c index 6721d10..f01c297 100644 --- a/src/rotrect.c +++ b/src/rotrect.c @@ -46,10 +46,10 @@ rotrect_draw(Rect rect, double x, double y) round(rect.y0 + y), round(rect.y1) + y, round(rect.y2 + y), round(rect.y3) + y, }; - LZY_DrawLine(xs[0], ys[0], xs[1], ys[1]); - LZY_DrawLine(xs[1], ys[1], xs[2], ys[2]); - LZY_DrawLine(xs[2], ys[2], xs[3], ys[3]); - LZY_DrawLine(xs[3], ys[3], xs[0], ys[0]); + (void)LZY_DrawLine(xs[0], ys[0], xs[1], ys[1]); + (void)LZY_DrawLine(xs[1], ys[1], xs[2], ys[2]); + (void)LZY_DrawLine(xs[2], ys[2], xs[3], ys[3]); + (void)LZY_DrawLine(xs[3], ys[3], xs[0], ys[0]); } void