Debian Codegraph @

src/sprite.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 <SDL2/SDL_log.h>
25 #include <SDL2/SDL_rect.h>
26 #include <SDL2/SDL_surface.h>
27 #include <SDL2/SDL_timer.h>
28 #include <cstring>
29 #include "sprite.h"
30 #include "preference.h"
31 #include "utils.h"
32
33 /*** Variables Globales ***/
34 /**************************/
35 extern SDL_Renderer *sdlRenderer;
36 extern Sprite *Sprites;
37 extern int NSprites;
38 extern sNewPreference Pref;
39
40 static const char *OrdreTexte = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-!?*+<>%$()&;";
41 static const char *OrdreTexte2 = "abcdefghijklmnopqrstuvwxyz0123456789,_|?*+<>%$[]&;";
42 static int TableTexte[256];
43
44 char Langue[31][16]; // Mémorise les noms des langues
45 int NTextes = 0;
46 bool AfficheC = false; // Si peut afficher le chargeur lors du chargement
47
48 #define N_SPRITESFOND 2
49
50 /*** Affiche le chargeur lors du chargement ***/
51 /**********************************************/
52 void AfficheChargeur()
53 {
54 static int NumAf = -1; // Numéro su sprite affiché
55 int i, Old;
56 int Hor;
57
58 if (AfficheC == true) {
59 Hor = SDL_GetTicks();
60 i = (Hor / (1000 / 25)) % Sprites[chargeur].N; // Calcule le numéro su sprite à afficher
61
62 if (i != NumAf) {
63 Old = NumAf;
64 NumAf = i;
65 Sprites[chargeur].Affiche(400, 300, NumAf);
66 SDL_RenderPresent(sdlRenderer);
67 // TODO if(Old!=-1) Sprites[chargeur].Efface(400,300,Old,sdlVideo);
68 }
69 }
70 }
71
72 /*** Charge les Sprites d'une langue ***/
73 /***************************************/
74 bool LoadLangue()
75 {
76 long L, P;
77 int i;
78 unsigned char *Buf;
79 char PathFile[512];
80
81 strcpy(PathFile, Langue[Pref.Langue]);
82 Utils::GetPath(PathFile);
83 if (Utils::FileExists(PathFile) == false) {
84 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find '%s'", Langue[Pref.Langue]);
85 return false;
86 }
87 L = Utils::LoadFile(PathFile, Buf);
88
89 // Lit les sprites
90 P = 0;
91 for (i = 0; i < NTextes; i++) {
92 if (Sprites[T_level + i].N) {
93 Sprites[T_level + i].Delete();
94 }
95 if (Sprites[T_level + i].Load(Buf, P) == false) {
96 return false;
97 }
98 }
99 delete[] Buf; // Libère la mémoire du fichier des sprites
100
101 return true;
102 }
103
104 /*** Charge les Sprites du jeu ***/
105 /*********************************/
106 bool LoadSprites()
107 {
108 long L, P;
109 int i;
110 unsigned char *Buf;
111 int NSp;
112
113 char PathFile[512] = "language.dat";
114 Utils::GetPath(PathFile);
115
116 // Initialise la table de caractaire des textes
117 for (i = 0; i < 256; i++) {
118 TableTexte[i] = -1;
119 }
120 i = 0;
121 while (OrdreTexte[i] != 0) {
122 TableTexte[(int)(OrdreTexte[i])] = i;
123 i++;
124 }
125 i = 0;
126 while (OrdreTexte2[i] != 0) {
127 TableTexte[(int)(OrdreTexte2[i])] = i;
128 i++;
129 }
130
131 // *** Charge le fichier des langues ***
132 // *************************************
133 if (Utils::FileExists(PathFile) == false) {
134 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find 'language.dat'");
135 return false;
136 }
137 L = Utils::LoadFile(PathFile, Buf);
138
139 // Prend le nombre de sprites
140 NSp = (int)(Buf[0]) * 256 + (int)(Buf[1]);
141 NSp += N_SPRITESFOND + 2;
142 NTextes = (int)(Buf[2]) * 256 + (int)(Buf[3]);
143 Pref.NLangues = (int)(Buf[4]) * 256 + (int)(Buf[5]);
144
145 NSprites = NSp + NTextes + Pref.NLangues;
146 Sprites = new Sprite[NSprites];
147
148 // Récupère les nom des langues
149 P = 6;
150 for (i = 0; i < Pref.NLangues; i++) {
151 strcpy(Langue[i], (char *)(Buf + P));
152 P += strlen((char *)(Buf + P)) + 1;
153 }
154
155 // Charge les sprites des langues
156 for (i = 0; i < Pref.NLangues; i++) {
157 if (Sprites[T_Langue + i].Load(Buf, P) == false) {
158 return false;
159 }
160 }
161
162 if (Sprites[chargeur].Load(Buf, P) == false) {
163 return false; // Sprite du chargeur
164 }
165 AfficheC = true; // Peut afficher le sprite du chargeur
166
167 delete[] Buf;
168
169 // *** Charge le fichier des sprites ***
170 // *************************************
171 strcpy(PathFile, "sprites.dat");
172 Utils::GetPath(PathFile);
173 if (Utils::FileExists(PathFile) == false) {
174 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find 'sprites.dat'");
175 return false;
176 }
177 L = Utils::LoadFile(PathFile, Buf);
178
179 // Lit les sprites
180 P = 0;
181 for (i = 0; i < NSp; i++) {
182 AfficheChargeur();
183 switch (i) {
184 case fjeu:
185 case fmenu:
186 if (Sprites[i].Nouveau(800, 600) == false) {
187 return false;
188 }
189 break;
190 case rope:
191 Sprites[i].N = 0;
192 break;
193 case chargeur:
194 break;
195 default:
196 if (Sprites[i].Load(Buf, P) == false) {
197 return false;
198 }
199 }
200 }
201
202 delete[] Buf; // Libère la mémoire du fichier des sprites
203
204 // *** Charge la langue ***
205 // ************************
206 if (Pref.Langue != -1) {
207 LoadLangue();
208 }
209
210 AfficheC = false; // N'affiche plus les sprites du chargeur
211 return true;
212 }
213
214 /*** Retourne la longueur d'un nombre ***/
215 /****************************************/
216 int LongueurChiffre(int C)
217 {
218 int l = 0;
219
220 do {
221 l += Sprites[chiffres].Dim[(C % 10)].L;
222 C /= 10;
223 if (C) {
224 l += ECART_ENTRE_CHIFFRE;
225 }
226 } while (C);
227
228 return l;
229 }
230
231 /*** Retourne la longueur d'un texte ***/
232 /***************************************/
233 int LongueurString(char *Texte)
234 {
235 int i = 0;
236 int l = 0;
237 int Le;
238
239 while (Texte[i] != 0) {
240 Le = (int)(Texte[i]);
241 if (TableTexte[Le] != -1) {
242 l += Sprites[lettres].Dim[(TableTexte[Le])].L;
243 if (Texte[i + 1] != 0) {
244 l += ECART_ENTRE_LETTRE;
245 }
246 }
247 else {
248 if (Le == (int)(' ')) {
249 l += LONGUEUR_ESPACE;
250 }
251 }
252
253 i++;
254 }
255
256 return l;
257 }
258
259 /*** Test si un caracataire existe ***/
260 /*************************************/
261 bool CharExiste(char C)
262 {
263 if ((int)(C) < 0) {
264 return false;
265 }
266 if (C == ' ') {
267 return true;
268 }
269 if (TableTexte[(int)(C)] != -1) {
270 return true;
271 }
272 return false;
273 }
274 /*** Affiche un nombre ***/
275 /*************************/
276 void AfficheChiffre(int x, int y, int Nombre, SDL_Texture *Fond)
277 {
278 int const l = LongueurChiffre(Nombre);
279
280 x += l / 2;
281 do {
282 Sprites[chiffres].Affiche(x - (Sprites[chiffres].Dim[(Nombre % 10)].L) / 2, y, Nombre % 10, Fond);
283 x -= Sprites[chiffres].Dim[(Nombre % 10)].L + ECART_ENTRE_CHIFFRE;
284 Nombre /= 10;
285 } while (Nombre);
286 }
287
288 /*** Affiche un Texte ***/
289 /************************/
290 void AfficheString(int x, int y, char *Texte, SDL_Texture *Fond)
291 {
292 int i = 0;
293 int Le;
294
295 // TODO Handle here unicode
296 while (Texte[i] != 0) {
297 Le = (int)(Texte[i]);
298
299 if (TableTexte[Le] != -1) { // Si un caractaire connue
300 Le = TableTexte[Le];
301 Sprites[lettres].Affiche(x + (Sprites[lettres].Dim[Le].L / 2), y, Le, Fond);
302 x += Sprites[lettres].Dim[Le].L + ECART_ENTRE_LETTRE;
303 }
304 else { // Si un espace
305 if (Le == (int)(' ')) {
306 x += LONGUEUR_ESPACE - ECART_ENTRE_LETTRE;
307 }
308 }
309
310 i++;
311 }
312 }
313
314 /*** Affiche un text dans la langue ***/
315 /**************************************/
316 void AfficheText(int x, int y, e_Sprite Text, SDL_Texture *Fond)
317 {
318 Sprites[Text].Affiche(x, y, 0, Fond);
319 }
320
321 /*** Constructeur ***/
322 /********************/
323 Sprite::~Sprite()
324 {
325 if (N) {
326 for (int i = 0; i < N; i++) {
327 SDL_DestroyTexture(Image[i]);
328 }
329 delete[] Image;
330 delete[] Dim;
331 }
332 }
333
334 /*** Charge les sprites ***/
335 /**************************/
336 bool Sprite::Load(unsigned char *Buf, long &P)
337 {
338 int i, j;
339 unsigned char *B;
340 unsigned long ul = 1; // test le type de processeur
341 unsigned char *pul = (unsigned char *)(&ul);
342
343 // Prend nombre de sprites
344 N = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
345 P += 2;
346 Dim = new s_Dim[N];
347 Image = new SDL_Texture *[N];
348
349 // Lit tous les sprites
350 for (i = 0; i < N; i++) {
351 // Affiche l'animation de chargement
352 AfficheChargeur();
353
354 // Lit les dimensions
355 Dim[i].L = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
356 P += 2;
357 Dim[i].H = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
358 P += 2;
359 Dim[i].cx = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
360 P += 2;
361 Dim[i].cy = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
362 P += 2;
363 Dim[i].bpp = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
364 P += 2;
365
366 // Fabrique la surface
367 SDL_Surface *surface = SDL_CreateRGBSurface(0, Dim[i].L, Dim[i].H, Dim[i].bpp * 8,
368 0xff, 0xff00, 0xff0000, 0xff000000 * (Dim[i].bpp - 3));
369 if (surface == nullptr) {
370 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the SDL Surface");
371 return false;
372 }
373
374 // Copie les pixels
375 SDL_LockSurface(surface);
376 B = (unsigned char *)surface->pixels;
377
378 if (pul[0] == 0) { // Processeur type Power PC, 68000, ..
379 for (j = 0; j < Dim[i].L * Dim[i].H * Dim[i].bpp; j += Dim[i].bpp) {
380 if (Dim[i].bpp == 4) {
381 B[j + 3] = Buf[P++];
382 }
383 B[j + 2] = Buf[P++];
384 B[j + 1] = Buf[P++];
385 B[j] = Buf[P++];
386 }
387 }
388 else {
389 for (j = 0; j < Dim[i].L * Dim[i].H * Dim[i].bpp; j++) {
390 B[j] = Buf[P++];
391 }
392 }
393
394 SDL_UnlockSurface(surface);
395 Image[i] = SDL_CreateTextureFromSurface(sdlRenderer, surface);
396 SDL_FreeSurface(surface);
397 }
398
399 return true;
400 }
401
402 /*** Affiche le sprite ***/
403 /*************************/
404 void Sprite::Affiche(int X, int Y, int NumSpr, SDL_Texture *Fond) const
405 {
406 SDL_Rect Position;
407 SDL_Rect Di;
408
409 Position.x = X - Dim[NumSpr].cx;
410 Position.y = Y - Dim[NumSpr].cy;
411
412 Position.w = Position.h = Di.x = Di.y = 0;
413 Di.w = Dim[NumSpr].L;
414 Di.h = Dim[NumSpr].H;
415 Position.w = Di.w;
416 Position.h = Di.h;
417 // TODO if(Fond==NULL) Fond=sdlRenderer;
418
419 SDL_RenderCopy(sdlRenderer, Image[NumSpr], nullptr, &Position);
420 }
421
422 /*** Print the white rope between two wagons ***/
423 /*********************************/
424 void Sprite::PrintRope(int dx, int dy, int fx, int fy)
425 {
426 SDL_SetRenderDrawColor(sdlRenderer, 255, 255, 255, 0);
427 SDL_RenderDrawLine(sdlRenderer, dx, dy, fx, fy);
428 SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 0);
429 }
430
431 /*** Alloue un nouveau sprite vide ***/
432 /*************************************/
433 bool Sprite::Nouveau(int Lx, int Ly)
434 {
435 Delete(); // Efface au cas ou
436
437 N = 1;
438 Dim = new s_Dim[N];
439 Image = new SDL_Texture *[N];
440
441 Dim[0].L = Lx;
442 Dim[0].H = Ly;
443 Dim[0].cx = Lx / 2;
444 Dim[0].cy = Ly / 2;
445 Dim[0].bpp = 3; // No transparency
446
447 // Fabrique la surface
448 SDL_Surface *surface = SDL_CreateRGBSurface(0, Dim[0].L, Dim[0].H, Dim[0].bpp * 8,
449 0xff, 0xff00, 0xff0000, 0xff000000 * (Dim[0].bpp - 3));
450 if (surface == nullptr) {
451 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the SDL Surface");
452 return false;
453 }
454 Image[0] = SDL_CreateTextureFromSurface(sdlRenderer, surface);
455 SDL_FreeSurface(surface);
456 return true;
457 }
458
459 /*** Efface le sprite ***/
460 /************************/
461 void Sprite::Delete()
462 {
463 if (N) {
464 for (int i = 0; i < N; i++) {
465 SDL_DestroyTexture(Image[i]);
466 }
467 delete[] Image;
468 delete[] Dim;
469 N = 0;
470 }
471 }