-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetManager.cpp
183 lines (130 loc) · 5.04 KB
/
AssetManager.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//---------------------------------------------------------------------//
// AssetManager.cpp //
// Singleton //
// Used to load all assets used in the game, //
// Caches all loaded assets so that nothing is loaded more than once //
// //
// By: Ather Omar //
//---------------------------------------------------------------------//
#include "AssetManager.h"
//-----------------------------------------------------------------
// QuickSDL
//-----------------------------------------------------------------
namespace QuickSDL {
//Initializing to NULL
AssetManager* AssetManager::sInstance = NULL;
AssetManager* AssetManager::Instance() {
//Create a new instance if no instance was created before
if(sInstance == NULL)
sInstance = new AssetManager();
return sInstance;
}
void AssetManager::Release() {
delete sInstance;
sInstance = NULL;
}
AssetManager::AssetManager() {
}
AssetManager::~AssetManager() {
//Freeing all loaded Textures
for(auto tex : mTextures) {
if(tex.second != NULL) {
SDL_DestroyTexture(tex.second);
}
}
mTextures.clear();
//Freeing all rendered text
for(auto text : mText) {
if(text.second != NULL) {
SDL_DestroyTexture(text.second);
}
}
mText.clear();
//Freeing all loaded fonts
for(auto font : mFonts) {
if(font.second != NULL) {
TTF_CloseFont(font.second);
}
}
mFonts.clear();
//Freeing all loaded music
for(auto music : mMusic) {
if(music.second != NULL) {
Mix_FreeMusic(music.second);
}
}
mMusic.clear();
//Freeing all loaded sound effects
for(auto sfx : mSFX) {
if(sfx.second != NULL) {
Mix_FreeChunk(sfx.second);
}
}
mSFX.clear();
}
SDL_Texture* AssetManager::GetTexture(std::string filename) {
//Get the full path of the file
std::string fullPath = SDL_GetBasePath();
fullPath.append("Assets/" + filename);
//If the file has not been already loaded, load it and add it to the mTextures map
if(mTextures[fullPath] == nullptr)
mTextures[fullPath] = Graphics::Instance()->LoadTexture(fullPath);
//returning the cached file from the map
return mTextures[fullPath];
}
TTF_Font* AssetManager::GetFont(std::string filename, int size) {
//Get the full path of the font
std::string fullPath = SDL_GetBasePath();
fullPath.append("Assets/" + filename);
//The key takes into account the size of the font aswell since the same font can be opened with different sizes
std::string key = fullPath + (char)size;
//If the font has not been already loaded, load it and add it to the mFonts map
if(mFonts[key] == nullptr) {
mFonts[key] = TTF_OpenFont(fullPath.c_str(), size);
//Error handling for opening the font
if(mFonts[key] == nullptr)
printf("Font Loading Error: Font-%s Error-%s", filename.c_str(), TTF_GetError());
}
//returning the cached font from the map
return mFonts[key];
}
SDL_Texture* AssetManager::GetText(std::string text, std::string filename, int size, SDL_Color color) {
//Get the font from the font cache
TTF_Font* font = GetFont(filename, size);
//The key takes into account the font, text, size, and color to differentiate text textures
std::string key = text + filename + (char)size + (char)color.r + (char)color.b + (char)color.g;
//If the same text has not been rendered before, render it and add it to the mText map
if(mText[key] == nullptr)
mText[key] = Graphics::Instance()->CreateTextTexture(font, text, color);
//returning the cached texture containing the text
return mText[key];
}
Mix_Music* AssetManager::GetMusic(std::string filename) {
//Get the full path of the WAV file
std::string fullPath = SDL_GetBasePath();
fullPath.append("Assets/" + filename);
//If the file has not been already loaded, load it and add it to the mMusic map
if(mMusic[fullPath] == nullptr) {
mMusic[fullPath] = Mix_LoadMUS(fullPath.c_str());
//Error handling for file loading
if(mMusic[fullPath] == NULL)
printf("Music Loading Error: File-%s Error-%s", filename.c_str(), Mix_GetError());
}
//returning the cached file from the map
return mMusic[fullPath];
}
Mix_Chunk* AssetManager::GetSFX(std::string filename) {
//Get the full path of the WAV file
std::string fullPath = SDL_GetBasePath();
fullPath.append("Assets/" + filename);
//If the file has not been already loaded, load it and add it to the mSFX map
if(mSFX[fullPath] == nullptr) {
mSFX[fullPath] = Mix_LoadWAV(fullPath.c_str());
//Error handling for file loading
if(mSFX[fullPath] == NULL)
printf("SFX Loading Error: File-%s Error-%s", filename.c_str(), Mix_GetError());
}
//returning the cached file from the map
return mSFX[fullPath];
}
}