-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtexturepool.cpp
97 lines (74 loc) · 2.68 KB
/
texturepool.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "prism/texturepool.h"
#include "prism/datastructures.h"
#include "prism/log.h"
#include "prism/memoryhandler.h"
#include "prism/system.h"
namespace prism {
typedef struct {
TextureData mTexture;
char mPath[1024];
int mCounter;
} TexturePoolEntry;
static struct {
StringMap mPathToLoadedTexture;
StringMap mTextureHashToLoadedTexture;
int mIsLoaded;
} gTexturePool;
void setupTexturePool() {
if (gTexturePool.mIsLoaded) {
logWarning("Attempting to use active texture pool. Resetting.");
shutdownTexturePool();
}
gTexturePool.mPathToLoadedTexture = new_string_map();
gTexturePool.mTextureHashToLoadedTexture = new_string_map();
gTexturePool.mIsLoaded = 1;
}
static void cleanSingleTexturePoolEntry(void* tCaller, char* tKey, void* tData) {
(void)tCaller;
(void)tKey;
TexturePoolEntry* e = (TexturePoolEntry*)tData;
unloadTexture(e->mTexture);
}
void shutdownTexturePool() {
string_map_map(&gTexturePool.mTextureHashToLoadedTexture, cleanSingleTexturePoolEntry, NULL);
delete_string_map(&gTexturePool.mTextureHashToLoadedTexture);
delete_string_map(&gTexturePool.mPathToLoadedTexture);
gTexturePool.mIsLoaded = 0;
}
static TextureData increaseCounterAndFetchTexture(const char* tPath) {
TexturePoolEntry* e = (TexturePoolEntry*)string_map_get(&gTexturePool.mPathToLoadedTexture, tPath);
e->mCounter++;
return e->mTexture;
}
TextureData loadTextureFromPool(const char* tPath) {
int isLoadNotNecessary = string_map_contains(&gTexturePool.mPathToLoadedTexture, tPath);
if (isLoadNotNecessary) {
return increaseCounterAndFetchTexture(tPath);
}
TexturePoolEntry* e = (TexturePoolEntry*)allocMemory(sizeof(TexturePoolEntry));
e->mCounter = 1;
e->mTexture = loadTexture(tPath);
strcpy(e->mPath, tPath);
char hashString[100];
sprintf(hashString, "%d", getTextureHash(e->mTexture));
string_map_push_owned(&gTexturePool.mPathToLoadedTexture, tPath, e);
string_map_push(&gTexturePool.mTextureHashToLoadedTexture, hashString, e);
return e->mTexture;
}
void unloadTextureFromPool(TextureData& tTexture) {
char hashString[100];
sprintf(hashString, "%d", getTextureHash(tTexture));
int hasBeenLoaded = string_map_contains(&gTexturePool.mTextureHashToLoadedTexture, hashString);
if (!hasBeenLoaded) {
logError("Unrecognized Texture.");
logErrorString(hashString);
recoverFromError();
}
TexturePoolEntry* e = (TexturePoolEntry*)string_map_get(&gTexturePool.mTextureHashToLoadedTexture, hashString);
e->mCounter--;
if (e->mCounter > 0) return;
unloadTexture(e->mTexture);
string_map_remove(&gTexturePool.mTextureHashToLoadedTexture, hashString);
string_map_remove(&gTexturePool.mPathToLoadedTexture, e->mPath);
}
}