-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtexturepool.cpp
More file actions
140 lines (112 loc) · 4.01 KB
/
Copy pathtexturepool.cpp
File metadata and controls
140 lines (112 loc) · 4.01 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "prism/texturepool.h"
#include "prism/datastructures.h"
#include "prism/log.h"
#include "prism/memoryhandler.h"
#include "prism/system.h"
#ifdef _WIN32
#include <imgui/imgui.h>
#include "prism/windows/debugimgui_win.h"
#endif
namespace prism {
typedef struct {
TextureData mTexture;
char mPath[1024];
int mCounter;
} TexturePoolEntry;
static struct {
StringMap mPathToLoadedTexture;
StringMap mTextureHashToLoadedTexture;
int mIsLoaded;
} gTexturePool;
#ifdef _WIN32
static void imguiTexturePoolEntry(void* tCaller, char* tKey, void* tData)
{
(void)tCaller;
TexturePoolEntry* e = (TexturePoolEntry*)tData;
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::Text("%s", tKey); ImGui::TableNextColumn();
ImGui::Text("%s", e->mPath); ImGui::TableNextColumn();
ImGui::Text("%d", e->mCounter); ImGui::TableNextColumn();
ImGui::Text("%d", getTextureHash(e->mTexture));
}
void imguiTexturePool()
{
static bool isWindowShown = false;
imguiPrismAddTab("Prism", "Texture Pool", &isWindowShown);
if (!isWindowShown) return;
ImGui::Begin("Texture Pool", &isWindowShown);
ImGui::Text("IsLoaded = %d", gTexturePool.mIsLoaded);
ImGui::Text("Loaded Textures = %d", string_map_size(&gTexturePool.mPathToLoadedTexture));
ImGui::Separator();
static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg;
if (ImGui::BeginTable("Textures", 4, flags))
{
ImGui::TableSetupColumn("Key");
ImGui::TableSetupColumn("Path");
ImGui::TableSetupColumn("RefCount");
ImGui::TableSetupColumn("Texture Hash");
ImGui::TableHeadersRow();
string_map_map(&gTexturePool.mPathToLoadedTexture, imguiTexturePoolEntry, NULL);
ImGui::EndTable();
}
ImGui::End();
}
#endif
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);
}
}