Debian Codegraph @

source/code/sago/SagoDataHolder.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2015 Poul Sander
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "SagoDataHolder.hpp"
26 #include <iostream>
27 #include <mutex>
28 #include <map>
29 #include <vector>
30 #include <physfs.h>
31 #include <memory>
32 #include <SDL_mixer.h>
33 #include "SagoMiscSdl2.hpp"
34 #include "SagoMisc.hpp"
35
36 #if PHYSFS_VER_MAJOR < 3
37 #define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z)
38 #define PHYSFS_writeBytes(X,Y,Z) PHYSFS_write(X,Y,1,Z)
39 #endif
40
41 namespace sago {
42
43 struct SagoDataHolder::SagoDataHolderData {
44 std::map<std::string, SDL_Texture*> textures;
45 std::map<std::string, std::map<int, TTF_Font*> > fonts; //font, ptsize
46 std::map<std::string, Mix_Music*> music;
47 std::map<std::string, Mix_Chunk*> sounds;
48 std::vector<SDL_RWops*> rwOpsToFree;
49 std::vector<std::unique_ptr<char[]>> dataToFree;
50 bool verbose = false;
51 Uint64 version = 1;
52 SDL_Renderer* renderer = nullptr;
53 };
54
55 static void printFileWeLoad(const std::string& value) {
56 std::cout << "Loading " << value << "\n";
57 }
58
59 SagoDataHolder::SagoDataHolder() {
60 data = new SagoDataHolderData();
61 }
62
63 SagoDataHolder::SagoDataHolder(SDL_Renderer* renderer) {
64 data = new SagoDataHolderData();
65 data->renderer = renderer;
66 }
67
68 void SagoDataHolder::invalidateAll(SDL_Renderer* renderer) {
69 invalidateAll();
70 data->renderer = renderer;
71 }
72
73 void SagoDataHolder::invalidateAll() {
74 data->version++;
75 for (auto& item : data->textures) {
76 SDL_DestroyTexture(item.second);
77 }
78 data->textures.clear();
79 for (auto& item : data->music) {
80 Mix_FreeMusic(item.second);
81 }
82 data->music.clear();
83 for (auto& item : data->sounds) {
84 Mix_FreeChunk(item.second);
85 }
86 data->sounds.clear();
87 for (auto& item : data->fonts) {
88 for (auto& item2 : item.second) {
89 TTF_CloseFont(item2.second);
90 }
91 }
92 data->fonts.clear();
93 for (auto& item : data->rwOpsToFree) {
94 SDL_FreeRW(item);
95 }
96 data->rwOpsToFree.clear();
97 }
98
99 SagoDataHolder::~SagoDataHolder() {
100 invalidateAll();
101 delete data;
102 }
103
104 SDL_Texture* SagoDataHolder::getTexturePtr(const std::string& textureName) const {
105 if (!data->renderer) {
106 throw std::runtime_error("SagoDataHolder used before setting the renderer");
107 }
108 SDL_Texture* ret = data->textures[textureName];
109 if (ret) {
110 return ret;
111 }
112 std::string path = "textures/"+textureName+".png";
113 if (data->verbose) {
114 printFileWeLoad(path);
115 }
116 if (!PHYSFS_exists(path.c_str())) {
117 // We did not find the png file. Try to see if there are a jpg file.
118 std::string jpg_path = "textures/"+textureName+".jpg";
119 if (!PHYSFS_exists(jpg_path.c_str())) {
120 sago::SagoFatalErrorF("getTextureFailed - Texture does not exist: %s", path.c_str());
121 }
122 path = jpg_path;
123 }
124 unsigned int m_size = 0;
125 std::unique_ptr<char[]> m_data;
126 ReadBytesFromFile(path.c_str(), m_data, m_size);
127 SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size);
128 //The above might fail an return null.
129 if (!rw) {
130 std::cerr << "Error. Corrupt data file!\n";
131 return NULL;
132 }
133 SDL_Surface* surface = IMG_Load_RW(rw,true);
134
135 ret = SDL_CreateTextureFromSurface(data->renderer, surface);
136
137 if (!ret) {
138 std::cerr << "getTextureFailed to load " << path << "\n";
139 }
140 SDL_FreeSurface(surface);
141 data->textures[textureName] = ret;
142 return ret;
143 }
144
145 TTF_Font* SagoDataHolder::getFontPtr(const std::string& fontName, int ptsize) const {
146 TTF_Font* ret = data->fonts[fontName][ptsize];
147 if (ret) {
148 return ret;
149 }
150 std::string path = "fonts/"+fontName+".ttf";
151 if (data->verbose) {
152 printFileWeLoad(path);
153 }
154 if (!PHYSFS_exists(path.c_str())) {
155 std::cerr << "getFontPtr - Font does not exists: " << path << "\n";
156 return ret;
157 }
158 unsigned int m_size = 0;
159 std::unique_ptr<char[]> m_data;
160 ReadBytesFromFile(path.c_str(), m_data, m_size);
161
162 SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size);
163
164 //The above might fail an return null.
165 if (!rw) {
166 std::cerr << "Error: Corrupt data file!\n";
167 return ret;
168 }
169
170 ret = TTF_OpenFontRW(rw, SDL_FALSE, ptsize);
171 if (!ret) {
172 std::cerr << "Error openening font: " << fontName << " because: " << TTF_GetError() << "\n";
173 }
174 data->fonts[fontName][ptsize] = ret;
175 data->dataToFree.push_back(std::move(m_data));
176 data->rwOpsToFree.push_back(rw);
177 return ret;
178 }
179
180 Mix_Music* SagoDataHolder::getMusicPtr(const std::string& musicName) const {
181 Mix_Music* ret = data->music[musicName];
182 if (ret) {
183 return ret;
184 }
185 std::string path = "music/"+musicName+".ogg";
186 if (data->verbose) {
187 printFileWeLoad(path);
188 }
189 if (!PHYSFS_exists(path.c_str())) {
190 std::cerr << "getMusicPtr - Music file does not exists: " << path << "\n";
191 return ret;
192 }
193 unsigned int m_size = 0;
194 std::unique_ptr<char[]> m_data;
195 ReadBytesFromFile(path.c_str(), m_data, m_size);
196 SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size);
197
198 //The above might fail an return null.
199 if (!rw) {
200 std::cerr << "Error. Corrupt data file!\n";
201 return NULL;
202 }
203
204 ret = Mix_LoadMUS_RW(rw, SDL_TRUE); //SDL_TRUE causes rw to be freed
205
206 if (!ret) {
207 std::cerr << "getMusicPtr to load " << path << " because: " << Mix_GetError() << "\n";
208 }
209 data->music[musicName] = ret;
210 data->dataToFree.push_back(std::move(m_data));
211 return ret;
212 }
213
214
215 Mix_Chunk* SagoDataHolder::getSoundPtr(const std::string& soundName) const {
216 Mix_Chunk* ret = data->sounds[soundName];
217 if (ret) {
218 return ret;
219 }
220 std::string path = "sounds/"+soundName+".ogg";
221 if (data->verbose) {
222 printFileWeLoad(path);
223 }
224 if (!PHYSFS_exists(path.c_str())) {
225 std::cerr << "getSoundPtr - Sound file does not exists: " << path << "\n";
226 return ret;
227 }
228 unsigned int m_size = 0;
229 std::unique_ptr<char[]> m_data;
230 ReadBytesFromFile(path.c_str(), m_data, m_size);
231 SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size);
232
233 //The above might fail an return null.
234 if (!rw) {
235 std::cerr << "Error. Corrupt data file!\n";
236 return NULL;
237 }
238
239 ret = Mix_LoadWAV_RW(rw, SDL_TRUE);
240 data->sounds[soundName] = ret;
241 data->dataToFree.push_back(std::move(m_data));
242 return ret;
243 }
244
245 void SagoDataHolder::setVerbose(bool value) {
246 data->verbose = value;
247 }
248
249 Uint64 SagoDataHolder::getVersion() const {
250 return data->version;
251 }
252
253 TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string& textureName) {
254 this->holder = holder;
255 this->version = 0;
256 this->textureName = textureName;
257 this->data = nullptr;
258 }
259
260 SDL_Texture* TextureHandler::get() const {
261 if (version != holder->getVersion()) {
262 //The holder has been invalidated
263 this->data = this->holder->getTexturePtr(textureName);
264 }
265 return data;
266 }
267
268
269 MusicHandler::MusicHandler(const SagoDataHolder* holder, const std::string& musicName) {
270 this->holder = holder;
271 this->version = 0;
272 this->musicName = musicName;
273 this->data = nullptr;
274 }
275
276 Mix_Music* MusicHandler::get() {
277 if (version != holder->getVersion()) {
278 //The holder has been invalidated
279 this->data = this->holder->getMusicPtr(musicName);
280 }
281 return data;
282 }
283
284 SoundHandler::SoundHandler(const SagoDataHolder* holder, const std::string& soundName) {
285 this->holder = holder;
286 this->version = 0;
287 this->soundName = soundName;
288 this->data = nullptr;
289 }
290
291 Mix_Chunk* SoundHandler::get() {
292 if (version != holder->getVersion()) {
293 //The holder has been invalidated
294 this->data = this->holder->getSoundPtr(soundName);
295 }
296 return data;
297 }
298
299
300 TextureHandler SagoDataHolder::getTextureHandler(const std::string& textureName) const {
301 return TextureHandler(this, textureName);
302 }
303
304 MusicHandler SagoDataHolder::getMusicHandler(const std::string& musicName) const {
305 return MusicHandler(this, musicName);
306 }
307
308 SoundHandler SagoDataHolder::getSoundHandler(const std::string& soundName) const {
309 return SoundHandler(this, soundName);
310 }
311
312 } //name space sago