Debian Codegraph @

src/opentyr.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "opentyr.h"
20
21 #include "config.h"
22 #include "destruct.h"
23 #include "editship.h"
24 #include "episodes.h"
25 #include "file.h"
26 #include "font.h"
27 #include "fonthand.h"
28 #include "helptext.h"
29 #include "joystick.h"
30 #include "jukebox.h"
31 #include "keyboard.h"
32 #include "loudness.h"
33 #include "mainint.h"
34 #include "mouse.h"
35 #include "mtrand.h"
36 #include "network.h"
37 #include "nortsong.h"
38 #include "nortvars.h"
39 #include "opentyrian_version.h"
40 #include "palette.h"
41 #include "params.h"
42 #include "picload.h"
43 #include "sprite.h"
44 #include "tyrian2.h"
45 #include "varz.h"
46 #include "vga256d.h"
47 #include "video.h"
48 #include "video_scale.h"
49 #include "xmas.h"
50
51 #include "SDL.h"
52
53 #include <assert.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58
59 const char *opentyrian_str = "OpenTyrian";
60 const char *opentyrian_version = OPENTYRIAN_VERSION;
61
62 static size_t getDisplayPickerItemsCount(void)
63 {
64 return 1 + (size_t)SDL_GetNumVideoDisplays();
65 }
66
67 static const char *getDisplayPickerItem(size_t i, char *buffer, size_t bufferSize)
68 {
69 if (i == 0)
70 return "Window";
71
72 snprintf(buffer, bufferSize, "Display %d", (int)i);
73 return buffer;
74 }
75
76 static size_t getScalerPickerItemsCount(void)
77 {
78 return (size_t)scalers_count;
79 }
80
81 static const char *getScalerPickerItem(size_t i, char *buffer, size_t bufferSize)
82 {
83 (void)buffer, (void)bufferSize;
84
85 return scalers[i].name;
86 }
87
88 static size_t getScalingModePickerItemsCount(void)
89 {
90 return (size_t)ScalingMode_MAX;
91 }
92
93 static const char *getScalingModePickerItem(size_t i, char *buffer, size_t bufferSize)
94 {
95 (void)buffer, (void)bufferSize;
96
97 return scaling_mode_names[i];
98 }
99
100 void setupMenu(void)
101 {
102 typedef enum
103 {
104 MENU_ITEM_NONE = 0,
105 MENU_ITEM_DONE,
106 MENU_ITEM_GRAPHICS,
107 MENU_ITEM_SOUND,
108 MENU_ITEM_JUKEBOX,
109 MENU_ITEM_DESTRUCT,
110 MENU_ITEM_DISPLAY,
111 MENU_ITEM_SCALER,
112 MENU_ITEM_SCALING_MODE,
113 MENU_ITEM_MUSIC_VOLUME,
114 MENU_ITEM_SOUND_VOLUME,
115 } MenuItemId;
116
117 typedef enum
118 {
119 MENU_NONE = 0,
120 MENU_SETUP,
121 MENU_GRAPHICS,
122 MENU_SOUND,
123 } MenuId;
124
125 typedef struct
126 {
127 MenuItemId id;
128 const char *name;
129 const char *description;
130 size_t (*getPickerItemsCount)(void);
131 const char *(*getPickerItem)(size_t i, char *buffer, size_t bufferSize);
132 } MenuItem;
133
134 typedef struct
135 {
136 const char *header;
137 const MenuItem items[6];
138 } Menu;
139
140 static const Menu menus[] = {
141 [MENU_SETUP] = {
142 .header = "Setup",
143 .items = {
144 { MENU_ITEM_GRAPHICS, "Graphics...", "Change the graphics settings." },
145 { MENU_ITEM_SOUND, "Sound...", "Change the sound settings." },
146 { MENU_ITEM_JUKEBOX, "Jukebox", "Listen to the music of Tyrian." },
147 // { MENU_ITEM_DESTRUCT, "Destruct", "Play a bonus mini-game." },
148 { MENU_ITEM_DONE, "Done", "Return to the main menu." },
149 { -1 }
150 },
151 },
152 [MENU_GRAPHICS] = {
153 .header = "Graphics",
154 .items = {
155 { MENU_ITEM_DISPLAY, "Display:", "Change the display mode.", getDisplayPickerItemsCount, getDisplayPickerItem },
156 { MENU_ITEM_SCALER, "Scaler:", "Change the pixel art scaling algorithm.", getScalerPickerItemsCount, getScalerPickerItem },
157 { MENU_ITEM_SCALING_MODE, "Scaling Mode:", "Change the scaling mode.", getScalingModePickerItemsCount, getScalingModePickerItem },
158 { MENU_ITEM_DONE, "Done", "Return to the previous menu." },
159 { -1 }
160 },
161 },
162 [MENU_SOUND] = {
163 .header = "Sound",
164 .items = {
165 { MENU_ITEM_MUSIC_VOLUME, "Music Volume", "Change volume with the left/right arrow keys." },
166 { MENU_ITEM_SOUND_VOLUME, "Sound Volume", "Change volume with the left/right arrow keys." },
167 { MENU_ITEM_DONE, "Done", "Return to the previous menu." },
168 { -1 }
169 },
170 },
171 };
172
173 char buffer[100];
174
175 if (shopSpriteSheet.data == NULL)
176 JE_loadCompShapes(&shopSpriteSheet, '1'); // need mouse pointer sprites
177
178 bool restart = true;
179
180 MenuId menuParents[COUNTOF(menus)] = { MENU_NONE };
181 size_t selectedMenuItemIndexes[COUNTOF(menus)] = { 0 };
182 MenuId currentMenu = MENU_SETUP;
183 MenuItemId currentPicker = MENU_ITEM_NONE;
184 size_t pickerSelectedIndex = 0;
185
186 const int xCenter = 320 / 2;
187 const int yMenuHeader = 4;
188 const int xMenuItem = 45;
189 const int xMenuItemName = xMenuItem;
190 const int wMenuItemName = 135;
191 const int xMenuItemValue = xMenuItemName + wMenuItemName;
192 const int wMenuItemValue = 95;
193 const int wMenuItem = wMenuItemName + wMenuItemValue;
194 const int yMenuItems = 37;
195 const int dyMenuItems = 21;
196 const int hMenuItem = 13;
197
198 for (; ; )
199 {
200 if (restart)
201 {
202 JE_loadPic(VGAScreen2, 2, false);
203 fill_rectangle_wh(VGAScreen2, 0, 192, 320, 8, 0);
204 }
205
206 // Restore background.
207 memcpy(VGAScreen->pixels, VGAScreen2->pixels, (size_t)VGAScreen->pitch * VGAScreen->h);
208
209 const Menu *menu = &menus[currentMenu];
210
211 // Draw header.
212 draw_font_hv_shadow(VGAScreen, xCenter, yMenuHeader, menu->header, large_font, centered, 15, -3, false, 2);
213
214 int yPicker = 0;
215 const int dyPickerItem = 15;
216 const int dyPickerItemPadding = 2;
217 const int hPickerItem = dyPickerItem - dyPickerItemPadding;
218
219 size_t *const selectedMenuItemIndex = &selectedMenuItemIndexes[currentMenu];
220 const MenuItem *const menuItems = menu->items;
221
222 // Draw menu items.
223
224 size_t menuItemsCount = 0;
225 for (size_t i = 0; menuItems[i].id != (MenuItemId)-1; ++i)
226 {
227 menuItemsCount += 1;
228
229 const MenuItem *const menuItem = &menuItems[i];
230
231 const int y = yMenuItems + dyMenuItems * i;
232
233 const bool selected = i == *selectedMenuItemIndex;
234 const bool disabled = currentPicker != MENU_ITEM_NONE && !selected;
235
236 if (selected)
237 yPicker = y;
238
239 const char *const name = menuItem->name;
240
241 draw_font_hv_shadow(VGAScreen, xMenuItemName, y, name, normal_font, left_aligned, 15, -3 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
242
243 switch (menuItem->id)
244 {
245 case MENU_ITEM_DISPLAY:;
246 const char *value = "Window";
247 if (fullscreen_display >= 0)
248 {
249 snprintf(buffer, sizeof(buffer), "Display %d", fullscreen_display + 1);
250 value = buffer;
251 }
252
253 draw_font_hv_shadow(VGAScreen, xMenuItemValue, y, value, normal_font, left_aligned, 15, -3 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
254 break;
255
256 case MENU_ITEM_SCALER:
257 draw_font_hv_shadow(VGAScreen, xMenuItemValue, y, scalers[scaler].name, normal_font, left_aligned, 15, -3 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
258 break;
259
260 case MENU_ITEM_SCALING_MODE:
261 draw_font_hv_shadow(VGAScreen, xMenuItemValue, y, scaling_mode_names[scaling_mode], normal_font, left_aligned, 15, -3 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
262 break;
263
264 case MENU_ITEM_MUSIC_VOLUME:
265 JE_barDrawShadow(VGAScreen, xMenuItemValue, y, 1, music_disabled ? 170 : 174, (tyrMusicVolume + 4) / 8, 2, 10);
266 JE_rectangle(VGAScreen, xMenuItemValue - 2, y - 2, xMenuItemValue + 96, y + 11, 242);
267 break;
268
269 case MENU_ITEM_SOUND_VOLUME:
270 JE_barDrawShadow(VGAScreen, xMenuItemValue, y, 1, samples_disabled ? 170 : 174, (fxVolume + 4) / 8, 2, 10);
271 JE_rectangle(VGAScreen, xMenuItemValue - 2, y - 2, xMenuItemValue + 96, y + 11, 242);
272 break;
273
274 default:
275 break;
276 }
277 }
278
279 // Draw status text.
280 JE_textShade(VGAScreen, xMenuItemName, 190, menuItems[*selectedMenuItemIndex].description, 15, 4, PART_SHADE);
281
282 // Draw picker box and items.
283
284 if (currentPicker != MENU_ITEM_NONE)
285 {
286 const MenuItem *selectedMenuItem = &menuItems[*selectedMenuItemIndex];
287 const size_t pickerItemsCount = selectedMenuItem->getPickerItemsCount();
288
289 const int hPicker = dyPickerItem * pickerItemsCount - dyPickerItemPadding;
290 yPicker = MIN(yPicker, 200 - 10 - (hPicker + 5 + 2));
291
292 JE_rectangle(VGAScreen, xMenuItemValue - 5, yPicker- 3, xMenuItemValue + wMenuItemValue + 5 - 1, yPicker + hPicker + 3 - 1, 248);
293 JE_rectangle(VGAScreen, xMenuItemValue - 4, yPicker- 4, xMenuItemValue + wMenuItemValue + 4 - 1, yPicker + hPicker + 4 - 1, 250);
294 JE_rectangle(VGAScreen, xMenuItemValue - 3, yPicker- 5, xMenuItemValue + wMenuItemValue + 3 - 1, yPicker + hPicker + 5 - 1, 248);
295 fill_rectangle_wh(VGAScreen, xMenuItemValue - 2, yPicker - 2, wMenuItemValue + 2 + 2, hPicker + 2 + 2, 224);
296
297 for (size_t i = 0; i < pickerItemsCount; ++i)
298 {
299 const int y = yPicker + dyPickerItem * (int)i;
300
301 const bool selected = i == pickerSelectedIndex;
302
303 const char *value = selectedMenuItem->getPickerItem(i, buffer, sizeof buffer);
304
305 draw_font_hv_shadow(VGAScreen, xMenuItemValue, y, value, normal_font, left_aligned, 15, -3 + (selected ? 2 : 0), false, 2);
306 }
307 }
308
309 if (restart)
310 {
311 mouseCursor = MOUSE_POINTER_NORMAL;
312
313 fade_palette(colors, 10, 0, 255);
314
315 restart = false;
316 }
317
318 service_SDL_events(true);
319
320 JE_mouseStart();
321 JE_showVGA();
322 JE_mouseReplace();
323
324 bool mouseMoved = false;
325 int oldFullscreenDisplay = fullscreen_display;
326 do
327 {
328 SDL_Delay(16);
329
330 Uint16 oldMouseX = mouse_x;
331 Uint16 oldMouseY = mouse_y;
332
333 push_joysticks_as_keyboard();
334 service_SDL_events(false);
335
336 mouseMoved = mouse_x != oldMouseX || mouse_y != oldMouseY;
337 } while (!(newkey || newmouse || mouseMoved || fullscreen_display != oldFullscreenDisplay));
338
339 if (currentPicker == MENU_ITEM_NONE)
340 {
341 // Handle menu item interaction.
342
343 bool action = false;
344
345 if (mouseMoved || newmouse)
346 {
347 // Find menu item name or value that was hovered or clicked.
348 if (mouse_x >= xMenuItem && mouse_x < xMenuItem + wMenuItem)
349 {
350 for (size_t i = 0; i < menuItemsCount; ++i)
351 {
352 const int yMenuItem = yMenuItems + dyMenuItems * i;
353 if (mouse_y >= yMenuItem && mouse_y < yMenuItem + hMenuItem)
354 {
355 if (*selectedMenuItemIndex != i)
356 {
357 JE_playSampleNum(S_CURSOR);
358
359 *selectedMenuItemIndex = i;
360 }
361
362 if (newmouse && lastmouse_but == SDL_BUTTON_LEFT &&
363 lastmouse_y >= yMenuItem && lastmouse_y < yMenuItem + hMenuItem)
364 {
365 // Act on menu item via name.
366 if (lastmouse_x >= xMenuItemName && lastmouse_x < xMenuItemName + wMenuItemName)
367 {
368 action = true;
369 }
370
371 // Act on menu item via value.
372 else if (lastmouse_x >= xMenuItemValue && lastmouse_x < xMenuItemValue + wMenuItemValue)
373 {
374 switch (menuItems[*selectedMenuItemIndex].id)
375 {
376 case MENU_ITEM_DISPLAY:
377 case MENU_ITEM_SCALER:
378 case MENU_ITEM_SCALING_MODE:
379 {
380 action = true;
381 break;
382 }
383 case MENU_ITEM_MUSIC_VOLUME:
384 {
385 JE_playSampleNum(S_CURSOR);
386
387 int value = (lastmouse_x - xMenuItemValue) * 255 / (wMenuItemValue - 1);
388 tyrMusicVolume = MIN(MAX(0, value), 255);
389
390 set_volume(tyrMusicVolume, fxVolume);
391 break;
392 }
393 case MENU_ITEM_SOUND_VOLUME:
394 {
395 int value = (lastmouse_x - xMenuItemValue) * 255 / (wMenuItemValue - 1);
396 fxVolume = MIN(MAX(0, value), 255);
397
398 set_volume(tyrMusicVolume, fxVolume);
399
400 JE_playSampleNum(S_CURSOR);
401 break;
402 }
403 default:
404 break;
405 }
406 }
407 }
408
409 break;
410 }
411 }
412 }
413 }
414
415 if (newmouse)
416 {
417 if (lastmouse_but == SDL_BUTTON_RIGHT)
418 {
419 JE_playSampleNum(S_SPRING);
420
421 currentMenu = menuParents[currentMenu];
422 }
423 }
424 else if (newkey)
425 {
426 switch (lastkey_scan)
427 {
428 case SDL_SCANCODE_UP:
429 {
430 JE_playSampleNum(S_CURSOR);
431
432 *selectedMenuItemIndex = *selectedMenuItemIndex == 0
433 ? menuItemsCount - 1
434 : *selectedMenuItemIndex - 1;
435 break;
436 }
437 case SDL_SCANCODE_DOWN:
438 {
439 JE_playSampleNum(S_CURSOR);
440
441 *selectedMenuItemIndex = *selectedMenuItemIndex == menuItemsCount - 1
442 ? 0
443 : *selectedMenuItemIndex + 1;
444 break;
445 }
446 case SDL_SCANCODE_LEFT:
447 {
448 switch (menuItems[*selectedMenuItemIndex].id)
449 {
450 case MENU_ITEM_MUSIC_VOLUME:
451 {
452 JE_playSampleNum(S_CURSOR);
453
454 JE_changeVolume(&tyrMusicVolume, -8, &fxVolume, 0);
455 break;
456 }
457 case MENU_ITEM_SOUND_VOLUME:
458 {
459 JE_changeVolume(&tyrMusicVolume, 0, &fxVolume, -8);
460
461 JE_playSampleNum(S_CURSOR);
462 break;
463 }
464 default:
465 break;
466 }
467 break;
468 }
469 case SDL_SCANCODE_RIGHT:
470 {
471 switch (menuItems[*selectedMenuItemIndex].id)
472 {
473 case MENU_ITEM_MUSIC_VOLUME:
474 {
475 JE_playSampleNum(S_CURSOR);
476
477 JE_changeVolume(&tyrMusicVolume, 8, &fxVolume, 0);
478 break;
479 }
480 case MENU_ITEM_SOUND_VOLUME:
481 {
482 JE_changeVolume(&tyrMusicVolume, 0, &fxVolume, 8);
483
484 JE_playSampleNum(S_CURSOR);
485 break;
486 }
487 default:
488 break;
489 }
490 break;
491 }
492 case SDL_SCANCODE_SPACE:
493 case SDL_SCANCODE_RETURN:
494 {
495 action = true;
496 break;
497 }
498 case SDL_SCANCODE_ESCAPE:
499 {
500 JE_playSampleNum(S_SPRING);
501
502 currentMenu = menuParents[currentMenu];
503 break;
504 }
505 default:
506 break;
507 }
508 }
509
510 if (action)
511 {
512 const MenuItemId selectedMenuItemId = menuItems[*selectedMenuItemIndex].id;
513
514 switch (selectedMenuItemId)
515 {
516 case MENU_ITEM_DONE:
517 {
518 JE_playSampleNum(S_SELECT);
519
520 currentMenu = menuParents[currentMenu];
521 break;
522 }
523 case MENU_ITEM_GRAPHICS:
524 {
525 JE_playSampleNum(S_SELECT);
526
527 menuParents[MENU_GRAPHICS] = currentMenu;
528 currentMenu = MENU_GRAPHICS;
529 selectedMenuItemIndexes[currentMenu] = 0;
530 break;
531 }
532 case MENU_ITEM_SOUND:
533 {
534 JE_playSampleNum(S_SELECT);
535
536 menuParents[MENU_SOUND] = currentMenu;
537 currentMenu = MENU_SOUND;
538 selectedMenuItemIndexes[currentMenu] = 0;
539 break;
540 }
541 case MENU_ITEM_JUKEBOX:
542 {
543 JE_playSampleNum(S_SELECT);
544
545 fade_black(10);
546
547 jukebox();
548
549 restart = true;
550 break;
551 }
552 case MENU_ITEM_DESTRUCT:
553 {
554 JE_playSampleNum(S_SELECT);
555
556 fade_black(10);
557
558 JE_destructGame();
559
560 restart = true;
561 break;
562 }
563 case MENU_ITEM_DISPLAY:
564 {
565 JE_playSampleNum(S_CLICK);
566
567 currentPicker = selectedMenuItemId;
568 pickerSelectedIndex = (size_t)(fullscreen_display + 1);
569 break;
570 }
571 case MENU_ITEM_SCALER:
572 {
573 JE_playSampleNum(S_CLICK);
574
575 currentPicker = selectedMenuItemId;
576 pickerSelectedIndex = scaler;
577 break;
578 }
579 case MENU_ITEM_SCALING_MODE:
580 {
581 JE_playSampleNum(S_CLICK);
582
583 currentPicker = selectedMenuItemId;
584 pickerSelectedIndex = scaling_mode;
585 break;
586 }
587 case MENU_ITEM_MUSIC_VOLUME:
588 {
589 JE_playSampleNum(S_CLICK);
590
591 music_disabled = !music_disabled;
592 if (!music_disabled)
593 restart_song();
594 break;
595 }
596 case MENU_ITEM_SOUND_VOLUME:
597 {
598 samples_disabled = !samples_disabled;
599
600 JE_playSampleNum(S_CLICK);
601 break;
602 }
603 default:
604 break;
605 }
606 }
607
608 if (currentMenu == MENU_NONE)
609 {
610 fade_black(10);
611
612 return;
613 }
614 }
615 else
616 {
617 const MenuItem *selectedMenuItem = &menuItems[*selectedMenuItemIndex];
618
619 // Handle picker interaction.
620
621 bool action = false;
622
623 if (mouseMoved || newmouse)
624 {
625 const size_t pickerItemsCount = selectedMenuItem->getPickerItemsCount();
626
627 // Find picker item that was hovered clicked.
628 if (mouse_x >= xMenuItemValue && mouse_x < xMenuItemValue + wMenuItemValue)
629 {
630 for (size_t i = 0; i < pickerItemsCount; ++i)
631 {
632 const int yPickerItem = yPicker + dyPickerItem * i;
633
634 if (mouse_y >= yPickerItem && mouse_y < yPickerItem + hPickerItem)
635 {
636 if (pickerSelectedIndex != i)
637 {
638 JE_playSampleNum(S_CURSOR);
639
640 pickerSelectedIndex = i;
641 }
642
643 // Act on picker item.
644 if (newmouse && lastmouse_but == SDL_BUTTON_LEFT &&
645 lastmouse_x >= xMenuItemValue && lastmouse_y < xMenuItemValue + wMenuItemName &&
646 lastmouse_y >= yPickerItem && lastmouse_y < yPickerItem + hPickerItem)
647 {
648 action = true;
649 }
650 }
651 }
652 }
653 }
654
655 if (newmouse)
656 {
657 if (lastmouse_but == SDL_BUTTON_RIGHT)
658 {
659 JE_playSampleNum(S_SPRING);
660
661 currentPicker = MENU_ITEM_NONE;
662 }
663 }
664 else if (newkey)
665 {
666 switch (lastkey_scan)
667 {
668 case SDL_SCANCODE_UP:
669 {
670 JE_playSampleNum(S_CURSOR);
671
672 const size_t pickerItemsCount = selectedMenuItem->getPickerItemsCount();
673
674 pickerSelectedIndex = pickerSelectedIndex == 0
675 ? pickerItemsCount - 1
676 : pickerSelectedIndex - 1;
677 break;
678 }
679 case SDL_SCANCODE_DOWN:
680 {
681 JE_playSampleNum(S_CURSOR);
682
683 const size_t pickerItemsCount = selectedMenuItem->getPickerItemsCount();
684
685 pickerSelectedIndex = pickerSelectedIndex == pickerItemsCount - 1
686 ? 0
687 : pickerSelectedIndex + 1;
688 break;
689 }
690 case SDL_SCANCODE_SPACE:
691 case SDL_SCANCODE_RETURN:
692 {
693 action = true;
694 break;
695 }
696 case SDL_SCANCODE_ESCAPE:
697 {
698 JE_playSampleNum(S_SPRING);
699
700 currentPicker = MENU_ITEM_NONE;
701 break;
702 }
703 default:
704 break;
705 }
706 }
707
708 if (action)
709 {
710 JE_playSampleNum(S_CLICK);
711
712 switch (selectedMenuItem->id)
713 {
714 case MENU_ITEM_DISPLAY:
715 {
716 if ((int)pickerSelectedIndex - 1 != fullscreen_display)
717 reinit_fullscreen((int)pickerSelectedIndex - 1);
718 break;
719 }
720 case MENU_ITEM_SCALER:
721 {
722 if (pickerSelectedIndex != scaler)
723 {
724 const int oldScaler = scaler;
725 if (!init_scaler(pickerSelectedIndex) && // try new scaler
726 !init_scaler(oldScaler)) // revert on fail
727 {
728 exit(EXIT_FAILURE);
729 }
730 }
731 break;
732 }
733 case MENU_ITEM_SCALING_MODE:
734 {
735 scaling_mode = pickerSelectedIndex;
736 break;
737 }
738 default:
739 break;
740 }
741
742 currentPicker = MENU_ITEM_NONE;
743 }
744 }
745 }
746 }
747
748 int main(int argc, char *argv[])
749 {
750 mt_srand(time(NULL));
751
752 printf("\nWelcome to... >> %s %s <<\n\n", opentyrian_str, opentyrian_version);
753
754 printf("Copyright (C) 2022 The OpenTyrian Development Team\n\n");
755
756 printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
757 printf("This is free software, and you are welcome to redistribute it\n");
758 printf("under certain conditions. See the file COPYING for details.\n\n");
759
760 if (SDL_Init(0))
761 {
762 printf("Failed to initialize SDL: %s\n", SDL_GetError());
763 return -1;
764 }
765
766 JE_loadConfiguration();
767
768 xmas = xmas_time(); // arg handler may override
769
770 JE_paramCheck(argc, argv);
771
772 JE_scanForEpisodes();
773
774 init_video();
775 init_keyboard();
776 init_joysticks();
777 printf("assuming mouse detected\n"); // SDL can't tell us if there isn't one
778
779 if (xmas && (!dir_file_exists(data_dir(), "tyrianc.shp") || !dir_file_exists(data_dir(), "voicesc.snd")))
780 {
781 xmas = false;
782
783 fprintf(stderr, "warning: Christmas is missing.\n");
784 }
785
786 JE_loadPals();
787 JE_loadMainShapeTables(xmas ? "tyrianc.shp" : "tyrian.shp");
788
789 if (xmas && !xmas_prompt())
790 {
791 xmas = false;
792
793 free_main_shape_tables();
794 JE_loadMainShapeTables("tyrian.shp");
795 }
796
797 /* Default Options */
798 youAreCheating = false;
799 smoothScroll = true;
800 loadDestruct = false;
801
802 if (!audio_disabled)
803 {
804 printf("initializing SDL audio...\n");
805
806 init_audio();
807
808 load_music();
809
810 loadSndFile(xmas);
811 }
812 else
813 {
814 printf("audio disabled\n");
815 }
816
817 if (record_demo)
818 printf("demo recording enabled (input limited to keyboard)\n");
819
820 JE_loadExtraShapes(); /*Editship*/
821
822 JE_loadHelpText();
823 /*debuginfo("Help text complete");*/
824
825 if (isNetworkGame)
826 {
827 #ifdef WITH_NETWORK
828 if (network_init())
829 {
830 network_tyrian_halt(3, false);
831 }
832 #else
833 fprintf(stderr, "OpenTyrian was compiled without networking support.");
834 JE_tyrianHalt(5);
835 #endif
836 }
837
838 #ifdef NDEBUG
839 if (!isNetworkGame)
840 intro_logos();
841 #endif
842
843 for (; ; )
844 {
845 JE_initPlayerData();
846 JE_sortHighScores();
847
848 play_demo = false;
849 stopped_demo = false;
850
851 gameLoaded = false;
852 jumpSection = false;
853
854 #ifdef WITH_NETWORK
855 if (isNetworkGame)
856 {
857 networkStartScreen();
858 }
859 else
860 #endif
861 {
862 if (!titleScreen())
863 {
864 // Player quit from title screen.
865 break;
866 }
867 }
868
869 if (loadDestruct)
870 {
871 JE_destructGame();
872
873 loadDestruct = false;
874 }
875 else
876 {
877 JE_main();
878
879 if (trentWin)
880 {
881 // Player beat SuperTyrian.
882 break;
883 }
884 }
885 }
886
887 JE_tyrianHalt(0);
888
889 return 0;
890 }