Debian Codegraph @

src/menu/mainmenu.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file mainmenu.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd of August 2005: Created menu.c
10 * - 3rd of February 2009: Renamed menu.c to menu.cpp
11 * - 19th July 2009: Created menumain.cpp from parts of menu.cpp
12 * - 26th July 2009: Renamed menumain.cpp to mainmenu.cpp
13 *
14 * @par Licence:
15 * Copyright (c) 2005-2017 AJ Thomson
16 *
17 * OpenJazz is distributed under the terms of
18 * the GNU General Public License, version 2.0
19 *
20 * @par Description:
21 * Deals with the running of the main menu and its generic sub-menus.
22 *
23 */
24
25
26 #include "menu.h"
27 #include "plasma.h"
28
29 #include "game/game.h"
30 #include "io/controls.h"
31 #include "io/gfx/video.h"
32 #include "io/gfx/font.h"
33 #include "io/sound.h"
34 #include "jj1/scene/jj1scene.h"
35 #include "loop.h"
36 #include "util.h"
37 #include "io/log.h"
38 #include "logo.h"
39 #include "version.h"
40
41 #include <miniz.h>
42 #include <time.h>
43
44
45 /**
46 * Create the main menu.
47 */
48 MainMenu::MainMenu () {
49
50 File *file;
51 time_t currentTime;
52
53 // Load the OpenJazz logo
54
55 unsigned char* pixels = new unsigned char[oj_logo.size];
56 size_t res = tinfl_decompress_mem_to_mem(pixels, oj_logo.size, oj_logo.data,
57 oj_logo.compressed_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF|TINFL_FLAG_PARSE_ZLIB_HEADER);
58 if (res == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED || res != oj_logo.size) {
59
60 LOG_WARN("Could not uncompress logo (expected: %d, actual: %zu).", oj_logo.size, res);
61 logo = nullptr;
62
63 } else {
64
65 logo = video.createSurface(pixels, oj_logo.width, oj_logo.height);
66
67 }
68 delete[] pixels;
69
70 // Load the menu graphics
71
72 try {
73
74 file = new File("MENU.000", PATH_TYPE_GAME);
75
76 } catch (int e) {
77
78 video.destroySurface(logo);
79
80 throw;
81
82 }
83
84 // only available in Holiday Hare 94/95
85 if (file->getSize() > 200000) {
86
87 time(&currentTime);
88
89 // In December, load the Christmas menu graphics
90 if (localtime(&currentTime)->tm_mon == 11) {
91
92 LOG_TRACE("Loading christmas menu theme.");
93
94 file->skipRLE();
95 file->skipRLE();
96 file->skipRLE();
97
98 file->loadPalette(palette);
99 background = file->loadSurface(SW, SH);
100 highlight = file->loadSurface(SW, SH);
101
102 } else {
103
104 file->loadPalette(palette);
105 background = file->loadSurface(SW, SH);
106 highlight = file->loadSurface(SW, SH);
107
108 file->skipRLE();
109 file->skipRLE();
110 file->skipRLE();
111
112 }
113
114 } else {
115
116 file->loadPalette(palette);
117 background = file->loadSurface(SW, SH);
118 highlight = file->loadSurface(SW, SH);
119
120 }
121
122 video.enableColorKey(background, 0);
123 video.enableColorKey(highlight, 0);
124 if (logo) video.enableColorKey(logo, 28);
125
126 gameMenu = new GameMenu(file);
127
128 delete file;
129
130 }
131
132
133 /**
134 * Delete the main menu.
135 */
136 MainMenu::~MainMenu () {
137
138 video.destroySurface(background);
139 video.destroySurface(highlight);
140 video.destroySurface(logo);
141
142 delete gameMenu;
143
144 }
145
146
147 /**
148 * Process a main menu selection.
149 *
150 * @param option Chosen menu option
151 *
152 * @return Error code
153 */
154 int MainMenu::select (int option) {
155
156 JJ1Scene *scene;
157 SetupMenu setupMenu;
158
159 playConfirmSound();
160
161 switch (option) {
162
163 case 0: // New game
164
165 if (gameMenu->newGame() == E_QUIT) return E_QUIT;
166
167 break;
168
169 case 1: // Load game
170
171 if (gameMenu->loadGame() == E_QUIT) return E_QUIT;
172
173 break;
174
175 case 2: // Instructions
176
177 try {
178
179 scene = new JJ1Scene("INSTRUCT.0SC");
180
181 } catch (int e) {
182
183 if (message("COULD NOT LOAD INSTRUCTIONS") == E_QUIT) return E_QUIT;
184
185 break;
186
187 }
188
189 if (scene->play() == E_QUIT) {
190
191 delete scene;
192
193 return E_QUIT;
194
195 }
196
197 delete scene;
198
199 break;
200
201 case 3: // Setup options
202
203 if (setupMenu.setupMain() == E_QUIT) return E_QUIT;
204
205 break;
206
207 case 4: // Order info
208
209 try {
210
211 scene = new JJ1Scene("ORDER.0SC");
212
213 } catch (int e) {
214
215 if (message("COULD NOT LOAD ORDER INFO") == E_QUIT) return E_QUIT;
216
217 break;
218
219 }
220
221 if (scene->play() == E_QUIT) {
222
223 delete scene;
224
225 return E_QUIT;
226
227 }
228
229 delete scene;
230
231 break;
232
233 case 5: // Exit
234
235 return E_RETURN;
236
237 }
238
239 // Restore the main menu palette
240 video.setPalette(palette);
241
242 return E_NONE;
243
244 }
245
246
247 /**
248 * Run the main menu.
249 *
250 * @return Error code
251 */
252 int MainMenu::main () {
253
254 SDL_Rect options[6] = {
255 {92, 35, 136, 22},
256 {92, 57, 140, 22},
257 {88, 83, 144, 22},
258 {86, 109, 150, 23},
259 {82, 137, 156, 26},
260 {78, 166, 166, 29}};
261 int macroType[4];
262 File* file;
263 char* fileName;
264 Plasma plasma;
265 SDL_Rect dst;
266 int option, macro, x, y, ret;
267 unsigned int idleTime;
268
269 option = 0;
270
271 video.setPalette(palette);
272
273 playMusic("MENUSNG.PSM");
274
275
276 // Demo timeout
277 idleTime = globalTicks + T_DEMO;
278
279 // Check for demo macros
280
281 fileName = createString("MACRO.1");
282
283 for (macro = 0; macro < 4; macro++)
284 {
285
286 try {
287
288 file = new File(fileName, PATH_TYPE_GAME);
289 macroType[macro] = file->loadChar();
290 delete file;
291
292 } catch (int e) {
293
294 macroType[macro] = -1;
295
296 }
297
298 fileName[6]++;
299
300 }
301
302 macro = 3;
303
304 delete[] fileName;
305
306
307 while (true) {
308
309 if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT;
310
311 if (controls.release(C_ESCAPE)) option = 5;
312
313 if (controls.release(C_UP)) option = (option + 5) % 6;
314
315 if (controls.release(C_DOWN)) option = (option + 1) % 6;
316
317 if (controls.release(C_ENTER)) {
318
319 ret = select(option);
320
321 if (ret < 0) return ret;
322
323 // New demo timeout
324 idleTime = globalTicks + T_DEMO;
325
326 }
327
328 if (controls.getCursor(x, y)) {
329
330 int count;
331
332 x -= (canvasW - SW) >> 1;
333 y -= (canvasH - SH) >> 1;
334
335 for (count = 0; count < 6; count++) {
336
337 if ((x >= options[count].x) &&
338 (x < options[count].x + options[count].w) &&
339 (y >= options[count].y) &&
340 (y < options[count].y + options[count].h)) {
341
342 option = count;
343
344 if (controls.wasCursorReleased()) {
345
346 ret = select(option);
347
348 if (ret < 0) return ret;
349
350 }
351
352 // New demo timeout
353 idleTime = globalTicks + T_DEMO;
354
355 break;
356
357 }
358 }
359
360 }
361
362
363 if (idleTime <= globalTicks) {
364
365 Game* game = NULL;
366
367 try {
368
369 game = new LocalGame("", difficultyType::Easy);
370
371 } catch (int e) {
372
373 // Do nothing
374
375 }
376
377 if (game) {
378
379 // Load the macro
380
381 x = macro;
382 macro = (macro + 1) & 3;
383
384 while ((macroType[macro] != 0xFF) && (macro != x))
385 macro = (macro + 1) & 3;
386
387 if (macro != x) {
388
389 fileName = createString("MACRO.1");
390 fileName[6] += macro;
391
392 if (game->playLevel(fileName) == E_QUIT) {
393
394 delete[] fileName;
395 delete game;
396
397 return E_QUIT;
398
399 }
400
401 delete[] fileName;
402
403 }
404
405
406 delete game;
407
408 playMusic("MENUSNG.PSM");
409
410 // Restore the main menu palette
411 video.setPalette(palette);
412
413 }
414
415 idleTime = globalTicks + T_DEMO;
416
417 }
418
419 SDL_Delay(T_MENU_FRAME);
420
421
422 //as long as we're drawing plasma, we don't need to clear the screen.
423 //video.clearScreen(28);
424
425 plasma.draw();
426
427 // draw logo and version string
428
429 if (logo) {
430 dst.x = (canvasW >> 2) - 72;
431 dst.y = canvasH - (canvasH >> 2);
432 SDL_BlitSurface(logo, NULL, canvas, &dst);
433 }
434
435 panelBigFont->mapPalette(0, MAX_PALETTE_COLORS, 8, 8);
436 if(canvasW > SW)
437 panelBigFont->showString("OpenJazz", 16, canvasH, alignX::Left, alignY::Bottom);
438 panelBigFont->showString(oj_version, canvasW - 16, canvasH, alignX::Right, alignY::Bottom);
439 panelBigFont->restorePalette();
440
441 dst.x = (canvasW - SW) >> 1;
442 dst.y = (canvasH - SH) >> 1;
443 SDL_BlitSurface(background, NULL, canvas, &dst);
444
445 dst.x = ((canvasW - SW) >> 1) + options[option].x;
446 dst.y = ((canvasH - SH) >> 1) + options[option].y;
447 SDL_BlitSurface(highlight, options + option, canvas, &dst);
448
449 }
450
451 return E_NONE;
452
453 }
454
455
456 /**
457 * Skip the main menu. Directly loads game
458 *
459 * @return Error code
460 */
461 int MainMenu::skip (int levelNum, int worldNum) {
462
463 if (gameMenu->loadGame(levelNum, worldNum) == E_QUIT) return E_QUIT;
464
465 return E_RETURN;
466
467 }