From 1c0cc775732201f4c4d3ee0d6772be786b3b4aa1 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 15 Oct 2023 21:28:29 -0300 Subject: A lot has certainly happened! --- src/gunner/texture_sys.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/gunner/texture_sys.c (limited to 'src/gunner/texture_sys.c') diff --git a/src/gunner/texture_sys.c b/src/gunner/texture_sys.c new file mode 100644 index 0000000..2865728 --- /dev/null +++ b/src/gunner/texture_sys.c @@ -0,0 +1,77 @@ +#define MAX_TEX_SIZE 16 + +static Texture2D texture_slots[MAX_TEX_SIZE] = {0}; +static i32 texture_modtimes[MAX_TEX_SIZE] = {0}; +static s8 texture_paths[MAX_TEX_SIZE] = {0}; + +void init_tex_sys(void) +{ + for(i32 i = 0; i < MAX_TEX_SIZE; ++i){ + texture_modtimes[i] = BLANK_DEFAULT; + } +} + +i32 load_new_tex(const char* path) +{ + s8 path_str = cstr_to_s8(path); + + i32 current_idx = BLANK_DEFAULT; + for(i32 i = 0; i < MAX_TEX_SIZE; ++i){ + + if(equal_s8(path_str, texture_paths[current_idx])){ + free_s8(&path_str); + return i; + } + + if(texture_slots[i].id <= 0){ + current_idx = i; + texture_slots[current_idx] = LoadTexture((const char*)path_str.buf); + texture_modtimes[current_idx] = GetFileModTime((const char*)path_str.buf); + texture_paths[current_idx] = path_str; + break; + } + } + //TODO: Handle slots a bit better. + assert(current_idx != BLANK_DEFAULT); + + return current_idx; +} + +void draw_texture(i32 idx, i32 x, i32 y) +{ + assert(idx >= 0 && idx < MAX_TEX_SIZE); + DrawTexture(texture_slots[idx], x, y, WHITE); +} + +void update_textures(void) +{ + for(i32 i = 0; i < MAX_TEX_SIZE; ++i){ + if(texture_slots[i].id > 0){ + i32 current_mod = GetFileModTime((const char*)texture_paths[i].buf); + + if(current_mod != texture_modtimes[i]){ + texture_modtimes[i] = current_mod; + + UnloadTexture(texture_slots[i]); + texture_slots[i] = LoadTexture((const char*)texture_paths[i].buf); + } + } + } +} + +void unload_tex(i32 idx) +{ + assert(idx >= 0 && idx < MAX_TEX_SIZE); + + UnloadTexture(texture_slots[idx]); + texture_modtimes[idx] = BLANK_DEFAULT; + free_s8(&texture_paths[idx]); +} + +void unload_active_textures(void) +{ + for(i32 i = 0; i < MAX_TEX_SIZE; ++i){ + if(texture_slots[i].id > 0) + unload_tex(i); + } +} -- cgit v1.2.3