From 11f6a2c707e8913e8c233b99dec1532dbb3766a0 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 22 Jan 2023 23:14:19 +0100 Subject: show errors in message box --- .gitignore | 2 ++ Makefile | 5 +++-- lzr.c | 33 ++++++++++++++++++++------------- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8912a72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +lzr +*.o diff --git a/Makefile b/Makefile index 506beaf..1772a7c 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ CC := gcc LD := $(CC) CFLAGS := -Wall -Wextra -std=c99 -pedantic \ - -D_POSIX_C_SOURCE=200809L $(shell sdl2-config --cflags) + -DLZR_DISABLE_IMAGE -DLZR_DISABLE_MIXER \ + $(shell sdl2-config --cflags) # -D_POSIX_C_SOURCE is only used by DEVMODE -LDFLAGS := $(shell sdl2-config --libs) -lSDL2_gfx -lSDL2_image +LDFLAGS := $(shell sdl2-config --libs) -lSDL2_gfx SRC := $(wildcard *.c) OBJ := $(patsubst %.c,%.o,$(SRC)) NAME := lzr diff --git a/lzr.c b/lzr.c index 89ef073..7652b49 100644 --- a/lzr.c +++ b/lzr.c @@ -1,6 +1,7 @@ /* Licensing information can be found at the end of the file. */ #include "lzr.h" #include +#include #ifdef LZR_ENABLE_GFX # include #endif @@ -168,15 +169,21 @@ exit_bind_menu: SDL_Log("leaving bind menu"); } +static void _LZR_ErrorBox(const char *msg, SDL_Window *win) +{ + if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "LZR", msg, win)) + SDL_Log("%s (%s)", msg, SDL_GetError()); +} + int LZR_Init(LZR_Config cfg) { memcpy(&config, &cfg, sizeof(config)); if (config.display_width == 0) { - SDL_Log("display_width can't be 0"); + _LZR_ErrorBox("display_width can't be 0", NULL); return -1; } if (config.display_height == 0) { - SDL_Log("display_height can't be 0"); + _LZR_ErrorBox("display_height can't be 0", NULL); return -1; } if (config.title == NULL) { @@ -193,19 +200,19 @@ int LZR_Init(LZR_Config cfg) config.ratio /= ratio; } if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } #ifdef LZR_ENABLE_IMAGE if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { - SDL_Log("%s", IMG_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } #endif #ifdef LZR_ENABLE_MIXER audio_mutex = SDL_CreateMutex(); if (audio_mutex == NULL) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } SDL_AudioSpec fmt = {.freq = 44100, @@ -217,7 +224,7 @@ int LZR_Init(LZR_Config cfg) audio_dev = SDL_OpenAudioDevice(NULL, 0, &fmt, &got, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (audio_dev == 0) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } cm_init(44100); @@ -226,7 +233,7 @@ int LZR_Init(LZR_Config cfg) #endif basepath = SDL_GetBasePath(); if (basepath == NULL) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } const int dwidth = config.display_width * config.ratio; @@ -234,25 +241,25 @@ int LZR_Init(LZR_Config cfg) SDL_WINDOWPOS_UNDEFINED, dwidth, config.display_height, SDL_WINDOW_RESIZABLE); if (window == NULL) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), NULL); return -1; } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), window); return -1; } target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, config.display_width, config.display_height); if (target == NULL) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), window); return -1; } points = calloc(cfg.display_width * cfg.display_height, sizeof(SDL_Point)); if (points == NULL) { - SDL_Log("calloc failed"); + _LZR_ErrorBox("calloc failed", window); return -1; } SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); @@ -261,7 +268,7 @@ int LZR_Init(LZR_Config cfg) next_time = SDL_GetTicks64(); } if (config.hide_cursor && SDL_ShowCursor(SDL_DISABLE) < 0) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), window); return -1; } if (LZR_DrawSetColor(1.0f, 1.0f, 1.0f, 1.0f)) @@ -608,7 +615,7 @@ int LZR_DrawSetColor(float r, float g, float b, float a) const unsigned int ub = (unsigned int)(b * 255) & 255; const unsigned int ua = (unsigned int)(a * 255) & 255; if (SDL_SetRenderDrawColor(renderer, ur, ug, ub, ua) < 0) { - SDL_Log("%s", SDL_GetError()); + _LZR_ErrorBox(SDL_GetError(), window); return -1; } color[0] = ur, color[1] = ug, color[2] = ub, color[3] = ua; -- cgit v1.2.3