diff options
Diffstat (limited to 'lzr.c')
-rw-r--r-- | lzr.c | 54 |
1 files changed, 40 insertions, 14 deletions
@@ -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; } |