Debian Codegraph @

src/main.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file main.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created main.c
10 * - 22nd July 2008: Created util.c from parts of main.c
11 * - 3rd February 2009: Renamed main.c to main.cpp
12 * - 4th February 2009: Created palette.cpp from parts of main.cpp and util.cpp
13 * - 13th July 2009: Created controls.cpp from parts of main.cpp
14 * - 21st July 2013: Created setup.cpp from parts of main.cpp and setupmenu.cpp
15 *
16 * @par Licence:
17 * Copyright (c) 2005-2017 AJ Thomson
18 * Copyright (c) 2015-2026 Carsten Teibes
19 *
20 * OpenJazz is distributed under the terms of
21 * the GNU General Public License, version 2.0
22 *
23 * @par Description:
24 * Contains the main function.
25 *
26 */
27
28
29 // consume all external variables
30 #define EXTERN
31
32 #include "game/game.h"
33 #include "io/controls.h"
34 #include "io/file.h"
35 #include "io/gfx/font.h"
36 #include "io/gfx/video.h"
37 #include "io/network.h"
38 #include "io/sound.h"
39 #ifdef ENABLE_JJ2
40 #include "jj2/level/jj2level.h"
41 #endif
42 #include "jj1/level/jj1level.h"
43 #include "menu/menu.h"
44 #include "player/player.h"
45 #include "jj1/scene/jj1scene.h"
46 #include "loop.h"
47 #include "setup.h"
48 #include "util.h"
49 #include "io/log.h"
50 #include "platforms/platforms.h"
51 #include "version.h"
52
53 #include <cstring>
54 #include <argparse.h>
55
56 #if !OJ_SDL3
57 // Define some stuff to be SDL3 compatible
58
59 #define SDL_EVENT_QUIT SDL_QUIT
60 #endif
61
62 #ifndef __SYMBIAN32__
63 #include <math.h>
64 #endif
65
66 #define PI 3.141592f
67
68 // Fallback links, should be provided by the build system
69 #ifndef OJ_BUGREPORT
70 #define OJ_BUGREPORT "https://github.com/AlisterT/openjazz/issues"
71 #endif
72 #ifndef OJ_URL
73 #define OJ_URL "http://alister.eu/jazz/oj/"
74 #endif
75
76 static struct CliOptions {
77 bool muteAudio;
78 int fullScreen;
79 int scaleFactor;
80 int level;
81 int world;
82 char *verboseLevel;
83 int quiet;
84 } cli = {
85 false, -1, -1, -1, -1, NULL, 0
86 };
87
88 #ifndef FULLSCREEN_ONLY
89 int display_mode_cb(struct argparse *, const struct argparse_option *option) {
90 cli.fullScreen = (option->short_name == 'f') ? 1 : 0;
91 return 0;
92 }
93 #endif
94
95 int version_cb(struct argparse *, const struct argparse_option */*option*/) {
96 printf("OpenJazz %s, built on %s.\n", oj_version, oj_date);
97 exit(EXIT_SUCCESS);
98 }
99
100 int checkOptions (int argc, char *argv[]) {
101
102 struct argparse argparse;
103
104 // generic usage message
105 const char *usage[] = {
106 "OpenJazz [options] [[--] <game directory 1> <game directory 2>]",
107 NULL,
108 };
109
110 // command line options
111 struct argparse_option opt[] = {
112 OPT_HELP(),
113 OPT_GROUP("Engine options"),
114 OPT_BOOLEAN('m', "mute", &cli.muteAudio, "Mute audio output", NULL, 0, 0),
115 #ifndef FULLSCREEN_ONLY
116 OPT_BOOLEAN('f', "fullscreen", NULL, "Display in Fullscreen mode",
117 display_mode_cb, 0, OPT_NONEG),
118 OPT_BOOLEAN('\0', "window", NULL, "Display in Window mode",
119 display_mode_cb, 0, OPT_NONEG),
120 #endif
121 OPT_INTEGER('s', "scale", &cli.scaleFactor, "Scale graphics <int> times", NULL, 0, 0),
122 OPT_GROUP("Developer options"),
123 OPT_INTEGER('w', "world", &cli.world, "Load specific World", NULL, 0, 0),
124 OPT_INTEGER('l', "level", &cli.level, "Load specific Level", NULL, 0, 0),
125 OPT_BOOLEAN('q', "quiet", &cli.quiet, "Disable console logging (Enable with --no-quiet)", NULL, 0, 0),
126 OPT_STRING('\0', "verbose", &cli.verboseLevel,
127 "Verbosity level: max, trace, debug, info, warn, error, fatal", NULL, 0, 0),
128 OPT_BOOLEAN('v', "version", NULL, "Show version information", version_cb, 0, OPT_NONEG),
129 OPT_END(),
130 };
131
132 argparse_init(&argparse, opt, usage, 0);
133 argparse_describe(&argparse,
134 "\nOpenJazz - Jack Jazzrabbit 1 game engine reimplementation",
135 "\nBug reports: " OJ_BUGREPORT " - Homepage: " OJ_URL);
136 argc = argparse_parse(&argparse, argc, argv);
137
138 // apply logger options
139 if (cli.quiet) logger.setQuiet(cli.quiet);
140 int verbosity = logger.getLevel();
141 if (cli.verboseLevel) {
142 if (!strcmp(cli.verboseLevel, "max")) verbosity = LL_MAX;
143 else if (!strcmp(cli.verboseLevel, "trace")) verbosity = LL_TRACE;
144 else if (!strcmp(cli.verboseLevel, "debug")) verbosity = LL_DEBUG;
145 else if (!strcmp(cli.verboseLevel, "info")) verbosity = LL_INFO;
146 else if (!strcmp(cli.verboseLevel, "warn")) verbosity = LL_WARN;
147 else if (!strcmp(cli.verboseLevel, "error")) verbosity = LL_ERROR;
148 else if (!strcmp(cli.verboseLevel, "fatal")) verbosity = LL_FATAL;
149 else {
150
151 fprintf(stderr, "error: option `--verbose` has invalid level\n");
152 exit(EXIT_FAILURE);
153
154 }
155 }
156 logger.setLevel(verbosity);
157
158 return argc;
159 }
160
161
162 /**
163 * Initialises OpenJazz.
164 *
165 * Establishes the paths from which to read files, loads configuration, sets up
166 * the game window and loads required data.
167 *
168 * @param argv0 program path
169 * @param pathCount Number of path arguments
170 * @param paths Array of path argument strings
171 */
172 void startUp (const char *argv0, int pathCount, char *paths[]) {
173
174 File* file;
175 unsigned char* pixels = NULL;
176 SetupOptions config;
177
178 // Determine paths
179 platform->AddGamePaths();
180
181 // Use any provided paths
182 for (int i = 0; i < pathCount; i++)
183 gamePaths.add(createString(paths[i]), PATH_TYPE_GAME);
184
185
186 // Use the path of the program, but check before, since it is not always available
187 // At least crashes in Dolphin emulator (Wii) and 3DS (.cia build)
188 if (argv0) {
189
190 int i = strlen(argv0) - 1;
191
192 // Search for directory separator
193 while ((argv0[i] != OJ_DIR_SEP) && (i > 0)) i--;
194
195 // If a directory was found, copy it to the path
196 if (i > 0) {
197
198 char *dir = createString(argv0);
199 dir[i+1] = '\0';
200 gamePaths.add(dir, PATH_TYPE_SYSTEM|PATH_TYPE_GAME);
201
202 }
203
204 }
205
206 // Use the current working directory
207 gamePaths.add(createString(""), PATH_TYPE_GAME|PATH_TYPE_CONFIG|PATH_TYPE_TEMP);
208
209 // Use hard-coded data paths, if available
210 #ifdef DATAPATH
211 gamePaths.add(createString(DATAPATH), PATH_TYPE_SYSTEM|PATH_TYPE_GAME);
212 #endif
213
214 // Default settings
215
216 // Sound settings
217 #if defined(WIZ) || defined(GP2X)
218 volume = 40;
219 #endif
220
221 // Create the network address
222 netAddress = createString(NET_ADDRESS);
223
224
225 // Load settings from config file
226 config = setup.load();
227 if (!config.valid) {
228
229 // Invalid config - apply defaults
230 config.videoWidth = DEFAULT_SCREEN_WIDTH;
231 config.videoHeight = DEFAULT_SCREEN_HEIGHT;
232 config.videoScale = MIN_SCALE;
233 config.scaleMethod = scalerType::None;
234
235 }
236
237 // Apply command-line override
238 if (cli.fullScreen > -1) config.fullScreen = cli.fullScreen;
239 if (cli.scaleFactor > 0) config.videoScale = cli.scaleFactor;
240 if (cli.muteAudio) {
241
242 setMusicVolume(0);
243 setSoundVolume(0);
244
245 }
246
247
248 // Create the game's window
249 canvas = NULL;
250 if (!video.init(config)) {
251
252 throw E_VIDEO;
253
254 }
255
256
257 controls.init();
258
259
260 // Set up audio
261 openAudio();
262
263
264 // Load fonts
265
266 // Open the panel, which contains two fonts
267
268 try {
269
270 file = new File("PANEL.000", PATH_TYPE_GAME);
271
272 } catch (int e) {
273
274 closeAudio();
275
276 LOG_FATAL("Unable to find game data files. When launching OpenJazz, \n"
277 " pass the location of the original game data, eg:\n"
278 " OpenJazz ~/jazz1");
279
280 platform->ErrorNoDatafiles();
281
282 throw;
283
284 }
285
286 pixels = file->loadRLE(46272);
287
288 delete file;
289
290 panelBigFont = nullptr;
291 panelSmallFont = nullptr;
292 font2 = nullptr;
293 fontbig = nullptr;
294 fontiny = nullptr;
295 fontmn1 = nullptr;
296
297 try {
298
299 panelBigFont = new Font(pixels + (40 * 320), true);
300 panelSmallFont = new Font(pixels + (48 * 320), false);
301 font2 = new Font("FONT2.0FN");
302 fontbig = new Font("FONTBIG.0FN");
303 fontiny = new Font("FONTINY.0FN");
304 fontmn1 = new Font("FONTMN1.0FN");
305 fontmn2 = new Font("FONTMN2.0FN");
306
307 } catch (int e) {
308
309 if (panelBigFont) delete panelBigFont;
310 if (panelSmallFont) delete panelSmallFont;
311 if (font2) delete font2;
312 if (fontbig) delete fontbig;
313 if (fontiny) delete fontiny;
314 if (fontmn1) delete fontmn1;
315
316 delete[] pixels;
317
318 closeAudio();
319
320 throw;
321
322 }
323 #if DEBUG_FONTS
324 panelBigFont->saveAtlasAsBMP("panelbigfont.bmp");
325 panelSmallFont->saveAtlasAsBMP("panelsmallfont.bmp");
326 font2->saveAtlasAsBMP("font2.bmp");
327 fontbig->saveAtlasAsBMP("fontbig.bmp");
328 fontiny->saveAtlasAsBMP("fontiny.bmp");
329 fontmn1->saveAtlasAsBMP("fontmn1.bmp");
330 #endif
331
332 delete[] pixels;
333
334 // Establish arbitrary timing
335 globalTicks = SDL_GetTicks() - 20;
336
337
338 // Fill trigonometric function look-up tables
339 for (int i = 0; i < 1024; i++)
340 sinLut[i] = fixed(sinf(2 * PI * float(i) / 1024.0f) * 1024.0f);
341
342
343 // Initiate networking
344 net = new Network();
345
346
347 level = NULL;
348
349 #ifdef ENABLE_JJ2
350 jj2Level = NULL;
351 #endif
352
353 }
354
355
356 /**
357 * De-initialises OpenJazz.
358 *
359 * Frees data, writes configuration, and shuts down SDL.
360 */
361 void shutDown () {
362
363 delete net;
364
365 delete panelBigFont;
366 delete panelSmallFont;
367 delete font2;
368 delete fontbig;
369 delete fontiny;
370 delete fontmn1;
371 delete fontmn2;
372
373 closeAudio();
374
375 controls.deinit();
376
377 video.deinit();
378
379 // Save settings to config file
380 setup.save();
381
382 }
383
384
385 /**
386 * Run the cutscenes and the main menu.
387 *
388 * @return Error code
389 */
390 int play () {
391
392 MainMenu *mainMenu = NULL;
393 JJ1Scene *scene = NULL;
394
395 // Start the opening music
396
397 playMusic("MENUSNG.PSM");
398
399 // Try loading the user-specified level
400
401 if (cli.level > -1 && cli.world > -1) {
402
403 try {
404
405 mainMenu = new MainMenu();
406
407 } catch (int e) {
408
409 return e;
410
411 }
412
413 if (mainMenu->skip(cli.level, cli.world) == E_QUIT) {
414
415 delete mainMenu;
416
417 }
418
419 return E_NONE;
420
421 }
422
423
424 // Load and play the startup cutscene
425
426 try {
427
428 scene = new JJ1Scene("STARTUP.0SC");
429
430 } catch (int e) {
431
432 return e;
433
434 }
435
436 if (scene->play() == E_QUIT) {
437
438 delete scene;
439
440 return E_NONE;
441
442 }
443
444 delete scene;
445
446
447 // Load and run the menu
448
449 try {
450
451 mainMenu = new MainMenu();
452
453 } catch (int e) {
454
455 return e;
456
457 }
458
459 if (mainMenu->main() == E_QUIT) {
460
461 delete mainMenu;
462
463 return E_NONE;
464
465 }
466
467 delete mainMenu;
468
469
470 // Load and play the ending cutscene
471
472 try {
473
474 scene = new JJ1Scene("END.0SC");
475
476 } catch (int e) {
477
478 return e;
479
480 }
481
482 scene->play();
483
484 delete scene;
485
486
487 return E_NONE;
488
489 }
490
491
492 /**
493 * Process iteration.
494 *
495 * Called once per game iteration. Updates timing, video, and input
496 *
497 * @param type Type of loop. Normal, typing, or input configuration
498 * @param paletteEffects Palette effects to apply to video output
499 * @param effectsStopped Whether the effects should be applied without advancing
500 *
501 * @return Error code
502 */
503 int loop (LoopType type, PaletteEffect* paletteEffects, bool effectsStopped) {
504
505 SDL_Event event;
506 int prevTicks, ret;
507
508
509 // Update tick count
510 prevTicks = globalTicks;
511 globalTicks = SDL_GetTicks();
512
513 if (globalTicks - prevTicks < 4) {
514
515 // Limit framerate
516 SDL_Delay(4 + prevTicks - globalTicks);
517 globalTicks = SDL_GetTicks();
518
519 }
520
521 // Show what has been drawn
522 video.flip(globalTicks - prevTicks, paletteEffects, effectsStopped);
523
524
525 // Process system events
526 while (SDL_PollEvent(&event)) {
527
528 if (event.type == SDL_EVENT_QUIT) return E_QUIT;
529
530 ret = controls.update(&event, type);
531
532 if (ret != E_NONE) return ret;
533
534 video.update(&event);
535
536 #if defined(WIZ) || defined(GP2X)
537 if ((event.type == SDL_JOYBUTTONDOWN) ||
538 (event.type == SDL_JOYBUTTONUP)) {
539
540 if (event.jbutton.button == GP2X_BUTTON_VOLUP ) {
541 if( event.type == SDL_JOYBUTTONDOWN )
542 volume_direction = VOLUME_UP;
543 else
544 volume_direction = VOLUME_NOCHG;
545 }
546 if (event.jbutton.button == GP2X_BUTTON_VOLDOWN ) {
547 if( event.type == SDL_JOYBUTTONDOWN )
548 volume_direction = VOLUME_DOWN;
549 else
550 volume_direction = VOLUME_NOCHG;
551 }
552
553 }
554 #endif
555
556 }
557
558 controls.loop();
559
560 if (platform->WantsExit()) return E_QUIT;
561
562 #if defined(WIZ) || defined(GP2X)
563 WIZ_AdjustVolume( volume_direction );
564 #endif
565
566 return E_NONE;
567
568 }
569
570 /**
571 * Shows version information of used SDL library.
572 *
573 */
574 void logSDLVersion() {
575 #if OJ_SDL3
576 // define a compatible struct
577 struct SDL_version { Uint8 major; Uint8 minor; Uint8 patch; };
578
579 const int compiledVer = SDL_VERSION;
580 const int linkedVer = SDL_GetVersion();
581
582 struct SDL_version compiled = { SDL_VERSIONNUM_MAJOR(compiledVer),
583 SDL_VERSIONNUM_MINOR(compiledVer), SDL_VERSIONNUM_MICRO(compiledVer) };
584 struct SDL_version linked = { SDL_VERSIONNUM_MAJOR(linkedVer),
585 SDL_VERSIONNUM_MINOR(linkedVer), SDL_VERSIONNUM_MICRO(linkedVer) };
586 #elif OJ_SDL2
587 SDL_version compiled, linked;
588
589 SDL_VERSION(&compiled);
590 SDL_GetVersion(&linked);
591 #else
592 SDL_version compiled, linked;
593
594 SDL_VERSION(&compiled);
595
596 const SDL_version* linkedVer = SDL_Linked_Version();
597 linked.major = linkedVer->major;
598 linked.minor = linkedVer->minor;
599 linked.patch = linkedVer->patch;
600 #endif
601
602 LOG_DEBUG("built with SDL %d.%d.%d (running with SDL %d.%d.%d)",
603 compiled.major, compiled.minor, compiled.patch,
604 linked.major, linked.minor, linked.patch);
605 }
606
607
608 /**
609 * Main.
610 *
611 * Initialises SDL and launches game.
612 */
613 int main(int argc, char *argv[]) {
614
615 int ret;
616 const char *argv0 = NULL;
617
618 // Initialize platform interface
619 platform = IPlatform::make();
620
621 // Some platforms (and emulators) do not provide arguments
622
623 if (argc) {
624
625 // Save program path
626 argv0 = argv[0];
627
628 // Check command line options
629 argc = checkOptions(argc, argv);
630
631 }
632
633 // Log current version
634 LOG_INFO("This is OpenJazz %s, built on %s.", oj_version, oj_date);
635
636 // Initialise SDL
637
638 bool sdlOk = false;
639 #if OJ_SDL3
640 sdlOk = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
641 #else
642 sdlOk = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) == 0;
643 #endif
644 if (!sdlOk) {
645 LOG_FATAL("Could not start SDL: %s\n", SDL_GetError());
646
647 return -1;
648 }
649 logSDLVersion();
650
651 // Load configuration and establish a window
652
653 try {
654
655 startUp(argv0, argc, argv);
656
657 } catch (int e) {
658
659 SDL_Quit();
660
661 return -1;
662
663 }
664
665
666 // Play the opening cutscene, run the main menu, etc.
667
668 ret = play();
669
670
671 // Save configuration and shut down
672
673 LOG_DEBUG("Shutting down engine.");
674
675 shutDown();
676
677 delete platform;
678
679 SDL_Quit();
680
681 return ret;
682
683 }