7513842e43eb8781fb8c469c05833be96cc0c7ec
[rocksndiamonds.git] / src / screens.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // screens.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "screens.h"
15 #include "events.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "editor.h"
19 #include "files.h"
20 #include "tape.h"
21 #include "anim.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26
27 #define DEBUG_JOYSTICKS         0
28
29
30 /* screens on the info screen */
31 #define INFO_MODE_MAIN                  0
32 #define INFO_MODE_TITLE                 1
33 #define INFO_MODE_ELEMENTS              2
34 #define INFO_MODE_MUSIC                 3
35 #define INFO_MODE_CREDITS               4
36 #define INFO_MODE_PROGRAM               5
37 #define INFO_MODE_VERSION               6
38 #define INFO_MODE_LEVELSET              7
39
40 #define MAX_INFO_MODES                  8
41
42 /* screens on the setup screen */
43 /* (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h) */
44 /* (should also match corresponding entries in src/conf_gfx.c) */
45 #define SETUP_MODE_MAIN                 0
46 #define SETUP_MODE_GAME                 1
47 #define SETUP_MODE_EDITOR               2
48 #define SETUP_MODE_GRAPHICS             3
49 #define SETUP_MODE_SOUND                4
50 #define SETUP_MODE_ARTWORK              5
51 #define SETUP_MODE_INPUT                6
52 #define SETUP_MODE_TOUCH                7
53 #define SETUP_MODE_SHORTCUTS            8
54 #define SETUP_MODE_SHORTCUTS_1          9
55 #define SETUP_MODE_SHORTCUTS_2          10
56 #define SETUP_MODE_SHORTCUTS_3          11
57 #define SETUP_MODE_SHORTCUTS_4          12
58 #define SETUP_MODE_SHORTCUTS_5          13
59
60 /* sub-screens on the setup screen (generic) */
61 #define SETUP_MODE_CHOOSE_ARTWORK       14
62 #define SETUP_MODE_CHOOSE_OTHER         15
63
64 /* sub-screens on the setup screen (specific) */
65 #define SETUP_MODE_CHOOSE_GAME_SPEED    16
66 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  17
67 #define SETUP_MODE_CHOOSE_SNAPSHOT_MODE 18
68 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   19
69 #define SETUP_MODE_CHOOSE_SCALING_TYPE  20
70 #define SETUP_MODE_CHOOSE_RENDERING     21
71 #define SETUP_MODE_CHOOSE_GRAPHICS      22
72 #define SETUP_MODE_CHOOSE_SOUNDS        23
73 #define SETUP_MODE_CHOOSE_MUSIC         24
74 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 25
75 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  26
76 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  27
77 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 28
78 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 29
79 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 30
80
81 #define MAX_SETUP_MODES                 31
82
83 #define MAX_MENU_MODES                  MAX(MAX_INFO_MODES, MAX_SETUP_MODES)
84
85 /* for input setup functions */
86 #define SETUPINPUT_SCREEN_POS_START     0
87 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
88 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
89 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
90
91 #define MENU_SETUP_FONT_TITLE           FONT_TEXT_1
92 #define MENU_SETUP_FONT_TEXT            FONT_TITLE_2
93
94 /* for various menu stuff  */
95 #define MENU_SCREEN_START_XPOS          1
96 #define MENU_SCREEN_START_YPOS          2
97 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
98 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
99 #define MENU_TITLE1_YPOS                8
100 #define MENU_TITLE2_YPOS                46
101 #define MENU_INFO_FONT_TITLE            FONT_TEXT_1
102 #define MENU_INFO_FONT_HEAD             FONT_TEXT_2
103 #define MENU_INFO_FONT_TEXT             FONT_TEXT_3
104 #define MENU_INFO_FONT_FOOT             FONT_TEXT_4
105 #define MENU_INFO_SPACE_HEAD            (menu.headline2_spacing_info[info_mode])
106 #define MENU_SCREEN_INFO_XSTART         16
107 #define MENU_SCREEN_INFO_YSTART1        100
108 #define MENU_SCREEN_INFO_YSTART2        (MENU_SCREEN_INFO_YSTART1 +            \
109                                          getMenuTextStep(MENU_INFO_SPACE_HEAD, \
110                                                          MENU_INFO_FONT_TITLE))
111 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
112 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - 20)
113 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
114                                          MENU_SCREEN_INFO_YSTART2 -     \
115                                          TILEY / 2)
116 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
117 #define STD_INFO_ELEMENTS_ON_SCREEN     (MENU_SCREEN_INFO_YSIZE /       \
118                                          MENU_SCREEN_INFO_YSTEP)
119 #define NUM_INFO_ELEMENTS_FROM_CONF     \
120   (menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] > 0 ?             \
121    menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] :                 \
122    MAX_MENU_ENTRIES_ON_SCREEN)
123 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MIN(STD_INFO_ELEMENTS_ON_SCREEN, \
124                                                 MAX_INFO_ELEMENTS_ON_SCREEN), \
125                                             NUM_INFO_ELEMENTS_FROM_CONF)
126 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
127 #define MAX_MENU_TEXT_LENGTH_BIG        13
128 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
129
130 /* buttons and scrollbars identifiers */
131 #define SCREEN_CTRL_ID_PREV_LEVEL       0
132 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
133 #define SCREEN_CTRL_ID_PREV_PLAYER      2
134 #define SCREEN_CTRL_ID_NEXT_PLAYER      3
135 #define SCREEN_CTRL_ID_SCROLL_UP        4
136 #define SCREEN_CTRL_ID_SCROLL_DOWN      5
137 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  6
138
139 #define NUM_SCREEN_GADGETS              7
140
141 #define NUM_SCREEN_MENUBUTTONS          4
142 #define NUM_SCREEN_SCROLLBUTTONS        2
143 #define NUM_SCREEN_SCROLLBARS           1
144
145 #define SCREEN_MASK_MAIN                (1 << 0)
146 #define SCREEN_MASK_INPUT               (1 << 1)
147
148 /* graphic position and size values for buttons and scrollbars */
149 #define SC_MENUBUTTON_XSIZE             TILEX
150 #define SC_MENUBUTTON_YSIZE             TILEY
151
152 #define SC_SCROLLBUTTON_XSIZE           TILEX
153 #define SC_SCROLLBUTTON_YSIZE           TILEY
154
155 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
156
157 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
158 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
159                                          SC_SCROLLBUTTON_YSIZE)
160
161 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
162 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
163
164 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
165 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
166                                          SC_SCROLLBUTTON_YSIZE)
167
168 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
169 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
170                                          SC_SCROLL_VERTICAL_YSIZE)
171
172 #define SC_BORDER_SIZE                  14
173
174
175 /* forward declarations of internal functions */
176 static void HandleScreenGadgets(struct GadgetInfo *);
177 static void HandleSetupScreen_Generic(int, int, int, int, int);
178 static void HandleSetupScreen_Input(int, int, int, int, int);
179 static void CustomizeKeyboard(int);
180 static void ConfigureJoystick(int);
181 static void execSetupGame(void);
182 static void execSetupGraphics(void);
183 static void execSetupSound(void);
184 static void execSetupTouch(void);
185 static void execSetupArtwork(void);
186 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
187
188 static void DrawChooseLevelSet(void);
189 static void DrawChooseLevelNr(void);
190 static void DrawInfoScreen(void);
191 static void DrawSetupScreen(void);
192
193 static void DrawInfoScreen_NotAvailable(char *, char *);
194 static void DrawInfoScreen_HelpAnim(int, int, boolean);
195 static void DrawInfoScreen_HelpText(int, int, int, int);
196 static void HandleInfoScreen_Main(int, int, int, int, int);
197 static void HandleInfoScreen_TitleScreen(int);
198 static void HandleInfoScreen_Elements(int);
199 static void HandleInfoScreen_Music(int);
200 static void HandleInfoScreen_Credits(int);
201 static void HandleInfoScreen_Program(int);
202 static void HandleInfoScreen_Version(int);
203
204 static void MapScreenMenuGadgets(int);
205 static void MapScreenGadgets(int);
206 static void MapScreenTreeGadgets(TreeInfo *);
207
208 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
209
210 static int info_mode = INFO_MODE_MAIN;
211 static int setup_mode = SETUP_MODE_MAIN;
212
213 static TreeInfo *window_sizes = NULL;
214 static TreeInfo *window_size_current = NULL;
215
216 static TreeInfo *scaling_types = NULL;
217 static TreeInfo *scaling_type_current = NULL;
218
219 static TreeInfo *rendering_modes = NULL;
220 static TreeInfo *rendering_mode_current = NULL;
221
222 static TreeInfo *scroll_delays = NULL;
223 static TreeInfo *scroll_delay_current = NULL;
224
225 static TreeInfo *snapshot_modes = NULL;
226 static TreeInfo *snapshot_mode_current = NULL;
227
228 static TreeInfo *game_speeds = NULL;
229 static TreeInfo *game_speed_current = NULL;
230
231 static TreeInfo *volumes_simple = NULL;
232 static TreeInfo *volume_simple_current = NULL;
233
234 static TreeInfo *volumes_loops = NULL;
235 static TreeInfo *volume_loops_current = NULL;
236
237 static TreeInfo *volumes_music = NULL;
238 static TreeInfo *volume_music_current = NULL;
239
240 static TreeInfo *touch_controls = NULL;
241 static TreeInfo *touch_control_current = NULL;
242
243 static TreeInfo *move_distances = NULL;
244 static TreeInfo *move_distance_current = NULL;
245
246 static TreeInfo *drop_distances = NULL;
247 static TreeInfo *drop_distance_current = NULL;
248
249 static TreeInfo *level_number = NULL;
250 static TreeInfo *level_number_current = NULL;
251
252 static struct
253 {
254   int value;
255   char *text;
256 } window_sizes_list[] =
257 {
258   {     50,     "50 %"                          },
259   {     80,     "80 %"                          },
260   {     90,     "90 %"                          },
261   {     100,    "100 % (Default)"               },
262   {     110,    "110 %"                         },
263   {     120,    "120 %"                         },
264   {     130,    "130 %"                         },
265   {     140,    "140 %"                         },
266   {     150,    "150 %"                         },
267   {     200,    "200 %"                         },
268   {     250,    "250 %"                         },
269   {     300,    "300 %"                         },
270
271   {     -1,     NULL                            },
272 };
273
274 static struct
275 {
276   char *value;
277   char *text;
278 } scaling_types_list[] =
279 {
280   {     SCALING_QUALITY_NEAREST, "None"         },
281   {     SCALING_QUALITY_LINEAR,  "Linear"       },
282   {     SCALING_QUALITY_BEST,    "Anisotropic"  },
283
284   {     NULL,                    NULL           },
285 };
286
287 static struct
288 {
289   char *value;
290   char *text;
291 } rendering_modes_list[] =
292 {
293   {     STR_SPECIAL_RENDERING_OFF,      "Off (May show artifacts, fast)" },
294   {     STR_SPECIAL_RENDERING_BITMAP,   "Bitmap/Texture mode (slower)"   },
295 #if DEBUG
296   // this mode may work under certain conditions, but does not work on Windows
297   {     STR_SPECIAL_RENDERING_TARGET,   "Target Texture mode (slower)"   },
298 #endif
299   {     STR_SPECIAL_RENDERING_DOUBLE,   "Double Texture mode (slower)"   },
300
301   {     NULL,                            NULL                            },
302 };
303
304 static struct
305 {
306   int value;
307   char *text;
308 } game_speeds_list[] =
309 {
310 #if 1
311   {     30,     "Very Slow"                     },
312   {     25,     "Slow"                          },
313   {     20,     "Normal"                        },
314   {     15,     "Fast"                          },
315   {     10,     "Very Fast"                     },
316 #else
317   {     1000,   "1/1s (Extremely Slow)"         },
318   {     500,    "1/2s"                          },
319   {     200,    "1/5s"                          },
320   {     100,    "1/10s"                         },
321   {     50,     "1/20s"                         },
322   {     29,     "1/35s (Original Supaplex)"     },
323   {     25,     "1/40s"                         },
324   {     20,     "1/50s (Normal Speed)"          },
325   {     14,     "1/70s (Maximum Supaplex)"      },
326   {     10,     "1/100s"                        },
327   {     5,      "1/200s"                        },
328   {     2,      "1/500s"                        },
329   {     1,      "1/1000s (Extremely Fast)"      },
330 #endif
331
332   {     -1,     NULL                            },
333 };
334
335 static struct
336 {
337   int value;
338   char *text;
339 } scroll_delays_list[] =
340 {
341   {     0,      "0 Tiles (No Scroll Delay)"     },
342   {     1,      "1 Tile"                        },
343   {     2,      "2 Tiles"                       },
344   {     3,      "3 Tiles (Default)"             },
345   {     4,      "4 Tiles"                       },
346   {     5,      "5 Tiles"                       },
347   {     6,      "6 Tiles"                       },
348   {     7,      "7 Tiles"                       },
349   {     8,      "8 Tiles (Maximum Scroll Delay)"},
350
351   {     -1,     NULL                            },
352 };
353
354 static struct
355 {
356   char *value;
357   char *text;
358 } snapshot_modes_list[] =
359 {
360   {     STR_SNAPSHOT_MODE_OFF,                  "Off"           },
361   {     STR_SNAPSHOT_MODE_EVERY_STEP,           "Every Step"    },
362   {     STR_SNAPSHOT_MODE_EVERY_MOVE,           "Every Move"    },
363   {     STR_SNAPSHOT_MODE_EVERY_COLLECT,        "Every Collect" },
364
365   {     NULL,                                   NULL            },
366 };
367
368 static struct
369 {
370   int value;
371   char *text;
372 } volumes_list[] =
373 {
374   {     0,      "0 %"                           },
375   {     1,      "1 %"                           },
376   {     2,      "2 %"                           },
377   {     5,      "5 %"                           },
378   {     10,     "10 %"                          },
379   {     20,     "20 %"                          },
380   {     30,     "30 %"                          },
381   {     40,     "40 %"                          },
382   {     50,     "50 %"                          },
383   {     60,     "60 %"                          },
384   {     70,     "70 %"                          },
385   {     80,     "80 %"                          },
386   {     90,     "90 %"                          },
387   {     100,    "100 %"                         },
388
389   {     -1,     NULL                            },
390 };
391
392 static struct
393 {
394   char *value;
395   char *text;
396 } touch_controls_list[] =
397 {
398   {     TOUCH_CONTROL_VIRTUAL_BUTTONS,  "Virtual Buttons"       },
399   {     TOUCH_CONTROL_WIPE_GESTURES,    "Wipe Gestures"         },
400   {     TOUCH_CONTROL_FOLLOW_FINGER,    "Follow Finger"         },
401
402   {     NULL,                           NULL                    },
403 };
404
405 static struct
406 {
407   int value;
408   char *text;
409 } distances_list[] =
410 {
411   {     1,      "1 %"                           },
412   {     2,      "2 %"                           },
413   {     3,      "3 %"                           },
414   {     4,      "4 %"                           },
415   {     5,      "5 %"                           },
416   {     10,     "10 %"                          },
417   {     15,     "15 %"                          },
418   {     20,     "20 %"                          },
419   {     25,     "25 %"                          },
420
421   {     -1,     NULL                            },
422 };
423
424 #define DRAW_MODE(s)            ((s) >= GAME_MODE_MAIN &&               \
425                                  (s) <= GAME_MODE_SETUP ? (s) :         \
426                                  (s) == GAME_MODE_PSEUDO_TYPENAME ?     \
427                                  GAME_MODE_MAIN : GAME_MODE_DEFAULT)
428
429 /* (there are no draw offset definitions needed for INFO_MODE_TITLE) */
430 #define DRAW_MODE_INFO(i)       ((i) >= INFO_MODE_TITLE &&              \
431                                  (i) <= INFO_MODE_LEVELSET ? (i) :      \
432                                  INFO_MODE_MAIN)
433
434 #define DRAW_MODE_SETUP(i)      ((i) >= SETUP_MODE_MAIN &&              \
435                                  (i) <= SETUP_MODE_SHORTCUTS_5 ? (i) :  \
436                                  (i) >= SETUP_MODE_CHOOSE_GRAPHICS &&   \
437                                  (i) <= SETUP_MODE_CHOOSE_MUSIC ?       \
438                                  SETUP_MODE_CHOOSE_ARTWORK :            \
439                                  SETUP_MODE_CHOOSE_OTHER)
440
441 #define DRAW_XOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
442                                  menu.draw_xoffset[GAME_MODE_INFO] :    \
443                                  menu.draw_xoffset_info[DRAW_MODE_INFO(i)])
444 #define DRAW_YOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
445                                  menu.draw_yoffset[GAME_MODE_INFO] :    \
446                                  menu.draw_yoffset_info[DRAW_MODE_INFO(i)])
447 #define EXTRA_SPACING_INFO(i)   (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ? \
448                                  menu.extra_spacing[GAME_MODE_INFO] :   \
449                                  menu.extra_spacing_info[DRAW_MODE_INFO(i)])
450
451 #define DRAW_XOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
452                                  menu.draw_xoffset[GAME_MODE_SETUP] :   \
453                                  menu.draw_xoffset_setup[DRAW_MODE_SETUP(i)])
454 #define DRAW_YOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
455                                  menu.draw_yoffset[GAME_MODE_SETUP] :   \
456                                  menu.draw_yoffset_setup[DRAW_MODE_SETUP(i)])
457 #define EXTRA_SPACING_SETUP(i)  (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
458                                  menu.extra_spacing[GAME_MODE_SETUP] :  \
459                                  menu.extra_spacing_setup[DRAW_MODE_SETUP(i)])
460
461 #define DRAW_XOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
462                                  DRAW_XOFFSET_INFO(info_mode) :         \
463                                  (s) == GAME_MODE_SETUP ?               \
464                                  DRAW_XOFFSET_SETUP(setup_mode) :       \
465                                  menu.draw_xoffset[DRAW_MODE(s)])
466 #define DRAW_YOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
467                                  DRAW_YOFFSET_INFO(info_mode) :         \
468                                  (s) == GAME_MODE_SETUP ?               \
469                                  DRAW_YOFFSET_SETUP(setup_mode) :       \
470                                  menu.draw_yoffset[DRAW_MODE(s)])
471 #define EXTRA_SPACING(s)        ((s) == GAME_MODE_INFO ?                \
472                                  EXTRA_SPACING_INFO(info_mode) :        \
473                                  (s) == GAME_MODE_SETUP ?               \
474                                  EXTRA_SPACING_SETUP(setup_mode) :      \
475                                  menu.extra_spacing[DRAW_MODE(s)])
476
477 #define mSX                     (SX + DRAW_XOFFSET(game_status))
478 #define mSY                     (SY + DRAW_YOFFSET(game_status))
479
480 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
481                                     menu.list_size[game_status] :       \
482                                     MAX_MENU_ENTRIES_ON_SCREEN)
483
484 #define IN_VIS_MENU(x, y)       IN_FIELD(x, y, SCR_FIELDX,              \
485                                          NUM_MENU_ENTRIES_ON_SCREEN)
486
487
488 /* title display and control definitions */
489
490 #define MAX_NUM_TITLE_SCREENS   (2 * MAX_NUM_TITLE_IMAGES +             \
491                                  2 * MAX_NUM_TITLE_MESSAGES)
492
493 #define NO_DIRECT_LEVEL_SELECT  (-1)
494
495
496 static int num_title_screens = 0;
497
498 struct TitleControlInfo
499 {
500   boolean is_image;
501   boolean initial;
502   boolean first;
503   int local_nr;
504   int sort_priority;
505 };
506
507 struct TitleControlInfo title_controls[MAX_NUM_TITLE_SCREENS];
508
509 /* main menu display and control definitions */
510
511 #define MAIN_CONTROL_NAME                       0
512 #define MAIN_CONTROL_LEVELS                     1
513 #define MAIN_CONTROL_SCORES                     2
514 #define MAIN_CONTROL_EDITOR                     3
515 #define MAIN_CONTROL_INFO                       4
516 #define MAIN_CONTROL_GAME                       5
517 #define MAIN_CONTROL_SETUP                      6
518 #define MAIN_CONTROL_QUIT                       7
519 #define MAIN_CONTROL_PREV_LEVEL                 8
520 #define MAIN_CONTROL_NEXT_LEVEL                 9
521 #define MAIN_CONTROL_FIRST_LEVEL                10
522 #define MAIN_CONTROL_LAST_LEVEL                 11
523 #define MAIN_CONTROL_LEVEL_NUMBER               12
524 #define MAIN_CONTROL_LEVEL_INFO_1               13
525 #define MAIN_CONTROL_LEVEL_INFO_2               14
526 #define MAIN_CONTROL_LEVEL_NAME                 15
527 #define MAIN_CONTROL_LEVEL_AUTHOR               16
528 #define MAIN_CONTROL_LEVEL_YEAR                 17
529 #define MAIN_CONTROL_LEVEL_IMPORTED_FROM        18
530 #define MAIN_CONTROL_LEVEL_IMPORTED_BY          19
531 #define MAIN_CONTROL_LEVEL_TESTED_BY            20
532 #define MAIN_CONTROL_TITLE_1                    21
533 #define MAIN_CONTROL_TITLE_2                    22
534 #define MAIN_CONTROL_TITLE_3                    23
535
536 static char str_main_text_name[10];
537 static char str_main_text_first_level[10];
538 static char str_main_text_last_level[10];
539 static char str_main_text_level_number[10];
540
541 static char *main_text_name                     = str_main_text_name;
542 static char *main_text_first_level              = str_main_text_first_level;
543 static char *main_text_last_level               = str_main_text_last_level;
544 static char *main_text_level_number             = str_main_text_level_number;
545 static char *main_text_levels                   = "Levelset";
546 static char *main_text_scores                   = "Hall Of Fame";
547 static char *main_text_editor                   = "Level Creator";
548 static char *main_text_info                     = "Info Screen";
549 static char *main_text_game                     = "Start Game";
550 static char *main_text_setup                    = "Setup";
551 static char *main_text_quit                     = "Quit";
552 static char *main_text_level_name               = level.name;
553 static char *main_text_level_author             = level.author;
554 static char *main_text_level_year               = NULL;
555 static char *main_text_level_imported_from      = NULL;
556 static char *main_text_level_imported_by        = NULL;
557 static char *main_text_level_tested_by          = NULL;
558
559 struct MainControlInfo
560 {
561   int nr;
562
563   struct MenuPosInfo *pos_button;
564   int button_graphic;
565
566   struct TextPosInfo *pos_text;
567   char **text;
568
569   struct TextPosInfo *pos_input;
570   char **input;
571 };
572
573 static struct MainControlInfo main_controls[] =
574 {
575   {
576     MAIN_CONTROL_NAME,
577     &menu.main.button.name,             IMG_MENU_BUTTON_NAME,
578     &menu.main.text.name,               &main_text_name,
579     &menu.main.input.name,              &setup.player_name,
580   },
581   {
582     MAIN_CONTROL_LEVELS,
583     &menu.main.button.levels,           IMG_MENU_BUTTON_LEVELS,
584     &menu.main.text.levels,             &main_text_levels,
585     NULL,                               NULL,
586   },
587   {
588     MAIN_CONTROL_SCORES,
589     &menu.main.button.scores,           IMG_MENU_BUTTON_SCORES,
590     &menu.main.text.scores,             &main_text_scores,
591     NULL,                               NULL,
592   },
593   {
594     MAIN_CONTROL_EDITOR,
595     &menu.main.button.editor,           IMG_MENU_BUTTON_EDITOR,
596     &menu.main.text.editor,             &main_text_editor,
597     NULL,                               NULL,
598   },
599   {
600     MAIN_CONTROL_INFO,
601     &menu.main.button.info,             IMG_MENU_BUTTON_INFO,
602     &menu.main.text.info,               &main_text_info,
603     NULL,                               NULL,
604   },
605   {
606     MAIN_CONTROL_GAME,
607     &menu.main.button.game,             IMG_MENU_BUTTON_GAME,
608     &menu.main.text.game,               &main_text_game,
609     NULL,                               NULL,
610   },
611   {
612     MAIN_CONTROL_SETUP,
613     &menu.main.button.setup,            IMG_MENU_BUTTON_SETUP,
614     &menu.main.text.setup,              &main_text_setup,
615     NULL,                               NULL,
616   },
617   {
618     MAIN_CONTROL_QUIT,
619     &menu.main.button.quit,             IMG_MENU_BUTTON_QUIT,
620     &menu.main.text.quit,               &main_text_quit,
621     NULL,                               NULL,
622   },
623 #if 0
624   /* (these two buttons are real gadgets) */
625   {
626     MAIN_CONTROL_PREV_LEVEL,
627     &menu.main.button.prev_level,       IMG_MENU_BUTTON_PREV_LEVEL,
628     NULL,                               NULL,
629     NULL,                               NULL,
630   },
631   {
632     MAIN_CONTROL_NEXT_LEVEL,
633     &menu.main.button.next_level,       IMG_MENU_BUTTON_NEXT_LEVEL,
634     NULL,                               NULL,
635     NULL,                               NULL,
636   },
637 #endif
638   {
639     MAIN_CONTROL_FIRST_LEVEL,
640     NULL,                               -1,
641     &menu.main.text.first_level,        &main_text_first_level,
642     NULL,                               NULL,
643   },
644   {
645     MAIN_CONTROL_LAST_LEVEL,
646     NULL,                               -1,
647     &menu.main.text.last_level,         &main_text_last_level,
648     NULL,                               NULL,
649   },
650   {
651     MAIN_CONTROL_LEVEL_NUMBER,
652     NULL,                               -1,
653     &menu.main.text.level_number,       &main_text_level_number,
654     NULL,                               NULL,
655   },
656   {
657     MAIN_CONTROL_LEVEL_INFO_1,
658     NULL,                               -1,
659     &menu.main.text.level_info_1,       NULL,
660     NULL,                               NULL,
661   },
662   {
663     MAIN_CONTROL_LEVEL_INFO_2,
664     NULL,                               -1,
665     &menu.main.text.level_info_2,       NULL,
666     NULL,                               NULL,
667   },
668   {
669     MAIN_CONTROL_LEVEL_NAME,
670     NULL,                               -1,
671     &menu.main.text.level_name,         &main_text_level_name,
672     NULL,                               NULL,
673   },
674   {
675     MAIN_CONTROL_LEVEL_AUTHOR,
676     NULL,                               -1,
677     &menu.main.text.level_author,       &main_text_level_author,
678     NULL,                               NULL,
679   },
680   {
681     MAIN_CONTROL_LEVEL_YEAR,
682     NULL,                               -1,
683     &menu.main.text.level_year,         &main_text_level_year,
684     NULL,                               NULL,
685   },
686   {
687     MAIN_CONTROL_LEVEL_IMPORTED_FROM,
688     NULL,                               -1,
689     &menu.main.text.level_imported_from, &main_text_level_imported_from,
690     NULL,                               NULL,
691   },
692   {
693     MAIN_CONTROL_LEVEL_IMPORTED_BY,
694     NULL,                               -1,
695     &menu.main.text.level_imported_by,  &main_text_level_imported_by,
696     NULL,                               NULL,
697   },
698   {
699     MAIN_CONTROL_LEVEL_TESTED_BY,
700     NULL,                               -1,
701     &menu.main.text.level_tested_by,    &main_text_level_tested_by,
702     NULL,                               NULL,
703   },
704   {
705     MAIN_CONTROL_TITLE_1,
706     NULL,                               -1,
707     &menu.main.text.title_1,            &setup.internal.program_title,
708     NULL,                               NULL,
709   },
710   {
711     MAIN_CONTROL_TITLE_2,
712     NULL,                               -1,
713     &menu.main.text.title_2,            &setup.internal.program_copyright,
714     NULL,                               NULL,
715   },
716   {
717     MAIN_CONTROL_TITLE_3,
718     NULL,                               -1,
719     &menu.main.text.title_3,            &setup.internal.program_company,
720     NULL,                               NULL,
721   },
722
723   {
724     -1,
725     NULL,                               -1,
726     NULL,                               NULL,
727     NULL,                               NULL,
728   }
729 };
730
731
732 static int getTitleScreenGraphic(int nr, boolean initial)
733 {
734   return (initial ? IMG_TITLESCREEN_INITIAL_1 : IMG_TITLESCREEN_1) + nr;
735 }
736
737 static struct TitleMessageInfo *getTitleMessageInfo(int nr, boolean initial)
738 {
739   return (initial ? &titlemessage_initial[nr] : &titlemessage[nr]);
740 }
741
742 #if 0
743 static int getTitleScreenGameMode(boolean initial)
744 {
745   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
746 }
747 #endif
748
749 static int getTitleMessageGameMode(boolean initial)
750 {
751   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
752 }
753
754 static int getTitleAnimMode(struct TitleControlInfo *tci)
755 {
756   int base = (tci->initial ? GAME_MODE_TITLE_INITIAL_1 : GAME_MODE_TITLE_1);
757
758   return base + tci->local_nr;
759 }
760
761 #if 0
762 static int getTitleScreenBackground(boolean initial)
763 {
764   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
765 }
766 #endif
767
768 #if 0
769 static int getTitleMessageBackground(int nr, boolean initial)
770 {
771   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
772 }
773 #endif
774
775 static int getTitleBackground(int nr, boolean initial, boolean is_image)
776 {
777   int base = (is_image ?
778               (initial ? IMG_BACKGROUND_TITLESCREEN_INITIAL_1 :
779                          IMG_BACKGROUND_TITLESCREEN_1) :
780               (initial ? IMG_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
781                          IMG_BACKGROUND_TITLEMESSAGE_1));
782   int graphic_global = (initial ? IMG_BACKGROUND_TITLE_INITIAL :
783                                   IMG_BACKGROUND_TITLE);
784   int graphic_local = base + nr;
785
786   if (graphic_info[graphic_local].bitmap != NULL)
787     return graphic_local;
788
789   if (graphic_info[graphic_global].bitmap != NULL)
790     return graphic_global;
791
792   return IMG_UNDEFINED;
793 }
794
795 static int getTitleSound(struct TitleControlInfo *tci)
796 {
797   boolean is_image = tci->is_image;
798   int initial = tci->initial;
799   int nr = tci->local_nr;
800   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
801   int base = (is_image ?
802               (initial ? SND_BACKGROUND_TITLESCREEN_INITIAL_1 :
803                          SND_BACKGROUND_TITLESCREEN_1) :
804               (initial ? SND_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
805                          SND_BACKGROUND_TITLEMESSAGE_1));
806   int sound_global = menu.sound[mode];
807   int sound_local = base + nr;
808
809 #if 0
810   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
811          nr, initial, is_image,
812          sound_global, getSoundListEntry(sound_global)->filename,
813          sound_local, getSoundListEntry(sound_local)->filename);
814 #endif
815
816   if (!strEqual(getSoundListEntry(sound_local)->filename, UNDEFINED_FILENAME))
817     return sound_local;
818
819   if (!strEqual(getSoundListEntry(sound_global)->filename, UNDEFINED_FILENAME))
820     return sound_global;
821
822   return SND_UNDEFINED;
823 }
824
825 static int getTitleMusic(struct TitleControlInfo *tci)
826 {
827   boolean is_image = tci->is_image;
828   int initial = tci->initial;
829   int nr = tci->local_nr;
830   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
831   int base = (is_image ?
832               (initial ? MUS_BACKGROUND_TITLESCREEN_INITIAL_1 :
833                          MUS_BACKGROUND_TITLESCREEN_1) :
834               (initial ? MUS_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
835                          MUS_BACKGROUND_TITLEMESSAGE_1));
836   int music_global = menu.music[mode];
837   int music_local = base + nr;
838
839 #if 0
840   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
841          nr, initial, is_image,
842          music_global, getMusicListEntry(music_global)->filename,
843          music_local, getMusicListEntry(music_local)->filename);
844 #endif
845
846   if (!strEqual(getMusicListEntry(music_local)->filename, UNDEFINED_FILENAME))
847     return music_local;
848
849   if (!strEqual(getMusicListEntry(music_global)->filename, UNDEFINED_FILENAME))
850     return music_global;
851
852   return MUS_UNDEFINED;
853 }
854
855 static struct TitleFadingInfo getTitleFading(struct TitleControlInfo *tci)
856 {
857   boolean is_image = tci->is_image;
858   boolean initial = tci->initial;
859   boolean first = tci->first;
860   int nr = tci->local_nr;
861   struct TitleMessageInfo tmi;
862   struct TitleFadingInfo ti;
863
864   tmi = (is_image ? (initial ? (first ?
865                                 titlescreen_initial_first[nr] :
866                                 titlescreen_initial[nr])
867                              : (first ?
868                                 titlescreen_first[nr] :
869                                 titlescreen[nr]))
870                   : (initial ? (first ?
871                                 titlemessage_initial_first[nr] :
872                                 titlemessage_initial[nr])
873                              : (first ?
874                                 titlemessage_first[nr] :
875                                 titlemessage[nr])));
876
877   ti.fade_mode  = tmi.fade_mode;
878   ti.fade_delay = tmi.fade_delay;
879   ti.post_delay = tmi.post_delay;
880   ti.auto_delay = tmi.auto_delay;
881
882   return ti;
883 }
884
885 static int compareTitleControlInfo(const void *object1, const void *object2)
886 {
887   const struct TitleControlInfo *tci1 = (struct TitleControlInfo *)object1;
888   const struct TitleControlInfo *tci2 = (struct TitleControlInfo *)object2;
889   int compare_result;
890
891   if (tci1->initial != tci2->initial)
892     compare_result = (tci1->initial ? -1 : +1);
893   else if (tci1->sort_priority != tci2->sort_priority)
894     compare_result = tci1->sort_priority - tci2->sort_priority;
895   else if (tci1->is_image != tci2->is_image)
896     compare_result = (tci1->is_image ? -1 : +1);
897   else
898     compare_result = tci1->local_nr - tci2->local_nr;
899
900   return compare_result;
901 }
902
903 static void InitializeTitleControlsExt_AddTitleInfo(boolean is_image,
904                                                     boolean initial,
905                                                     int nr, int sort_priority)
906 {
907   title_controls[num_title_screens].is_image = is_image;
908   title_controls[num_title_screens].initial = initial;
909   title_controls[num_title_screens].local_nr = nr;
910   title_controls[num_title_screens].sort_priority = sort_priority;
911
912   title_controls[num_title_screens].first = FALSE;      /* will be set later */
913
914   num_title_screens++;
915 }
916
917 static void InitializeTitleControls_CheckTitleInfo(boolean initial)
918 {
919   int i;
920
921   for (i = 0; i < MAX_NUM_TITLE_IMAGES; i++)
922   {
923     int graphic = getTitleScreenGraphic(i, initial);
924     Bitmap *bitmap = graphic_info[graphic].bitmap;
925     int sort_priority = graphic_info[graphic].sort_priority;
926
927     if (bitmap != NULL)
928       InitializeTitleControlsExt_AddTitleInfo(TRUE, initial, i, sort_priority);
929   }
930
931   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
932   {
933     struct TitleMessageInfo *tmi = getTitleMessageInfo(i, initial);
934     char *filename = getLevelSetTitleMessageFilename(i, initial);
935     int sort_priority = tmi->sort_priority;
936
937     if (filename != NULL)
938       InitializeTitleControlsExt_AddTitleInfo(FALSE, initial, i, sort_priority);
939   }
940 }
941
942 static void InitializeTitleControls(boolean show_title_initial)
943 {
944   num_title_screens = 0;
945
946   /* 1st step: initialize title screens for game start (only when starting) */
947   if (show_title_initial)
948     InitializeTitleControls_CheckTitleInfo(TRUE);
949
950   /* 2nd step: initialize title screens for current level set */
951   InitializeTitleControls_CheckTitleInfo(FALSE);
952
953   /* sort title screens according to sort_priority and title number */
954   qsort(title_controls, num_title_screens, sizeof(struct TitleControlInfo),
955         compareTitleControlInfo);
956
957   /* mark first title screen */
958   title_controls[0].first = TRUE;
959 }
960
961 static boolean visibleMenuPos(struct MenuPosInfo *pos)
962 {
963   return (pos != NULL && pos->x != -1 && pos->y != -1);
964 }
965
966 static boolean visibleTextPos(struct TextPosInfo *pos)
967 {
968   return (pos != NULL && pos->x != -1 && pos->y != -1);
969 }
970
971 static void InitializeMainControls()
972 {
973   boolean local_team_mode = (!options.network && setup.team_mode);
974   int i;
975
976   /* set main control text values to dynamically determined values */
977   sprintf(main_text_name,         "%s",   local_team_mode ? "Team:" : "Name:");
978
979   strcpy(main_text_first_level,  int2str(leveldir_current->first_level,
980                                          menu.main.text.first_level.size));
981   strcpy(main_text_last_level,   int2str(leveldir_current->last_level,
982                                          menu.main.text.last_level.size));
983   strcpy(main_text_level_number, int2str(level_nr,
984                                          menu.main.text.level_number.size));
985
986   main_text_level_year          = leveldir_current->year;
987   main_text_level_imported_from = leveldir_current->imported_from;
988   main_text_level_imported_by   = leveldir_current->imported_by;
989   main_text_level_tested_by     = leveldir_current->tested_by;
990
991   /* set main control screen positions to dynamically determined values */
992   for (i = 0; main_controls[i].nr != -1; i++)
993   {
994     struct MainControlInfo *mci = &main_controls[i];
995     int nr                         = mci->nr;
996     struct MenuPosInfo *pos_button = mci->pos_button;
997     struct TextPosInfo *pos_text   = mci->pos_text;
998     struct TextPosInfo *pos_input  = mci->pos_input;
999     char *text                     = (mci->text  ? *mci->text  : NULL);
1000     char *input                    = (mci->input ? *mci->input : NULL);
1001     int button_graphic             = mci->button_graphic;
1002     int font_text                  = (pos_text  ? pos_text->font  : -1);
1003     int font_input                 = (pos_input ? pos_input->font : -1);
1004
1005     int font_text_width   = (font_text  != -1 ? getFontWidth(font_text)   : 0);
1006     int font_text_height  = (font_text  != -1 ? getFontHeight(font_text)  : 0);
1007     int font_input_width  = (font_input != -1 ? getFontWidth(font_input)  : 0);
1008     int font_input_height = (font_input != -1 ? getFontHeight(font_input) : 0);
1009     int text_chars  = (text  != NULL ? strlen(text)  : 0);
1010     int input_chars = (input != NULL ? strlen(input) : 0);
1011
1012     int button_width =
1013       (button_graphic != -1 ? graphic_info[button_graphic].width  : 0);
1014     int button_height =
1015       (button_graphic != -1 ? graphic_info[button_graphic].height : 0);
1016     int text_width   = font_text_width * text_chars;
1017     int text_height  = font_text_height;
1018     int input_width  = font_input_width * input_chars;
1019     int input_height = font_input_height;
1020
1021     if (nr == MAIN_CONTROL_NAME)
1022     {
1023       menu.main.input.name.width  = input_width;
1024       menu.main.input.name.height = input_height;
1025     }
1026
1027     if (pos_button != NULL)             /* (x/y may be -1/-1 here) */
1028     {
1029       pos_button->width  = button_width;
1030       pos_button->height = button_height;
1031     }
1032
1033     if (pos_text != NULL)               /* (x/y may be -1/-1 here) */
1034     {
1035       /* calculate size for non-clickable text -- needed for text alignment */
1036       boolean calculate_text_size = (pos_button == NULL && text != NULL);
1037
1038       if (pos_text->width == -1 || calculate_text_size)
1039         pos_text->width = text_width;
1040       if (pos_text->height == -1 || calculate_text_size)
1041         pos_text->height = text_height;
1042
1043       if (visibleMenuPos(pos_button))
1044       {
1045         if (pos_text->x == -1)
1046           pos_text->x = pos_button->x + pos_button->width;
1047         if (pos_text->y == -1)
1048           pos_text->y =
1049             pos_button->y + (pos_button->height - pos_text->height) / 2;
1050       }
1051     }
1052
1053     if (pos_input != NULL)              /* (x/y may be -1/-1 here) */
1054     {
1055       if (visibleTextPos(pos_text))
1056       {
1057         if (pos_input->x == -1)
1058           pos_input->x = pos_text->x + pos_text->width;
1059         if (pos_input->y == -1)
1060           pos_input->y = pos_text->y;
1061       }
1062
1063       if (pos_input->width == -1)
1064         pos_input->width = input_width;
1065       if (pos_input->height == -1)
1066         pos_input->height = input_height;
1067     }
1068   }
1069 }
1070
1071 static void DrawPressedGraphicThruMask(int dst_x, int dst_y,
1072                                        int graphic, boolean pressed)
1073 {
1074   struct GraphicInfo *g = &graphic_info[graphic];
1075   Bitmap *src_bitmap;
1076   int src_x, src_y;
1077   int xoffset = (pressed ? g->pressed_xoffset : 0);
1078   int yoffset = (pressed ? g->pressed_yoffset : 0);
1079
1080   getFixedGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
1081
1082   BlitBitmapMasked(src_bitmap, drawto, src_x + xoffset, src_y + yoffset,
1083                    g->width, g->height, dst_x, dst_y);
1084 }
1085
1086 static void DrawCursorAndText_Main_Ext(int nr, boolean active_text,
1087                                        boolean active_input,
1088                                        boolean pressed_button)
1089 {
1090   int i;
1091
1092   for (i = 0; main_controls[i].nr != -1; i++)
1093   {
1094     struct MainControlInfo *mci = &main_controls[i];
1095
1096     if (mci->nr == nr || nr == -1)
1097     {
1098       struct MenuPosInfo *pos_button = mci->pos_button;
1099       struct TextPosInfo *pos_text   = mci->pos_text;
1100       struct TextPosInfo *pos_input  = mci->pos_input;
1101       char *text                     = (mci->text  ? *mci->text  : NULL);
1102       char *input                    = (mci->input ? *mci->input : NULL);
1103       int button_graphic             = mci->button_graphic;
1104       int font_text                  = (pos_text  ? pos_text->font  : -1);
1105       int font_input                 = (pos_input ? pos_input->font : -1);
1106
1107       if (active_text)
1108       {
1109         button_graphic = BUTTON_ACTIVE(button_graphic);
1110         font_text = FONT_ACTIVE(font_text);
1111       }
1112
1113       if (active_input)
1114       {
1115         font_input = FONT_ACTIVE(font_input);
1116       }
1117
1118       if (visibleMenuPos(pos_button))
1119       {
1120         struct MenuPosInfo *pos = pos_button;
1121         int x = mSX + pos->x;
1122         int y = mSY + pos->y;
1123
1124         DrawBackgroundForGraphic(x, y, pos->width, pos->height, button_graphic);
1125         DrawPressedGraphicThruMask(x, y, button_graphic, pressed_button);
1126       }
1127
1128       if (visibleTextPos(pos_text) && text != NULL)
1129       {
1130         struct TextPosInfo *pos = pos_text;
1131         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1132         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1133
1134 #if 1
1135         /* (check why/if this is needed) */
1136         DrawBackgroundForFont(x, y, pos->width, pos->height, font_text);
1137 #endif
1138         DrawText(x, y, text, font_text);
1139       }
1140
1141       if (visibleTextPos(pos_input) && input != NULL)
1142       {
1143         struct TextPosInfo *pos = pos_input;
1144         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1145         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1146
1147 #if 1
1148         /* (check why/if this is needed) */
1149         DrawBackgroundForFont(x, y, pos->width, pos->height, font_input);
1150 #endif
1151         DrawText(x, y, input, font_input);
1152       }
1153     }
1154   }
1155 }
1156
1157 static void DrawCursorAndText_Main(int nr, boolean active_text,
1158                                    boolean pressed_button)
1159 {
1160   DrawCursorAndText_Main_Ext(nr, active_text, FALSE, pressed_button);
1161 }
1162
1163 #if 0
1164 static void DrawCursorAndText_Main_Input(int nr, boolean active_text,
1165                                          boolean pressed_button)
1166 {
1167   DrawCursorAndText_Main_Ext(nr, active_text, TRUE, pressed_button);
1168 }
1169 #endif
1170
1171 static struct MainControlInfo *getMainControlInfo(int nr)
1172 {
1173   int i;
1174
1175   for (i = 0; main_controls[i].nr != -1; i++)
1176     if (main_controls[i].nr == nr)
1177       return &main_controls[i];
1178
1179   return NULL;
1180 }
1181
1182 static boolean insideMenuPosRect(struct MenuPosInfo *rect, int x, int y)
1183 {
1184   if (rect == NULL)
1185     return FALSE;
1186
1187   int rect_x = ALIGNED_TEXT_XPOS(rect);
1188   int rect_y = ALIGNED_TEXT_YPOS(rect);
1189
1190   return (x >= rect_x && x < rect_x + rect->width &&
1191           y >= rect_y && y < rect_y + rect->height);
1192 }
1193
1194 static boolean insideTextPosRect(struct TextPosInfo *rect, int x, int y)
1195 {
1196   if (rect == NULL)
1197     return FALSE;
1198
1199   int rect_x = ALIGNED_TEXT_XPOS(rect);
1200   int rect_y = ALIGNED_TEXT_YPOS(rect);
1201
1202 #if 0
1203   printf("::: insideTextPosRect: (%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d\n",
1204          x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1205          (x >= rect_x && x < rect_x + rect->width &&
1206           y >= rect_y && y < rect_y + rect->height));
1207 #endif
1208
1209   return (x >= rect_x && x < rect_x + rect->width &&
1210           y >= rect_y && y < rect_y + rect->height);
1211 }
1212
1213 static boolean insidePreviewRect(struct PreviewInfo *preview, int x, int y)
1214 {
1215   int rect_width  = preview->xsize * preview->tile_size;
1216   int rect_height = preview->ysize * preview->tile_size;
1217   int rect_x = ALIGNED_XPOS(preview->x, rect_width,  preview->align);
1218   int rect_y = ALIGNED_YPOS(preview->y, rect_height, preview->valign);
1219
1220   return (x >= rect_x && x < rect_x + rect_width &&
1221           y >= rect_y && y < rect_y + rect_height);
1222 }
1223
1224 static void AdjustScrollbar(int id, int items_max, int items_visible,
1225                             int item_position)
1226 {
1227   struct GadgetInfo *gi = screen_gadget[id];
1228
1229   if (item_position > items_max - items_visible)
1230     item_position = items_max - items_visible;
1231
1232   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1233                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1234                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1235 }
1236
1237 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1238 {
1239   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
1240                   first_entry);
1241 }
1242
1243 static void clearMenuListArea()
1244 {
1245   int scrollbar_xpos = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
1246
1247   /* correct scrollbar position if placed outside menu (playfield) area */
1248   if (scrollbar_xpos > SX + SC_SCROLLBAR_XPOS)
1249     scrollbar_xpos = SX + SC_SCROLLBAR_XPOS;
1250
1251   /* clear menu list area, but not title or scrollbar */
1252   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1253                  scrollbar_xpos - mSX, NUM_MENU_ENTRIES_ON_SCREEN * 32);
1254 }
1255
1256 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1257 {
1258   static int cursor_array[MAX_LEV_FIELDY];
1259   int x = mSX + TILEX * xpos;
1260   int y = mSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1261
1262   if (xpos == 0)
1263   {
1264     if (graphic != -1)
1265       cursor_array[ypos] = graphic;
1266     else
1267       graphic = cursor_array[ypos];
1268   }
1269
1270   if (active)
1271     graphic = BUTTON_ACTIVE(graphic);
1272
1273   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1274   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1275 }
1276
1277 static void initCursor(int ypos, int graphic)
1278 {
1279   drawCursorExt(0, ypos, FALSE, graphic);
1280 }
1281
1282 static void drawCursor(int ypos, boolean active)
1283 {
1284   drawCursorExt(0, ypos, active, -1);
1285 }
1286
1287 static void drawCursorXY(int xpos, int ypos, int graphic)
1288 {
1289   drawCursorExt(xpos, ypos, FALSE, graphic);
1290 }
1291
1292 static void drawChooseTreeCursor(int ypos, boolean active)
1293 {
1294   drawCursorExt(0, ypos, active, -1);
1295 }
1296
1297 void DrawHeadline()
1298 {
1299   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, getProgramTitleString());
1300   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
1301                     setup.internal.program_copyright);
1302 }
1303
1304 void DrawTitleScreenImage(int nr, boolean initial)
1305 {
1306   int graphic = getTitleScreenGraphic(nr, initial);
1307   Bitmap *bitmap = graphic_info[graphic].bitmap;
1308   int width  = graphic_info[graphic].width;
1309   int height = graphic_info[graphic].height;
1310   int src_x = graphic_info[graphic].src_x;
1311   int src_y = graphic_info[graphic].src_y;
1312   int dst_x, dst_y;
1313
1314   if (bitmap == NULL)
1315     return;
1316
1317   if (width > WIN_XSIZE)
1318   {
1319     /* image width too large for window => center image horizontally */
1320     src_x = (width - WIN_XSIZE) / 2;
1321     width = WIN_XSIZE;
1322   }
1323
1324   if (height > WIN_YSIZE)
1325   {
1326     /* image height too large for window => center image vertically */
1327     src_y = (height - WIN_YSIZE) / 2;
1328     height = WIN_YSIZE;
1329   }
1330
1331   /* always display title screens centered */
1332   dst_x = (WIN_XSIZE - width) / 2;
1333   dst_y = (WIN_YSIZE - height) / 2;
1334
1335   SetDrawBackgroundMask(REDRAW_ALL);
1336   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1337
1338   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1339
1340   if (DrawingOnBackground(dst_x, dst_y))
1341     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1342   else
1343     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1344
1345   redraw_mask = REDRAW_ALL;
1346 }
1347
1348 void DrawTitleScreenMessage(int nr, boolean initial)
1349 {
1350   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1351   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1352
1353   if (filename == NULL)
1354     return;
1355
1356   /* force TITLE font on title message screen */
1357   SetFontStatus(getTitleMessageGameMode(initial));
1358
1359   /* if chars *and* width set to "-1", automatically determine width */
1360   if (tmi->chars == -1 && tmi->width == -1)
1361     tmi->width = viewport.window[game_status].width;
1362
1363   /* if lines *and* height set to "-1", automatically determine height */
1364   if (tmi->lines == -1 && tmi->height == -1)
1365     tmi->height = viewport.window[game_status].height;
1366
1367   /* if chars set to "-1", automatically determine by text and font width */
1368   if (tmi->chars == -1)
1369     tmi->chars = tmi->width / getFontWidth(tmi->font);
1370   else
1371     tmi->width = tmi->chars * getFontWidth(tmi->font);
1372
1373   /* if lines set to "-1", automatically determine by text and font height */
1374   if (tmi->lines == -1)
1375     tmi->lines = tmi->height / getFontHeight(tmi->font);
1376   else
1377     tmi->height = tmi->lines * getFontHeight(tmi->font);
1378
1379   /* if x set to "-1", automatically determine by width and alignment */
1380   if (tmi->x == -1)
1381     tmi->x = -1 * ALIGNED_XPOS(0, tmi->width, tmi->align);
1382
1383   /* if y set to "-1", automatically determine by height and alignment */
1384   if (tmi->y == -1)
1385     tmi->y = -1 * ALIGNED_YPOS(0, tmi->height, tmi->valign);
1386
1387   SetDrawBackgroundMask(REDRAW_ALL);
1388   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1389
1390   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1391
1392   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1393                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1394                tmi->autowrap, tmi->centered, tmi->parse_comments);
1395
1396   ResetFontStatus();
1397 }
1398
1399 void DrawTitleScreen()
1400 {
1401   KeyboardAutoRepeatOff();
1402
1403   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1404 }
1405
1406 boolean CheckTitleScreen(boolean levelset_has_changed)
1407 {
1408   static boolean show_title_initial = TRUE;
1409   boolean show_titlescreen = FALSE;
1410
1411   /* needed to be able to skip title screen, if no image or message defined */
1412   InitializeTitleControls(show_title_initial);
1413
1414   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1415     show_titlescreen = TRUE;
1416
1417   /* show initial title images and messages only once at program start */
1418   show_title_initial = FALSE;
1419
1420   return (show_titlescreen && num_title_screens > 0);
1421 }
1422
1423 void DrawMainMenu()
1424 {
1425   static LevelDirTree *leveldir_last_valid = NULL;
1426   boolean levelset_has_changed = FALSE;
1427   int fade_mask = REDRAW_FIELD;
1428
1429   LimitScreenUpdates(FALSE);
1430
1431   FadeSetLeaveScreen();
1432
1433   /* do not fade out here -- function may continue and fade on editor screen */
1434
1435   UnmapAllGadgets();
1436   FadeMenuSoundsAndMusic();
1437
1438   ExpireSoundLoops(FALSE);
1439
1440   KeyboardAutoRepeatOn();
1441
1442   audio.sound_deactivated = FALSE;
1443
1444   GetPlayerConfig();
1445
1446   /* needed if last screen was the playing screen, invoked from level editor */
1447   if (level_editor_test_game)
1448   {
1449     CloseDoor(DOOR_CLOSE_ALL);
1450
1451     SetGameStatus(GAME_MODE_EDITOR);
1452
1453     DrawLevelEd();
1454
1455     return;
1456   }
1457
1458   /* needed if last screen was the setup screen and fullscreen state changed */
1459   // (moved to "execSetupGraphics()" to change fullscreen state directly)
1460   // ToggleFullscreenOrChangeWindowScalingIfNeeded();
1461
1462   /* leveldir_current may be invalid (level group, parent link) */
1463   if (!validLevelSeries(leveldir_current))
1464     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1465
1466   if (leveldir_current != leveldir_last_valid)
1467     levelset_has_changed = TRUE;
1468
1469   /* store valid level series information */
1470   leveldir_last_valid = leveldir_current;
1471
1472   init_last = init;                     /* switch to new busy animation */
1473
1474   /* needed if last screen (level choice) changed graphics, sounds or music */
1475   ReloadCustomArtwork(0);
1476
1477   if (CheckTitleScreen(levelset_has_changed))
1478   {
1479     SetGameStatus(GAME_MODE_TITLE);
1480
1481     DrawTitleScreen();
1482
1483     return;
1484   }
1485
1486   if (redraw_mask & REDRAW_ALL)
1487     fade_mask = REDRAW_ALL;
1488
1489   if (CheckIfGlobalBorderHasChanged())
1490     fade_mask = REDRAW_ALL;
1491
1492   FadeOut(fade_mask);
1493
1494   /* needed if different viewport properties defined for menues */
1495   ChangeViewportPropertiesIfNeeded();
1496
1497   SetDrawtoField(DRAW_TO_BACKBUFFER);
1498
1499   /* level_nr may have been set to value over handicap with level editor */
1500   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1501     level_nr = leveldir_current->handicap_level;
1502
1503   LoadLevel(level_nr);
1504   LoadScore(level_nr);
1505
1506   SaveLevelSetup_SeriesInfo();
1507
1508   // set this after "ChangeViewportPropertiesIfNeeded()" (which may reset it)
1509   SetDrawDeactivationMask(REDRAW_NONE);
1510   SetDrawBackgroundMask(REDRAW_FIELD);
1511
1512   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1513
1514 #if 0
1515   if (fade_mask == REDRAW_ALL)
1516     RedrawGlobalBorder();
1517 #endif
1518
1519   ClearField();
1520
1521   InitializeMainControls();
1522
1523   DrawCursorAndText_Main(-1, FALSE, FALSE);
1524   DrawPreviewLevelInitial();
1525
1526   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1527
1528   TapeStop();
1529   if (TAPE_IS_EMPTY(tape))
1530     LoadTape(level_nr);
1531   DrawCompleteVideoDisplay();
1532
1533   PlayMenuSoundsAndMusic();
1534
1535   /* create gadgets for main menu screen */
1536   FreeScreenGadgets();
1537   CreateScreenGadgets();
1538
1539   /* map gadgets for main menu screen */
1540   MapTapeButtons();
1541   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1542
1543   /* copy actual game door content to door double buffer for OpenDoor() */
1544   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
1545   BlitBitmap(drawto, bitmap_db_door_2, VX, VY, VXSIZE, VYSIZE, 0, 0);
1546
1547   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1548
1549   DrawMaskedBorder(fade_mask);
1550
1551   FadeIn(fade_mask);
1552   FadeSetEnterMenu();
1553
1554   /* update screen area with special editor door */
1555   redraw_mask |= REDRAW_ALL;
1556   BackToFront();
1557
1558   SetMouseCursor(CURSOR_DEFAULT);
1559
1560   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1561 }
1562
1563 static void gotoTopLevelDir()
1564 {
1565   /* move upwards until inside (but not above) top level directory */
1566   while (leveldir_current->node_parent &&
1567          !strEqual(leveldir_current->node_parent->subdir, STRING_TOP_DIRECTORY))
1568   {
1569     /* write a "path" into level tree for easy navigation to last level */
1570     if (leveldir_current->node_parent->node_group->cl_first == -1)
1571     {
1572       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1573       int leveldir_pos = posTreeInfo(leveldir_current);
1574       int num_page_entries;
1575       int cl_first, cl_cursor;
1576
1577       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
1578         num_page_entries = num_leveldirs;
1579       else
1580         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1581
1582       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1583       cl_cursor = leveldir_pos - cl_first;
1584
1585       leveldir_current->node_parent->node_group->cl_first = cl_first;
1586       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1587     }
1588
1589     leveldir_current = leveldir_current->node_parent;
1590   }
1591 }
1592
1593 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1594 {
1595   static unsigned int title_delay = 0;
1596   static int title_screen_nr = 0;
1597   static int last_sound = -1, last_music = -1;
1598   boolean return_to_main_menu = FALSE;
1599   struct TitleControlInfo *tci;
1600   int sound, music;
1601
1602   if (button == MB_MENU_INITIALIZE)
1603   {
1604     title_delay = 0;
1605     title_screen_nr = 0;
1606     tci = &title_controls[title_screen_nr];
1607
1608     SetAnimStatus(getTitleAnimMode(tci));
1609
1610     last_sound = SND_UNDEFINED;
1611     last_music = MUS_UNDEFINED;
1612
1613     if (num_title_screens != 0)
1614     {
1615       FadeSetEnterScreen();
1616
1617       /* use individual title fading instead of global "enter screen" fading */
1618       fading = getTitleFading(tci);
1619     }
1620
1621     if (game_status_last_screen == GAME_MODE_INFO)
1622     {
1623       if (num_title_screens == 0)
1624       {
1625         /* switch game mode from title screen mode back to info screen mode */
1626         SetGameStatus(GAME_MODE_INFO);
1627
1628         /* store that last screen was info screen, not main menu screen */
1629         game_status_last_screen = GAME_MODE_INFO;
1630
1631         DrawInfoScreen_NotAvailable("Title screen information:",
1632                                     "No title screen for this level set.");
1633         return;
1634       }
1635
1636       FadeMenuSoundsAndMusic();
1637     }
1638
1639     FadeOut(REDRAW_ALL);
1640
1641     /* title screens may have different window size */
1642     ChangeViewportPropertiesIfNeeded();
1643
1644     /* only required to update logic for redrawing global border */
1645     ClearField();
1646
1647     if (tci->is_image)
1648       DrawTitleScreenImage(tci->local_nr, tci->initial);
1649     else
1650       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1651
1652     sound = getTitleSound(tci);
1653     music = getTitleMusic(tci);
1654
1655     if (sound != last_sound)
1656       PlayMenuSoundExt(sound);
1657     if (music != last_music)
1658       PlayMenuMusicExt(music);
1659
1660     last_sound = sound;
1661     last_music = music;
1662
1663     SetMouseCursor(CURSOR_NONE);
1664
1665     FadeIn(REDRAW_ALL);
1666
1667     DelayReached(&title_delay, 0);      /* reset delay counter */
1668
1669     return;
1670   }
1671
1672   if (fading.auto_delay > 0 && DelayReached(&title_delay, fading.auto_delay))
1673     button = MB_MENU_CHOICE;
1674
1675   if (button == MB_MENU_LEAVE)
1676   {
1677     return_to_main_menu = TRUE;
1678   }
1679   else if (button == MB_MENU_CHOICE)
1680   {
1681     if (game_status_last_screen == GAME_MODE_INFO && num_title_screens == 0)
1682     {
1683       SetGameStatus(GAME_MODE_INFO);
1684
1685       info_mode = INFO_MODE_MAIN;
1686
1687       DrawInfoScreen();
1688
1689       return;
1690     }
1691
1692     title_screen_nr++;
1693
1694     if (title_screen_nr < num_title_screens)
1695     {
1696       tci = &title_controls[title_screen_nr];
1697
1698       SetAnimStatus(getTitleAnimMode(tci));
1699
1700       sound = getTitleSound(tci);
1701       music = getTitleMusic(tci);
1702
1703       if (last_sound != SND_UNDEFINED && sound != last_sound)
1704         FadeSound(last_sound);
1705       if (last_music != MUS_UNDEFINED && music != last_music)
1706         FadeMusic();
1707
1708       fading = getTitleFading(tci);
1709
1710       FadeOut(REDRAW_ALL);
1711
1712       if (tci->is_image)
1713         DrawTitleScreenImage(tci->local_nr, tci->initial);
1714       else
1715         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1716
1717       sound = getTitleSound(tci);
1718       music = getTitleMusic(tci);
1719
1720       if (sound != last_sound)
1721         PlayMenuSoundExt(sound);
1722       if (music != last_music)
1723         PlayMenuMusicExt(music);
1724
1725       last_sound = sound;
1726       last_music = music;
1727
1728       FadeIn(REDRAW_ALL);
1729
1730       DelayReached(&title_delay, 0);    /* reset delay counter */
1731     }
1732     else
1733     {
1734       FadeMenuSoundsAndMusic();
1735
1736       return_to_main_menu = TRUE;
1737     }
1738   }
1739
1740   if (return_to_main_menu)
1741   {
1742     SetMouseCursor(CURSOR_DEFAULT);
1743
1744     /* force full menu screen redraw after displaying title screens */
1745     redraw_mask = REDRAW_ALL;
1746
1747     if (game_status_last_screen == GAME_MODE_INFO)
1748     {
1749       SetGameStatus(GAME_MODE_INFO);
1750
1751       info_mode = INFO_MODE_MAIN;
1752
1753       DrawInfoScreen();
1754     }
1755     else        /* default: return to main menu */
1756     {
1757       SetGameStatus(GAME_MODE_MAIN);
1758
1759       DrawMainMenu();
1760     }
1761   }
1762 }
1763
1764 void HandleMainMenu_SelectLevel(int step, int direction, int selected_level_nr)
1765 {
1766   int old_level_nr = level_nr;
1767   int new_level_nr;
1768
1769   if (selected_level_nr != NO_DIRECT_LEVEL_SELECT)
1770     new_level_nr = selected_level_nr;
1771   else
1772     new_level_nr = old_level_nr + step * direction;
1773
1774   if (new_level_nr < leveldir_current->first_level)
1775     new_level_nr = leveldir_current->first_level;
1776   if (new_level_nr > leveldir_current->last_level)
1777     new_level_nr = leveldir_current->last_level;
1778
1779   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1780   {
1781     /* skipping levels is only allowed when trying to skip single level */
1782     if (setup.skip_levels && new_level_nr == old_level_nr + 1 &&
1783         Request("Level still unsolved! Skip despite handicap?", REQ_ASK))
1784     {
1785       leveldir_current->handicap_level++;
1786       SaveLevelSetup_SeriesInfo();
1787     }
1788
1789     new_level_nr = leveldir_current->handicap_level;
1790   }
1791
1792   if (new_level_nr != old_level_nr)
1793   {
1794     struct MainControlInfo *mci= getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1795
1796     PlaySound(SND_MENU_ITEM_SELECTING);
1797
1798     level_nr = new_level_nr;
1799
1800     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1801              int2str(level_nr, menu.main.text.level_number.size),
1802              mci->pos_text->font);
1803
1804     LoadLevel(level_nr);
1805     DrawPreviewLevelInitial();
1806
1807     TapeErase();
1808     LoadTape(level_nr);
1809     DrawCompleteVideoDisplay();
1810
1811     SaveLevelSetup_SeriesInfo();
1812
1813     /* needed because DrawPreviewLevelInitial() takes some time */
1814     BackToFront();
1815     /* SyncDisplay(); */
1816   }
1817 }
1818
1819 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1820 {
1821   static int choice = MAIN_CONTROL_GAME;
1822   static boolean button_pressed_last = FALSE;
1823   boolean button_pressed = FALSE;
1824   int pos = choice;
1825   int i;
1826
1827   if (button == MB_MENU_INITIALIZE)
1828   {
1829     DrawCursorAndText_Main(choice, TRUE, FALSE);
1830
1831     return;
1832   }
1833
1834   if (mx || my)         /* mouse input */
1835   {
1836     pos = -1;
1837
1838     for (i = 0; main_controls[i].nr != -1; i++)
1839     {
1840       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1841           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1842           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1843       {
1844         pos = main_controls[i].nr;
1845
1846         break;
1847       }
1848     }
1849
1850     /* check if level preview was clicked */
1851     if (insidePreviewRect(&preview, mx - SX, my - SY))
1852       pos = MAIN_CONTROL_GAME;
1853
1854     // handle pressed/unpressed state for active/inactive menu buttons
1855     // (if pos != -1, "i" contains index position corresponding to "pos")
1856     if (button &&
1857         pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT &&
1858         insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY))
1859       button_pressed = TRUE;
1860
1861     if (button_pressed != button_pressed_last)
1862     {
1863       DrawCursorAndText_Main(choice, TRUE, button_pressed);
1864
1865       if (button_pressed)
1866         PlaySound(SND_MENU_BUTTON_PRESSING);
1867       else
1868         PlaySound(SND_MENU_BUTTON_RELEASING);
1869     }
1870   }
1871   else if (dx || dy)    /* keyboard input */
1872   {
1873     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
1874                    choice == MAIN_CONTROL_SETUP))
1875       button = MB_MENU_CHOICE;
1876     else if (dy)
1877       pos = choice + dy;
1878   }
1879
1880   if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
1881   {
1882     HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
1883   }
1884   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
1885   {
1886     HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
1887   }
1888   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
1889   {
1890     CloseDoor(DOOR_CLOSE_2);
1891
1892     SetGameStatus(GAME_MODE_LEVELNR);
1893
1894     DrawChooseLevelNr();
1895   }
1896   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
1897   {
1898     if (button)
1899     {
1900       if (pos != choice)
1901       {
1902         PlaySound(SND_MENU_ITEM_ACTIVATING);
1903
1904         DrawCursorAndText_Main(choice, FALSE, FALSE);
1905         DrawCursorAndText_Main(pos, TRUE, button_pressed);
1906
1907         choice = pos;
1908       }
1909       else if (dx != 0)
1910       {
1911         if (choice != MAIN_CONTROL_INFO &&
1912             choice != MAIN_CONTROL_SETUP)
1913           HandleMainMenu_SelectLevel(1, dx, NO_DIRECT_LEVEL_SELECT);
1914       }
1915     }
1916     else
1917     {
1918       PlaySound(SND_MENU_ITEM_SELECTING);
1919
1920       if (pos == MAIN_CONTROL_NAME)
1921       {
1922         SetGameStatus(GAME_MODE_PSEUDO_TYPENAME);
1923
1924         HandleTypeName(strlen(setup.player_name), 0);
1925       }
1926       else if (pos == MAIN_CONTROL_LEVELS)
1927       {
1928         if (leveldir_first)
1929         {
1930           CloseDoor(DOOR_CLOSE_2);
1931
1932           SetGameStatus(GAME_MODE_LEVELS);
1933
1934           SaveLevelSetup_LastSeries();
1935           SaveLevelSetup_SeriesInfo();
1936
1937           if (setup.internal.choose_from_top_leveldir)
1938             gotoTopLevelDir();
1939
1940           DrawChooseLevelSet();
1941         }
1942       }
1943       else if (pos == MAIN_CONTROL_SCORES)
1944       {
1945         CloseDoor(DOOR_CLOSE_2);
1946
1947         SetGameStatus(GAME_MODE_SCORES);
1948
1949         DrawHallOfFame(-1);
1950       }
1951       else if (pos == MAIN_CONTROL_EDITOR)
1952       {
1953         if (leveldir_current->readonly &&
1954             !strEqual(setup.player_name, "Artsoft"))
1955           Request("This level is read only!", REQ_CONFIRM);
1956
1957         CloseDoor(DOOR_CLOSE_2);
1958
1959         SetGameStatus(GAME_MODE_EDITOR);
1960
1961         FadeSetEnterScreen();
1962
1963         DrawLevelEd();
1964       }
1965       else if (pos == MAIN_CONTROL_INFO)
1966       {
1967         CloseDoor(DOOR_CLOSE_2);
1968
1969         SetGameStatus(GAME_MODE_INFO);
1970
1971         info_mode = INFO_MODE_MAIN;
1972
1973         DrawInfoScreen();
1974       }
1975       else if (pos == MAIN_CONTROL_GAME)
1976       {
1977         StartGameActions(options.network, setup.autorecord, level.random_seed);
1978       }
1979       else if (pos == MAIN_CONTROL_SETUP)
1980       {
1981         CloseDoor(DOOR_CLOSE_2);
1982
1983         SetGameStatus(GAME_MODE_SETUP);
1984
1985         setup_mode = SETUP_MODE_MAIN;
1986
1987         DrawSetupScreen();
1988       }
1989       else if (pos == MAIN_CONTROL_QUIT)
1990       {
1991         SaveLevelSetup_LastSeries();
1992         SaveLevelSetup_SeriesInfo();
1993
1994         if (Request("Do you really want to quit?", REQ_ASK | REQ_STAY_CLOSED))
1995           SetGameStatus(GAME_MODE_QUIT);
1996       }
1997     }
1998   }
1999
2000   button_pressed_last = button_pressed;
2001 }
2002
2003
2004 /* ========================================================================= */
2005 /* info screen functions                                                     */
2006 /* ========================================================================= */
2007
2008 static struct TokenInfo *info_info;
2009 static int num_info_info;       /* number of info entries shown on screen */
2010 static int max_info_info;       /* total number of info entries in list */
2011
2012 static void execInfoTitleScreen()
2013 {
2014   info_mode = INFO_MODE_TITLE;
2015
2016   DrawInfoScreen();
2017 }
2018
2019 static void execInfoElements()
2020 {
2021   info_mode = INFO_MODE_ELEMENTS;
2022
2023   DrawInfoScreen();
2024 }
2025
2026 static void execInfoMusic()
2027 {
2028   info_mode = INFO_MODE_MUSIC;
2029
2030   DrawInfoScreen();
2031 }
2032
2033 static void execInfoCredits()
2034 {
2035   info_mode = INFO_MODE_CREDITS;
2036
2037   DrawInfoScreen();
2038 }
2039
2040 static void execInfoProgram()
2041 {
2042   info_mode = INFO_MODE_PROGRAM;
2043
2044   DrawInfoScreen();
2045 }
2046
2047 static void execInfoVersion()
2048 {
2049   info_mode = INFO_MODE_VERSION;
2050
2051   DrawInfoScreen();
2052 }
2053
2054 static void execInfoLevelSet()
2055 {
2056   info_mode = INFO_MODE_LEVELSET;
2057
2058   DrawInfoScreen();
2059 }
2060
2061 static void execExitInfo()
2062 {
2063   SetGameStatus(GAME_MODE_MAIN);
2064
2065   DrawMainMenu();
2066 }
2067
2068 static struct TokenInfo info_info_main[] =
2069 {
2070   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
2071   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
2072   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
2073   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
2074   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
2075   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
2076   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
2077   { TYPE_EMPTY,         NULL,                   ""                      },
2078   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
2079
2080   { 0,                  NULL,                   NULL                    }
2081 };
2082
2083 static int getMenuTextFont(int type)
2084 {
2085   if (type & (TYPE_SWITCH       |
2086               TYPE_YES_NO       |
2087               TYPE_YES_NO_AUTO  |
2088               TYPE_STRING       |
2089               TYPE_ECS_AGA      |
2090               TYPE_KEYTEXT      |
2091               TYPE_ENTER_LIST))
2092     return FONT_MENU_2;
2093   else
2094     return FONT_MENU_1;
2095 }
2096
2097 static struct TokenInfo *setup_info;
2098 static struct TokenInfo setup_info_input[];
2099
2100 static struct TokenInfo *menu_info;
2101
2102 static void DrawCursorAndText_Menu_Ext(struct TokenInfo *token_info,
2103                                        int screen_pos, int menu_info_pos_raw,
2104                                        boolean active)
2105 {
2106   int pos = (menu_info_pos_raw < 0 ? screen_pos : menu_info_pos_raw);
2107   struct TokenInfo *ti = &token_info[pos];
2108   int xpos = MENU_SCREEN_START_XPOS;
2109   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
2110   int font_nr = getMenuTextFont(ti->type);
2111
2112   if (token_info == setup_info_input)
2113     font_nr = FONT_MENU_1;
2114
2115   if (active)
2116     font_nr = FONT_ACTIVE(font_nr);
2117
2118   DrawText(mSX + xpos * 32, mSY + ypos * 32, ti->text, font_nr);
2119
2120   if (ti->type & ~TYPE_SKIP_ENTRY)
2121     drawCursor(screen_pos, active);
2122 }
2123
2124 static void DrawCursorAndText_Menu(int screen_pos, int menu_info_pos_raw,
2125                                    boolean active)
2126 {
2127   DrawCursorAndText_Menu_Ext(menu_info, screen_pos, menu_info_pos_raw, active);
2128 }
2129
2130 static void DrawCursorAndText_Setup(int screen_pos, int menu_info_pos_raw,
2131                                     boolean active)
2132 {
2133   DrawCursorAndText_Menu_Ext(setup_info, screen_pos, menu_info_pos_raw, active);
2134 }
2135
2136 static char *window_size_text;
2137 static char *scaling_type_text;
2138
2139 static void drawSetupValue(int, int);
2140
2141 static void drawMenuInfoList(int first_entry, int num_page_entries,
2142                              int max_page_entries)
2143 {
2144   int i;
2145
2146   if (first_entry + num_page_entries > max_page_entries)
2147     first_entry = 0;
2148
2149   clearMenuListArea();
2150
2151   for (i = 0; i < num_page_entries; i++)
2152   {
2153     int menu_info_pos = first_entry + i;
2154     struct TokenInfo *si = &menu_info[menu_info_pos];
2155     void *value_ptr = si->value;
2156
2157     /* set some entries to "unchangeable" according to other variables */
2158     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2159         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2160         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2161         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
2162         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
2163         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
2164       si->type |= TYPE_GHOSTED;
2165
2166     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2167       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2168     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2169       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2170     else if (si->type & ~TYPE_SKIP_ENTRY)
2171       initCursor(i, IMG_MENU_BUTTON);
2172
2173     DrawCursorAndText_Menu(i, menu_info_pos, FALSE);
2174
2175     if (si->type & TYPE_VALUE &&
2176         menu_info == setup_info)
2177       drawSetupValue(i, menu_info_pos);
2178   }
2179 }
2180
2181 static void DrawInfoScreen_Main()
2182 {
2183   int fade_mask = REDRAW_FIELD;
2184   int i;
2185
2186   if (redraw_mask & REDRAW_ALL)
2187     fade_mask = REDRAW_ALL;
2188
2189   if (CheckIfGlobalBorderHasChanged())
2190     fade_mask = REDRAW_ALL;
2191
2192   UnmapAllGadgets();
2193   FadeMenuSoundsAndMusic();
2194
2195   FreeScreenGadgets();
2196   CreateScreenGadgets();
2197
2198   /* (needed after displaying title screens which disable auto repeat) */
2199   KeyboardAutoRepeatOn();
2200
2201   FadeSetLeaveScreen();
2202
2203   FadeOut(fade_mask);
2204
2205   /* needed if different viewport properties defined for info screen */
2206   ChangeViewportPropertiesIfNeeded();
2207
2208   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
2209
2210   ClearField();
2211
2212   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2213
2214   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2215
2216   info_info = info_info_main;
2217
2218   // determine maximal number of info entries that can be displayed on screen
2219   num_info_info = 0;
2220   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2221     num_info_info++;
2222
2223   // determine maximal number of info entries available for menu of info screen
2224   max_info_info = 0;
2225   for (i = 0; info_info[i].type != 0; i++)
2226     max_info_info++;
2227
2228   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2229
2230   MapScreenGadgets(max_info_info);
2231
2232   PlayMenuSoundsAndMusic();
2233
2234   DrawMaskedBorder(fade_mask);
2235
2236   FadeIn(fade_mask);
2237 }
2238
2239 static void changeSetupValue(int, int, int);
2240
2241 void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
2242                       int mode, int num_page_entries, int max_page_entries)
2243 {
2244   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2245   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2246   static int first_entry_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2247   int *num_page_entries_last = num_page_entries_all_last[game_status];
2248   int *choice_store = choice_stores[game_status];
2249   int *first_entry_store = first_entry_stores[game_status];
2250   int choice = choice_store[mode];              /* starts with 0 */
2251   int first_entry = first_entry_store[mode];    /* starts with 0 */
2252   int x = 0;
2253   int y = choice - first_entry;
2254   int y_old = y;
2255   boolean position_set_by_scrollbar = (dx == 999);
2256   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
2257   int i;
2258
2259   if (button == MB_MENU_INITIALIZE)
2260   {
2261     // check if number of menu page entries has changed (may happen by change
2262     // of custom artwork definition value for 'list_size' for this menu screen)
2263     // (in this case, the last menu position most probably has to be corrected)
2264     if (num_page_entries != num_page_entries_last[mode])
2265     {
2266       choice_store[mode] = first_entry_store[mode] = 0;
2267
2268       choice = first_entry = 0;
2269       y = y_old = 0;
2270
2271       num_page_entries_last[mode] = num_page_entries;
2272     }
2273
2274     /* advance to first valid menu entry */
2275     while (choice < num_page_entries &&
2276            menu_info[choice].type & TYPE_SKIP_ENTRY)
2277       choice++;
2278
2279     if (position_set_by_scrollbar)
2280       first_entry = first_entry_store[mode] = dy;
2281     else
2282       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2283                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2284
2285     drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2286
2287     if (choice < first_entry)
2288     {
2289       choice = first_entry;
2290
2291       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2292         choice++;
2293     }
2294     else if (choice > first_entry + num_page_entries - 1)
2295     {
2296       choice = first_entry + num_page_entries - 1;
2297
2298       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2299         choice--;
2300     }
2301
2302     choice_store[mode] = choice;
2303
2304     DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2305
2306     return;
2307   }
2308   else if (button == MB_MENU_LEAVE)
2309   {
2310     PlaySound(SND_MENU_ITEM_SELECTING);
2311
2312     for (i = 0; i < max_page_entries; i++)
2313     {
2314       if (menu_info[i].type & TYPE_LEAVE_MENU)
2315       {
2316         void (*menu_callback_function)(void) = menu_info[i].value;
2317
2318         FadeSetLeaveMenu();
2319
2320         menu_callback_function();
2321
2322         break;  /* absolutely needed because function changes 'menu_info'! */
2323       }
2324     }
2325
2326     return;
2327   }
2328
2329   if (mx || my)         /* mouse input */
2330   {
2331     x = (mx - mSX) / 32;
2332     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2333   }
2334   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
2335   {
2336     /* move cursor instead of scrolling when already at start/end of list */
2337     if (dy == -1 * SCROLL_LINE && first_entry == 0)
2338       dy = -1;
2339     else if (dy == +1 * SCROLL_LINE &&
2340              first_entry + num_page_entries == max_page_entries)
2341       dy = 1;
2342
2343     /* handle scrolling screen one line or page */
2344     if (y + dy < 0 ||
2345         y + dy > num_page_entries - 1)
2346     {
2347       boolean redraw = FALSE;
2348
2349       if (ABS(dy) == SCROLL_PAGE)
2350         step = num_page_entries - 1;
2351
2352       if (dy < 0 && first_entry > 0)
2353       {
2354         /* scroll page/line up */
2355
2356         first_entry -= step;
2357         if (first_entry < 0)
2358           first_entry = 0;
2359
2360         redraw = TRUE;
2361       }
2362       else if (dy > 0 && first_entry + num_page_entries < max_page_entries)
2363       {
2364         /* scroll page/line down */
2365
2366         first_entry += step;
2367         if (first_entry + num_page_entries > max_page_entries)
2368           first_entry = MAX(0, max_page_entries - num_page_entries);
2369
2370         redraw = TRUE;
2371       }
2372
2373       if (redraw)
2374       {
2375         choice += first_entry - first_entry_store[mode];
2376
2377         if (choice < first_entry)
2378         {
2379           choice = first_entry;
2380
2381           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2382             choice++;
2383         }
2384         else if (choice > first_entry + num_page_entries - 1)
2385         {
2386           choice = first_entry + num_page_entries - 1;
2387
2388           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2389             choice--;
2390         }
2391         else if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2392         {
2393           choice += SIGN(dy);
2394
2395           if (choice < first_entry ||
2396               choice > first_entry + num_page_entries - 1)
2397           first_entry += SIGN(dy);
2398         }
2399
2400         first_entry_store[mode] = first_entry;
2401         choice_store[mode] = choice;
2402
2403         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2404
2405         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2406
2407         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2408                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2409       }
2410
2411       return;
2412     }
2413
2414     if (dx)
2415     {
2416       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2417
2418       if (menu_info[choice].type & menu_navigation_type ||
2419           menu_info[choice].type & TYPE_BOOLEAN_STYLE ||
2420           menu_info[choice].type & TYPE_YES_NO_AUTO)
2421         button = MB_MENU_CHOICE;
2422     }
2423     else if (dy)
2424       y += dy;
2425
2426     /* jump to next non-empty menu entry (up or down) */
2427     while (first_entry + y > 0 &&
2428            first_entry + y < max_page_entries - 1 &&
2429            menu_info[first_entry + y].type & TYPE_SKIP_ENTRY)
2430       y += dy;
2431
2432     if (!IN_VIS_MENU(x, y))
2433     {
2434       choice += y - y_old;
2435
2436       if (choice < first_entry)
2437         first_entry = choice;
2438       else if (choice > first_entry + num_page_entries - 1)
2439         first_entry = choice - num_page_entries + 1;
2440
2441       if (first_entry >= 0 &&
2442           first_entry + num_page_entries <= max_page_entries)
2443       {
2444         first_entry_store[mode] = first_entry;
2445
2446         if (choice < first_entry)
2447           choice = first_entry;
2448         else if (choice > first_entry + num_page_entries - 1)
2449           choice = first_entry + num_page_entries - 1;
2450
2451         choice_store[mode] = choice;
2452
2453         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2454
2455         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2456
2457         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2458                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2459       }
2460
2461       return;
2462     }
2463   }
2464
2465   if (!anyScrollbarGadgetActive() &&
2466       IN_VIS_MENU(x, y) &&
2467       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
2468       y >= 0 && y < num_page_entries)
2469   {
2470     if (button)
2471     {
2472       if (first_entry + y != choice &&
2473           menu_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
2474       {
2475         PlaySound(SND_MENU_ITEM_ACTIVATING);
2476
2477         DrawCursorAndText_Menu(choice - first_entry, choice, FALSE);
2478         DrawCursorAndText_Menu(y, first_entry + y, TRUE);
2479
2480         choice = choice_store[mode] = first_entry + y;
2481       }
2482       else if (dx < 0)
2483       {
2484         PlaySound(SND_MENU_ITEM_SELECTING);
2485
2486         for (i = 0; menu_info[i].type != 0; i++)
2487         {
2488           if (menu_info[i].type & TYPE_LEAVE_MENU)
2489           {
2490             void (*menu_callback_function)(void) = menu_info[i].value;
2491
2492             FadeSetLeaveMenu();
2493
2494             menu_callback_function();
2495
2496             /* absolutely needed because function changes 'menu_info'! */
2497             break;
2498           }
2499         }
2500
2501         return;
2502       }
2503     }
2504     else if (!(menu_info[first_entry + y].type & TYPE_GHOSTED))
2505     {
2506       PlaySound(SND_MENU_ITEM_SELECTING);
2507
2508       /* when selecting key headline, execute function for key value change */
2509       if (menu_info[first_entry + y].type & TYPE_KEYTEXT &&
2510           menu_info[first_entry + y + 1].type & TYPE_KEY)
2511         y++;
2512
2513       /* when selecting string value, execute function for list selection */
2514       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2515           menu_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
2516         y--;
2517
2518       if (menu_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
2519       {
2520         void (*menu_callback_function)(void) =
2521           menu_info[first_entry + y].value;
2522
2523         FadeSetFromType(menu_info[first_entry + y].type);
2524
2525         menu_callback_function();
2526       }
2527       else if (menu_info[first_entry + y].type & TYPE_VALUE &&
2528                menu_info == setup_info)
2529       {
2530         changeSetupValue(y, first_entry + y, dx);
2531       }
2532     }
2533   }
2534 }
2535
2536 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2537 {
2538   menu_info = info_info;
2539
2540   HandleMenuScreen(mx, my, dx, dy, button,
2541                    info_mode, num_info_info, max_info_info);
2542 }
2543
2544 static int getMenuFontSpacing(int spacing_height, int font_nr)
2545 {
2546   int font_spacing = getFontHeight(font_nr) + EXTRA_SPACING(game_status);
2547
2548   return (spacing_height < 0 ? ABS(spacing_height) * font_spacing :
2549           spacing_height);
2550 }
2551
2552 static int getMenuTextSpacing(int spacing_height, int font_nr)
2553 {
2554   return (getMenuFontSpacing(spacing_height, font_nr) +
2555           EXTRA_SPACING(game_status));
2556 }
2557
2558 static int getMenuTextStep(int spacing_height, int font_nr)
2559 {
2560   return getFontHeight(font_nr) + getMenuTextSpacing(spacing_height, font_nr);
2561 }
2562
2563 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2564 {
2565   int font_title = MENU_INFO_FONT_TITLE;
2566   int font_error = FONT_TEXT_2;
2567   int font_foot  = MENU_INFO_FONT_FOOT;
2568   int spacing_title = menu.headline1_spacing_info[info_mode];
2569   int ystep_title = getMenuTextStep(spacing_title, font_title);
2570   int ystart1 = mSY - SY + 100;
2571   int ystart2 = ystart1 + ystep_title;
2572   int ybottom = mSY - SY + SYSIZE - 20;
2573
2574   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO);
2575
2576   FadeOut(REDRAW_FIELD);
2577
2578   ClearField();
2579   DrawHeadline();
2580
2581   DrawTextSCentered(ystart1, font_title, text_title);
2582   DrawTextSCentered(ystart2, font_error, text_error);
2583
2584   DrawTextSCentered(ybottom, font_foot,
2585                     "Press any key or button for info menu");
2586
2587   FadeIn(REDRAW_FIELD);
2588 }
2589
2590 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2591 {
2592   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2593   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2594   int font_title = MENU_INFO_FONT_TITLE;
2595   int font_foot  = MENU_INFO_FONT_FOOT;
2596   int xstart = mSX + MENU_SCREEN_INFO_XSTART;
2597   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2598   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2599   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2600   int ystep = MENU_SCREEN_INFO_YSTEP;
2601   int element, action, direction;
2602   int graphic;
2603   int delay;
2604   int sync_frame;
2605   int i, j;
2606
2607   if (init)
2608   {
2609     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2610       infoscreen_step[i] = infoscreen_frame[i] = 0;
2611
2612     ClearField();
2613     DrawHeadline();
2614
2615     DrawTextSCentered(ystart1, font_title, "The Game Elements:");
2616
2617     DrawTextSCentered(ybottom, font_foot,
2618                       "Press any key or button for next page");
2619
2620     FrameCounter = 0;
2621   }
2622
2623   i = j = 0;
2624   while (helpanim_info[j].element != HELPANIM_LIST_END)
2625   {
2626     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2627         i >= max_anims)
2628       break;
2629     else if (i < start)
2630     {
2631       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2632         j++;
2633
2634       j++;
2635       i++;
2636
2637       continue;
2638     }
2639
2640     j += infoscreen_step[i - start];
2641
2642     element = helpanim_info[j].element;
2643     action = helpanim_info[j].action;
2644     direction = helpanim_info[j].direction;
2645
2646     if (element < 0)
2647       element = EL_UNKNOWN;
2648
2649     if (action != -1 && direction != -1)
2650       graphic = el_act_dir2img(element, action, direction);
2651     else if (action != -1)
2652       graphic = el_act2img(element, action);
2653     else if (direction != -1)
2654       graphic = el_dir2img(element, direction);
2655     else
2656       graphic = el2img(element);
2657
2658     delay = helpanim_info[j++].delay;
2659
2660     if (delay == -1)
2661       delay = 1000000;
2662
2663     if (infoscreen_frame[i - start] == 0)
2664     {
2665       sync_frame = 0;
2666       infoscreen_frame[i - start] = delay - 1;
2667     }
2668     else
2669     {
2670       sync_frame = delay - infoscreen_frame[i - start];
2671       infoscreen_frame[i - start]--;
2672     }
2673
2674     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2675     {
2676       if (!infoscreen_frame[i - start])
2677         infoscreen_step[i - start] = 0;
2678     }
2679     else
2680     {
2681       if (!infoscreen_frame[i - start])
2682         infoscreen_step[i - start]++;
2683       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2684         j++;
2685     }
2686
2687     j++;
2688
2689     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2690                                TILEX, TILEY);
2691     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2692                                  graphic, sync_frame, USE_MASKING);
2693
2694     if (init)
2695       DrawInfoScreen_HelpText(element, action, direction, i - start);
2696
2697     i++;
2698   }
2699
2700   redraw_mask |= REDRAW_FIELD;
2701
2702   FrameCounter++;
2703 }
2704
2705 static char *getHelpText(int element, int action, int direction)
2706 {
2707   char token[MAX_LINE_LEN];
2708
2709   strcpy(token, element_info[element].token_name);
2710
2711   if (action != -1)
2712     strcat(token, element_action_info[action].suffix);
2713
2714   if (direction != -1)
2715     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2716
2717   return getHashEntry(helptext_info, token);
2718 }
2719
2720 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2721 {
2722   int font_nr = FONT_INFO_ELEMENTS;
2723   int font_width = getFontWidth(font_nr);
2724   int font_height = getFontHeight(font_nr);
2725   int yoffset = (TILEX - 2 * font_height) / 2;
2726   int xstart = mSX + MINI_TILEX + TILEX + MINI_TILEX;
2727   int ystart = mSY + MENU_SCREEN_INFO_YSTART2 + yoffset;
2728   int ystep = TILEY + 4;
2729   int pad_x = xstart - SX;
2730   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
2731   int max_lines_per_text = 2;    
2732   char *text = NULL;
2733
2734   if (action != -1 && direction != -1)          /* element.action.direction */
2735     text = getHelpText(element, action, direction);
2736
2737   if (text == NULL && action != -1)             /* element.action */
2738     text = getHelpText(element, action, -1);
2739
2740   if (text == NULL && direction != -1)          /* element.direction */
2741     text = getHelpText(element, -1, direction);
2742
2743   if (text == NULL)                             /* base element */
2744     text = getHelpText(element, -1, -1);
2745
2746   if (text == NULL)                             /* not found */
2747     text = "No description available";
2748
2749   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2750     ystart += getFontHeight(font_nr) / 2;
2751
2752   DrawTextBuffer(xstart, ystart + ypos * ystep, text, font_nr,
2753                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2754                  TRUE, FALSE, FALSE);
2755 }
2756
2757 void DrawInfoScreen_TitleScreen()
2758 {
2759   SetGameStatus(GAME_MODE_TITLE);
2760
2761   DrawTitleScreen();
2762 }
2763
2764 void HandleInfoScreen_TitleScreen(int button)
2765 {
2766   HandleTitleScreen(0, 0, 0, 0, button);
2767 }
2768
2769 void DrawInfoScreen_Elements()
2770 {
2771   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2772
2773   FadeOut(REDRAW_FIELD);
2774
2775   LoadHelpAnimInfo();
2776   LoadHelpTextInfo();
2777
2778   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2779
2780   FadeIn(REDRAW_FIELD);
2781 }
2782
2783 void HandleInfoScreen_Elements(int button)
2784 {
2785   static unsigned int info_delay = 0;
2786   static int num_anims;
2787   static int num_pages;
2788   static int page;
2789   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
2790   int i;
2791
2792   if (button == MB_MENU_INITIALIZE)
2793   {
2794     boolean new_element = TRUE;
2795
2796     num_anims = 0;
2797
2798     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2799     {
2800       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2801         new_element = TRUE;
2802       else if (new_element)
2803       {
2804         num_anims++;
2805         new_element = FALSE;
2806       }
2807     }
2808
2809     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
2810     page = 0;
2811   }
2812
2813   if (button == MB_MENU_LEAVE)
2814   {
2815     PlaySound(SND_MENU_ITEM_SELECTING);
2816
2817     info_mode = INFO_MODE_MAIN;
2818     DrawInfoScreen();
2819
2820     return;
2821   }
2822   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2823   {
2824     if (button != MB_MENU_INITIALIZE)
2825     {
2826       PlaySound(SND_MENU_ITEM_SELECTING);
2827
2828       page++;
2829     }
2830
2831     if (page >= num_pages)
2832     {
2833       FadeMenuSoundsAndMusic();
2834
2835       info_mode = INFO_MODE_MAIN;
2836       DrawInfoScreen();
2837
2838       return;
2839     }
2840
2841     if (page > 0)
2842       FadeSetNextScreen();
2843
2844     if (button != MB_MENU_INITIALIZE)
2845       FadeOut(REDRAW_FIELD);
2846
2847     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
2848
2849     if (button != MB_MENU_INITIALIZE)
2850       FadeIn(REDRAW_FIELD);
2851   }
2852   else
2853   {
2854     if (DelayReached(&info_delay, GameFrameDelay))
2855       if (page < num_pages)
2856         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
2857
2858     PlayMenuSoundIfLoop();
2859   }
2860 }
2861
2862 void DrawInfoScreen_Music()
2863 {
2864   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
2865
2866   FadeOut(REDRAW_FIELD);
2867
2868   ClearField();
2869   DrawHeadline();
2870
2871   LoadMusicInfo();
2872
2873   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
2874
2875   FadeIn(REDRAW_FIELD);
2876 }
2877
2878 void HandleInfoScreen_Music(int button)
2879 {
2880   static struct MusicFileInfo *list = NULL;
2881   int font_title = MENU_INFO_FONT_TITLE;
2882   int font_head  = MENU_INFO_FONT_HEAD;
2883   int font_text  = MENU_INFO_FONT_TEXT;
2884   int font_foot  = MENU_INFO_FONT_FOOT;
2885   int spacing_title = menu.headline1_spacing_info[info_mode];
2886   int spacing_head  = menu.headline2_spacing_info[info_mode];
2887   int ystep_title = getMenuTextStep(spacing_title, font_title);
2888   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
2889   int ystart = mSY - SY + 100;
2890   int ybottom = mSY - SY + SYSIZE - 20;
2891
2892   if (button == MB_MENU_INITIALIZE)
2893   {
2894     list = music_file_info;
2895
2896     if (list == NULL)
2897     {
2898       FadeMenuSoundsAndMusic();
2899
2900       ClearField();
2901       DrawHeadline();
2902
2903       DrawTextSCentered(ystart, font_title,
2904                         "No music info for this level set.");
2905
2906       DrawTextSCentered(ybottom, font_foot,
2907                         "Press any key or button for info menu");
2908
2909       return;
2910     }
2911   }
2912
2913   if (button == MB_MENU_LEAVE)
2914   {
2915     PlaySound(SND_MENU_ITEM_SELECTING);
2916
2917     FadeMenuSoundsAndMusic();
2918
2919     info_mode = INFO_MODE_MAIN;
2920     DrawInfoScreen();
2921
2922     return;
2923   }
2924   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2925   {
2926     if (button != MB_MENU_INITIALIZE)
2927     {
2928       PlaySound(SND_MENU_ITEM_SELECTING);
2929
2930       if (list != NULL)
2931         list = list->next;
2932     }
2933
2934     if (list == NULL)
2935     {
2936       FadeMenuSoundsAndMusic();
2937
2938       info_mode = INFO_MODE_MAIN;
2939       DrawInfoScreen();
2940
2941       return;
2942     }
2943
2944     FadeMenuSoundsAndMusic();
2945
2946     if (list != music_file_info)
2947       FadeSetNextScreen();
2948
2949     if (button != MB_MENU_INITIALIZE)
2950       FadeOut(REDRAW_FIELD);
2951
2952     ClearField();
2953     DrawHeadline();
2954
2955     if (list->is_sound)
2956     {
2957       int sound = list->music;
2958
2959       if (sound_info[sound].loop)
2960         PlaySoundLoop(sound);
2961       else
2962         PlaySound(sound);
2963
2964       DrawTextSCentered(ystart, font_title, "The Game Background Sounds:");
2965     }
2966     else
2967     {
2968       PlayMusic(list->music);
2969
2970       DrawTextSCentered(ystart, font_title, "The Game Background Music:");
2971     }
2972
2973     ystart += ystep_title;
2974
2975     if (!strEqual(list->title, UNKNOWN_NAME))
2976     {
2977       if (!strEqual(list->title_header, UNKNOWN_NAME))
2978       {
2979         DrawTextSCentered(ystart, font_head, list->title_header);
2980         ystart += ystep_head;
2981       }
2982
2983       DrawTextFCentered(ystart, font_text, "\"%s\"", list->title);
2984       ystart += ystep_head;
2985     }
2986
2987     if (!strEqual(list->artist, UNKNOWN_NAME))
2988     {
2989       if (!strEqual(list->artist_header, UNKNOWN_NAME))
2990         DrawTextSCentered(ystart, font_head, list->artist_header);
2991       else
2992         DrawTextSCentered(ystart, font_head, "by");
2993
2994       ystart += ystep_head;
2995
2996       DrawTextFCentered(ystart, font_text, "%s", list->artist);
2997       ystart += ystep_head;
2998     }
2999
3000     if (!strEqual(list->album, UNKNOWN_NAME))
3001     {
3002       if (!strEqual(list->album_header, UNKNOWN_NAME))
3003         DrawTextSCentered(ystart, font_head, list->album_header);
3004       else
3005         DrawTextSCentered(ystart, font_head, "from the album");
3006
3007       ystart += ystep_head;
3008
3009       DrawTextFCentered(ystart, font_text, "\"%s\"", list->album);
3010       ystart += ystep_head;
3011     }
3012
3013     if (!strEqual(list->year, UNKNOWN_NAME))
3014     {
3015       if (!strEqual(list->year_header, UNKNOWN_NAME))
3016         DrawTextSCentered(ystart, font_head, list->year_header);
3017       else
3018         DrawTextSCentered(ystart, font_head, "from the year");
3019
3020       ystart += ystep_head;
3021
3022       DrawTextFCentered(ystart, font_text, "%s", list->year);
3023       ystart += ystep_head;
3024     }
3025
3026     DrawTextSCentered(ybottom, FONT_TEXT_4,
3027                       "Press any key or button for next page");
3028
3029     if (button != MB_MENU_INITIALIZE)
3030       FadeIn(REDRAW_FIELD);
3031   }
3032
3033   if (list != NULL && list->is_sound && sound_info[list->music].loop)
3034     PlaySoundLoop(list->music);
3035 }
3036
3037 static void DrawInfoScreen_CreditsScreen(int screen_nr)
3038 {
3039   int font_title = MENU_INFO_FONT_TITLE;
3040   int font_head  = MENU_INFO_FONT_HEAD;
3041   int font_text  = MENU_INFO_FONT_TEXT;
3042   int font_foot  = MENU_INFO_FONT_FOOT;
3043   int spacing_title = menu.headline1_spacing_info[info_mode];
3044   int spacing_head  = menu.headline2_spacing_info[info_mode];
3045   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3046   int spacing_line  = menu.line_spacing_info[info_mode];
3047   int ystep_title = getMenuTextStep(spacing_title, font_title);
3048   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3049   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3050   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3051   int ystart = mSY - SY + 100;
3052   int ybottom = mSY - SY + SYSIZE - 20;
3053
3054   ClearField();
3055   DrawHeadline();
3056
3057   DrawTextSCentered(ystart, font_title, "Credits:");
3058   ystart += ystep_title;
3059
3060   if (screen_nr == 0)
3061   {
3062     DrawTextSCentered(ystart, font_head,
3063                       "Special thanks to");
3064     ystart += ystep_head;
3065     DrawTextSCentered(ystart, font_text,
3066                       "Peter Liepa");
3067     ystart += ystep_head;
3068     DrawTextSCentered(ystart, font_head,
3069                       "for creating");
3070     ystart += ystep_head;
3071     DrawTextSCentered(ystart, font_text,
3072                       "\"Boulder Dash\"");
3073     ystart += ystep_head;
3074     DrawTextSCentered(ystart, font_head,
3075                       "in the year");
3076     ystart += ystep_head;
3077     DrawTextSCentered(ystart, font_text,
3078                       "1984");
3079     ystart += ystep_head;
3080     DrawTextSCentered(ystart, font_head,
3081                       "published by");
3082     ystart += ystep_head;
3083     DrawTextSCentered(ystart, font_text,
3084                       "First Star Software");
3085   }
3086   else if (screen_nr == 1)
3087   {
3088     DrawTextSCentered(ystart, font_head,
3089                       "Special thanks to");
3090     ystart += ystep_head;
3091     DrawTextSCentered(ystart, font_text,
3092                       "Klaus Heinz & Volker Wertich");
3093     ystart += ystep_head;
3094     DrawTextSCentered(ystart, font_head,
3095                       "for creating");
3096     ystart += ystep_head;
3097     DrawTextSCentered(ystart, font_text,
3098                       "\"Emerald Mine\"");
3099     ystart += ystep_head;
3100     DrawTextSCentered(ystart, font_head,
3101                       "in the year");
3102     ystart += ystep_head;
3103     DrawTextSCentered(ystart, font_text,
3104                       "1987");
3105     ystart += ystep_head;
3106     DrawTextSCentered(ystart, font_head,
3107                       "published by");
3108     ystart += ystep_head;
3109     DrawTextSCentered(ystart, font_text,
3110                       "Kingsoft");
3111   }
3112   else if (screen_nr == 2)
3113   {
3114     DrawTextSCentered(ystart, font_head,
3115                       "Special thanks to");
3116     ystart += ystep_head;
3117     DrawTextSCentered(ystart, font_text,
3118                       "Michael Stopp & Philip Jespersen");
3119     ystart += ystep_head;
3120     DrawTextSCentered(ystart, font_head,
3121                       "for creating");
3122     ystart += ystep_head;
3123     DrawTextSCentered(ystart, font_text,
3124                       "\"Supaplex\"");
3125     ystart += ystep_head;
3126     DrawTextSCentered(ystart, font_head,
3127                       "in the year");
3128     ystart += ystep_head;
3129     DrawTextSCentered(ystart, font_text,
3130                       "1991");
3131     ystart += ystep_head;
3132     DrawTextSCentered(ystart, font_head,
3133                       "published by");
3134     ystart += ystep_head;
3135     DrawTextSCentered(ystart, font_text,
3136                       "Digital Integration");
3137   }
3138   else if (screen_nr == 3)
3139   {
3140     DrawTextSCentered(ystart, font_head,
3141                       "Special thanks to");
3142     ystart += ystep_head;
3143     DrawTextSCentered(ystart, font_text,
3144                       "Hiroyuki Imabayashi");
3145     ystart += ystep_head;
3146     DrawTextSCentered(ystart, font_head,
3147                       "for creating");
3148     ystart += ystep_head;
3149     DrawTextSCentered(ystart, font_text,
3150                       "\"Sokoban\"");
3151     ystart += ystep_head;
3152     DrawTextSCentered(ystart, font_head,
3153                       "in the year");
3154     ystart += ystep_head;
3155     DrawTextSCentered(ystart, font_text,
3156                       "1982");
3157     ystart += ystep_head;
3158     DrawTextSCentered(ystart, font_head,
3159                       "published by");
3160     ystart += ystep_head;
3161     DrawTextSCentered(ystart, font_text,
3162                       "Thinking Rabbit");
3163   }
3164   else if (screen_nr == 4)
3165   {
3166     DrawTextSCentered(ystart, font_head,
3167                       "Special thanks to");
3168     ystart += ystep_head;
3169     DrawTextSCentered(ystart, font_text,
3170                       "Alan Bond");
3171     ystart += ystep_head;
3172     DrawTextSCentered(ystart, font_head,
3173                       "and");
3174     ystart += ystep_head;
3175     DrawTextSCentered(ystart, font_text,
3176                       "J\xfcrgen Bonhagen");
3177     ystart += ystep_head;
3178     DrawTextSCentered(ystart, font_head,
3179                       "for the continuous creation");
3180     ystart += ystep_line;
3181     DrawTextSCentered(ystart, font_head,
3182                       "of outstanding level sets");
3183   }
3184   else if (screen_nr == 5)
3185   {
3186     DrawTextSCentered(ystart, font_head,
3187                       "Thanks to");
3188     ystart += ystep_head;
3189     DrawTextSCentered(ystart, font_text,
3190                       "Peter Elzner");
3191     ystart += ystep_head;
3192     DrawTextSCentered(ystart, font_head,
3193                       "for ideas and inspiration by");
3194     ystart += ystep_head;
3195     DrawTextSCentered(ystart, font_text,
3196                       "Diamond Caves");
3197     ystart += ystep_para;
3198
3199     DrawTextSCentered(ystart, font_head,
3200                       "Thanks to");
3201     ystart += ystep_head;
3202     DrawTextSCentered(ystart, font_text,
3203                       "Steffest");
3204     ystart += ystep_head;
3205     DrawTextSCentered(ystart, font_head,
3206                       "for ideas and inspiration by");
3207     ystart += ystep_head;
3208     DrawTextSCentered(ystart, font_text,
3209                       "DX-Boulderdash");
3210   }
3211   else if (screen_nr == 6)
3212   {
3213     DrawTextSCentered(ystart, font_head,
3214                       "Thanks to");
3215     ystart += ystep_head;
3216     DrawTextSCentered(ystart, font_text,
3217                       "David Tritscher");
3218     ystart += ystep_head;
3219     DrawTextSCentered(ystart, font_head,
3220                       "for the code base used for the");
3221     ystart += ystep_line;
3222     DrawTextSCentered(ystart, font_head,
3223                       "native Emerald Mine engine");
3224   }
3225   else if (screen_nr == 7)
3226   {
3227     DrawTextSCentered(ystart, font_head,
3228                       "Thanks to");
3229     ystart += ystep_head;
3230     DrawTextSCentered(ystart, font_text,
3231                       "Guido Schulz");
3232     ystart += ystep_head;
3233     DrawTextSCentered(ystart, font_head,
3234                       "for the initial DOS port");
3235     ystart += ystep_para;
3236
3237     DrawTextSCentered(ystart, font_head,
3238                       "Thanks to");
3239     ystart += ystep_head;
3240     DrawTextSCentered(ystart, font_text,
3241                       "Karl H\xf6rnell");
3242     ystart += ystep_head;
3243     DrawTextSCentered(ystart, font_head,
3244                       "for some additional toons");
3245   }
3246   else if (screen_nr == 8)
3247   {
3248     DrawTextSCentered(ystart, font_head,
3249                       "And not to forget:");
3250     ystart += ystep_head;
3251     DrawTextSCentered(ystart, font_head,
3252                       "Many thanks to");
3253     ystart += ystep_head;
3254     DrawTextSCentered(ystart, font_text,
3255                       "All those who contributed");
3256     ystart += ystep_line;
3257     DrawTextSCentered(ystart, font_text,
3258                       "levels to this game");
3259     ystart += ystep_line;
3260     DrawTextSCentered(ystart, font_text,
3261                       "since 1995");
3262   }
3263
3264   DrawTextSCentered(ybottom, font_foot,
3265                     "Press any key or button for next page");
3266 }
3267
3268 void DrawInfoScreen_Credits()
3269 {
3270   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3271
3272   FadeMenuSoundsAndMusic();
3273
3274   FadeOut(REDRAW_FIELD);
3275
3276   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3277
3278   FadeIn(REDRAW_FIELD);
3279 }
3280
3281 void HandleInfoScreen_Credits(int button)
3282 {
3283   static int screen_nr = 0;
3284   int num_screens = 9;
3285
3286   if (button == MB_MENU_INITIALIZE)
3287   {
3288     screen_nr = 0;
3289
3290     // DrawInfoScreen_CreditsScreen(screen_nr);
3291   }
3292
3293   if (button == MB_MENU_LEAVE)
3294   {
3295     PlaySound(SND_MENU_ITEM_SELECTING);
3296
3297     info_mode = INFO_MODE_MAIN;
3298     DrawInfoScreen();
3299
3300     return;
3301   }
3302   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3303   {
3304     if (button != MB_MENU_INITIALIZE)
3305     {
3306       PlaySound(SND_MENU_ITEM_SELECTING);
3307
3308       screen_nr++;
3309     }
3310
3311     if (screen_nr >= num_screens)
3312     {
3313       FadeMenuSoundsAndMusic();
3314
3315       info_mode = INFO_MODE_MAIN;
3316       DrawInfoScreen();
3317
3318       return;
3319     }
3320
3321     if (screen_nr > 0)
3322       FadeSetNextScreen();
3323
3324     if (button != MB_MENU_INITIALIZE)
3325       FadeOut(REDRAW_FIELD);
3326
3327     DrawInfoScreen_CreditsScreen(screen_nr);
3328
3329     if (button != MB_MENU_INITIALIZE)
3330       FadeIn(REDRAW_FIELD);
3331   }
3332   else
3333   {
3334     PlayMenuSoundIfLoop();
3335   }
3336 }
3337
3338 void DrawInfoScreen_Program()
3339 {
3340   int font_title = MENU_INFO_FONT_TITLE;
3341   int font_head  = MENU_INFO_FONT_HEAD;
3342   int font_text  = MENU_INFO_FONT_TEXT;
3343   int font_foot  = MENU_INFO_FONT_FOOT;
3344   int spacing_title = menu.headline1_spacing_info[info_mode];
3345   int spacing_head  = menu.headline2_spacing_info[info_mode];
3346   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3347   int spacing_line  = menu.line_spacing_info[info_mode];
3348   int ystep_title = getMenuTextStep(spacing_title, font_title);
3349   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3350   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3351   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3352   int ystart = mSY - SY + 100;
3353   int ybottom = mSY - SY + SYSIZE - 20;
3354
3355   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3356
3357   FadeOut(REDRAW_FIELD);
3358
3359   ClearField();
3360   DrawHeadline();
3361
3362   DrawTextSCentered(ystart, font_title, "Program Information:");
3363   ystart += ystep_title;
3364
3365   DrawTextSCentered(ystart, font_head,
3366                     "This game is Freeware!");
3367   ystart += ystep_head;
3368   DrawTextSCentered(ystart, font_head,
3369                     "If you like it, send e-mail to:");
3370   ystart += ystep_head;
3371   DrawTextSCentered(ystart, font_text,
3372                     setup.internal.program_email);
3373   ystart += ystep_para;
3374
3375   DrawTextSCentered(ystart, font_head,
3376                     "More information and levels:");
3377   ystart += ystep_head;
3378   DrawTextSCentered(ystart, font_text,
3379                     setup.internal.program_website);
3380   ystart += ystep_para;
3381
3382   DrawTextSCentered(ystart, font_head,
3383                     "If you have created new levels,");
3384   ystart += ystep_line;
3385   DrawTextSCentered(ystart, font_head,
3386                     "send them to me to include them!");
3387   ystart += ystep_head;
3388   DrawTextSCentered(ystart, font_head,
3389                     ":-)");
3390
3391   DrawTextSCentered(ybottom, font_foot,
3392                     "Press any key or button for info menu");
3393
3394   FadeIn(REDRAW_FIELD);
3395 }
3396
3397 void HandleInfoScreen_Program(int button)
3398 {
3399   if (button == MB_MENU_LEAVE)
3400   {
3401     PlaySound(SND_MENU_ITEM_SELECTING);
3402
3403     info_mode = INFO_MODE_MAIN;
3404     DrawInfoScreen();
3405
3406     return;
3407   }
3408   else if (button == MB_MENU_CHOICE)
3409   {
3410     PlaySound(SND_MENU_ITEM_SELECTING);
3411
3412     FadeMenuSoundsAndMusic();
3413
3414     info_mode = INFO_MODE_MAIN;
3415     DrawInfoScreen();
3416   }
3417   else
3418   {
3419     PlayMenuSoundIfLoop();
3420   }
3421 }
3422
3423 void DrawInfoScreen_Version()
3424 {
3425   int font_title = MENU_INFO_FONT_TITLE;
3426   int font_head  = MENU_INFO_FONT_HEAD;
3427   int font_text  = MENU_INFO_FONT_TEXT;
3428   int font_foot  = MENU_INFO_FONT_FOOT;
3429   int spacing_title = menu.headline1_spacing_info[info_mode];
3430   int spacing_head  = menu.headline2_spacing_info[info_mode];
3431   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3432   int spacing_line  = menu.line_spacing_info[info_mode];
3433   int xstep = getFontWidth(font_text);
3434   int ystep_title = getMenuTextStep(spacing_title, font_title);
3435   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3436   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3437   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3438   int ystart  = mSY - SY + 100;
3439   int ybottom = mSY - SY + SYSIZE - 20;
3440   int xstart1 = mSX - SX + 2 * xstep;
3441   int xstart2 = mSX - SX + 18 * xstep;
3442   int xstart3 = mSX - SX + 28 * xstep;
3443   SDL_version sdl_version_compiled;
3444   const SDL_version *sdl_version_linked;
3445   int driver_name_len = 10;
3446 #if defined(TARGET_SDL2)
3447   SDL_version sdl_version_linked_ext;
3448   const char *driver_name = NULL;
3449 #else
3450   char driver_name[driver_name_len];
3451 #endif
3452
3453   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3454
3455   FadeOut(REDRAW_FIELD);
3456
3457   ClearField();
3458   DrawHeadline();
3459
3460   DrawTextSCentered(ystart, font_title, "Version Information:");
3461   ystart += ystep_title;
3462
3463   DrawTextF(xstart1, ystart, font_head, "Name");
3464   DrawTextF(xstart2, ystart, font_text, getProgramTitleString());
3465   ystart += ystep_line;
3466
3467   if (!strEqual(getProgramVersionString(), getProgramRealVersionString()))
3468   {
3469     DrawTextF(xstart1, ystart, font_head, "Version (fake)");
3470     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3471     ystart += ystep_line;
3472
3473     DrawTextF(xstart1, ystart, font_head, "Version (real)");
3474     DrawTextF(xstart2, ystart, font_text, getProgramRealVersionString());
3475     ystart += ystep_line;
3476   }
3477   else
3478   {
3479     DrawTextF(xstart1, ystart, font_head, "Version");
3480     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3481     ystart += ystep_line;
3482   }
3483
3484   DrawTextF(xstart1, ystart, font_head, "Platform");
3485   DrawTextF(xstart2, ystart, font_text, PLATFORM_STRING);
3486   ystart += ystep_line;
3487
3488   DrawTextF(xstart1, ystart, font_head, "Target");
3489   DrawTextF(xstart2, ystart, font_text, TARGET_STRING);
3490   ystart += ystep_line;
3491
3492   DrawTextF(xstart1, ystart, font_head, "Source date");
3493   DrawTextF(xstart2, ystart, font_text, getSourceDateString());
3494   ystart += ystep_para;
3495
3496   DrawTextF(xstart1, ystart, font_head, "Library");
3497   DrawTextF(xstart2, ystart, font_head, "compiled");
3498   DrawTextF(xstart3, ystart, font_head, "linked");
3499   ystart += ystep_head;
3500
3501   SDL_VERSION(&sdl_version_compiled);
3502 #if defined(TARGET_SDL2)
3503   SDL_GetVersion(&sdl_version_linked_ext);
3504   sdl_version_linked = &sdl_version_linked_ext;
3505 #else
3506   sdl_version_linked = SDL_Linked_Version();
3507 #endif
3508
3509   DrawTextF(xstart1, ystart, font_text, "SDL");
3510   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3511             sdl_version_compiled.major,
3512             sdl_version_compiled.minor,
3513             sdl_version_compiled.patch);
3514   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3515             sdl_version_linked->major,
3516             sdl_version_linked->minor,
3517             sdl_version_linked->patch);
3518   ystart += ystep_line;
3519
3520   SDL_IMAGE_VERSION(&sdl_version_compiled);
3521   sdl_version_linked = IMG_Linked_Version();
3522
3523   DrawTextF(xstart1, ystart, font_text, "SDL_image");
3524   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3525             sdl_version_compiled.major,
3526             sdl_version_compiled.minor,
3527             sdl_version_compiled.patch);
3528   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3529             sdl_version_linked->major,
3530             sdl_version_linked->minor,
3531             sdl_version_linked->patch);
3532   ystart += ystep_line;
3533
3534   SDL_MIXER_VERSION(&sdl_version_compiled);
3535   sdl_version_linked = Mix_Linked_Version();
3536
3537   DrawTextF(xstart1, ystart, font_text, "SDL_mixer");
3538   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3539             sdl_version_compiled.major,
3540             sdl_version_compiled.minor,
3541             sdl_version_compiled.patch);
3542   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3543             sdl_version_linked->major,
3544             sdl_version_linked->minor,
3545             sdl_version_linked->patch);
3546   ystart += ystep_line;
3547
3548   SDL_NET_VERSION(&sdl_version_compiled);
3549   sdl_version_linked = SDLNet_Linked_Version();
3550
3551   DrawTextF(xstart1, ystart, font_text, "SDL_net");
3552   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3553             sdl_version_compiled.major,
3554             sdl_version_compiled.minor,
3555             sdl_version_compiled.patch);
3556   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3557             sdl_version_linked->major,
3558             sdl_version_linked->minor,
3559             sdl_version_linked->patch);
3560   ystart += ystep_para;
3561
3562   DrawTextF(xstart1, ystart, font_head, "Driver");
3563   DrawTextF(xstart2, ystart, font_head, "Requested");
3564   DrawTextF(xstart3, ystart, font_head, "Used");
3565   ystart += ystep_head;
3566
3567 #if defined(TARGET_SDL2)
3568   driver_name = getStringCopyNStatic(SDL_GetVideoDriver(0), driver_name_len);
3569 #else
3570   SDL_VideoDriverName(driver_name, driver_name_len);
3571 #endif
3572
3573   DrawTextF(xstart1, ystart, font_text, "SDL_VideoDriver");
3574   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_videodriver);
3575   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3576   ystart += ystep_line;
3577
3578 #if defined(TARGET_SDL2)
3579   driver_name = getStringCopyNStatic(SDL_GetAudioDriver(0), driver_name_len);
3580 #else
3581   SDL_AudioDriverName(driver_name, driver_name_len);
3582 #endif
3583
3584   DrawTextF(xstart1, ystart, font_text, "SDL_AudioDriver");
3585   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_audiodriver);
3586   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3587
3588   DrawTextSCentered(ybottom, font_foot,
3589                     "Press any key or button for info menu");
3590
3591   FadeIn(REDRAW_FIELD);
3592 }
3593
3594 void HandleInfoScreen_Version(int button)
3595 {
3596   if (button == MB_MENU_LEAVE)
3597   {
3598     PlaySound(SND_MENU_ITEM_SELECTING);
3599
3600     info_mode = INFO_MODE_MAIN;
3601     DrawInfoScreen();
3602
3603     return;
3604   }
3605   else if (button == MB_MENU_CHOICE)
3606   {
3607     PlaySound(SND_MENU_ITEM_SELECTING);
3608
3609     FadeMenuSoundsAndMusic();
3610
3611     info_mode = INFO_MODE_MAIN;
3612     DrawInfoScreen();
3613   }
3614   else
3615   {
3616     PlayMenuSoundIfLoop();
3617   }
3618 }
3619
3620 void DrawInfoScreen_LevelSet()
3621 {
3622   struct TitleMessageInfo *tmi = &readme;
3623   char *filename = getLevelSetInfoFilename();
3624   char *title = "Level Set Information:";
3625   int ystart = mSY - SY + 100;
3626   int ybottom = mSY - SY + SYSIZE - 20;
3627
3628   if (filename == NULL)
3629   {
3630     DrawInfoScreen_NotAvailable(title, "No information for this level set.");
3631
3632     return;
3633   }
3634
3635   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3636
3637   FadeOut(REDRAW_FIELD);
3638
3639   ClearField();
3640   DrawHeadline();
3641
3642   DrawTextSCentered(ystart, FONT_TEXT_1, title);
3643
3644   /* if x position set to "-1", automatically determine by playfield width */
3645   if (tmi->x == -1)
3646     tmi->x = SXSIZE / 2;
3647
3648   /* if y position set to "-1", use static default value */
3649   if (tmi->y == -1)
3650     tmi->y = 150;
3651
3652   /* if width set to "-1", automatically determine by playfield width */
3653   if (tmi->width == -1)
3654     tmi->width = SXSIZE - 2 * TILEX;
3655
3656   /* if height set to "-1", automatically determine by playfield height */
3657   if (tmi->height == -1)
3658     tmi->height = SYSIZE - 20 - tmi->y - 10;
3659
3660   /* if chars set to "-1", automatically determine by text and font width */
3661   if (tmi->chars == -1)
3662     tmi->chars = tmi->width / getFontWidth(tmi->font);
3663   else
3664     tmi->width = tmi->chars * getFontWidth(tmi->font);
3665
3666   /* if lines set to "-1", automatically determine by text and font height */
3667   if (tmi->lines == -1)
3668     tmi->lines = tmi->height / getFontHeight(tmi->font);
3669   else
3670     tmi->height = tmi->lines * getFontHeight(tmi->font);
3671
3672   DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3673                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3674                tmi->autowrap, tmi->centered, tmi->parse_comments);
3675
3676   DrawTextSCentered(ybottom, FONT_TEXT_4,
3677                     "Press any key or button for info menu");
3678
3679   FadeIn(REDRAW_FIELD);
3680 }
3681
3682 void HandleInfoScreen_LevelSet(int button)
3683 {
3684   if (button == MB_MENU_LEAVE)
3685   {
3686     PlaySound(SND_MENU_ITEM_SELECTING);
3687
3688     info_mode = INFO_MODE_MAIN;
3689     DrawInfoScreen();
3690
3691     return;
3692   }
3693   else if (button == MB_MENU_CHOICE)
3694   {
3695     PlaySound(SND_MENU_ITEM_SELECTING);
3696
3697     FadeMenuSoundsAndMusic();
3698
3699     info_mode = INFO_MODE_MAIN;
3700     DrawInfoScreen();
3701   }
3702   else
3703   {
3704     PlayMenuSoundIfLoop();
3705   }
3706 }
3707
3708 static void DrawInfoScreen()
3709 {
3710   if (info_mode == INFO_MODE_TITLE)
3711     DrawInfoScreen_TitleScreen();
3712   else if (info_mode == INFO_MODE_ELEMENTS)
3713     DrawInfoScreen_Elements();
3714   else if (info_mode == INFO_MODE_MUSIC)
3715     DrawInfoScreen_Music();
3716   else if (info_mode == INFO_MODE_CREDITS)
3717     DrawInfoScreen_Credits();
3718   else if (info_mode == INFO_MODE_PROGRAM)
3719     DrawInfoScreen_Program();
3720   else if (info_mode == INFO_MODE_VERSION)
3721     DrawInfoScreen_Version();
3722   else if (info_mode == INFO_MODE_LEVELSET)
3723     DrawInfoScreen_LevelSet();
3724   else
3725     DrawInfoScreen_Main();
3726
3727   if (info_mode != INFO_MODE_MAIN &&
3728       info_mode != INFO_MODE_TITLE &&
3729       info_mode != INFO_MODE_MUSIC)
3730     PlayMenuSoundsAndMusic();
3731 }
3732
3733 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3734 {
3735   if (info_mode == INFO_MODE_TITLE)
3736     HandleInfoScreen_TitleScreen(button);
3737   else if (info_mode == INFO_MODE_ELEMENTS)
3738     HandleInfoScreen_Elements(button);
3739   else if (info_mode == INFO_MODE_MUSIC)
3740     HandleInfoScreen_Music(button);
3741   else if (info_mode == INFO_MODE_CREDITS)
3742     HandleInfoScreen_Credits(button);
3743   else if (info_mode == INFO_MODE_PROGRAM)
3744     HandleInfoScreen_Program(button);
3745   else if (info_mode == INFO_MODE_VERSION)
3746     HandleInfoScreen_Version(button);
3747   else if (info_mode == INFO_MODE_LEVELSET)
3748     HandleInfoScreen_LevelSet(button);
3749   else
3750     HandleInfoScreen_Main(mx, my, dx, dy, button);
3751 }
3752
3753
3754 /* ========================================================================= */
3755 /* type name functions                                                       */
3756 /* ========================================================================= */
3757
3758 void HandleTypeName(int newxpos, Key key)
3759 {
3760   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3761   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3762   struct TextPosInfo *pos = mci->pos_input;
3763   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3764   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3765   static int xpos = 0;
3766   int font_nr = pos->font;
3767   int font_active_nr = FONT_ACTIVE(font_nr);
3768   int font_width = getFontWidth(font_active_nr);
3769   char key_char = getValidConfigValueChar(getCharFromKey(key));
3770   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3771   boolean is_active = TRUE;
3772
3773   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3774
3775   if (newxpos)
3776   {
3777     strcpy(last_player_name, setup.player_name);
3778
3779     xpos = newxpos;
3780
3781     StartTextInput(startx, starty, pos->width, pos->height);
3782   }
3783   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3784   {
3785     setup.player_name[xpos] = key_char;
3786     setup.player_name[xpos + 1] = 0;
3787
3788     xpos++;
3789   }
3790   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3791   {
3792     xpos--;
3793
3794     setup.player_name[xpos] = 0;
3795   }
3796   else if (key == KSYM_Return && xpos > 0)
3797   {
3798     SaveSetup();
3799
3800     is_active = FALSE;
3801
3802     SetGameStatus(GAME_MODE_MAIN);
3803   }
3804   else if (key == KSYM_Escape)
3805   {
3806     strcpy(setup.player_name, last_player_name);
3807
3808     is_active = FALSE;
3809
3810     SetGameStatus(GAME_MODE_MAIN);
3811   }
3812
3813   if (is_active)
3814   {
3815     pos->width = (strlen(setup.player_name) + 1) * font_width;
3816     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3817
3818     DrawText(startx, starty, setup.player_name, font_active_nr);
3819     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3820   }
3821   else
3822   {
3823     pos->width = strlen(setup.player_name) * font_width;
3824     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3825
3826     DrawText(startx, starty, setup.player_name, font_nr);
3827
3828     StopTextInput();
3829   }
3830 }
3831
3832
3833 /* ========================================================================= */
3834 /* tree menu functions                                                       */
3835 /* ========================================================================= */
3836
3837 static void DrawChooseTree(TreeInfo **ti_ptr)
3838 {
3839   int fade_mask = REDRAW_FIELD;
3840
3841   if (CheckIfGlobalBorderHasChanged())
3842     fade_mask = REDRAW_ALL;
3843
3844   if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
3845   {
3846     SetGameStatus(GAME_MODE_MAIN);
3847
3848     DrawMainMenu();
3849
3850     return;
3851   }
3852
3853   UnmapAllGadgets();
3854
3855   FreeScreenGadgets();
3856   CreateScreenGadgets();
3857
3858   FadeOut(fade_mask);
3859
3860   /* needed if different viewport properties defined for choosing level (set) */
3861   ChangeViewportPropertiesIfNeeded();
3862
3863   if (game_status == GAME_MODE_LEVELNR)
3864     SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
3865   else if (game_status == GAME_MODE_LEVELS)
3866     SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
3867
3868   ClearField();
3869
3870   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
3871
3872   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3873   MapScreenTreeGadgets(*ti_ptr);
3874
3875   DrawMaskedBorder(fade_mask);
3876
3877   FadeIn(fade_mask);
3878 }
3879
3880 static void drawChooseTreeList(int first_entry, int num_page_entries,
3881                                TreeInfo *ti)
3882 {
3883   int i;
3884   char *title_string = NULL;
3885   int yoffset_sets = MENU_TITLE1_YPOS;
3886   int yoffset_setup = 16;
3887   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
3888                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
3889
3890   title_string = ti->infotext;
3891
3892   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3893
3894   clearMenuListArea();
3895
3896   for (i = 0; i < num_page_entries; i++)
3897   {
3898     TreeInfo *node, *node_first;
3899     int entry_pos = first_entry + i;
3900     int xpos = MENU_SCREEN_START_XPOS;
3901     int ypos = MENU_SCREEN_START_YPOS + i;
3902     int startx = mSX + xpos * 32;
3903     int starty = mSY + ypos * 32;
3904     int font_nr = FONT_TEXT_1;
3905     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3906     int startx_text = startx + font_xoffset;
3907     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3908     int text_size = startx_scrollbar - startx_text;
3909     int max_buffer_len = text_size / getFontWidth(font_nr);
3910     char buffer[max_buffer_len + 1];
3911
3912     node_first = getTreeInfoFirstGroupEntry(ti);
3913     node = getTreeInfoFromPos(node_first, entry_pos);
3914
3915     strncpy(buffer, node->name, max_buffer_len);
3916     buffer[max_buffer_len] = '\0';
3917
3918     DrawText(startx, starty, buffer, font_nr + node->color);
3919
3920     if (node->parent_link)
3921       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3922     else if (node->level_group)
3923       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3924     else
3925       initCursor(i, IMG_MENU_BUTTON);
3926   }
3927
3928   redraw_mask |= REDRAW_FIELD;
3929 }
3930
3931 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3932 {
3933   TreeInfo *node, *node_first;
3934   int x, last_redraw_mask = redraw_mask;
3935   int ypos = MENU_TITLE2_YPOS;
3936   int font_nr = FONT_TITLE_2;
3937
3938   if (ti->type == TREE_TYPE_LEVEL_NR)
3939     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
3940
3941   if (ti->type != TREE_TYPE_LEVEL_DIR)
3942     return;
3943
3944   node_first = getTreeInfoFirstGroupEntry(ti);
3945   node = getTreeInfoFromPos(node_first, entry_pos);
3946
3947   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3948
3949   if (node->parent_link)
3950     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
3951                       node->node_parent->name);
3952   else if (node->level_group)
3953     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
3954                       node->name);
3955   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3956     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
3957                       node->levels, (node->levels > 1 ? "levels" : "level"),
3958                       node->class_desc);
3959
3960   /* let BackToFront() redraw only what is needed */
3961   redraw_mask = last_redraw_mask;
3962   for (x = 0; x < SCR_FIELDX; x++)
3963     MarkTileDirty(x, 1);
3964 }
3965
3966 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3967                              TreeInfo **ti_ptr)
3968 {
3969   TreeInfo *ti = *ti_ptr;
3970   int x = 0;
3971   int y = ti->cl_cursor;
3972   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3973   int num_entries = numTreeInfoInGroup(ti);
3974   int num_page_entries;
3975   boolean position_set_by_scrollbar = (dx == 999);
3976
3977   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3978     num_page_entries = num_entries;
3979   else
3980     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3981
3982   if (button == MB_MENU_INITIALIZE)
3983   {
3984     int num_entries = numTreeInfoInGroup(ti);
3985     int entry_pos = posTreeInfo(ti);
3986
3987     if (ti->cl_first == -1)
3988     {
3989       /* only on initialization */
3990       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3991       ti->cl_cursor = entry_pos - ti->cl_first;
3992     }
3993     else if (ti->cl_cursor >= num_page_entries ||
3994              (num_entries > num_page_entries &&
3995               num_entries - ti->cl_first < num_page_entries))
3996     {
3997       /* only after change of list size (by custom graphic configuration) */
3998       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3999       ti->cl_cursor = entry_pos - ti->cl_first;
4000     }
4001
4002     if (position_set_by_scrollbar)
4003       ti->cl_first = dy;
4004     else
4005       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4006                                 ti->cl_first, ti);
4007
4008     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4009     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4010     drawChooseTreeCursor(ti->cl_cursor, TRUE);
4011
4012     return;
4013   }
4014   else if (button == MB_MENU_LEAVE)
4015   {
4016     FadeSetLeaveMenu();
4017
4018     PlaySound(SND_MENU_ITEM_SELECTING);
4019
4020     if (ti->node_parent)
4021     {
4022       *ti_ptr = ti->node_parent;
4023       DrawChooseTree(ti_ptr);
4024     }
4025     else if (game_status == GAME_MODE_SETUP)
4026     {
4027       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4028           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4029           setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4030         execSetupGame();
4031       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4032                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4033                setup_mode == SETUP_MODE_CHOOSE_RENDERING)
4034         execSetupGraphics();
4035       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4036                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4037                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4038         execSetupSound();
4039       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4040                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4041                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
4042         execSetupTouch();
4043       else
4044         execSetupArtwork();
4045     }
4046     else
4047     {
4048       if (game_status == GAME_MODE_LEVELNR)
4049       {
4050         int new_level_nr = atoi(level_number_current->identifier);
4051
4052         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4053       }
4054
4055       SetGameStatus(GAME_MODE_MAIN);
4056
4057       DrawMainMenu();
4058     }
4059
4060     return;
4061   }
4062
4063   if (mx || my)         /* mouse input */
4064   {
4065     x = (mx - mSX) / 32;
4066     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
4067   }
4068   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
4069   {
4070     /* move cursor instead of scrolling when already at start/end of list */
4071     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
4072       dy = -1;
4073     else if (dy == +1 * SCROLL_LINE &&
4074              ti->cl_first + num_page_entries == num_entries)
4075       dy = 1;
4076
4077     /* handle scrolling screen one line or page */
4078     if (ti->cl_cursor + dy < 0 ||
4079         ti->cl_cursor + dy > num_page_entries - 1)
4080     {
4081       boolean redraw = FALSE;
4082
4083       if (ABS(dy) == SCROLL_PAGE)
4084         step = num_page_entries - 1;
4085
4086       if (dy < 0 && ti->cl_first > 0)
4087       {
4088         /* scroll page/line up */
4089
4090         ti->cl_first -= step;
4091         if (ti->cl_first < 0)
4092           ti->cl_first = 0;
4093
4094         redraw = TRUE;
4095       }
4096       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
4097       {
4098         /* scroll page/line down */
4099
4100         ti->cl_first += step;
4101         if (ti->cl_first + num_page_entries > num_entries)
4102           ti->cl_first = MAX(0, num_entries - num_page_entries);
4103
4104         redraw = TRUE;
4105       }
4106
4107       if (redraw)
4108       {
4109         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4110         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4111         drawChooseTreeCursor(ti->cl_cursor, TRUE);
4112
4113         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4114                                   ti->cl_first, ti);
4115       }
4116
4117       return;
4118     }
4119
4120     /* handle moving cursor one line */
4121     y = ti->cl_cursor + dy;
4122   }
4123
4124   if (dx == 1)
4125   {
4126     TreeInfo *node_first, *node_cursor;
4127     int entry_pos = ti->cl_first + y;
4128
4129     node_first = getTreeInfoFirstGroupEntry(ti);
4130     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4131
4132     if (node_cursor->node_group)
4133     {
4134       FadeSetEnterMenu();
4135
4136       PlaySound(SND_MENU_ITEM_SELECTING);
4137
4138       node_cursor->cl_first = ti->cl_first;
4139       node_cursor->cl_cursor = ti->cl_cursor;
4140       *ti_ptr = node_cursor->node_group;
4141       DrawChooseTree(ti_ptr);
4142
4143       return;
4144     }
4145   }
4146   else if (dx == -1 && ti->node_parent)
4147   {
4148     FadeSetLeaveMenu();
4149
4150     PlaySound(SND_MENU_ITEM_SELECTING);
4151
4152     *ti_ptr = ti->node_parent;
4153     DrawChooseTree(ti_ptr);
4154
4155     return;
4156   }
4157
4158   if (!anyScrollbarGadgetActive() &&
4159       IN_VIS_MENU(x, y) &&
4160       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
4161       y >= 0 && y < num_page_entries)
4162   {
4163     if (button)
4164     {
4165       if (y != ti->cl_cursor)
4166       {
4167         PlaySound(SND_MENU_ITEM_ACTIVATING);
4168
4169         drawChooseTreeCursor(ti->cl_cursor, FALSE);
4170         drawChooseTreeCursor(y, TRUE);
4171         drawChooseTreeInfo(ti->cl_first + y, ti);
4172
4173         ti->cl_cursor = y;
4174       }
4175       else if (dx < 0)
4176       {
4177         if (game_status == GAME_MODE_SETUP)
4178         {
4179           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4180               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4181               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4182             execSetupGame();
4183           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4184                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4185                    setup_mode == SETUP_MODE_CHOOSE_RENDERING)
4186             execSetupGraphics();
4187           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4188                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4189                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4190             execSetupSound();
4191           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4192                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4193                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
4194             execSetupTouch();
4195           else
4196             execSetupArtwork();
4197         }
4198       }
4199     }
4200     else
4201     {
4202       TreeInfo *node_first, *node_cursor;
4203       int entry_pos = ti->cl_first + y;
4204
4205       PlaySound(SND_MENU_ITEM_SELECTING);
4206
4207       node_first = getTreeInfoFirstGroupEntry(ti);
4208       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4209
4210       if (node_cursor->node_group)
4211       {
4212         FadeSetEnterMenu();
4213
4214         node_cursor->cl_first = ti->cl_first;
4215         node_cursor->cl_cursor = ti->cl_cursor;
4216         *ti_ptr = node_cursor->node_group;
4217         DrawChooseTree(ti_ptr);
4218       }
4219       else if (node_cursor->parent_link)
4220       {
4221         FadeSetLeaveMenu();
4222
4223         *ti_ptr = node_cursor->node_parent;
4224         DrawChooseTree(ti_ptr);
4225       }
4226       else
4227       {
4228         FadeSetEnterMenu();
4229
4230         node_cursor->cl_first = ti->cl_first;
4231         node_cursor->cl_cursor = ti->cl_cursor;
4232         *ti_ptr = node_cursor;
4233
4234         if (ti->type == TREE_TYPE_LEVEL_DIR)
4235         {
4236           LoadLevelSetup_SeriesInfo();
4237
4238           SaveLevelSetup_LastSeries();
4239           SaveLevelSetup_SeriesInfo();
4240           TapeErase();
4241         }
4242
4243         if (game_status == GAME_MODE_SETUP)
4244         {
4245           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4246               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4247               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4248             execSetupGame();
4249           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4250                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4251                    setup_mode == SETUP_MODE_CHOOSE_RENDERING)
4252             execSetupGraphics();
4253           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4254                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4255                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4256             execSetupSound();
4257           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4258                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4259                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
4260             execSetupTouch();
4261           else
4262             execSetupArtwork();
4263         }
4264         else
4265         {
4266           if (game_status == GAME_MODE_LEVELNR)
4267           {
4268             int new_level_nr = atoi(level_number_current->identifier);
4269
4270             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4271           }
4272
4273           SetGameStatus(GAME_MODE_MAIN);
4274
4275           DrawMainMenu();
4276         }
4277       }
4278     }
4279   }
4280 }
4281
4282 void DrawChooseLevelSet()
4283 {
4284   FadeMenuSoundsAndMusic();
4285
4286   DrawChooseTree(&leveldir_current);
4287
4288   PlayMenuSoundsAndMusic();
4289 }
4290
4291 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
4292 {
4293   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4294 }
4295
4296 void DrawChooseLevelNr()
4297 {
4298   int i;
4299
4300   FadeMenuSoundsAndMusic();
4301
4302   if (level_number != NULL)
4303   {
4304     freeTreeInfo(level_number);
4305
4306     level_number = NULL;
4307   }
4308
4309   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
4310   {
4311     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
4312     char identifier[32], name[32];
4313     int value = i;
4314
4315     /* temporarily load level info to get level name */
4316     LoadLevelInfoOnly(i);
4317
4318     ti->node_top = &level_number;
4319     ti->sort_priority = 10000 + value;
4320     ti->color = (level.no_level_file ? FC_BLUE :
4321                  LevelStats_getSolved(i) ? FC_GREEN :
4322                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
4323
4324     snprintf(identifier, sizeof(identifier), "%d", value);
4325     snprintf(name, sizeof(name), "%03d: %s", value,
4326              (level.no_level_file ? "(no file)" : level.name));
4327
4328     setString(&ti->identifier, identifier);
4329     setString(&ti->name, name);
4330     setString(&ti->name_sorting, name);
4331
4332     pushTreeInfo(&level_number, ti);
4333   }
4334
4335   /* sort level number values to start with lowest level number */
4336   sortTreeInfo(&level_number);
4337
4338   /* set current level number to current level number */
4339   level_number_current =
4340     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
4341
4342   /* if that also fails, set current level number to first available level */
4343   if (level_number_current == NULL)
4344     level_number_current = level_number;
4345
4346   DrawChooseTree(&level_number_current);
4347
4348   PlayMenuSoundsAndMusic();
4349 }
4350
4351 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
4352 {
4353   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
4354 }
4355
4356 void DrawHallOfFame(int highlight_position)
4357 {
4358   int fade_mask = REDRAW_FIELD;
4359
4360   if (CheckIfGlobalBorderHasChanged())
4361     fade_mask = REDRAW_ALL;
4362
4363   UnmapAllGadgets();
4364   FadeMenuSoundsAndMusic();
4365
4366   /* (this is needed when called from GameEnd() after winning a game) */
4367   KeyboardAutoRepeatOn();
4368
4369   /* (this is needed when called from GameEnd() after winning a game) */
4370   SetDrawDeactivationMask(REDRAW_NONE);
4371   SetDrawBackgroundMask(REDRAW_FIELD);
4372
4373   if (highlight_position < 0) 
4374     LoadScore(level_nr);
4375   else
4376     SetAnimStatus(GAME_MODE_PSEUDO_SCORESNEW);
4377
4378   FadeSetEnterScreen();
4379
4380   FadeOut(fade_mask);
4381
4382   /* needed if different viewport properties defined for scores */
4383   ChangeViewportPropertiesIfNeeded();
4384
4385   PlayMenuSoundsAndMusic();
4386
4387   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4388
4389   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
4390
4391   DrawMaskedBorder(fade_mask);
4392
4393   FadeIn(fade_mask);
4394 }
4395
4396 static void drawHallOfFameList(int first_entry, int highlight_position)
4397 {
4398   int i, j;
4399
4400   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4401   ClearField();
4402
4403   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4404   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4405                     "HighScores of Level %d", level_nr);
4406
4407   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4408   {
4409     int entry = first_entry + i;
4410     boolean active = (entry == highlight_position);
4411     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4412     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4413     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4414     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4415     int dxoff = getFontDrawOffsetX(font_nr1);
4416     int dx1 = 3 * getFontWidth(font_nr1);
4417     int dx2 = dx1 + getFontWidth(font_nr1);
4418     int dx3 = SXSIZE - 2 * (mSX - SX + dxoff) - 5 * getFontWidth(font_nr4);
4419     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
4420     int sy = mSY + 64 + i * 32;
4421
4422     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4423     DrawText(mSX + dx1, sy, ".", font_nr1);
4424
4425     for (j = 0; j < num_dots; j++)
4426       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
4427
4428     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4429       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4430
4431     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4432   }
4433
4434   redraw_mask |= REDRAW_FIELD;
4435 }
4436
4437 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4438 {
4439   static int first_entry = 0;
4440   static int highlight_position = 0;
4441   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4442
4443   if (button == MB_MENU_INITIALIZE)
4444   {
4445     first_entry = 0;
4446     highlight_position = mx;
4447     drawHallOfFameList(first_entry, highlight_position);
4448
4449     return;
4450   }
4451
4452   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
4453     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4454
4455   if (dy < 0)
4456   {
4457     if (first_entry > 0)
4458     {
4459       first_entry -= step;
4460       if (first_entry < 0)
4461         first_entry = 0;
4462
4463       drawHallOfFameList(first_entry, highlight_position);
4464     }
4465   }
4466   else if (dy > 0)
4467   {
4468     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4469     {
4470       first_entry += step;
4471       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4472         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4473
4474       drawHallOfFameList(first_entry, highlight_position);
4475     }
4476   }
4477   else if (button == MB_MENU_LEAVE)
4478   {
4479     PlaySound(SND_MENU_ITEM_SELECTING);
4480
4481     FadeSound(SND_BACKGROUND_SCORES);
4482
4483     SetGameStatus(GAME_MODE_MAIN);
4484
4485     DrawMainMenu();
4486   }
4487   else if (button == MB_MENU_CHOICE)
4488   {
4489     PlaySound(SND_MENU_ITEM_SELECTING);
4490
4491     FadeSound(SND_BACKGROUND_SCORES);
4492
4493     SetGameStatus(GAME_MODE_MAIN);
4494
4495     DrawMainMenu();
4496   }
4497
4498   if (game_status == GAME_MODE_SCORES)
4499     PlayMenuSoundIfLoop();
4500 }
4501
4502
4503 /* ========================================================================= */
4504 /* setup screen functions                                                    */
4505 /* ========================================================================= */
4506
4507 static struct TokenInfo *setup_info;
4508 static int num_setup_info;      /* number of setup entries shown on screen */
4509 static int max_setup_info;      /* total number of setup entries in list */
4510
4511 static char *window_size_text;
4512 static char *scaling_type_text;
4513 static char *rendering_mode_text;
4514 static char *scroll_delay_text;
4515 static char *snapshot_mode_text;
4516 static char *game_speed_text;
4517 static char *graphics_set_name;
4518 static char *sounds_set_name;
4519 static char *music_set_name;
4520 static char *volume_simple_text;
4521 static char *volume_loops_text;
4522 static char *volume_music_text;
4523 static char *touch_controls_text;
4524 static char *move_distance_text;
4525 static char *drop_distance_text;
4526
4527 static void execSetupMain()
4528 {
4529   setup_mode = SETUP_MODE_MAIN;
4530
4531   DrawSetupScreen();
4532 }
4533
4534 static void execSetupGame_setGameSpeeds()
4535 {
4536   if (game_speeds == NULL)
4537   {
4538     int i;
4539
4540     for (i = 0; game_speeds_list[i].value != -1; i++)
4541     {
4542       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4543       char identifier[32], name[32];
4544       int value = game_speeds_list[i].value;
4545       char *text = game_speeds_list[i].text;
4546
4547       ti->node_top = &game_speeds;
4548       ti->sort_priority = 10000 - value;
4549
4550       sprintf(identifier, "%d", value);
4551       sprintf(name, "%s", text);
4552
4553       setString(&ti->identifier, identifier);
4554       setString(&ti->name, name);
4555       setString(&ti->name_sorting, name);
4556       setString(&ti->infotext, "Game Speed");
4557
4558       pushTreeInfo(&game_speeds, ti);
4559     }
4560
4561     /* sort game speed values to start with slowest game speed */
4562     sortTreeInfo(&game_speeds);
4563
4564     /* set current game speed to configured game speed value */
4565     game_speed_current =