-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResourceManager.cpp
132 lines (124 loc) · 3.04 KB
/
ResourceManager.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
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
#include "ResourceManager.hpp"
using namespace std;
string dataDir = "data/";
string modelDir = dataDir+"models/";
string fontDir = dataDir+"fonts/";
string imgDir = dataDir+"textures/";
string soundDir = dataDir+"audio/";
string defaultFont = "DejaVuSansMono.ttf";
/**
* @param name Name of model, .iqm suffix currently required
* @return Pointer to the model in the resource manager
* Returns a pointer to a loaded model from the resource manager,
* and loads it first if it is not already loaded.
*/
Model *ResourceManager::loadModel(string name){
map<string,Model>::iterator it = models.find(name);
//If model is not loaded, load it
if(it == models.end()){
Model model;
if(loadIQM(modelDir+name,model) == false){
return NULL;
};
models[name] = model;
return &models[name];
}else{
return &it->second;
}
}
Model *ResourceManager::loadNoGLModel(std::string name){
map<string,Model>::iterator it = noGLModels.find(name);
//If model is not loaded, load it
if(it == noGLModels.end()){
Model model;
if(noGLLoadIQM(modelDir+name,model) == false){
return NULL;
};
noGLModels[name] = model;
return &noGLModels[name];
}else{
return &it->second;
}
}
sf::Font *ResourceManager::loadFont(string name){
map<string, sf::Font>::iterator it = fonts.find(name);
if(it == fonts.end()){
sf::Font f;
if(f.loadFromFile(fontDir+name) == false){
return NULL;
}
fonts[name] = f;
return &fonts[name];
} else {
return &it->second;
}
}
sf::Image *ResourceManager::loadImage(string name){
map<string, sf::Image>::iterator it = images.find(name);
if(it == images.end()){
sf::Image i;
if(i.loadFromFile(imgDir+name) == false){
return NULL;
}
images[name] = i;
return &images[name];
} else {
return &it->second;
}
}
sf::SoundBuffer *ResourceManager::loadSound(string name){
map<string, sf::SoundBuffer>::iterator it = sounds.find(name);
if(it == sounds.end()){
sf::SoundBuffer b;
if(b.loadFromFile(soundDir+name) == false){
return NULL;
}
sounds[name] = b;
return &sounds[name];
} else {
return &it->second;
}
}
Texture ResourceManager::loadTexture(string name){
if(name.length() == 0){
Texture error;
error.id = -1;
cerr << "Texture name is empty." << endl;
return error;
}
map<string, Texture>::iterator it = texs.find(name);
if(it == texs.end()){
GLuint i;
sf::Vector2i size;
sf::Image *img = loadImage(name);
if(img == NULL){
Texture error;
error.id = -1;
return error;
}
size.x = img->getSize().x;
size.y = img->getSize().y;
glGenTextures(1, &i);
glBindTexture(GL_TEXTURE_2D, i);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
size.x,
size.y,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
img->getPixelsPtr());
cout << "IMAGE X:"<<img->getSize().x << " IMAGE Y:" << img->getSize().y << endl;
Texture out;
out.height = size.y;
out.width = size.x;
out.id = i;
texs[name] = out;
return texs[name];
} else {
return it->second;
}
}