Debian Codegraph @

src/menus.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 "menus.h"
20
21 #include "config.h"
22 #include "episodes.h"
23 #include "font.h"
24 #include "fonthand.h"
25 #include "joystick.h"
26 #include "keyboard.h"
27 #include "mouse.h"
28 #include "network.h"
29 #include "nortsong.h"
30 #include "opentyr.h"
31 #include "palette.h"
32 #include "picload.h"
33 #include "sprite.h"
34 #include "vga256d.h"
35 #include "video.h"
36
37 char episode_name[6][31];
38 char difficulty_name[7][21];
39 char gameplay_name[5][26];
40
41 bool gameplaySelect(void)
42 {
43 enum MenuItemIndex
44 {
45 MENU_ITEM_1_PLAYER_FULL_GAME = 0,
46 MENU_ITEM_1_PLAYER_ARCADE,
47 MENU_ITEM_2_PLAYER_ARCADE,
48 MENU_ITEM_NETWORK,
49 };
50
51 if (shopSpriteSheet.data == NULL)
52 JE_loadCompShapes(&shopSpriteSheet, '1'); // need mouse pointer sprites
53
54 bool restart = true;
55
56 const size_t menuItemsCount = COUNTOF(gameplay_name) - 1;
57 size_t selectedIndex = MENU_ITEM_1_PLAYER_FULL_GAME;
58
59 const int xCenter = 320 / 2;
60 const int yMenuHeader = 20;
61 const int yMenuItems = 54;
62 const int dyMenuItems = 24;
63 const int hMenuItem = 13;
64 int wMenuItem[COUNTOF(gameplay_name) - 1] = { 0 };
65
66 for (; ; )
67 {
68 if (restart)
69 {
70 JE_loadPic(VGAScreen2, 2, false);
71
72 // Draw header.
73 draw_font_hv_shadow(VGAScreen2, xCenter, yMenuHeader, gameplay_name[0], large_font, centered, 15, -3, false, 2);
74 }
75
76 // Restore background and header.
77 memcpy(VGAScreen->pixels, VGAScreen2->pixels, (size_t)VGAScreen->pitch * VGAScreen->h);
78
79 // Draw menu items.
80 for (size_t i = 0; i < menuItemsCount; ++i)
81 {
82 const char *const text = gameplay_name[i + 1];
83
84 wMenuItem[i] = JE_textWidth(text, normal_font);
85 const int x = xCenter - wMenuItem[i] / 2;
86 const int y = yMenuItems + dyMenuItems * i;
87
88 const bool selected = i == selectedIndex;
89 const bool disabled = i == MENU_ITEM_NETWORK;
90
91 draw_font_hv_shadow(VGAScreen, x, y, text, normal_font, left_aligned, 15, -4 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
92 }
93
94 if (restart)
95 {
96 mouseCursor = MOUSE_POINTER_NORMAL;
97
98 fade_palette(colors, 10, 0, 255);
99
100 restart = false;
101 }
102
103 service_SDL_events(true);
104
105 JE_mouseStart();
106 JE_showVGA();
107 JE_mouseReplace();
108
109 bool mouseMoved = false;
110 do
111 {
112 SDL_Delay(16);
113
114 Uint16 oldMouseX = mouse_x;
115 Uint16 oldMouseY = mouse_y;
116
117 push_joysticks_as_keyboard();
118 service_SDL_events(false);
119
120 mouseMoved = mouse_x != oldMouseX || mouse_y != oldMouseY;
121 } while (!(newkey || newmouse || mouseMoved));
122
123 // Handle interaction.
124
125 bool action = false;
126 bool cancel = false;
127
128 if (mouseMoved || newmouse)
129 {
130 // Find menu item that was hovered or clicked.
131 for (size_t i = 0; i < menuItemsCount; ++i)
132 {
133 const int xMenuItem = xCenter - wMenuItem[i] / 2;
134 if (mouse_x >= xMenuItem && mouse_x < xMenuItem + wMenuItem[i])
135 {
136 const int yMenuItem = yMenuItems + dyMenuItems * i;
137 if (mouse_y >= yMenuItem && mouse_y < yMenuItem + hMenuItem)
138 {
139 if (selectedIndex != i)
140 {
141 JE_playSampleNum(S_CURSOR);
142
143 selectedIndex = i;
144 }
145
146 if (newmouse && lastmouse_but == SDL_BUTTON_LEFT &&
147 lastmouse_x >= xMenuItem && lastmouse_x < xMenuItem + wMenuItem[i] &&
148 lastmouse_y >= yMenuItem && lastmouse_y < yMenuItem + hMenuItem)
149 {
150 action = true;
151 }
152
153 break;
154 }
155 }
156 }
157 }
158
159 if (newmouse)
160 {
161 if (lastmouse_but == SDL_BUTTON_RIGHT)
162 {
163 JE_playSampleNum(S_SPRING);
164
165 cancel = true;
166 }
167 }
168 else if (newkey)
169 {
170 switch (lastkey_scan)
171 {
172 case SDL_SCANCODE_UP:
173 {
174 JE_playSampleNum(S_CURSOR);
175
176 selectedIndex = selectedIndex == 0
177 ? menuItemsCount - 1
178 : selectedIndex - 1;
179 break;
180 }
181 case SDL_SCANCODE_DOWN:
182 {
183 JE_playSampleNum(S_CURSOR);
184
185 selectedIndex = selectedIndex == menuItemsCount - 1
186 ? 0
187 : selectedIndex + 1;
188 break;
189 }
190 case SDL_SCANCODE_SPACE:
191 case SDL_SCANCODE_RETURN:
192 {
193 action = true;
194 break;
195 }
196 case SDL_SCANCODE_ESCAPE:
197 {
198 JE_playSampleNum(S_SPRING);
199
200 cancel = true;
201 break;
202 }
203 default:
204 break;
205 }
206 }
207
208 if (action)
209 {
210 switch (selectedIndex)
211 {
212 case MENU_ITEM_1_PLAYER_FULL_GAME:
213 case MENU_ITEM_1_PLAYER_ARCADE:
214 case MENU_ITEM_2_PLAYER_ARCADE:
215 {
216 JE_playSampleNum(S_SELECT);
217
218 fade_black(10);
219
220 onePlayerAction = selectedIndex == MENU_ITEM_1_PLAYER_ARCADE;
221 twoPlayerMode = selectedIndex == MENU_ITEM_2_PLAYER_ARCADE;
222 return true;
223 }
224 case MENU_ITEM_NETWORK:
225 {
226 JE_playSampleNum(S_SPRING);
227 break;
228 }
229 default:
230 break;
231 }
232 }
233
234 if (cancel)
235 {
236 fade_black(15);
237
238 return false;
239 }
240 }
241 }
242
243 bool episodeSelect(void)
244 {
245 if (shopSpriteSheet.data == NULL)
246 JE_loadCompShapes(&shopSpriteSheet, '1'); // need mouse pointer sprites
247
248 bool restart = true;
249
250 const size_t menuItemsCount = EPISODE_AVAILABLE;
251 size_t selectedIndex = 0;
252
253 const int xCenter = 320 / 2;
254 const int yMenuHeader = 20;
255 const int xMenuItem = 20;
256 const int yMenuItems = 50;
257 const int dyMenuItems = 30;
258 const int hMenuItem = 13;
259 int wMenuItem[EPISODE_AVAILABLE] = { 0 };
260
261 for (; ; )
262 {
263 if (restart)
264 {
265 JE_loadPic(VGAScreen2, 2, false);
266
267 // Draw header.
268 draw_font_hv_shadow(VGAScreen2, xCenter, yMenuHeader, episode_name[0], large_font, centered, 15, -3, false, 2);
269 }
270
271 // Restore background and header.
272 memcpy(VGAScreen->pixels, VGAScreen2->pixels, (size_t)VGAScreen->pitch * VGAScreen->h);
273
274 // Draw menu items.
275 for (size_t i = 0; i < menuItemsCount; ++i)
276 {
277 const char *const text = episode_name[i + 1];
278
279 wMenuItem[i] = JE_textWidth(text, normal_font);
280 const int y = yMenuItems + dyMenuItems * i;
281
282 const bool selected = i == selectedIndex;
283 const bool disabled = !episodeAvail[i];
284
285 draw_font_hv_shadow(VGAScreen, xMenuItem, y, text, normal_font, left_aligned, 15, -4 + (selected ? 2 : 0) + (disabled ? -4 : 0), false, 2);
286 }
287
288 if (restart)
289 {
290 mouseCursor = MOUSE_POINTER_NORMAL;
291
292 fade_palette(colors, 10, 0, 255);
293
294 restart = false;
295 }
296
297 service_SDL_events(true);
298
299 JE_mouseStart();
300 JE_showVGA();
301 JE_mouseReplace();
302
303 bool mouseMoved = false;
304 do
305 {
306 SDL_Delay(16);
307
308 Uint16 oldMouseX = mouse_x;
309 Uint16 oldMouseY = mouse_y;
310
311 push_joysticks_as_keyboard();
312 service_SDL_events(false);
313
314 NETWORK_KEEP_ALIVE();
315
316 mouseMoved = mouse_x != oldMouseX || mouse_y != oldMouseY;
317 } while (!(newkey || newmouse || mouseMoved));
318
319 // Handle interaction.
320
321 bool action = false;
322 bool cancel = false;
323
324 if (mouseMoved || newmouse)
325 {
326 // Find menu item that was hovered or clicked.
327 for (size_t i = 0; i < menuItemsCount; ++i)
328 {
329 if (mouse_x >= xMenuItem && mouse_x < xMenuItem + wMenuItem[i])
330 {
331 const int yMenuItem = yMenuItems + dyMenuItems * i;
332 if (mouse_y >= yMenuItem && mouse_y < yMenuItem + hMenuItem)
333 {
334 if (selectedIndex != i)
335 {
336 JE_playSampleNum(S_CURSOR);
337
338 selectedIndex = i;
339 }
340
341 if (newmouse && lastmouse_but == SDL_BUTTON_LEFT &&
342 lastmouse_x >= xMenuItem && lastmouse_x < xMenuItem + wMenuItem[i] &&
343 lastmouse_y >= yMenuItem && lastmouse_y < yMenuItem + hMenuItem)
344 {
345 action = true;
346 }
347
348 break;
349 }
350 }
351 }
352 }
353
354 if (newmouse)
355 {
356 if (lastmouse_but == SDL_BUTTON_RIGHT)
357 {
358 JE_playSampleNum(S_SPRING);
359
360 cancel = true;
361 }
362 }
363 else if (newkey)
364 {
365 switch (lastkey_scan)
366 {
367 case SDL_SCANCODE_UP:
368 {
369 JE_playSampleNum(S_CURSOR);
370
371 selectedIndex = selectedIndex == 0
372 ? menuItemsCount - 1
373 : selectedIndex - 1;
374 break;
375 }
376 case SDL_SCANCODE_DOWN:
377 {
378 JE_playSampleNum(S_CURSOR);
379
380 selectedIndex = selectedIndex == menuItemsCount - 1
381 ? 0
382 : selectedIndex + 1;
383 break;
384 }
385 case SDL_SCANCODE_SPACE:
386 case SDL_SCANCODE_RETURN:
387 {
388 action = true;
389 break;
390 }
391 case SDL_SCANCODE_ESCAPE:
392 {
393 JE_playSampleNum(S_SPRING);
394
395 cancel = true;
396 break;
397 }
398 default:
399 break;
400 }
401 }
402
403 if (action)
404 {
405 if (episodeAvail[selectedIndex])
406 {
407 JE_playSampleNum(S_SELECT);
408
409 fade_black(10);
410
411 JE_initEpisode(selectedIndex + 1);
412 initial_episode_num = episodeNum;
413 return true;
414 }
415 else
416 {
417 JE_playSampleNum(S_SPRING);
418 }
419 }
420
421 if (cancel)
422 {
423 fade_black(15);
424
425 return false;
426 }
427 }
428 }
429
430 bool difficultySelect(void)
431 {
432 if (shopSpriteSheet.data == NULL)
433 JE_loadCompShapes(&shopSpriteSheet, '1'); // need mouse pointer sprites
434
435 bool restart = true;
436
437 const size_t menuItemsCount = COUNTOF(difficulty_name) - 1;
438 size_t menuItemsVisibleCount = menuItemsCount - 3;
439 size_t selectedIndex = 1;
440 size_t lordProgress = 0;
441
442 const int xCenter = 320 / 2;
443 const int yMenuHeader = 20;
444 const int yMenuItems = 54;
445 const int dyMenuItems = 24;
446 const int hMenuItem = 13;
447 int wMenuItem[COUNTOF(difficulty_name) - 1] = { 0 };
448
449 for (; ; )
450 {
451 if (restart)
452 {
453 JE_loadPic(VGAScreen2, 2, false);
454
455 // Draw header.
456 draw_font_hv_shadow(VGAScreen2, xCenter, yMenuHeader, difficulty_name[0], large_font, centered, 15, -3, false, 2);
457 }
458
459 // Restore background and header.
460 memcpy(VGAScreen->pixels, VGAScreen2->pixels, (size_t)VGAScreen->pitch * VGAScreen->h);
461
462 // Draw menu items.
463 for (size_t i = 0; i < menuItemsVisibleCount; ++i)
464 {
465 const char *const text = difficulty_name[i + 1];
466
467 wMenuItem[i] = JE_textWidth(text, normal_font);
468 const int x = xCenter - wMenuItem[i] / 2;
469 const int y = yMenuItems + dyMenuItems * i;
470
471 const bool selected = i == selectedIndex;
472
473 draw_font_hv_shadow(VGAScreen, x, y, text, normal_font, left_aligned, 15, -4 + (selected ? 2 : 0), false, 2);
474 }
475
476 if (restart)
477 {
478 mouseCursor = MOUSE_POINTER_NORMAL;
479
480 fade_palette(colors, 10, 0, 255);
481
482 restart = false;
483 }
484
485 service_SDL_events(true);
486
487 JE_mouseStart();
488 JE_showVGA();
489 JE_mouseReplace();
490
491 bool mouseMoved = false;
492 do
493 {
494 SDL_Delay(16);
495
496 Uint16 oldMouseX = mouse_x;
497 Uint16 oldMouseY = mouse_y;
498
499 push_joysticks_as_keyboard();
500 service_SDL_events(false);
501
502 NETWORK_KEEP_ALIVE();
503
504 mouseMoved = mouse_x != oldMouseX || mouse_y != oldMouseY;
505 } while (!(newkey || newmouse || mouseMoved));
506
507 // Handle interaction.
508
509 if (menuItemsVisibleCount == 5)
510 {
511 const SDL_Scancode lordKeys[] = { SDL_SCANCODE_L, SDL_SCANCODE_O, SDL_SCANCODE_R, SDL_SCANCODE_D };
512
513 for (size_t i = 0; ; ++i)
514 {
515 if (i == COUNTOF(lordKeys))
516 {
517 menuItemsVisibleCount = 6;
518 break;
519 }
520
521 if (!keysactive[lordKeys[i]])
522 break;
523 }
524
525 if (newkey)
526 {
527 // Due to key jamming, holding down 4 keys simultaneous may not be possible, so an
528 // alternate input method is also provided.
529 if (lordProgress < COUNTOF(lordKeys) &&
530 lastkey_scan == lordKeys[lordProgress])
531 {
532 lordProgress += 1;
533
534 if (lordProgress == COUNTOF(lordKeys))
535 menuItemsVisibleCount = 6;
536 }
537 else
538 {
539 lordProgress = 0;
540 }
541 }
542 }
543 if (SDL_GetModState() & KMOD_SHIFT)
544 {
545 if (menuItemsVisibleCount == 4 && keysactive[SDL_SCANCODE_RIGHTBRACKET])
546 menuItemsVisibleCount = 5;
547 if (menuItemsVisibleCount == 3 && keysactive[SDL_SCANCODE_G])
548 menuItemsVisibleCount = 4;
549 }
550
551 bool action = false;
552 bool cancel = false;
553
554 if (mouseMoved || newmouse)
555 {
556 // Find menu item that was hovered or clicked.
557 for (size_t i = 0; i < menuItemsVisibleCount; ++i)
558 {
559 const int xMenuItem = xCenter - wMenuItem[i] / 2;
560 if (mouse_x >= xMenuItem && mouse_x < xMenuItem + wMenuItem[i])
561 {
562 const int yMenuItem = yMenuItems + dyMenuItems * i;
563 if (mouse_y >= yMenuItem && mouse_y < yMenuItem + hMenuItem)
564 {
565 if (selectedIndex != i)
566 {
567 JE_playSampleNum(S_CURSOR);
568
569 selectedIndex = i;
570 }
571
572 if (newmouse && lastmouse_but == SDL_BUTTON_LEFT &&
573 lastmouse_x >= xMenuItem && lastmouse_x < xMenuItem + wMenuItem[i] &&
574 lastmouse_y >= yMenuItem && lastmouse_y < yMenuItem + hMenuItem)
575 {
576 action = true;
577 }
578
579 break;
580 }
581 }
582 }
583 }
584
585 if (newmouse)
586 {
587 if (lastmouse_but == SDL_BUTTON_RIGHT)
588 {
589 JE_playSampleNum(S_SPRING);
590
591 cancel = true;
592 }
593 }
594 else if (newkey)
595 {
596 switch (lastkey_scan)
597 {
598 case SDL_SCANCODE_UP:
599 {
600 JE_playSampleNum(S_CURSOR);
601
602 selectedIndex = selectedIndex == 0
603 ? menuItemsVisibleCount - 1
604 : selectedIndex - 1;
605 break;
606 }
607 case SDL_SCANCODE_DOWN:
608 {
609 JE_playSampleNum(S_CURSOR);
610
611 selectedIndex = selectedIndex == menuItemsVisibleCount - 1
612 ? 0
613 : selectedIndex + 1;
614 break;
615 }
616 case SDL_SCANCODE_SPACE:
617 case SDL_SCANCODE_RETURN:
618 {
619 action = true;
620 break;
621 }
622 case SDL_SCANCODE_ESCAPE:
623 {
624 JE_playSampleNum(S_SPRING);
625
626 cancel = true;
627 break;
628 }
629 default:
630 break;
631 }
632 }
633
634 if (action)
635 {
636 JE_playSampleNum(S_SELECT);
637
638 switch (selectedIndex)
639 {
640 case 0:
641 difficultyLevel = DIFFICULTY_EASY;
642 break;
643 case 1:
644 difficultyLevel = DIFFICULTY_NORMAL;
645 break;
646 case 2:
647 difficultyLevel = DIFFICULTY_HARD;
648 break;
649 case 3:
650 difficultyLevel = DIFFICULTY_IMPOSSIBLE;
651 break;
652 case 4:
653 difficultyLevel = DIFFICULTY_SUICIDE;
654 break;
655 case 5:
656 difficultyLevel = DIFFICULTY_ZINGLON;
657 break;
658 }
659
660 fade_black(10);
661
662 return true;
663 }
664
665 if (cancel)
666 {
667 fade_black(15);
668
669 return false;
670 }
671 }
672 }