aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-03 11:37:15 +0100
committerkdx <kikoodx@paranoici.org>2023-01-04 03:06:09 +0100
commit39c730648e34881f223cabbcf6083c63a76377c2 (patch)
tree2445326143e7fa015405f75871c2edcf75bf1e2b
parentd9a3fe539659dea21cded08bcb80887d53751462 (diff)
downloadlzr-39c730648e34881f223cabbcf6083c63a76377c2.tar.gz
reload images at runtime w/ devmode
-rw-r--r--.clang-format1
-rw-r--r--Makefile4
-rw-r--r--lzr.c54
-rw-r--r--lzr.h17
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 <SDL2/SDL.h>
#ifdef LZR_ENABLE_GFX
-#include <SDL2/SDL2_gfxPrimitives.h>
+# include <SDL2/SDL2_gfxPrimitives.h>
#endif
#ifdef LZR_ENABLE_IMAGE
-#include <SDL2/SDL_image.h>
+# include <SDL2/SDL_image.h>
#endif
#ifdef LZR_ENABLE_MIXER
-#include <SDL2/SDL_mixer.h>
+# include <SDL2/SDL_mixer.h>
+#endif
+#ifdef LZR_ENABLE_DEVMODE
+# include <sys/stat.h>
#endif
#include <stdbool.h>
#include <stdint.h>
@@ -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 <stdbool.h>
#include <stdint.h>
-#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