-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.h
99 lines (72 loc) · 2.05 KB
/
storage.h
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
#pragma once
#include<array>
#include<cstdlib>
#include<iostream>
#include<string>
#include<type_traits>
#include<unordered_map>
#include"raylib.h"
template <typename T>
struct Storage{
T& add(const char* filename);
void unloadAll();
private:
int index{-1};
std::array<T,maxTextures> textures;
std::unordered_map<std::string,int> textureIndex;
};
// Crate stores in global scope
Storage<Texture2D> textureStore;
Storage<Font> fontStore;
// Texture Storage
template<>
Texture2D& Storage<Texture2D>::add(const char* filename){
// check if texture is already loaded
if (textureIndex.contains(filename)){
return textures[textureIndex.at(filename)];
}
// else add new texture
else{
// increment texture index and check if limit reached
if (static_cast<size_t>(++index) == textures.size()){
std::cerr << "Texture storage limit reached. Cannot load \"" << filename << "\".\n";
std::exit(EXIT_FAILURE);
}
textures[index] = LoadTexture(filename);
// add to uniqueTextures in case of future requests
textureIndex[filename] = index;
return textures[index];
}
}
template<>
void Storage<Texture2D>::unloadAll(){
for (int i=0; i<=index; i++){
UnloadTexture(textures[i]);
}
}
// Font Storage
template<>
Font& Storage<Font>::add(const char* filename){
// check if texture is already loaded
if (textureIndex.contains(filename)){
return textures[textureIndex.at(filename)];
}
// else add new texture
else{
// increment texture index and check if limit reached
if (static_cast<size_t>(++index) == textures.size()){
std::cerr << "Font storage limit reached. Cannot load \"" << filename << "\".\n";
std::exit(EXIT_FAILURE);
}
textures[index] = LoadFont(filename);
// add to uniqueTextures in case of future requests
textureIndex[filename] = index;
return textures[index];
}
}
template<>
void Storage<Font>::unloadAll(){
for (int i=0; i<=index; i++){
UnloadFont(textures[i]);
}
}