aboutsummaryrefslogtreecommitdiff
path: root/src/texture_sys.c
diff options
context:
space:
mode:
authorUneven Prankster <unevenprankster@protonmail.com>2023-07-12 13:22:29 -0300
committerUneven Prankster <unevenprankster@protonmail.com>2023-07-12 13:22:29 -0300
commitfa2bdd711212ba6b7a94a20971e8bfa281e73296 (patch)
tree6713b3c0379507d49558287b71dd360ce188a2f0 /src/texture_sys.c
lol
Diffstat (limited to 'src/texture_sys.c')
-rw-r--r--src/texture_sys.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/texture_sys.c b/src/texture_sys.c
new file mode 100644
index 0000000..170e124
--- /dev/null
+++ b/src/texture_sys.c
@@ -0,0 +1,82 @@
+#include "utils.h"
+
+#include <stddef.h>
+
+#define MAX_TEX_SIZE 16
+
+static Texture2D texture_slots[MAX_TEX_SIZE] = {0};
+static long texture_modtimes[MAX_TEX_SIZE] = {0};
+static const char* texture_paths[MAX_TEX_SIZE] = {0};
+
+void init_tex_sys(void)
+{
+ for(int i = 0; i < MAX_TEX_SIZE; ++i){
+ texture_slots[i] = (Texture2D){0};
+ texture_modtimes[i] = BLANK_DEFAULT;
+ texture_paths[i] = NULL;
+ }
+}
+
+int load_new_tex(const char* path)
+{
+ int current_idx = BLANK_DEFAULT;
+ for(int i = 0; i < MAX_TEX_SIZE; ++i){
+ if(texture_slots[i].id <= 0){
+ current_idx = i;
+ texture_slots[current_idx] = LoadTexture(path);
+ texture_modtimes[current_idx] = GetFileModTime(path);
+ texture_paths[current_idx] = path;
+ break;
+ }
+ }
+ //TODO: Handle slots a bit better.
+ if(current_idx == BLANK_DEFAULT)
+ TRACELOG(LOG_WARNING, "Warning: No more empty texture slots.\n");
+
+ return current_idx;
+}
+
+void draw_texture(int idx, int x, int y)
+{
+ if(idx < 0 || idx >= MAX_TEX_SIZE){
+ TRACELOG(LOG_WARNING,"Bounds check: Attempted to draw texture from non-existent index %i.\n", idx);
+ return;
+ }
+ DrawTexture(texture_slots[idx], x, y, WHITE);
+}
+
+void update_textures(void)
+{
+ for(int i = 0; i < MAX_TEX_SIZE; ++i){
+ if(texture_slots[i].id > 0){
+ long current_mod = GetFileModTime(texture_paths[i]);
+
+ if(current_mod != texture_modtimes[i]){
+ texture_modtimes[i] = current_mod;
+
+ UnloadTexture(texture_slots[i]);
+ texture_slots[i] = LoadTexture(texture_paths[i]);
+ }
+ }
+ }
+}
+
+void unload_tex(int idx)
+{
+ if(idx < 0 || idx >= MAX_TEX_SIZE){
+ TRACELOG(LOG_WARNING,"Bounds check: Attempted to unload texture from non-existent index %i.\n", idx);
+ return;
+ }
+ UnloadTexture(texture_slots[idx]);
+ texture_slots[idx] = (Texture2D){0};
+ texture_modtimes[idx] = BLANK_DEFAULT;
+ texture_paths[idx] = NULL;
+}
+
+void unload_active_textures(void)
+{
+ for(int i = 0; i < MAX_TEX_SIZE; ++i){
+ if(texture_slots[i].id > 0)
+ unload_tex(i);
+ }
+}