Debian Codegraph @

src/editor.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_events.h> // for SDL_PollEvent, SDL_PRESSED, SDL_Event
25 #include <SDL2/SDL_keycode.h> // for SDLK_DELETE, SDLK_DOWN, SDLK_ESCAPE
26 #include <SDL2/SDL_log.h> // for SDL_LogError, SDL_LOG_CATEGORY_APPLICA...
27 #include <SDL2/SDL_render.h> // for SDL_RenderPresent, SDL_Renderer
28 #include <SDL2/SDL_timer.h> // for SDL_GetTicks
29 #include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_ENTER
30 #include <cstdlib>
31
32 #include "editor.h"
33 #include "menu.h"
34 #include "game.h"
35 #include "level.h"
36 #include "gamepad.h"
37 #include "mouse.h"
38
39 /*** Variables globales ***/
40 /**************************/
41 extern SDL_Renderer *sdlRenderer;
42
43 extern Sprite *Sprites;
44 extern sNewPreference Pref;
45
46 extern int Horloge;
47 extern int HorlogeAvant;
48
49 extern Level level;
50
51 static int NumRail[] = { 10, 10, 10, 0, 10, 1, 2, 3, 10, 4, 5, 6, 7, 8, 9, 10 };
52
53 /*** SDL Main ***/
54 /****************/
55 eMenu Editor::SDLMain(int NumNiv)
56 {
57 int PyE;
58 int cx = 0, cy = 0;
59 bool Boutton = false;
60 int TypeB = -1;
61 int i, d, dx, dy;
62
63 NumN = NumNiv;
64
65 Affiche(); // Charge le tableau
66 SDL_RenderPresent(sdlRenderer);
67
68 Horloge = SDL_GetTicks(); // Prend l'horloge
69
70 Option = rail;
71
72 // Initialise la sourie
73 m_mouse.Init(nullptr);
74
75 // Prend les evenements
76 do {
77 SDL_Event event;
78 while (SDL_PollEvent(&event)) {
79 m_mouse.GetEvent(event, PyE); // Handle mouse
80 m_gamepad.GetEvent(event); // Handle gamepad
81
82 switch (event.type) {
83 case SDL_WINDOWEVENT:
84 if (event.window.event == SDL_WINDOWEVENT_ENTER) {
85 Affiche();
86 }
87 break;
88 case SDL_KEYDOWN:
89 if (event.key.state == SDL_PRESSED) {
90 if (event.key.keysym.sym == SDLK_ESCAPE) {
91 return mMenu;
92 }
93 PrendTouche(event.key.keysym.sym);
94 }
95 break;
96 case SDL_MOUSEBUTTONDOWN:
97 if (event.button.state == SDL_PRESSED) {
98 Boutton = true;
99 TypeB = -1;
100 }
101 break;
102 case SDL_MOUSEBUTTONUP:
103 Boutton = false;
104 if (TypeB != -1 && Option == deco && cx >= LT) { // Si doit effacer une décoration
105 level.T[NumN].NDeco--;
106 }
107 TypeB = -1;
108 break;
109 case SDL_QUIT:
110 return mQuit;
111 break;
112 }
113 }
114
115 // Gère l'appuis du boutton de la sourie
116 cx = m_mouse.Px / D_Case;
117 cy = m_mouse.Py / D_Case;
118
119 if (Boutton && cx < LT) {
120 switch (Option) {
121 case deco:
122 if (TypeB == -1) { // Si première fois que appuis sur la touche
123 for (i = 0; i < level.T[NumN].NDeco; i++) { // Recherche si décoration proche du clic
124 dx = level.T[NumN].Deco[i].x - m_mouse.Px;
125 dy = level.T[NumN].Deco[i].y - m_mouse.Py;
126 d = dx * dx + dy * dy;
127 if (d <= (D_Case * 2) * (D_Case * 2)) {
128 TypeB = i;
129 }
130 }
131 if (TypeB == -1) { // Si doit fair un nouveau décor
132 level.T[NumN].NDeco++;
133 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].NumSpr = NumDeco;
134 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].x = m_mouse.Px;
135 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].y = m_mouse.Py;
136 TypeB = 1;
137 }
138 else { // Fait passe la selection au premier plan
139 level.T[NumN].Deco[level.T[NumN].NDeco].NumSpr = level.T[NumN].Deco[TypeB].NumSpr;
140 level.T[NumN].Deco[level.T[NumN].NDeco].x = level.T[NumN].Deco[TypeB].x;
141 level.T[NumN].Deco[level.T[NumN].NDeco].y = level.T[NumN].Deco[TypeB].y;
142 for (d = TypeB; d < level.T[NumN].NDeco; d++) {
143 level.T[NumN].Deco[d].NumSpr = level.T[NumN].Deco[d + 1].NumSpr;
144 level.T[NumN].Deco[d].x = level.T[NumN].Deco[d + 1].x;
145 level.T[NumN].Deco[d].y = level.T[NumN].Deco[d + 1].y;
146 }
147 NumDeco = level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].NumSpr;
148 }
149 }
150 else { // Si pas la première fois remplace
151 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].NumSpr = NumDeco;
152 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].x = m_mouse.Px;
153 level.T[NumN].Deco[(level.T[NumN].NDeco - 1)].y = m_mouse.Py;
154 }
155 break;
156 case rail:
157 if (TypeB == -1) {
158 if (level.T[NumN].T[cy * LT + cx] != C_Rail) {
159 TypeB = C_Rail;
160 }
161 else {
162 TypeB = C_None;
163 }
164 }
165 level.T[NumN].T[cy * LT + cx] = TypeB;
166 break;
167 case wagon:
168 level.T[NumN].T[cy * LT + cx] = C_Wagon;
169 break;
170 case pluslong:
171 level.T[NumN].T[cy * LT + cx] = C_Allonge;
172 break;
173 case pluscourt:
174 level.T[NumN].T[cy * LT + cx] = C_Reduit;
175 break;
176 case vitesse:
177 level.T[NumN].T[cy * LT + cx] = C_Speed;
178 break;
179 case vie:
180 level.T[NumN].T[cy * LT + cx] = C_Live;
181 break;
182 case (e_Sprite)(locomotive + D_Top):
183 case (e_Sprite)(locomotive + D_Bottom):
184 case (e_Sprite)(locomotive + D_Left):
185 case (e_Sprite)(locomotive + D_Right):
186 level.T[NumN].DepX = cx;
187 level.T[NumN].DepY = cy;
188 level.T[NumN].DepDir = (int)(Option) - (int)(locomotive);
189 break;
190 default:
191 break;
192 }
193 }
194
195 // Gère les Horloges et la pose
196 HorlogeAvant = Horloge;
197 Horloge = SDL_GetTicks();
198 Sleeping();
199
200 // Fait l'affichage
201 Affiche();
202 SDL_RenderPresent(sdlRenderer);
203
204 } while (true);
205
206 return mQuit;
207 }
208
209 /*** Charge un tableau ***/
210 /*************************/
211 void Editor::Affiche() const
212 {
213 int i, x, y, m, cx, cy;
214 unsigned char *T;
215
216 // Prend l'adresse du niveau
217 T = level.T[NumN].T;
218
219 // Fabrique le fond du jeu
220 Sprites[fond].Affiche(400, 300, 0);
221
222 // Affiche le circuit
223 for (i = 0; i < LT * HT; i++) {
224 if (T[i] >= C_Rail && T[i] < C_Fin) {
225 y = i / LT * D_Case + D_Case / 2;
226 x = i % LT * D_Case + D_Case / 2;
227
228 m = 0;
229 cx = i % LT;
230 cy = i / LT;
231 if (cy > 0 && T[i - LT] >= 1 && T[i - LT] < C_Fin) {
232 m += 8;
233 }
234 if (cy < HT - 1 && T[i + LT] >= 1 && T[i + LT] < C_Fin) {
235 m += 4;
236 }
237 if (cx > 0 && T[i - 1] >= 1 && T[i - 1] < C_Fin) {
238 m += 2;
239 }
240 if (cx < LT - 1 && T[i + 1] >= 1 && T[i + 1] < C_Fin) {
241 m += 1;
242 }
243
244 Sprites[rail].Affiche(x, y, NumRail[m]);
245 }
246 }
247
248 // Affiche les décorations
249 for (i = 0; i < level.T[NumN].NDeco; i++) {
250 Sprites[deco].Affiche(level.T[NumN].Deco[i].x, level.T[NumN].Deco[i].y, level.T[NumN].Deco[i].NumSpr);
251 }
252
253 // Affiche numero du niveau
254 AfficheChiffre(740, 130, NumN + 1);
255
256 // Affiche les options
257 for (i = 0; i < LT * HT; i++) {
258 switch (T[i]) {
259 case C_Wagon: // Si un loco
260 Sprites[wagon].Affiche(i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2, 25);
261 break;
262 case C_Allonge: // Si plus long
263 Sprites[pluslong].Affiche(i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2, 25);
264 break;
265 case C_Reduit: // Si plus court
266 Sprites[pluscourt].Affiche(i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2, 25);
267 break;
268 case C_Speed: // Si plus vite
269 Sprites[vitesse].Affiche(i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2, 25);
270 break;
271 case C_Live: // Si une vie
272 Sprites[vie].Affiche(i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2, 25);
273 break;
274 }
275 }
276
277 // Affiche le départ de la locomotive
278 switch (level.T[NumN].DepDir) {
279 case D_Top:
280 Sprites[locomotive].Affiche(level.T[NumN].DepX * D_Case + D_Case / 2, level.T[NumN].DepY * D_Case + D_Case / 2, 0);
281 break;
282 case D_Bottom:
283 Sprites[locomotive].Affiche(level.T[NumN].DepX * D_Case + D_Case / 2, level.T[NumN].DepY * D_Case + D_Case / 2, 40);
284 break;
285 case D_Left:
286 Sprites[locomotive].Affiche(level.T[NumN].DepX * D_Case + D_Case / 2, level.T[NumN].DepY * D_Case + D_Case / 2, 80);
287 break;
288 case D_Right:
289 Sprites[locomotive].Affiche(level.T[NumN].DepX * D_Case + D_Case / 2, level.T[NumN].DepY * D_Case + D_Case / 2, 120);
290 break;
291 }
292
293 // Affiche l'option choisi dans le menu
294 switch (Option) {
295 case rail:
296 case wagon:
297 case pluslong:
298 case pluscourt:
299 case vitesse:
300 case vie:
301 Sprites[Option].Affiche(740, 200, 0);
302 break;
303 case (e_Sprite)(locomotive + D_Top):
304 Sprites[locomotive].Affiche(740, 200, 0);
305 break;
306 case (e_Sprite)(locomotive + D_Bottom):
307 Sprites[locomotive].Affiche(740, 200, 40);
308 break;
309 case (e_Sprite)(locomotive + D_Left):
310 Sprites[locomotive].Affiche(740, 200, 80);
311 break;
312 case (e_Sprite)(locomotive + D_Right):
313 Sprites[locomotive].Affiche(740, 200, 120);
314 break;
315 case deco:
316 Sprites[deco].Affiche(740, 200, NumDeco);
317 break;
318 default:
319 break;
320 }
321
322 // Affiche le curseur
323 if (Option != deco) {
324 Sprites[curseur].Affiche(m_mouse.Px, m_mouse.Py, 0);
325 }
326 else {
327 Sprites[deco].Affiche(m_mouse.Px, m_mouse.Py, NumDeco);
328 }
329 }
330
331 /*** Prend les touches enfoncées ***/
332 /***********************************/
333 void Editor::PrendTouche(int Tou)
334 {
335 int i, j;
336
337 switch (Tou) {
338 case SDLK_PAGEUP:
339 if (NumN < level.N - 1) {
340 NumN++;
341 }
342 else {
343 j = 0;
344 for (i = 0; i < LT * HT; i++) {
345 j += level.T[NumN].T[i];
346 }
347 if (j != 0) {
348 NumN++;
349 level.N++;
350 level.Clear(NumN);
351 }
352 }
353 break;
354 case SDLK_PAGEDOWN:
355 if (NumN) {
356 NumN--;
357 }
358 break;
359 case 'p':
360 Option = deco;
361 NumDeco++;
362 if (NumDeco >= Sprites[deco].N) {
363 NumDeco = 0;
364 }
365 break;
366 case 'o':
367 Option = deco;
368 NumDeco--;
369 if (NumDeco < 0) {
370 NumDeco = Sprites[deco].N - 1;
371 }
372 break;
373 case SDLK_INSERT:
374 level.Ins(NumN);
375 break;
376 case SDLK_DELETE:
377 level.Del(NumN);
378 break;
379 case SDLK_UP:
380 Option = (e_Sprite)(locomotive + D_Top);
381 break;
382 case SDLK_DOWN:
383 Option = (e_Sprite)(locomotive + D_Bottom);
384 break;
385 case SDLK_LEFT:
386 Option = (e_Sprite)(locomotive + D_Left);
387 break;
388 case SDLK_RIGHT:
389 Option = (e_Sprite)(locomotive + D_Right);
390 break;
391 case 'c':
392 level.Clear(NumN);
393 break;
394 case '$':
395 // test si le dernier niveau est vide
396 for (j = i = 0; i < LT * HT; i++) {
397 j += level.T[level.N - 1].T[i];
398 }
399 if (j == 0) {
400 if (NumN < level.N - 1) {
401 level.N--; // Si vide ne le compte pas
402 }
403 }
404
405 // Sauve le niveau
406 if (level.Save() == false) {
407 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error while saving levels");
408 exit(-1);
409 }
410
411 // test le niveau
412 Pref.Niveau = NumN;
413 m_game.SDLMain();
414 m_mouse.Init(nullptr);
415 Affiche();
416 break;
417 case 'a':
418 Option = rail;
419 break;
420 case 'z':
421 Option = wagon;
422 break;
423 case 'e':
424 Option = pluslong;
425 break;
426 case 'r':
427 Option = pluscourt;
428 break;
429 case 't':
430 Option = vitesse;
431 break;
432 case 'y':
433 Option = vie;
434 break;
435 }
436 }