Debian Codegraph @

src/utils.cc

raw symbols SCIP
1 // (_||_/
2 // ( )
3 // ( o 0 )
4 //-OOO°--(_)---°OOO---------------------------------------
5 // Copyright (C) 2006 By Dominique Roux-Serret
6 // .OOOo oOOO. [email protected]
7 //-( )------( )---------------------------------------
8 // ( ( ) / Copyright (C) 2023 By Johnny Jazeix
9 // (_) (_/ [email protected]
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 or version 3 of the License.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License along
21 // with this program; if not, write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 #include <cstdio>
25 #include <cstring>
26 #include <string>
27
28 #include <SDL2/SDL_filesystem.h> // for SDL_GetPrefPath
29 #include <SDL2/SDL_log.h> // for SDL_LogError, SDL_LOG_CATEGORY_APPL...
30 #include <SDL2/SDL_rwops.h> // for SDL_RWclose, SDL_RWFromFile, SDL_RW...
31 #include <SDL2/SDL_stdinc.h> // for SDL_free
32 #include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_ARGB8888
33 #include <SDL2/SDL_surface.h> // for SDL_CreateRGBSurface, SDL_FreeSurface
34
35 #include "config.h"
36 #include "utils.h"
37 #include "preference.h"
38 #include "sprite.h"
39
40 #include "SimpleIni.h"
41
42 /*** Variables globales ***/
43 /**************************/
44 extern sNewPreference Pref;
45 #if defined(__unix__) || defined(__HAIKU__)
46 extern char DefPath[]; // Chemin par defaut dans arg
47 #endif
48
49 /*** Définition générals ***/
50 /***************************/
51 #ifdef __APPLE__
52 #define MAC_LINUX
53 #endif
54
55 #ifdef __unix__
56 #define MAC_LINUX
57 #endif
58
59 #ifdef ANDROID
60 #define MAC_LINUX
61 #endif
62
63 /*** Test si un fichier exite ***/
64 /********************************/
65 bool Utils::FileExists(const char *Path)
66 {
67 SDL_RWops *file = SDL_RWFromFile(Path, "rb");
68
69 if (file == nullptr) {
70 return false;
71 }
72
73 SDL_RWclose(file);
74 return true;
75 }
76
77 /*** Charge un fichier en Mémoire ***/
78 /************************************/
79 long Utils::LoadFile(const char *Path, unsigned char *&Buf)
80 {
81 SDL_RWops *file = SDL_RWFromFile(Path, "rb");
82 if (!file) {
83 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open '%s'", Path);
84 return -1;
85 }
86
87 long const L = SDL_RWsize(file);
88
89 Buf = new unsigned char[L + 1];
90 if (Buf == nullptr) {
91 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Not enough memory");
92 SDL_RWclose(file);
93 return -1;
94 }
95
96 long Compt = L;
97 unsigned char *Po = Buf;
98
99 while (Compt > 1024) {
100 AfficheChargeur();
101 if (SDL_RWread(file, Po, 1, 1024) != 1024) {
102 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error while reading '%s'", Path);
103 SDL_RWclose(file);
104 delete[] Buf;
105 return -1;
106 }
107 Compt -= 1024;
108 Po += 1024;
109 }
110
111 if (Compt) { // Ne fait pas le test à cause d'un bug dans windows
112 SDL_RWread(file, Po, 1, (unsigned int)Compt);
113 }
114
115 SDL_RWclose(file);
116 return L;
117 }
118
119 /*** Sauve un Fichier ***/
120 /************************/
121 bool Utils::SaveFile(const char *Path, char *Buf, long L)
122 {
123 FILE *file;
124
125 file = fopen(Path, "w");
126 if (!file) {
127 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open '%s'", Path);
128 return false;
129 }
130
131 while (L > 512) {
132 if (fwrite(Buf, 1, 512, file) != 512) {
133 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to write in '%s'", Path);
134 fclose(file);
135 return false;
136 }
137 L -= 512;
138 Buf += 512;
139 }
140
141 if (L > 0) {
142 if (fwrite(Buf, 1, (size_t)L, file) != (size_t)L) {
143 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to write in '%s'", Path);
144 fclose(file);
145 return false;
146 }
147 }
148
149 fclose(file);
150 return true;
151 }
152
153 /*** Met le bon chemin pour charger un fichier ***/
154 /*************************************************/
155 #if (defined(__unix__) || defined(ANDROID)) || defined(__HAIKU__) && !defined(__AMIGAOS4__)
156 // Version Linux
157 void Utils::GetPath(char *Path)
158 {
159 char Provi[512];
160
161 strcpy(Provi, Path);
162
163 #ifndef ANDROID
164 if (DefPath[0]) {
165 sprintf(Path, "%s%s", DefPath, Provi);
166 if (Utils::FileExists(Path)) {
167 return;
168 }
169 }
170 #endif
171
172 // Android is directly the filename
173 sprintf(Path, "%s", Provi);
174 if (Utils::FileExists(Path)) {
175 return;
176 }
177
178 sprintf(Path, "%s/%s", LIRI_DATA_DIR, Provi);
179 if (Utils::FileExists(Path)) {
180 return;
181 }
182
183 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find '%s'", Provi);
184 }
185 #endif
186
187 #ifdef __AMIGAOS4__
188 // Version AmigaOS4
189 void Utils::GetPath(char *Path)
190 {
191 char Provi[512];
192
193 strcpy(Provi, Path);
194
195 sprintf(Path, "PROGDIR:%s", Provi);
196 if (Utils::FileExists(Path)) {
197 return;
198 }
199
200 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find '%s'", Path);
201 exit(-1);
202 }
203 #endif
204
205 #ifdef __APPLE__
206 // Version Mac OSX
207 void Utils::GetPath(char *Path)
208 {
209 char Provi[512];
210
211 strcpy(Provi, Path);
212
213 sprintf(Path, "Li-ri.app/Contents/Resources/%s", Provi);
214 if (Utils::FileExists(Path)) {
215 return;
216 }
217
218 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find '%s'", Path);
219 exit(-1);
220 }
221 #endif
222
223 #ifdef _WIN32
224 // Version Windows , chemin directe
225 void Utils::GetPath(char *Path)
226 {
227 char Provi[512];
228
229 strcpy(Provi, Path);
230 char *basePath = SDL_GetBasePath();
231 sprintf(Path, "%s/%s", basePath, Provi);
232 if (Utils::FileExists(Path)) {
233 SDL_free(basePath);
234 return;
235 }
236 SDL_free(basePath);
237 }
238 #endif
239
240 /*** Charge les préferences ***/
241 /******************************/
242 bool Utils::LoadPref()
243 {
244 int L;
245 unsigned char *Provi;
246
247 char oldPathPref[512];
248 char *PrefFolder = SDL_GetPrefPath("Li-Ri", "Li-Ri");
249 sprintf(oldPathPref, "%sli-ri.pref", PrefFolder);
250
251 char newPathPref[512];
252 sprintf(newPathPref, "%sli-ri.ini", PrefFolder);
253
254 SDL_free(PrefFolder);
255
256 // Load new config file if it exists
257 if (Utils::FileExists(newPathPref)) {
258 CSimpleIniA ini(true); // true for unicode
259 SI_Error rc = ini.LoadFile(newPathPref);
260 const char *pv;
261 pv = ini.GetValue("main", "fullscreen");
262 if (pv) {
263 Pref.FullScreen = std::stoi(pv);
264 }
265 pv = ini.GetValue("main", "locale");
266 if (pv) {
267 Pref.Langue = std::stoi(pv);
268 }
269 pv = ini.GetValue("main", "audioVolume");
270 if (pv) {
271 Pref.Volume = std::stof(pv);
272 }
273 pv = ini.GetValue("main", "musicVolume");
274 if (pv) {
275 Pref.VolumeM = std::stof(pv);
276 }
277 pv = ini.GetValue("main", "humanRightsQuiz");
278 if (pv) {
279 Pref.HumanRightsQuiz = std::stoi(pv);
280 }
281 pv = ini.GetValue("easy", "maxLevel");
282 if (pv) {
283 Pref.NiveauMax[0] = std::stof(pv);
284 }
285 pv = ini.GetValue("normal", "maxLevel");
286 if (pv) {
287 Pref.NiveauMax[1] = std::stof(pv);
288 }
289 pv = ini.GetValue("difficult", "maxLevel");
290 if (pv) {
291 Pref.NiveauMax[2] = std::stof(pv);
292 }
293 for (int i = 0; i < 8; ++i) {
294 std::string scoreKey = "score_" + std::to_string(i);
295 std::string nameKey = "name_" + std::to_string(i);
296 if (pv) {
297 pv = ini.GetValue("highscore", scoreKey.c_str());
298 }
299 Pref.Sco[i].Score = std::stoi(pv);
300 if (pv) {
301 pv = ini.GetValue("highscore", nameKey.c_str(), Pref.Sco[i].Name);
302 }
303 strncpy(Pref.Sco[i].Name, pv, 80);
304 }
305 return true;
306 }
307
308 // In case we don't have yet a file in the new format, we check if we don't have a file in the previous format to restore it.
309 if (Utils::FileExists(oldPathPref)) {
310 L = Utils::LoadFile(oldPathPref, Provi);
311 if (L > 0) {
312 sOldPreference oldPref;
313 memcpy((char *)&oldPref, Provi, L);
314 delete[] Provi;
315 Pref.FullScreen = oldPref.FullScreen;
316 Pref.Langue = oldPref.Langue;
317 Pref.Volume = oldPref.Volume;
318 Pref.VolumeM = oldPref.VolumeM;
319 // Try to restore max difficulty from the level stored in conf
320 Pref.NiveauMax[0] = oldPref.Difficulte == Easy ? oldPref.NiveauMax : 0;
321 Pref.NiveauMax[1] = oldPref.Difficulte == Normal ? oldPref.NiveauMax : 0;
322 Pref.NiveauMax[2] = oldPref.Difficulte == Hard ? oldPref.NiveauMax : 0;
323 for (int i = 0; i < 8; ++i) {
324 Pref.Sco[i].Score = oldPref.Sco[i].Score;
325 strncpy(Pref.Sco[i].Name, oldPref.Sco[i].Name, 80);
326 }
327 // TODO Delete old file at some file
328 return true;
329 }
330 }
331
332 return false;
333 }
334
335 /*** Sauve les preferences ***/
336 /*****************************/
337 void Utils::SauvePref()
338 {
339 char PathPref[512];
340 char *PrefFolder = SDL_GetPrefPath("Li-Ri", "Li-Ri");
341 sprintf(PathPref, "%sli-ri.ini", PrefFolder);
342
343 SDL_free(PrefFolder);
344 CSimpleIniA ini(true); // true for unicode
345 ini.SetValue("main", "fullscreen", std::to_string(Pref.FullScreen).c_str());
346 ini.SetValue("main", "locale", std::to_string(Pref.Langue).c_str());
347 ini.SetValue("main", "audioVolume", std::to_string(Pref.Volume).c_str());
348 ini.SetValue("main", "musicVolume", std::to_string(Pref.VolumeM).c_str());
349 ini.SetValue("main", "humanRightsQuiz", std::to_string(Pref.HumanRightsQuiz).c_str());
350 ini.SetValue("easy", "maxLevel", std::to_string(Pref.NiveauMax[0]).c_str());
351 ini.SetValue("normal", "maxLevel", std::to_string(Pref.NiveauMax[1]).c_str());
352 ini.SetValue("difficult", "maxLevel", std::to_string(Pref.NiveauMax[2]).c_str());
353 for (int i = 0; i < 8; ++i) {
354 std::string scoreKey = "score_" + std::to_string(i);
355 std::string nameKey = "name_" + std::to_string(i);
356 ini.SetValue("highscore", scoreKey.c_str(), std::to_string(Pref.Sco[i].Score).c_str());
357 ini.SetValue("highscore", nameKey.c_str(), Pref.Sco[i].Name);
358 }
359 ini.SaveFile(PathPref);
360 }
361
362 void Utils::doScreenshot(SDL_Renderer *renderer)
363 {
364 static int screenshotNumber = 0;
365 char screenshotName[80];
366 int w, h;
367 SDL_GetRendererOutputSize(renderer, &w, &h);
368 SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24,
369 SDL_PIXELFORMAT_RGB24);
370 SDL_RenderReadPixels(renderer, nullptr, SDL_PIXELFORMAT_RGB24, surface->pixels, surface->pitch);
371 sprintf(screenshotName, "screenshot%i.bmp", screenshotNumber++);
372 SDL_SaveBMP(surface, screenshotName);
373 SDL_FreeSurface(surface);
374 }