From d1ba7e3713cd999635104591295d1923235c9289 Mon Sep 17 00:00:00 2001 From: citrons Date: Thu, 12 Oct 2023 16:32:32 -0500 Subject: make error handling worse --- Makefile | 2 +- die.c | 16 ++++++++++++++++ die.h | 17 ++++++++--------- main.c | 9 ++++++--- 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 die.c diff --git a/Makefile b/Makefile index 5ffa84a..1bf7aae 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SDL_HEADERS=/usr/include/SDL2 CFLAGS=-g -Wall -pedantic -std=c99 $$(sdl2-config --cflags) LFLAGS=$$(sdl2-config --libs) -findgame: main.o world.o random.o generator.o save.o +findgame: main.o world.o random.o generator.o save.o die.o $(CC) -o $@ $^ $(LFLAGS) main.o: world.h random.h save.h diff --git a/die.c b/die.c new file mode 100644 index 0000000..95ca7c2 --- /dev/null +++ b/die.c @@ -0,0 +1,16 @@ +#include + +void die(void) { + while (1) { + // draw error message text? + SDL_Event e; + while (SDL_WaitEvent(&e)) SDL_Delay(1); + } +} + +void sdl_error_assert(SDL_bool condition) { + if (!condition) { + SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError()); + die(); + } +} diff --git a/die.h b/die.h index 5048ced..9639bf1 100644 --- a/die.h +++ b/die.h @@ -1,13 +1,12 @@ +#ifndef DIE_H +#define DIE_H + #include -#include +void die(void); +void sdl_error_assert(SDL_bool condition); -#define die(...) (SDL_Log(__VA_ARGS__), abort()) -static inline void sdl_error_assert(SDL_bool condition) { - if (!condition) { - SDL_Log("%s", SDL_GetError()); - // banish to hell forever - abort(); - } -} +#define die(...) (SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, __VA_ARGS__), die()) #define sdl_error_assert(c) (sdl_error_assert(c && SDL_TRUE)) + +#endif diff --git a/main.c b/main.c index 2443d7a..39eb282 100644 --- a/main.c +++ b/main.c @@ -68,19 +68,22 @@ static int run() { int main(int argc, char *argv[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { - SDL_Log("error initializing SDL: %s", SDL_GetError()); + SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, + "error initializing SDL: %s", SDL_GetError()); return 1; } win = SDL_CreateWindow("find", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if (!win) { - SDL_Log("error creating window: %s", SDL_GetError()); + SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, + "error creating window: %s", SDL_GetError()); return 1; } rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC); if (!rend) { - SDL_Log("error creating renderer: %s", SDL_GetError()); + SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, + "error creating renderer: %s", SDL_GetError()); return 1; } -- cgit v1.2.3