aboutsummaryrefslogtreecommitdiff
path: root/src/texture_sys.c
blob: 3254d75e71ff66034b3c88500cbfa4cdc4c7602b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "utils.h"
#include "config.h"

#include <stddef.h>

#define MAX_TEX_SIZE 16

static Texture2D texture_slots[MAX_TEX_SIZE] = {0};
static i32 texture_modtimes[MAX_TEX_SIZE] = {0};
static const char* texture_paths[MAX_TEX_SIZE] = {0};

void init_tex_sys(void)
{
    for(i32 i = 0; i < MAX_TEX_SIZE; ++i){
        texture_slots[i] = (Texture2D){0};
        texture_modtimes[i] = BLANK_DEFAULT;
        texture_paths[i] = NULL;
    }
}

i32 load_new_tex(const char* path)
{
    i32 current_idx = BLANK_DEFAULT;
    for(i32 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(i32 idx, i32 x, i32 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(i32 i = 0; i < MAX_TEX_SIZE; ++i){
        if(texture_slots[i].id > 0){
            i32 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(i32 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(i32 i = 0; i < MAX_TEX_SIZE; ++i){
        if(texture_slots[i].id > 0)
            unload_tex(i);
    }
}