From 39c730648e34881f223cabbcf6083c63a76377c2 Mon Sep 17 00:00:00 2001 From: kdx Date: Tue, 3 Jan 2023 11:37:15 +0100 Subject: reload images at runtime w/ devmode --- .clang-format | 1 + Makefile | 4 +++- lzr.c | 54 ++++++++++++++++++++++++++++++++++++++++-------------- lzr.h | 17 ++++++++++++++--- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/.clang-format b/.clang-format index d749f15..b74a5bf 100644 --- a/.clang-format +++ b/.clang-format @@ -5,5 +5,6 @@ BreakBeforeBraces: Linux AllowShortIfStatementsOnASingleLine: Never AllowShortFunctionsOnASingleLine: None IndentCaseLabels: false +IndentPPDirectives: AfterHash ColumnLimit: 80 AlignConsecutiveMacros: true diff --git a/Makefile b/Makefile index dbdefd0..66592c0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ CC := gcc LD := $(CC) -CFLAGS := -Wall -Wextra -std=c99 -pedantic $(shell sdl2-config --cflags) +CFLAGS := -Wall -Wextra -std=c99 -pedantic \ + -D_POSIX_C_SOURCE=200809L $(shell sdl2-config --cflags) +# -D_POSIX_C_SOURCE is only used by DEVMODE LDFLAGS := $(shell sdl2-config --libs) -lSDL2_gfx -lSDL2_image -lSDL2_mixer SRC := $(wildcard *.c) OBJ := $(patsubst %.c,%.o,$(SRC)) diff --git a/lzr.c b/lzr.c index ad46021..1bb773a 100644 --- a/lzr.c +++ b/lzr.c @@ -2,13 +2,16 @@ #include "lzr.h" #include #ifdef LZR_ENABLE_GFX -#include +# include #endif #ifdef LZR_ENABLE_IMAGE -#include +# include #endif #ifdef LZR_ENABLE_MIXER -#include +# include +#endif +#ifdef LZR_ENABLE_DEVMODE +# include #endif #include #include @@ -31,6 +34,7 @@ static struct { SDL_Texture *tex; int width, height; char *path; + long mtime; } images[LZR_MAX_IMAGES] = {0}; static unsigned int color[3] = {0}; static unsigned int map[LZR_BUTTON_COUNT] = { @@ -46,7 +50,9 @@ static int mouse_x = 0; static int mouse_y = 0; #ifdef LZR_ENABLE_MIXER -static Mix_Chunk *sounds[LZR_MAX_SOUNDS] = {0}; +static struct { + Mix_Chunk *ptr; +} sounds[LZR_MAX_SOUNDS] = {0}; static Mix_Music *music = NULL; #endif @@ -235,9 +241,9 @@ void LZR_Quit(void) LZR_StopMusic(); #ifdef LZR_ENABLE_MIXER for (int i = 0; i < LZR_MAX_SOUNDS; i++) - if (sounds[i] != NULL) { - Mix_FreeChunk(sounds[i]); - sounds[i] = NULL; + if (sounds[i].ptr != NULL) { + Mix_FreeChunk(sounds[i].ptr); + sounds[i].ptr = NULL; SDL_Log("destroyed sound %d", i); } Mix_CloseAudio(); @@ -307,10 +313,26 @@ char *LZR_PathPrefix(const char *path) int LZR_ImageLoad(const char *path) { + char *apath; + long mtime = 0; +#ifdef LZR_ENABLE_DEVMODE + apath = LZR_PathPrefix(path); + if (apath == NULL) + return -1; + struct stat st = {0}; + (void)stat(apath, &st); /* stat can fail safely */ + mtime = st.st_mtim.tv_nsec; +#endif int i; for (i = 0; i < LZR_MAX_IMAGES; i++) { - if (images[i].path != NULL && strcmp(images[i].path, path) == 0) + if (images[i].path != NULL && + strcmp(images[i].path, path) == 0) { + if (mtime != images[i].mtime) { + SDL_Log("reloading %d", i); + break; + } return i; + } if (images[i].tex == NULL) break; } @@ -318,7 +340,7 @@ int LZR_ImageLoad(const char *path) SDL_Log("reached image limit (%d)", LZR_MAX_IMAGES); return -1; } - char *const apath = LZR_PathPrefix(path); + apath = LZR_PathPrefix(path); if (apath == NULL) { SDL_Log("LZR_PathPrefix failed"); return -1; @@ -339,7 +361,10 @@ int LZR_ImageLoad(const char *path) SDL_Log("%s", SDL_GetError()); return -1; } + if (images[i].tex != NULL) + SDL_DestroyTexture(images[i].tex); images[i].tex = tex; + images[i].mtime = mtime; if (SDL_SetTextureBlendMode(images[i].tex, SDL_BLENDMODE_BLEND) < 0) { SDL_Log("%s", SDL_GetError()); return -1; @@ -349,7 +374,8 @@ int LZR_ImageLoad(const char *path) SDL_Log("%s", SDL_GetError()); return -1; } - images[i].path = _lzrstrdup(path); + if (images[i].path == NULL) + images[i].path = _lzrstrdup(path); return i; } @@ -358,7 +384,7 @@ int LZR_SoundLoad(const char *path, float volume) #ifdef LZR_ENABLE_MIXER int i; for (i = 0; i < LZR_MAX_SOUNDS; i++) - if (sounds[i] == NULL) + if (sounds[i].ptr == NULL) break; if (i >= LZR_MAX_SOUNDS) { SDL_Log("reached sounds limit (%d)", LZR_MAX_SOUNDS); @@ -376,7 +402,7 @@ int LZR_SoundLoad(const char *path, float volume) return -1; } Mix_VolumeChunk(chunk, volume * MIX_MAX_VOLUME); - sounds[i] = chunk; + sounds[i].ptr = chunk; return i; #else (void)path, (void)volume; @@ -758,11 +784,11 @@ int LZR_PlaySound(int id) SDL_Log("id is negative"); return -1; } - if (id >= LZR_MAX_SOUNDS || sounds[id] == NULL) { + if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) { SDL_Log("no sound with id %d", id); return -1; } - if (Mix_PlayChannel(-1, sounds[id], 0) < 0) { + if (Mix_PlayChannel(-1, sounds[id].ptr, 0) < 0) { SDL_Log("%s", Mix_GetError()); return -1; } diff --git a/lzr.h b/lzr.h index c9e8320..cafd35a 100644 --- a/lzr.h +++ b/lzr.h @@ -7,9 +7,20 @@ extern "C" { #include #include -#define LZR_ENABLE_IMAGE -#define LZR_ENABLE_MIXER -#define LZR_ENABLE_GFX +#ifndef LZR_DISABLE_IMAGE +# define LZR_ENABLE_IMAGE +#endif +#ifndef LZR_DISABLE_MIXER +# define LZR_ENABLE_MIXER +#endif +#ifndef LZR_DISABLE_GFX +# define LZR_ENABLE_GFX +#endif + +/* devmode tracks and autoreloads ressources on change */ +#ifndef LZR_DISABLE_DEVMODE +# define LZR_ENABLE_DEVMODE +#endif #define LZR_MAX_IMAGES 32 #define LZR_MAX_SOUNDS 64 -- cgit v1.2.3