eliminated ISO-8859-1 characters from source code files
[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 "cartoons.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26 /* screens on the info screen */
27 #define INFO_MODE_MAIN                  0
28 #define INFO_MODE_TITLE                 1
29 #define INFO_MODE_ELEMENTS              2
30 #define INFO_MODE_MUSIC                 3
31 #define INFO_MODE_CREDITS               4
32 #define INFO_MODE_PROGRAM               5
33 #define INFO_MODE_VERSION               6
34 #define INFO_MODE_LEVELSET              7
35
36 #define MAX_INFO_MODES                  8
37
38 /* screens on the setup screen */
39 /* (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h) */
40 /* (should also match corresponding entries in src/conf_gfx.c) */
41 #define SETUP_MODE_MAIN                 0
42 #define SETUP_MODE_GAME                 1
43 #define SETUP_MODE_EDITOR               2
44 #define SETUP_MODE_GRAPHICS             3
45 #define SETUP_MODE_SOUND                4
46 #define SETUP_MODE_ARTWORK              5
47 #define SETUP_MODE_INPUT                6
48 #define SETUP_MODE_TOUCH                7
49 #define SETUP_MODE_SHORTCUTS            8
50 #define SETUP_MODE_SHORTCUTS_1          9
51 #define SETUP_MODE_SHORTCUTS_2          10
52 #define SETUP_MODE_SHORTCUTS_3          11
53 #define SETUP_MODE_SHORTCUTS_4          12
54 #define SETUP_MODE_SHORTCUTS_5          13
55
56 /* sub-screens on the setup screen (generic) */
57 #define SETUP_MODE_CHOOSE_ARTWORK       14
58 #define SETUP_MODE_CHOOSE_OTHER         15
59
60 /* sub-screens on the setup screen (specific) */
61 #define SETUP_MODE_CHOOSE_GAME_SPEED    16
62 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  17
63 #define SETUP_MODE_CHOOSE_SCREEN_MODE   18
64 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   19
65 #define SETUP_MODE_CHOOSE_SCALING_TYPE  20
66 #define SETUP_MODE_CHOOSE_GRAPHICS      21
67 #define SETUP_MODE_CHOOSE_SOUNDS        22
68 #define SETUP_MODE_CHOOSE_MUSIC         23
69 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 24
70 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  25
71 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  26
72 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 27
73 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 28
74 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 29
75
76 #define MAX_SETUP_MODES                 30
77
78 /* for input setup functions */
79 #define SETUPINPUT_SCREEN_POS_START     0
80 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
81 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
82 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
83
84 /* for various menu stuff  */
85 #define MENU_SCREEN_START_XPOS          1
86 #define MENU_SCREEN_START_YPOS          2
87 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
88 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
89 #define MENU_TITLE1_YPOS                8
90 #define MENU_TITLE2_YPOS                46
91 #define MENU_SCREEN_INFO_XSTART         16
92 #define MENU_SCREEN_INFO_YSTART1        100
93 #define MENU_SCREEN_INFO_YSTART2        128
94 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
95 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - 20)
96 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
97                                          MENU_SCREEN_INFO_YSTART2 -     \
98                                          TILEY / 2)
99 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
100 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MENU_SCREEN_INFO_YSIZE /    \
101                                             MENU_SCREEN_INFO_YSTEP,     \
102                                             MAX_INFO_ELEMENTS_ON_SCREEN)
103 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
104 #define MAX_MENU_TEXT_LENGTH_BIG        13
105 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
106
107 /* buttons and scrollbars identifiers */
108 #define SCREEN_CTRL_ID_PREV_LEVEL       0
109 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
110 #define SCREEN_CTRL_ID_PREV_PLAYER      2
111 #define SCREEN_CTRL_ID_NEXT_PLAYER      3
112 #define SCREEN_CTRL_ID_SCROLL_UP        4
113 #define SCREEN_CTRL_ID_SCROLL_DOWN      5
114 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  6
115
116 #define NUM_SCREEN_GADGETS              7
117
118 #define NUM_SCREEN_MENUBUTTONS          4
119 #define NUM_SCREEN_SCROLLBUTTONS        2
120 #define NUM_SCREEN_SCROLLBARS           1
121
122 #define SCREEN_MASK_MAIN                (1 << 0)
123 #define SCREEN_MASK_INPUT               (1 << 1)
124
125 /* graphic position and size values for buttons and scrollbars */
126 #define SC_MENUBUTTON_XSIZE             TILEX
127 #define SC_MENUBUTTON_YSIZE             TILEY
128
129 #define SC_SCROLLBUTTON_XSIZE           TILEX
130 #define SC_SCROLLBUTTON_YSIZE           TILEY
131
132 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
133
134 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
135 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
136                                          SC_SCROLLBUTTON_YSIZE)
137
138 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
139 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
140
141 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
142 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
143                                          SC_SCROLLBUTTON_YSIZE)
144
145 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
146 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
147                                          SC_SCROLL_VERTICAL_YSIZE)
148
149 #define SC_BORDER_SIZE                  14
150
151
152 /* forward declarations of internal functions */
153 static void HandleScreenGadgets(struct GadgetInfo *);
154 static void HandleSetupScreen_Generic(int, int, int, int, int);
155 static void HandleSetupScreen_Input(int, int, int, int, int);
156 static void CustomizeKeyboard(int);
157 static void CalibrateJoystick(int);
158 static void execSetupGame(void);
159 static void execSetupGraphics(void);
160 static void execSetupSound(void);
161 static void execSetupTouch(void);
162 static void execSetupArtwork(void);
163 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
164
165 static void DrawChooseLevelSet(void);
166 static void DrawChooseLevelNr(void);
167 static void DrawInfoScreen(void);
168 static void DrawAndFadeInInfoScreen(int);
169 static void DrawSetupScreen(void);
170
171 static void DrawInfoScreenExt(int, int);
172 static void DrawInfoScreen_NotAvailable(char *, char *);
173 static void DrawInfoScreen_HelpAnim(int, int, boolean);
174 static void DrawInfoScreen_HelpText(int, int, int, int);
175 static void HandleInfoScreen_Main(int, int, int, int, int);
176 static void HandleInfoScreen_TitleScreen(int);
177 static void HandleInfoScreen_Elements(int);
178 static void HandleInfoScreen_Music(int);
179 static void HandleInfoScreen_Credits(int);
180 static void HandleInfoScreen_Program(int);
181 static void HandleInfoScreen_Version(int);
182
183 static void MapScreenMenuGadgets(int);
184 static void MapScreenGadgets(int);
185 static void MapScreenTreeGadgets(TreeInfo *);
186
187 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
188
189 static int info_mode = INFO_MODE_MAIN;
190 static int setup_mode = SETUP_MODE_MAIN;
191
192 static TreeInfo *screen_modes = NULL;
193 static TreeInfo *screen_mode_current = NULL;
194
195 static TreeInfo *window_sizes = NULL;
196 static TreeInfo *window_size_current = NULL;
197
198 static TreeInfo *scaling_types = NULL;
199 static TreeInfo *scaling_type_current = NULL;
200
201 static TreeInfo *scroll_delays = NULL;
202 static TreeInfo *scroll_delay_current = NULL;
203
204 static TreeInfo *game_speeds = NULL;
205 static TreeInfo *game_speed_current = NULL;
206
207 static TreeInfo *volumes_simple = NULL;
208 static TreeInfo *volume_simple_current = NULL;
209
210 static TreeInfo *volumes_loops = NULL;
211 static TreeInfo *volume_loops_current = NULL;
212
213 static TreeInfo *volumes_music = NULL;
214 static TreeInfo *volume_music_current = NULL;
215
216 static TreeInfo *touch_controls = NULL;
217 static TreeInfo *touch_control_current = NULL;
218
219 static TreeInfo *move_distances = NULL;
220 static TreeInfo *move_distance_current = NULL;
221
222 static TreeInfo *drop_distances = NULL;
223 static TreeInfo *drop_distance_current = NULL;
224
225 static TreeInfo *level_number = NULL;
226 static TreeInfo *level_number_current = NULL;
227
228 static struct
229 {
230   int value;
231   char *text;
232 } window_sizes_list[] =
233 {
234   {     50,     "50 %"                          },
235   {     80,     "80 %"                          },
236   {     90,     "90 %"                          },
237   {     100,    "100 % (Default)"               },
238   {     110,    "110 %"                         },
239   {     120,    "120 %"                         },
240   {     130,    "130 %"                         },
241   {     140,    "140 %"                         },
242   {     150,    "150 %"                         },
243   {     200,    "200 %"                         },
244   {     250,    "250 %"                         },
245   {     300,    "300 %"                         },
246
247   {     -1,     NULL                            },
248 };
249
250 static struct
251 {
252   char *value;
253   char *text;
254 } scaling_types_list[] =
255 {
256   {     SCALING_QUALITY_NEAREST, "None"         },
257   {     SCALING_QUALITY_LINEAR,  "Linear"       },
258   {     SCALING_QUALITY_BEST,    "Anisotropic"  },
259
260   {     NULL,                    NULL           },
261 };
262
263 static struct
264 {
265   int value;
266   char *text;
267 } game_speeds_list[] =
268 {
269 #if 1
270   {     30,     "Very Slow"                     },
271   {     25,     "Slow"                          },
272   {     20,     "Normal"                        },
273   {     15,     "Fast"                          },
274   {     10,     "Very Fast"                     },
275 #else
276   {     1000,   "1/1s (Extremely Slow)"         },
277   {     500,    "1/2s"                          },
278   {     200,    "1/5s"                          },
279   {     100,    "1/10s"                         },
280   {     50,     "1/20s"                         },
281   {     29,     "1/35s (Original Supaplex)"     },
282   {     25,     "1/40s"                         },
283   {     20,     "1/50s (Normal Speed)"          },
284   {     14,     "1/70s (Maximum Supaplex)"      },
285   {     10,     "1/100s"                        },
286   {     5,      "1/200s"                        },
287   {     2,      "1/500s"                        },
288   {     1,      "1/1000s (Extremely Fast)"      },
289 #endif
290
291   {     -1,     NULL                            },
292 };
293
294 static struct
295 {
296   int value;
297   char *text;
298 } scroll_delays_list[] =
299 {
300   {     0,      "0 Tiles (No Scroll Delay)"     },
301   {     1,      "1 Tile"                        },
302   {     2,      "2 Tiles"                       },
303   {     3,      "3 Tiles (Default)"             },
304   {     4,      "4 Tiles"                       },
305   {     5,      "5 Tiles"                       },
306   {     6,      "6 Tiles"                       },
307   {     7,      "7 Tiles"                       },
308   {     8,      "8 Tiles (Maximum Scroll Delay)"},
309
310   {     -1,     NULL                            },
311 };
312
313 static struct
314 {
315   int value;
316   char *text;
317 } volumes_list[] =
318 {
319   {     0,      "0 %"                           },
320   {     10,     "10 %"                          },
321   {     20,     "20 %"                          },
322   {     30,     "30 %"                          },
323   {     40,     "40 %"                          },
324   {     50,     "50 %"                          },
325   {     60,     "60 %"                          },
326   {     70,     "70 %"                          },
327   {     80,     "80 %"                          },
328   {     90,     "90 %"                          },
329   {     100,    "100 %"                         },
330
331   {     -1,     NULL                            },
332 };
333
334 static struct
335 {
336   char *value;
337   char *text;
338 } touch_controls_list[] =
339 {
340   {     TOUCH_CONTROL_VIRTUAL_BUTTONS,  "Virtual Buttons"       },
341   {     TOUCH_CONTROL_WIPE_GESTURES,    "Wipe Gestures"         },
342
343   {     NULL,                           NULL                    },
344 };
345
346 static struct
347 {
348   int value;
349   char *text;
350 } distances_list[] =
351 {
352   {     1,      "1 %"                           },
353   {     2,      "2 %"                           },
354   {     3,      "3 %"                           },
355   {     4,      "4 %"                           },
356   {     5,      "5 %"                           },
357   {     10,     "10 %"                          },
358   {     15,     "15 %"                          },
359   {     20,     "20 %"                          },
360   {     25,     "25 %"                          },
361
362   {     -1,     NULL                            },
363 };
364
365 #define DRAW_MODE(s)            ((s) >= GAME_MODE_MAIN &&               \
366                                  (s) <= GAME_MODE_SETUP ? (s) :         \
367                                  (s) == GAME_MODE_PSEUDO_TYPENAME ?     \
368                                  GAME_MODE_MAIN : GAME_MODE_DEFAULT)
369
370 /* (there are no draw offset definitions needed for INFO_MODE_TITLE) */
371 #define DRAW_MODE_INFO(i)       ((i) >= INFO_MODE_ELEMENTS &&           \
372                                  (i) <= INFO_MODE_LEVELSET ? (i) :      \
373                                  INFO_MODE_MAIN)
374
375 #define DRAW_MODE_SETUP(i)      ((i) >= SETUP_MODE_MAIN &&              \
376                                  (i) <= SETUP_MODE_SHORTCUTS_5 ? (i) :  \
377                                  (i) >= SETUP_MODE_CHOOSE_GRAPHICS &&   \
378                                  (i) <= SETUP_MODE_CHOOSE_MUSIC ?       \
379                                  SETUP_MODE_CHOOSE_ARTWORK :            \
380                                  SETUP_MODE_CHOOSE_OTHER)
381
382 #define DRAW_XOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
383                                  menu.draw_xoffset[GAME_MODE_INFO] :    \
384                                  menu.draw_xoffset_info[DRAW_MODE_INFO(i)])
385 #define DRAW_YOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
386                                  menu.draw_yoffset[GAME_MODE_INFO] :    \
387                                  menu.draw_yoffset_info[DRAW_MODE_INFO(i)])
388
389 #define DRAW_XOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
390                                  menu.draw_xoffset[GAME_MODE_SETUP] :   \
391                                  menu.draw_xoffset_setup[DRAW_MODE_SETUP(i)])
392 #define DRAW_YOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
393                                  menu.draw_yoffset[GAME_MODE_SETUP] :   \
394                                  menu.draw_yoffset_setup[DRAW_MODE_SETUP(i)])
395
396 #define DRAW_XOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
397                                  DRAW_XOFFSET_INFO(info_mode) :         \
398                                  (s) == GAME_MODE_SETUP ?               \
399                                  DRAW_XOFFSET_SETUP(setup_mode) :       \
400                                  menu.draw_xoffset[DRAW_MODE(s)])
401 #define DRAW_YOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
402                                  DRAW_YOFFSET_INFO(info_mode) :         \
403                                  (s) == GAME_MODE_SETUP ?               \
404                                  DRAW_YOFFSET_SETUP(setup_mode) :       \
405                                  menu.draw_yoffset[DRAW_MODE(s)])
406
407 #define mSX                     (SX + DRAW_XOFFSET(game_status))
408 #define mSY                     (SY + DRAW_YOFFSET(game_status))
409
410 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
411                                     menu.list_size[game_status] :       \
412                                     MAX_MENU_ENTRIES_ON_SCREEN)
413
414
415 /* title display and control definitions */
416
417 #define MAX_NUM_TITLE_SCREENS   (2 * MAX_NUM_TITLE_IMAGES +             \
418                                  2 * MAX_NUM_TITLE_MESSAGES)
419
420 static int num_title_screens = 0;
421
422 struct TitleControlInfo
423 {
424   boolean is_image;
425   boolean initial;
426   int local_nr;
427   int sort_priority;
428 };
429
430 struct TitleControlInfo title_controls[MAX_NUM_TITLE_SCREENS];
431
432 /* main menu display and control definitions */
433
434 #define MAIN_CONTROL_NAME                       0
435 #define MAIN_CONTROL_LEVELS                     1
436 #define MAIN_CONTROL_SCORES                     2
437 #define MAIN_CONTROL_EDITOR                     3
438 #define MAIN_CONTROL_INFO                       4
439 #define MAIN_CONTROL_GAME                       5
440 #define MAIN_CONTROL_SETUP                      6
441 #define MAIN_CONTROL_QUIT                       7
442 #define MAIN_CONTROL_PREV_LEVEL                 8
443 #define MAIN_CONTROL_NEXT_LEVEL                 9
444 #define MAIN_CONTROL_FIRST_LEVEL                10
445 #define MAIN_CONTROL_LAST_LEVEL                 11
446 #define MAIN_CONTROL_LEVEL_NUMBER               12
447 #define MAIN_CONTROL_LEVEL_INFO_1               13
448 #define MAIN_CONTROL_LEVEL_INFO_2               14
449 #define MAIN_CONTROL_LEVEL_NAME                 15
450 #define MAIN_CONTROL_LEVEL_AUTHOR               16
451 #define MAIN_CONTROL_LEVEL_YEAR                 17
452 #define MAIN_CONTROL_LEVEL_IMPORTED_FROM        18
453 #define MAIN_CONTROL_LEVEL_IMPORTED_BY          19
454 #define MAIN_CONTROL_LEVEL_TESTED_BY            20
455 #define MAIN_CONTROL_TITLE_1                    21
456 #define MAIN_CONTROL_TITLE_2                    22
457 #define MAIN_CONTROL_TITLE_3                    23
458
459 static char str_main_text_name[10];
460 static char str_main_text_first_level[10];
461 static char str_main_text_last_level[10];
462 static char str_main_text_level_number[10];
463
464 static char *main_text_name                     = str_main_text_name;
465 static char *main_text_first_level              = str_main_text_first_level;
466 static char *main_text_last_level               = str_main_text_last_level;
467 static char *main_text_level_number             = str_main_text_level_number;
468 static char *main_text_levels                   = "Levelset";
469 static char *main_text_scores                   = "Hall Of Fame";
470 static char *main_text_editor                   = "Level Creator";
471 static char *main_text_info                     = "Info Screen";
472 static char *main_text_game                     = "Start Game";
473 static char *main_text_setup                    = "Setup";
474 static char *main_text_quit                     = "Quit";
475 static char *main_text_level_name               = level.name;
476 static char *main_text_level_author             = level.author;
477 static char *main_text_level_year               = NULL;
478 static char *main_text_level_imported_from      = NULL;
479 static char *main_text_level_imported_by        = NULL;
480 static char *main_text_level_tested_by          = NULL;
481 static char *main_text_title_1                  = PROGRAM_TITLE_STRING;
482 static char *main_text_title_2                  = PROGRAM_COPYRIGHT_STRING;
483 static char *main_text_title_3                  = PROGRAM_GAME_BY_STRING;
484
485 struct MainControlInfo
486 {
487   int nr;
488
489   struct MenuPosInfo *pos_button;
490   int button_graphic;
491
492   struct TextPosInfo *pos_text;
493   char **text;
494
495   struct TextPosInfo *pos_input;
496   char **input;
497 };
498
499 static struct MainControlInfo main_controls[] =
500 {
501   {
502     MAIN_CONTROL_NAME,
503     &menu.main.button.name,             IMG_MENU_BUTTON_NAME,
504     &menu.main.text.name,               &main_text_name,
505     &menu.main.input.name,              &setup.player_name,
506   },
507   {
508     MAIN_CONTROL_LEVELS,
509     &menu.main.button.levels,           IMG_MENU_BUTTON_LEVELS,
510     &menu.main.text.levels,             &main_text_levels,
511     NULL,                               NULL,
512   },
513   {
514     MAIN_CONTROL_SCORES,
515     &menu.main.button.scores,           IMG_MENU_BUTTON_SCORES,
516     &menu.main.text.scores,             &main_text_scores,
517     NULL,                               NULL,
518   },
519   {
520     MAIN_CONTROL_EDITOR,
521     &menu.main.button.editor,           IMG_MENU_BUTTON_EDITOR,
522     &menu.main.text.editor,             &main_text_editor,
523     NULL,                               NULL,
524   },
525   {
526     MAIN_CONTROL_INFO,
527     &menu.main.button.info,             IMG_MENU_BUTTON_INFO,
528     &menu.main.text.info,               &main_text_info,
529     NULL,                               NULL,
530   },
531   {
532     MAIN_CONTROL_GAME,
533     &menu.main.button.game,             IMG_MENU_BUTTON_GAME,
534     &menu.main.text.game,               &main_text_game,
535     NULL,                               NULL,
536   },
537   {
538     MAIN_CONTROL_SETUP,
539     &menu.main.button.setup,            IMG_MENU_BUTTON_SETUP,
540     &menu.main.text.setup,              &main_text_setup,
541     NULL,                               NULL,
542   },
543   {
544     MAIN_CONTROL_QUIT,
545     &menu.main.button.quit,             IMG_MENU_BUTTON_QUIT,
546     &menu.main.text.quit,               &main_text_quit,
547     NULL,                               NULL,
548   },
549 #if 0
550   /* (these two buttons are real gadgets) */
551   {
552     MAIN_CONTROL_PREV_LEVEL,
553     &menu.main.button.prev_level,       IMG_MENU_BUTTON_PREV_LEVEL,
554     NULL,                               NULL,
555     NULL,                               NULL,
556   },
557   {
558     MAIN_CONTROL_NEXT_LEVEL,
559     &menu.main.button.next_level,       IMG_MENU_BUTTON_NEXT_LEVEL,
560     NULL,                               NULL,
561     NULL,                               NULL,
562   },
563 #endif
564   {
565     MAIN_CONTROL_FIRST_LEVEL,
566     NULL,                               -1,
567     &menu.main.text.first_level,        &main_text_first_level,
568     NULL,                               NULL,
569   },
570   {
571     MAIN_CONTROL_LAST_LEVEL,
572     NULL,                               -1,
573     &menu.main.text.last_level,         &main_text_last_level,
574     NULL,                               NULL,
575   },
576   {
577     MAIN_CONTROL_LEVEL_NUMBER,
578     NULL,                               -1,
579     &menu.main.text.level_number,       &main_text_level_number,
580     NULL,                               NULL,
581   },
582   {
583     MAIN_CONTROL_LEVEL_INFO_1,
584     NULL,                               -1,
585     &menu.main.text.level_info_1,       NULL,
586     NULL,                               NULL,
587   },
588   {
589     MAIN_CONTROL_LEVEL_INFO_2,
590     NULL,                               -1,
591     &menu.main.text.level_info_2,       NULL,
592     NULL,                               NULL,
593   },
594   {
595     MAIN_CONTROL_LEVEL_NAME,
596     NULL,                               -1,
597     &menu.main.text.level_name,         &main_text_level_name,
598     NULL,                               NULL,
599   },
600   {
601     MAIN_CONTROL_LEVEL_AUTHOR,
602     NULL,                               -1,
603     &menu.main.text.level_author,       &main_text_level_author,
604     NULL,                               NULL,
605   },
606   {
607     MAIN_CONTROL_LEVEL_YEAR,
608     NULL,                               -1,
609     &menu.main.text.level_year,         &main_text_level_year,
610     NULL,                               NULL,
611   },
612   {
613     MAIN_CONTROL_LEVEL_IMPORTED_FROM,
614     NULL,                               -1,
615     &menu.main.text.level_imported_from, &main_text_level_imported_from,
616     NULL,                               NULL,
617   },
618   {
619     MAIN_CONTROL_LEVEL_IMPORTED_BY,
620     NULL,                               -1,
621     &menu.main.text.level_imported_by,  &main_text_level_imported_by,
622     NULL,                               NULL,
623   },
624   {
625     MAIN_CONTROL_LEVEL_TESTED_BY,
626     NULL,                               -1,
627     &menu.main.text.level_tested_by,    &main_text_level_tested_by,
628     NULL,                               NULL,
629   },
630   {
631     MAIN_CONTROL_TITLE_1,
632     NULL,                               -1,
633     &menu.main.text.title_1,            &main_text_title_1,
634     NULL,                               NULL,
635   },
636   {
637     MAIN_CONTROL_TITLE_2,
638     NULL,                               -1,
639     &menu.main.text.title_2,            &main_text_title_2,
640     NULL,                               NULL,
641   },
642   {
643     MAIN_CONTROL_TITLE_3,
644     NULL,                               -1,
645     &menu.main.text.title_3,            &main_text_title_3,
646     NULL,                               NULL,
647   },
648
649   {
650     -1,
651     NULL,                               -1,
652     NULL,                               NULL,
653     NULL,                               NULL,
654   }
655 };
656
657
658 static int getTitleScreenGraphic(int nr, boolean initial)
659 {
660   return (initial ? IMG_TITLESCREEN_INITIAL_1 : IMG_TITLESCREEN_1) + nr;
661 }
662
663 static struct TitleMessageInfo *getTitleMessageInfo(int nr, boolean initial)
664 {
665   return (initial ? &titlemessage_initial[nr] : &titlemessage[nr]);
666 }
667
668 #if 0
669 static int getTitleScreenGameMode(boolean initial)
670 {
671   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
672 }
673 #endif
674
675 static int getTitleMessageGameMode(boolean initial)
676 {
677   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
678 }
679
680 #if 0
681 static int getTitleScreenBackground(boolean initial)
682 {
683   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
684 }
685 #endif
686
687 #if 0
688 static int getTitleMessageBackground(int nr, boolean initial)
689 {
690   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
691 }
692 #endif
693
694 static int getTitleBackground(int nr, boolean initial, boolean is_image)
695 {
696   int base = (is_image ?
697               (initial ? IMG_BACKGROUND_TITLESCREEN_INITIAL_1 :
698                          IMG_BACKGROUND_TITLESCREEN_1) :
699               (initial ? IMG_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
700                          IMG_BACKGROUND_TITLEMESSAGE_1));
701   int graphic_global = (initial ? IMG_BACKGROUND_TITLE_INITIAL :
702                                   IMG_BACKGROUND_TITLE);
703   int graphic_local = base + nr;
704
705   if (graphic_info[graphic_local].bitmap != NULL)
706     return graphic_local;
707
708   if (graphic_info[graphic_global].bitmap != NULL)
709     return graphic_global;
710
711   return IMG_UNDEFINED;
712 }
713
714 static int getTitleSound(struct TitleControlInfo *tci)
715 {
716   boolean is_image = tci->is_image;
717   int initial = tci->initial;
718   int nr = tci->local_nr;
719   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
720   int base = (is_image ?
721               (initial ? SND_BACKGROUND_TITLESCREEN_INITIAL_1 :
722                          SND_BACKGROUND_TITLESCREEN_1) :
723               (initial ? SND_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
724                          SND_BACKGROUND_TITLEMESSAGE_1));
725   int sound_global = menu.sound[mode];
726   int sound_local = base + nr;
727
728 #if 0
729   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
730          nr, initial, is_image,
731          sound_global, getSoundListEntry(sound_global)->filename,
732          sound_local, getSoundListEntry(sound_local)->filename);
733 #endif
734
735   if (!strEqual(getSoundListEntry(sound_local)->filename, UNDEFINED_FILENAME))
736     return sound_local;
737
738   if (!strEqual(getSoundListEntry(sound_global)->filename, UNDEFINED_FILENAME))
739     return sound_global;
740
741   return SND_UNDEFINED;
742 }
743
744 static int getTitleMusic(struct TitleControlInfo *tci)
745 {
746   boolean is_image = tci->is_image;
747   int initial = tci->initial;
748   int nr = tci->local_nr;
749   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
750   int base = (is_image ?
751               (initial ? MUS_BACKGROUND_TITLESCREEN_INITIAL_1 :
752                          MUS_BACKGROUND_TITLESCREEN_1) :
753               (initial ? MUS_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
754                          MUS_BACKGROUND_TITLEMESSAGE_1));
755   int music_global = menu.music[mode];
756   int music_local = base + nr;
757
758 #if 0
759   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
760          nr, initial, is_image,
761          music_global, getMusicListEntry(music_global)->filename,
762          music_local, getMusicListEntry(music_local)->filename);
763 #endif
764
765   if (!strEqual(getMusicListEntry(music_local)->filename, UNDEFINED_FILENAME))
766     return music_local;
767
768   if (!strEqual(getMusicListEntry(music_global)->filename, UNDEFINED_FILENAME))
769     return music_global;
770
771   return MUS_UNDEFINED;
772 }
773
774 static struct TitleFadingInfo getTitleFading(struct TitleControlInfo *tci)
775 {
776   boolean is_image = tci->is_image;
777   int initial = tci->initial;
778   int nr = tci->local_nr;
779   struct TitleFadingInfo ti;
780
781   if (is_image)
782   {
783     int graphic = getTitleScreenGraphic(nr, initial);
784
785     /* initialize fading control values to default title config settings */
786     ti = (initial ? title_initial_default : title_default);
787
788     /* override default settings with image config settings, if defined */
789     if (graphic_info[graphic].fade_mode != FADE_MODE_DEFAULT)
790       ti.fade_mode = graphic_info[graphic].fade_mode;
791     if (graphic_info[graphic].fade_delay > -1)
792       ti.fade_delay = graphic_info[graphic].fade_delay;
793     if (graphic_info[graphic].post_delay > -1)
794       ti.post_delay = graphic_info[graphic].post_delay;
795     if (graphic_info[graphic].auto_delay > -1)
796       ti.auto_delay = graphic_info[graphic].auto_delay;
797   }
798   else
799   {
800     if (initial)
801     {
802       ti.fade_mode  = titlemessage_initial[nr].fade_mode;
803       ti.fade_delay = titlemessage_initial[nr].fade_delay;
804       ti.post_delay = titlemessage_initial[nr].post_delay;
805       ti.auto_delay = titlemessage_initial[nr].auto_delay;
806     }
807     else
808     {
809       ti.fade_mode  = titlemessage[nr].fade_mode;
810       ti.fade_delay = titlemessage[nr].fade_delay;
811       ti.post_delay = titlemessage[nr].post_delay;
812       ti.auto_delay = titlemessage[nr].auto_delay;
813     }
814   }
815
816   return ti;
817 }
818
819 static int compareTitleControlInfo(const void *object1, const void *object2)
820 {
821   const struct TitleControlInfo *tci1 = (struct TitleControlInfo *)object1;
822   const struct TitleControlInfo *tci2 = (struct TitleControlInfo *)object2;
823   int compare_result;
824
825   if (tci1->initial != tci2->initial)
826     compare_result = (tci1->initial ? -1 : +1);
827   else if (tci1->sort_priority != tci2->sort_priority)
828     compare_result = tci1->sort_priority - tci2->sort_priority;
829   else if (tci1->is_image != tci2->is_image)
830     compare_result = (tci1->is_image ? -1 : +1);
831   else
832     compare_result = tci1->local_nr - tci2->local_nr;
833
834   return compare_result;
835 }
836
837 static void InitializeTitleControlsExt_AddTitleInfo(boolean is_image,
838                                                     boolean initial,
839                                                     int nr, int sort_priority)
840 {
841   title_controls[num_title_screens].is_image = is_image;
842   title_controls[num_title_screens].initial = initial;
843   title_controls[num_title_screens].local_nr = nr;
844   title_controls[num_title_screens].sort_priority = sort_priority;
845
846   num_title_screens++;
847 }
848
849 static void InitializeTitleControls_CheckTitleInfo(boolean initial)
850 {
851   int i;
852
853   for (i = 0; i < MAX_NUM_TITLE_IMAGES; i++)
854   {
855     int graphic = getTitleScreenGraphic(i, initial);
856     Bitmap *bitmap = graphic_info[graphic].bitmap;
857     int sort_priority = graphic_info[graphic].sort_priority;
858
859     if (bitmap != NULL)
860       InitializeTitleControlsExt_AddTitleInfo(TRUE, initial, i, sort_priority);
861   }
862
863   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
864   {
865     struct TitleMessageInfo *tmi = getTitleMessageInfo(i, initial);
866     char *filename = getLevelSetTitleMessageFilename(i, initial);
867     int sort_priority = tmi->sort_priority;
868
869     if (filename != NULL)
870       InitializeTitleControlsExt_AddTitleInfo(FALSE, initial, i, sort_priority);
871   }
872 }
873
874 static void InitializeTitleControls(boolean show_title_initial)
875 {
876   num_title_screens = 0;
877
878   /* 1st step: initialize title screens for game start (only when starting) */
879   if (show_title_initial)
880     InitializeTitleControls_CheckTitleInfo(TRUE);
881
882   /* 2nd step: initialize title screens for current level set */
883   InitializeTitleControls_CheckTitleInfo(FALSE);
884
885   /* sort title screens according to sort_priority and title number */
886   qsort(title_controls, num_title_screens, sizeof(struct TitleControlInfo),
887         compareTitleControlInfo);
888 }
889
890 static boolean visibleMenuPos(struct MenuPosInfo *pos)
891 {
892   return (pos != NULL && pos->x != -1 && pos->y != -1);
893 }
894
895 static boolean visibleTextPos(struct TextPosInfo *pos)
896 {
897   return (pos != NULL && pos->x != -1 && pos->y != -1);
898 }
899
900 static void InitializeMainControls()
901 {
902   boolean local_team_mode = (!options.network && setup.team_mode);
903   int i;
904
905   /* set main control text values to dynamically determined values */
906   sprintf(main_text_name,         "%s",   local_team_mode ? "Team:" : "Name:");
907
908   strcpy(main_text_first_level,  int2str(leveldir_current->first_level,
909                                          menu.main.text.first_level.size));
910   strcpy(main_text_last_level,   int2str(leveldir_current->last_level,
911                                          menu.main.text.last_level.size));
912   strcpy(main_text_level_number, int2str(level_nr,
913                                          menu.main.text.level_number.size));
914
915   main_text_level_year          = leveldir_current->year;
916   main_text_level_imported_from = leveldir_current->imported_from;
917   main_text_level_imported_by   = leveldir_current->imported_by;
918   main_text_level_tested_by     = leveldir_current->tested_by;
919
920   /* set main control screen positions to dynamically determined values */
921   for (i = 0; main_controls[i].nr != -1; i++)
922   {
923     struct MainControlInfo *mci = &main_controls[i];
924     int nr                         = mci->nr;
925     struct MenuPosInfo *pos_button = mci->pos_button;
926     struct TextPosInfo *pos_text   = mci->pos_text;
927     struct TextPosInfo *pos_input  = mci->pos_input;
928     char *text                     = (mci->text  ? *mci->text  : NULL);
929     char *input                    = (mci->input ? *mci->input : NULL);
930     int button_graphic             = mci->button_graphic;
931     int font_text                  = (pos_text  ? pos_text->font  : -1);
932     int font_input                 = (pos_input ? pos_input->font : -1);
933
934     int font_text_width   = (font_text  != -1 ? getFontWidth(font_text)   : 0);
935     int font_text_height  = (font_text  != -1 ? getFontHeight(font_text)  : 0);
936     int font_input_width  = (font_input != -1 ? getFontWidth(font_input)  : 0);
937     int font_input_height = (font_input != -1 ? getFontHeight(font_input) : 0);
938     int text_chars  = (text  != NULL ? strlen(text)  : 0);
939     int input_chars = (input != NULL ? strlen(input) : 0);
940
941     int button_width =
942       (button_graphic != -1 ? graphic_info[button_graphic].width  : 0);
943     int button_height =
944       (button_graphic != -1 ? graphic_info[button_graphic].height : 0);
945     int text_width   = font_text_width * text_chars;
946     int text_height  = font_text_height;
947     int input_width  = font_input_width * input_chars;
948     int input_height = font_input_height;
949
950     if (nr == MAIN_CONTROL_NAME)
951     {
952       menu.main.input.name.width  = input_width;
953       menu.main.input.name.height = input_height;
954     }
955
956     if (pos_button != NULL)             /* (x/y may be -1/-1 here) */
957     {
958       if (pos_button->width == 0)
959         pos_button->width = button_width;
960       if (pos_button->height == 0)
961         pos_button->height = button_height;
962     }
963
964     if (pos_text != NULL)               /* (x/y may be -1/-1 here) */
965     {
966       /* calculate size for non-clickable text -- needed for text alignment */
967       boolean calculate_text_size = (pos_button == NULL && text != NULL);
968
969       if (visibleMenuPos(pos_button))
970       {
971         if (pos_text->x == -1)
972           pos_text->x = pos_button->x + pos_button->width;
973         if (pos_text->y == -1)
974           pos_text->y = pos_button->y;
975       }
976
977       if (pos_text->width == -1 || calculate_text_size)
978         pos_text->width = text_width;
979       if (pos_text->height == -1 || calculate_text_size)
980         pos_text->height = text_height;
981     }
982
983     if (pos_input != NULL)              /* (x/y may be -1/-1 here) */
984     {
985       if (visibleTextPos(pos_text))
986       {
987         if (pos_input->x == -1)
988           pos_input->x = pos_text->x + pos_text->width;
989         if (pos_input->y == -1)
990           pos_input->y = pos_text->y;
991       }
992
993       if (pos_input->width == -1)
994         pos_input->width = input_width;
995       if (pos_input->height == -1)
996         pos_input->height = input_height;
997     }
998   }
999 }
1000
1001 static void DrawCursorAndText_Main_Ext(int nr, boolean active_text,
1002                                        boolean active_input)
1003 {
1004   int i;
1005
1006   for (i = 0; main_controls[i].nr != -1; i++)
1007   {
1008     struct MainControlInfo *mci = &main_controls[i];
1009
1010     if (mci->nr == nr || nr == -1)
1011     {
1012       struct MenuPosInfo *pos_button = mci->pos_button;
1013       struct TextPosInfo *pos_text   = mci->pos_text;
1014       struct TextPosInfo *pos_input  = mci->pos_input;
1015       char *text                     = (mci->text  ? *mci->text  : NULL);
1016       char *input                    = (mci->input ? *mci->input : NULL);
1017       int button_graphic             = mci->button_graphic;
1018       int font_text                  = (pos_text  ? pos_text->font  : -1);
1019       int font_input                 = (pos_input ? pos_input->font : -1);
1020
1021       if (active_text)
1022       {
1023         button_graphic = BUTTON_ACTIVE(button_graphic);
1024         font_text = FONT_ACTIVE(font_text);
1025       }
1026
1027       if (active_input)
1028       {
1029         font_input = FONT_ACTIVE(font_input);
1030       }
1031
1032       if (visibleMenuPos(pos_button))
1033       {
1034         struct MenuPosInfo *pos = pos_button;
1035         int x = mSX + pos->x;
1036         int y = mSY + pos->y;
1037
1038         DrawBackgroundForGraphic(x, y, pos->width, pos->height, button_graphic);
1039         DrawFixedGraphicThruMaskExt(drawto, x, y, button_graphic, 0);
1040       }
1041
1042       if (visibleTextPos(pos_text) && text != NULL)
1043       {
1044         struct TextPosInfo *pos = pos_text;
1045         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1046         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1047
1048 #if 1
1049         /* (check why/if this is needed) */
1050         DrawBackgroundForFont(x, y, pos->width, pos->height, font_text);
1051 #endif
1052         DrawText(x, y, text, font_text);
1053       }
1054
1055       if (visibleTextPos(pos_input) && input != NULL)
1056       {
1057         struct TextPosInfo *pos = pos_input;
1058         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1059         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1060
1061 #if 1
1062         /* (check why/if this is needed) */
1063         DrawBackgroundForFont(x, y, pos->width, pos->height, font_input);
1064 #endif
1065         DrawText(x, y, input, font_input);
1066       }
1067     }
1068   }
1069 }
1070
1071 static void DrawCursorAndText_Main(int nr, boolean active_text)
1072 {
1073   DrawCursorAndText_Main_Ext(nr, active_text, FALSE);
1074 }
1075
1076 #if 0
1077 static void DrawCursorAndText_Main_Input(int nr, boolean active_text)
1078 {
1079   DrawCursorAndText_Main_Ext(nr, active_text, TRUE);
1080 }
1081 #endif
1082
1083 static struct MainControlInfo *getMainControlInfo(int nr)
1084 {
1085   int i;
1086
1087   for (i = 0; main_controls[i].nr != -1; i++)
1088     if (main_controls[i].nr == nr)
1089       return &main_controls[i];
1090
1091   return NULL;
1092 }
1093
1094 static boolean insideMenuPosRect(struct MenuPosInfo *rect, int x, int y)
1095 {
1096   if (rect == NULL)
1097     return FALSE;
1098
1099   int rect_x = ALIGNED_TEXT_XPOS(rect);
1100   int rect_y = ALIGNED_TEXT_YPOS(rect);
1101
1102   return (x >= rect_x && x < rect_x + rect->width &&
1103           y >= rect_y && y < rect_y + rect->height);
1104 }
1105
1106 static boolean insideTextPosRect(struct TextPosInfo *rect, int x, int y)
1107 {
1108   if (rect == NULL)
1109     return FALSE;
1110
1111   int rect_x = ALIGNED_TEXT_XPOS(rect);
1112   int rect_y = ALIGNED_TEXT_YPOS(rect);
1113
1114 #if 0
1115   printf("::: insideTextPosRect: (%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d\n",
1116          x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1117          (x >= rect_x && x < rect_x + rect->width &&
1118           y >= rect_y && y < rect_y + rect->height));
1119 #endif
1120
1121   return (x >= rect_x && x < rect_x + rect->width &&
1122           y >= rect_y && y < rect_y + rect->height);
1123 }
1124
1125 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1126 {
1127   static int cursor_array[MAX_LEV_FIELDY];
1128   int x = mSX + TILEX * xpos;
1129   int y = mSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1130
1131   if (xpos == 0)
1132   {
1133     if (graphic != -1)
1134       cursor_array[ypos] = graphic;
1135     else
1136       graphic = cursor_array[ypos];
1137   }
1138
1139   if (active)
1140     graphic = BUTTON_ACTIVE(graphic);
1141
1142   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1143   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1144 }
1145
1146 static void initCursor(int ypos, int graphic)
1147 {
1148   drawCursorExt(0, ypos, FALSE, graphic);
1149 }
1150
1151 static void drawCursor(int ypos, boolean active)
1152 {
1153   drawCursorExt(0, ypos, active, -1);
1154 }
1155
1156 static void drawCursorXY(int xpos, int ypos, int graphic)
1157 {
1158   drawCursorExt(xpos, ypos, FALSE, graphic);
1159 }
1160
1161 static void drawChooseTreeCursor(int ypos, boolean active)
1162 {
1163   int last_game_status = game_status;   /* save current game status */
1164
1165   drawCursorExt(0, ypos, active, -1);
1166
1167   game_status = last_game_status;       /* restore current game status */
1168 }
1169
1170 void DrawHeadline()
1171 {
1172   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, PROGRAM_TITLE_STRING);
1173   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, PROGRAM_COPYRIGHT_STRING);
1174 }
1175
1176 int effectiveGameStatus()
1177 {
1178   if (game_status == GAME_MODE_INFO && info_mode == INFO_MODE_TITLE)
1179     return GAME_MODE_TITLE;
1180
1181   return game_status;
1182 }
1183
1184 void DrawTitleScreenImage(int nr, boolean initial)
1185 {
1186   int graphic = getTitleScreenGraphic(nr, initial);
1187   Bitmap *bitmap = graphic_info[graphic].bitmap;
1188   int width  = graphic_info[graphic].width;
1189   int height = graphic_info[graphic].height;
1190   int src_x = graphic_info[graphic].src_x;
1191   int src_y = graphic_info[graphic].src_y;
1192   int dst_x, dst_y;
1193
1194   if (bitmap == NULL)
1195     return;
1196
1197   if (width > WIN_XSIZE)
1198   {
1199     /* image width too large for window => center image horizontally */
1200     src_x = (width - WIN_XSIZE) / 2;
1201     width = WIN_XSIZE;
1202   }
1203
1204   if (height > WIN_YSIZE)
1205   {
1206     /* image height too large for window => center image vertically */
1207     src_y = (height - WIN_YSIZE) / 2;
1208     height = WIN_YSIZE;
1209   }
1210
1211   /* always display title screens centered */
1212   dst_x = (WIN_XSIZE - width) / 2;
1213   dst_y = (WIN_YSIZE - height) / 2;
1214
1215   SetDrawBackgroundMask(REDRAW_ALL);
1216   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1217
1218   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1219
1220   if (DrawingOnBackground(dst_x, dst_y))
1221   {
1222     SetClipOrigin(bitmap, bitmap->stored_clip_gc, dst_x - src_x, dst_y - src_y);
1223     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1224   }
1225   else
1226     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1227
1228   redraw_mask = REDRAW_ALL;
1229 }
1230
1231 void DrawTitleScreenMessage(int nr, boolean initial)
1232 {
1233   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1234   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1235   int last_game_status = game_status;   /* save current game status */
1236
1237   if (filename == NULL)
1238     return;
1239
1240   /* force TITLE font on title message screen */
1241   game_status = getTitleMessageGameMode(initial);
1242
1243   /* if chars set to "-1", automatically determine by text and font width */
1244   if (tmi->chars == -1)
1245     tmi->chars = tmi->width / getFontWidth(tmi->font);
1246   else
1247     tmi->width = tmi->chars * getFontWidth(tmi->font);
1248
1249   /* if lines set to "-1", automatically determine by text and font height */
1250   if (tmi->lines == -1)
1251     tmi->lines = tmi->height / getFontHeight(tmi->font);
1252   else
1253     tmi->height = tmi->lines * getFontHeight(tmi->font);
1254
1255   SetDrawBackgroundMask(REDRAW_ALL);
1256   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1257
1258   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1259
1260   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1261                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1262                tmi->autowrap, tmi->centered, tmi->parse_comments);
1263
1264   game_status = last_game_status;       /* restore current game status */
1265 }
1266
1267 void DrawTitleScreen()
1268 {
1269   KeyboardAutoRepeatOff();
1270
1271   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1272
1273   StopAnimation();
1274 }
1275
1276 boolean CheckTitleScreen(boolean levelset_has_changed)
1277 {
1278   static boolean show_title_initial = TRUE;
1279   boolean show_titlescreen = FALSE;
1280
1281   /* needed to be able to skip title screen, if no image or message defined */
1282   InitializeTitleControls(show_title_initial);
1283
1284   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1285     show_titlescreen = TRUE;
1286
1287   /* show initial title images and messages only once at program start */
1288   show_title_initial = FALSE;
1289
1290   return (show_titlescreen && num_title_screens > 0);
1291 }
1292
1293 void DrawMainMenuExt(int fade_mask, boolean do_fading)
1294 {
1295   static LevelDirTree *leveldir_last_valid = NULL;
1296   boolean levelset_has_changed = FALSE;
1297
1298   LimitScreenUpdates(FALSE);
1299
1300   FadeSetLeaveScreen();
1301
1302   /* do not fade out here -- function may continue and fade on editor screen */
1303
1304   UnmapAllGadgets();
1305   FadeSoundsAndMusic();
1306
1307   KeyboardAutoRepeatOn();
1308   ActivateJoystick();
1309
1310   SetDrawDeactivationMask(REDRAW_NONE);
1311   SetDrawBackgroundMask(REDRAW_FIELD);
1312
1313   audio.sound_deactivated = FALSE;
1314
1315   GetPlayerConfig();
1316
1317   /* needed if last screen was the playing screen, invoked from level editor */
1318   if (level_editor_test_game)
1319   {
1320     game_status = GAME_MODE_EDITOR;
1321     DrawLevelEd();
1322
1323     return;
1324   }
1325
1326   /* needed if last screen was the setup screen and fullscreen state changed */
1327   // (moved to "execSetupGraphics()" to change fullscreen state directly)
1328   // ToggleFullscreenOrChangeWindowScalingIfNeeded();
1329
1330   /* leveldir_current may be invalid (level group, parent link) */
1331   if (!validLevelSeries(leveldir_current))
1332     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1333
1334   if (leveldir_current != leveldir_last_valid)
1335     levelset_has_changed = TRUE;
1336
1337   /* store valid level series information */
1338   leveldir_last_valid = leveldir_current;
1339
1340   init_last = init;                     /* switch to new busy animation */
1341
1342   /* needed if last screen (level choice) changed graphics, sounds or music */
1343   ReloadCustomArtwork(0);
1344
1345   if (redraw_mask & REDRAW_ALL)
1346     fade_mask = REDRAW_ALL;
1347
1348   FadeOut(fade_mask);
1349
1350   /* needed if different viewport properties defined for menues */
1351   ChangeViewportPropertiesIfNeeded();
1352
1353   /* needed if last screen was the editor screen */
1354   UndrawSpecialEditorDoor();
1355
1356   SetDrawtoField(DRAW_BACKBUFFER);
1357
1358   if (CheckTitleScreen(levelset_has_changed))
1359   {
1360     game_status = GAME_MODE_TITLE;
1361
1362     DrawTitleScreen();
1363
1364     return;
1365   }
1366
1367   /* level_nr may have been set to value over handicap with level editor */
1368   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1369     level_nr = leveldir_current->handicap_level;
1370
1371   LoadLevel(level_nr);
1372   LoadScore(level_nr);
1373
1374   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1375
1376   if (fade_mask == REDRAW_ALL)
1377     RedrawBackground();
1378
1379   ClearField();
1380
1381   InitializeMainControls();
1382
1383   DrawCursorAndText_Main(-1, FALSE);
1384   DrawPreviewLevelInitial();
1385
1386   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1387
1388   TapeStop();
1389   if (TAPE_IS_EMPTY(tape))
1390     LoadTape(level_nr);
1391   DrawCompleteVideoDisplay();
1392
1393   PlayMenuSound();
1394   PlayMenuMusic();
1395
1396   /* create gadgets for main menu screen */
1397   FreeScreenGadgets();
1398   CreateScreenGadgets();
1399
1400   /* map gadgets for main menu screen */
1401   MapTapeButtons();
1402   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1403
1404   /* copy actual game door content to door double buffer for OpenDoor() */
1405   BlitBitmap(drawto, bitmap_db_door_2, VX, VY, VXSIZE, VYSIZE, 0, 0);
1406
1407   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1408
1409   DrawMaskedBorder(REDRAW_ALL);
1410
1411   FadeIn(fade_mask);
1412   FadeSetEnterMenu();
1413
1414   /* update screen area with special editor door */
1415   redraw_mask |= REDRAW_ALL;
1416   BackToFront();
1417
1418   SetMouseCursor(CURSOR_DEFAULT);
1419
1420   InitAnimation();
1421
1422   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1423 }
1424
1425 void DrawAndFadeInMainMenu(int fade_mask)
1426 {
1427   DrawMainMenuExt(fade_mask, TRUE);
1428 }
1429
1430 void DrawMainMenu()
1431 {
1432   DrawMainMenuExt(REDRAW_ALL, FALSE);
1433 }
1434
1435 #if defined(CREATE_SPECIAL_EDITION_RND_JUE)
1436 static void gotoTopLevelDir()
1437 {
1438   /* move upwards to top level directory */
1439   while (leveldir_current->node_parent)
1440   {
1441     /* write a "path" into level tree for easy navigation to last level */
1442     if (leveldir_current->node_parent->node_group->cl_first == -1)
1443     {
1444       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1445       int leveldir_pos = posTreeInfo(leveldir_current);
1446       int num_page_entries;
1447       int cl_first, cl_cursor;
1448
1449       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
1450         num_page_entries = num_leveldirs;
1451       else
1452         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1453
1454       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1455       cl_cursor = leveldir_pos - cl_first;
1456
1457       leveldir_current->node_parent->node_group->cl_first = cl_first;
1458       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1459     }
1460
1461     leveldir_current = leveldir_current->node_parent;
1462   }
1463 }
1464 #endif
1465
1466 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1467 {
1468   static unsigned int title_delay = 0;
1469   static int title_screen_nr = 0;
1470   static int last_sound = -1, last_music = -1;
1471   boolean return_to_main_menu = FALSE;
1472   boolean use_fading_main_menu = TRUE;
1473   struct TitleControlInfo *tci;
1474   struct TitleFadingInfo fading_default;
1475   struct TitleFadingInfo fading_last = fading;
1476   struct TitleFadingInfo fading_next;
1477   int sound, music;
1478
1479   if (button == MB_MENU_INITIALIZE)
1480   {
1481     title_delay = 0;
1482     title_screen_nr = 0;
1483     tci = &title_controls[title_screen_nr];
1484
1485     last_sound = SND_UNDEFINED;
1486     last_music = MUS_UNDEFINED;
1487
1488     if (game_status == GAME_MODE_INFO)
1489     {
1490       if (num_title_screens == 0)
1491       {
1492         DrawInfoScreen_NotAvailable("Title screen information:",
1493                                     "No title screen for this level set.");
1494
1495         return;
1496       }
1497
1498       FadeSoundsAndMusic();
1499
1500       FadeOut(REDRAW_ALL);
1501     }
1502
1503     if (tci->is_image)
1504       DrawTitleScreenImage(tci->local_nr, tci->initial);
1505     else
1506       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1507
1508     fading_default = (tci->initial ? title_initial_default : title_default);
1509
1510     fading = fading_next = getTitleFading(tci);
1511
1512     if (!(fading_last.fade_mode & FADE_TYPE_TRANSFORM) &&
1513         fading_next.fade_mode & FADE_TYPE_TRANSFORM)
1514     {
1515       fading.fade_mode = FADE_MODE_FADE;
1516       fading.fade_delay = fading_default.fade_delay;
1517     }
1518
1519     sound = getTitleSound(tci);
1520     music = getTitleMusic(tci);
1521
1522     if (sound != last_sound)
1523       PlayMenuSoundExt(sound);
1524     if (music != last_music)
1525       PlayMenuMusicExt(music);
1526
1527     last_sound = sound;
1528     last_music = music;
1529
1530     SetMouseCursor(CURSOR_NONE);
1531
1532     FadeIn(REDRAW_ALL);
1533
1534     fading = fading_next;
1535
1536     DelayReached(&title_delay, 0);      /* reset delay counter */
1537
1538     return;
1539   }
1540
1541   if (fading.auto_delay > 0 && DelayReached(&title_delay, fading.auto_delay))
1542     button = MB_MENU_CHOICE;
1543
1544   if (button == MB_MENU_LEAVE)
1545   {
1546     return_to_main_menu = TRUE;
1547     use_fading_main_menu = FALSE;
1548   }
1549   else if (button == MB_MENU_CHOICE)
1550   {
1551     if (game_status == GAME_MODE_INFO && num_title_screens == 0)
1552     {
1553       FadeSetEnterScreen();
1554
1555       info_mode = INFO_MODE_MAIN;
1556       DrawAndFadeInInfoScreen(REDRAW_FIELD);
1557
1558       return;
1559     }
1560
1561     title_screen_nr++;
1562     tci = &title_controls[title_screen_nr];
1563
1564     if (title_screen_nr < num_title_screens)
1565     {
1566       sound = getTitleSound(tci);
1567       music = getTitleMusic(tci);
1568
1569       if (sound == SND_UNDEFINED || sound != last_sound)
1570         FadeSounds();
1571       if (music == MUS_UNDEFINED || music != last_music)
1572         FadeMusic();
1573
1574       FadeOut(REDRAW_ALL);
1575
1576       if (tci->is_image)
1577         DrawTitleScreenImage(tci->local_nr, tci->initial);
1578       else
1579         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1580
1581       fading_next = getTitleFading(tci);
1582
1583       sound = getTitleSound(tci);
1584       music = getTitleMusic(tci);
1585
1586       if (sound != last_sound)
1587         PlayMenuSoundExt(sound);
1588       if (music != last_music)
1589         PlayMenuMusicExt(music);
1590
1591       last_sound = sound;
1592       last_music = music;
1593
1594       /* last screen already faded out, next screen has no animation */
1595       if (!(fading.fade_mode & FADE_TYPE_TRANSFORM) &&
1596           fading_next.fade_mode == FADE_MODE_NONE)
1597         fading = fading_next;
1598
1599       FadeIn(REDRAW_ALL);
1600
1601       fading = fading_next;
1602
1603       DelayReached(&title_delay, 0);    /* reset delay counter */
1604     }
1605     else
1606     {
1607       FadeSoundsAndMusic();
1608
1609       return_to_main_menu = TRUE;
1610     }
1611   }
1612
1613   if (return_to_main_menu)
1614   {
1615     SetMouseCursor(CURSOR_DEFAULT);
1616
1617     if (game_status == GAME_MODE_INFO)
1618     {
1619       info_mode = INFO_MODE_MAIN;
1620
1621       DrawInfoScreenExt(REDRAW_ALL, use_fading_main_menu);
1622     }
1623     else        /* default: return to main menu */
1624     {
1625       game_status = GAME_MODE_MAIN;
1626
1627       DrawMainMenuExt(REDRAW_ALL, use_fading_main_menu);
1628     }
1629   }
1630 }
1631
1632 void HandleMainMenu_SelectLevel(int step, int direction)
1633 {
1634   int old_level_nr = level_nr;
1635   int new_level_nr;
1636
1637   new_level_nr = old_level_nr + step * direction;
1638   if (new_level_nr < leveldir_current->first_level)
1639     new_level_nr = leveldir_current->first_level;
1640   if (new_level_nr > leveldir_current->last_level)
1641     new_level_nr = leveldir_current->last_level;
1642
1643   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1644   {
1645     /* skipping levels is only allowed when trying to skip single level */
1646     if (setup.skip_levels && step == 1 &&
1647         Request("Level still unsolved! Skip despite handicap?", REQ_ASK))
1648     {
1649       leveldir_current->handicap_level++;
1650       SaveLevelSetup_SeriesInfo();
1651     }
1652
1653     new_level_nr = leveldir_current->handicap_level;
1654   }
1655
1656   if (new_level_nr != old_level_nr)
1657   {
1658     struct MainControlInfo *mci= getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1659
1660     PlaySound(SND_MENU_ITEM_SELECTING);
1661
1662     level_nr = new_level_nr;
1663
1664     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1665              int2str(level_nr, menu.main.text.level_number.size),
1666              mci->pos_text->font);
1667
1668     LoadLevel(level_nr);
1669     DrawPreviewLevelInitial();
1670
1671     TapeErase();
1672     LoadTape(level_nr);
1673     DrawCompleteVideoDisplay();
1674
1675     /* needed because DrawPreviewLevelInitial() takes some time */
1676     BackToFront();
1677     SyncDisplay();
1678   }
1679 }
1680
1681 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1682 {
1683   static int choice = MAIN_CONTROL_GAME;
1684   int pos = choice;
1685   int i;
1686
1687   if (button == MB_MENU_INITIALIZE)
1688   {
1689     DrawCursorAndText_Main(choice, TRUE);
1690
1691     return;
1692   }
1693
1694   if (mx || my)         /* mouse input */
1695   {
1696     pos = -1;
1697
1698     for (i = 0; main_controls[i].nr != -1; i++)
1699     {
1700       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1701           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1702           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1703       {
1704         pos = main_controls[i].nr;
1705
1706         break;
1707       }
1708     }
1709   }
1710   else if (dx || dy)    /* keyboard input */
1711   {
1712     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
1713                    choice == MAIN_CONTROL_SETUP))
1714       button = MB_MENU_CHOICE;
1715     else if (dy)
1716       pos = choice + dy;
1717   }
1718
1719   if (pos == MAIN_CONTROL_LEVELS && dx != 0 && button)
1720   {
1721     HandleMainMenu_SelectLevel(1, dx < 0 ? -1 : +1);
1722   }
1723   else if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
1724   {
1725     HandleMainMenu_SelectLevel(MAX_LEVELS, -1);
1726   }
1727   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
1728   {
1729     HandleMainMenu_SelectLevel(MAX_LEVELS, +1);
1730   }
1731   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
1732   {
1733     game_status = GAME_MODE_LEVELNR;
1734
1735     ChangeViewportPropertiesIfNeeded();
1736
1737     DrawChooseLevelNr();
1738   }
1739   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
1740   {
1741     if (button)
1742     {
1743       if (pos != choice)
1744       {
1745         PlaySound(SND_MENU_ITEM_ACTIVATING);
1746
1747         DrawCursorAndText_Main(choice, FALSE);
1748         DrawCursorAndText_Main(pos, TRUE);
1749
1750         choice = pos;
1751       }
1752     }
1753     else
1754     {
1755       PlaySound(SND_MENU_ITEM_SELECTING);
1756
1757       if (pos == MAIN_CONTROL_NAME)
1758       {
1759         game_status = GAME_MODE_PSEUDO_TYPENAME;
1760
1761         HandleTypeName(strlen(setup.player_name), 0);
1762       }
1763       else if (pos == MAIN_CONTROL_LEVELS)
1764       {
1765         if (leveldir_first)
1766         {
1767           game_status = GAME_MODE_LEVELS;
1768
1769           SaveLevelSetup_LastSeries();
1770           SaveLevelSetup_SeriesInfo();
1771
1772 #if defined(CREATE_SPECIAL_EDITION_RND_JUE)
1773           gotoTopLevelDir();
1774 #endif
1775
1776           ChangeViewportPropertiesIfNeeded();
1777
1778           DrawChooseLevelSet();
1779         }
1780       }
1781       else if (pos == MAIN_CONTROL_SCORES)
1782       {
1783         game_status = GAME_MODE_SCORES;
1784
1785         ChangeViewportPropertiesIfNeeded();
1786
1787         DrawHallOfFame(-1);
1788       }
1789       else if (pos == MAIN_CONTROL_EDITOR)
1790       {
1791         if (leveldir_current->readonly &&
1792             !strEqual(setup.player_name, "Artsoft"))
1793           Request("This level is read only!", REQ_CONFIRM);
1794
1795         CloseDoor(DOOR_CLOSE_2);
1796
1797         game_status = GAME_MODE_EDITOR;
1798
1799         FadeSetEnterScreen();
1800
1801         DrawLevelEd();
1802       }
1803       else if (pos == MAIN_CONTROL_INFO)
1804       {
1805         game_status = GAME_MODE_INFO;
1806         info_mode = INFO_MODE_MAIN;
1807
1808         ChangeViewportPropertiesIfNeeded();
1809
1810         DrawInfoScreen();
1811       }
1812       else if (pos == MAIN_CONTROL_GAME)
1813       {
1814         StartGameActions(options.network, setup.autorecord, level.random_seed);
1815       }
1816       else if (pos == MAIN_CONTROL_SETUP)
1817       {
1818         game_status = GAME_MODE_SETUP;
1819         setup_mode = SETUP_MODE_MAIN;
1820
1821         ChangeViewportPropertiesIfNeeded();
1822
1823         DrawSetupScreen();
1824       }
1825       else if (pos == MAIN_CONTROL_QUIT)
1826       {
1827         SaveLevelSetup_LastSeries();
1828         SaveLevelSetup_SeriesInfo();
1829
1830         if (Request("Do you really want to quit?", REQ_ASK | REQ_STAY_CLOSED))
1831           game_status = GAME_MODE_QUIT;
1832       }
1833     }
1834   }
1835 }
1836
1837
1838 /* ========================================================================= */
1839 /* info screen functions                                                     */
1840 /* ========================================================================= */
1841
1842 static struct TokenInfo *info_info;
1843 static int num_info_info;
1844
1845 static void execInfoTitleScreen()
1846 {
1847   info_mode = INFO_MODE_TITLE;
1848
1849   DrawInfoScreen();
1850 }
1851
1852 static void execInfoElements()
1853 {
1854   info_mode = INFO_MODE_ELEMENTS;
1855
1856   DrawInfoScreen();
1857 }
1858
1859 static void execInfoMusic()
1860 {
1861   info_mode = INFO_MODE_MUSIC;
1862
1863   DrawInfoScreen();
1864 }
1865
1866 static void execInfoCredits()
1867 {
1868   info_mode = INFO_MODE_CREDITS;
1869
1870   DrawInfoScreen();
1871 }
1872
1873 static void execInfoProgram()
1874 {
1875   info_mode = INFO_MODE_PROGRAM;
1876
1877   DrawInfoScreen();
1878 }
1879
1880 static void execInfoVersion()
1881 {
1882   info_mode = INFO_MODE_VERSION;
1883
1884   DrawInfoScreen();
1885 }
1886
1887 static void execInfoLevelSet()
1888 {
1889   info_mode = INFO_MODE_LEVELSET;
1890
1891   DrawInfoScreen();
1892 }
1893
1894 static void execExitInfo()
1895 {
1896   game_status = GAME_MODE_MAIN;
1897
1898   DrawMainMenuExt(REDRAW_FIELD, FALSE);
1899 }
1900
1901 static struct TokenInfo info_info_main[] =
1902 {
1903   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
1904   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
1905   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
1906   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
1907   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
1908   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
1909   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
1910   { TYPE_EMPTY,         NULL,                   ""                      },
1911   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
1912
1913   { 0,                  NULL,                   NULL                    }
1914 };
1915
1916 static void DrawCursorAndText_Info(int pos, boolean active)
1917 {
1918   int xpos = MENU_SCREEN_START_XPOS;
1919   int ypos = MENU_SCREEN_START_YPOS + pos;
1920   int font_nr = FONT_MENU_1;
1921
1922   if (active)
1923     font_nr = FONT_ACTIVE(font_nr);
1924
1925   DrawText(mSX + xpos * 32, mSY + ypos * 32, info_info[pos].text, font_nr);
1926
1927   if (info_info[pos].type & ~TYPE_SKIP_ENTRY)
1928     drawCursor(pos, active);
1929 }
1930
1931 static void DrawInfoScreen_Main(int fade_mask, boolean do_fading)
1932 {
1933   int i;
1934
1935   UnmapAllGadgets();
1936   CloseDoor(DOOR_CLOSE_2);
1937
1938   /* (needed after displaying title screens which disable auto repeat) */
1939   KeyboardAutoRepeatOn();
1940
1941   FadeSetLeaveScreen();
1942
1943   FadeOut(fade_mask);
1944
1945   if (fade_mask == REDRAW_ALL)
1946   {
1947     RedrawBackground();
1948
1949     OpenDoor(DOOR_CLOSE_1 | DOOR_CLOSE_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1950   }
1951
1952   ClearField();
1953
1954   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
1955
1956   info_info = info_info_main;
1957   num_info_info = 0;
1958
1959   for (i = 0; info_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
1960   {
1961     if (info_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
1962       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
1963     else if (info_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
1964       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
1965     else if (info_info[i].type & ~TYPE_SKIP_ENTRY)
1966       initCursor(i, IMG_MENU_BUTTON);
1967
1968     DrawCursorAndText_Info(i, FALSE);
1969
1970     num_info_info++;
1971   }
1972
1973   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
1974
1975   PlayMenuSound();
1976   PlayMenuMusic();
1977
1978   DrawMaskedBorder(fade_mask);
1979
1980   FadeIn(fade_mask);
1981
1982   InitAnimation();
1983 }
1984
1985 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
1986 {
1987   static int choice_store[MAX_INFO_MODES];
1988   int choice = choice_store[info_mode];         /* always starts with 0 */
1989   int x = 0;
1990   int y = choice;
1991
1992   if (button == MB_MENU_INITIALIZE)
1993   {
1994     /* advance to first valid menu entry */
1995     while (choice < num_info_info &&
1996            info_info[choice].type & TYPE_SKIP_ENTRY)
1997       choice++;
1998     choice_store[info_mode] = choice;
1999
2000     DrawCursorAndText_Info(choice, TRUE);
2001
2002     return;
2003   }
2004   else if (button == MB_MENU_LEAVE)
2005   {
2006     for (y = 0; y < num_info_info; y++)
2007     {
2008       if (info_info[y].type & TYPE_LEAVE_MENU)
2009       {
2010         void (*menu_callback_function)(void) = info_info[y].value;
2011
2012         FadeSetLeaveMenu();
2013
2014         menu_callback_function();
2015
2016         break;  /* absolutely needed because function changes 'info_info'! */
2017       }
2018     }
2019
2020     return;
2021   }
2022
2023   if (mx || my)         /* mouse input */
2024   {
2025     x = (mx - mSX) / 32;
2026     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2027   }
2028   else if (dx || dy)    /* keyboard input */
2029   {
2030     if (dx)
2031     {
2032       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2033
2034       if (info_info[choice].type & menu_navigation_type ||
2035           info_info[choice].type & TYPE_ENTER_SCREEN ||
2036           info_info[choice].type & TYPE_BOOLEAN_STYLE ||
2037           info_info[choice].type & TYPE_YES_NO_AUTO)
2038         button = MB_MENU_CHOICE;
2039     }
2040     else if (dy)
2041       y = choice + dy;
2042
2043     /* jump to next non-empty menu entry (up or down) */
2044     while (y > 0 && y < num_info_info - 1 &&
2045            info_info[y].type & TYPE_SKIP_ENTRY)
2046       y += dy;
2047   }
2048
2049   if (IN_VIS_FIELD(x, y) &&
2050       y >= 0 && y < num_info_info && info_info[y].type & ~TYPE_SKIP_ENTRY)
2051   {
2052     if (button)
2053     {
2054       if (y != choice)
2055       {
2056         PlaySound(SND_MENU_ITEM_ACTIVATING);
2057
2058         DrawCursorAndText_Info(choice, FALSE);
2059         DrawCursorAndText_Info(y, TRUE);
2060
2061         choice = choice_store[info_mode] = y;
2062       }
2063     }
2064     else if (!(info_info[y].type & TYPE_GHOSTED))
2065     {
2066       PlaySound(SND_MENU_ITEM_SELECTING);
2067
2068       if (info_info[y].type & TYPE_ENTER_OR_LEAVE)
2069       {
2070         void (*menu_callback_function)(void) = info_info[choice].value;
2071
2072         FadeSetFromType(info_info[y].type);
2073
2074         menu_callback_function();
2075       }
2076     }
2077   }
2078 }
2079
2080 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2081 {
2082   int ystart1 = mSY - SY + 100;
2083   int ystart2 = mSY - SY + 150;
2084   int ybottom = mSY - SY + SYSIZE - 20;
2085
2086   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
2087
2088   FadeOut(REDRAW_FIELD);
2089
2090   ClearField();
2091   DrawHeadline();
2092
2093   DrawTextSCentered(ystart1, FONT_TEXT_1, text_title);
2094   DrawTextSCentered(ystart2, FONT_TEXT_2, text_error);
2095
2096   DrawTextSCentered(ybottom, FONT_TEXT_4,
2097                     "Press any key or button for info menu");
2098
2099   FadeIn(REDRAW_FIELD);
2100 }
2101
2102 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2103 {
2104   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2105   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2106   int xstart = mSX + MENU_SCREEN_INFO_XSTART;
2107   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2108   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2109   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2110   int ystep = MENU_SCREEN_INFO_YSTEP;
2111   int element, action, direction;
2112   int graphic;
2113   int delay;
2114   int sync_frame;
2115   int i, j;
2116
2117   if (init)
2118   {
2119     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2120       infoscreen_step[i] = infoscreen_frame[i] = 0;
2121
2122     ClearField();
2123     DrawHeadline();
2124
2125     DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Elements:");
2126
2127     DrawTextSCentered(ybottom, FONT_TEXT_4,
2128                       "Press any key or button for next page");
2129
2130     FrameCounter = 0;
2131   }
2132
2133   i = j = 0;
2134   while (helpanim_info[j].element != HELPANIM_LIST_END)
2135   {
2136     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2137         i >= max_anims)
2138       break;
2139     else if (i < start)
2140     {
2141       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2142         j++;
2143
2144       j++;
2145       i++;
2146
2147       continue;
2148     }
2149
2150     j += infoscreen_step[i - start];
2151
2152     element = helpanim_info[j].element;
2153     action = helpanim_info[j].action;
2154     direction = helpanim_info[j].direction;
2155
2156     if (element < 0)
2157       element = EL_UNKNOWN;
2158
2159     if (action != -1 && direction != -1)
2160       graphic = el_act_dir2img(element, action, direction);
2161     else if (action != -1)
2162       graphic = el_act2img(element, action);
2163     else if (direction != -1)
2164       graphic = el_dir2img(element, direction);
2165     else
2166       graphic = el2img(element);
2167
2168     delay = helpanim_info[j++].delay;
2169
2170     if (delay == -1)
2171       delay = 1000000;
2172
2173     if (infoscreen_frame[i - start] == 0)
2174     {
2175       sync_frame = 0;
2176       infoscreen_frame[i - start] = delay - 1;
2177     }
2178     else
2179     {
2180       sync_frame = delay - infoscreen_frame[i - start];
2181       infoscreen_frame[i - start]--;
2182     }
2183
2184     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2185     {
2186       if (!infoscreen_frame[i - start])
2187         infoscreen_step[i - start] = 0;
2188     }
2189     else
2190     {
2191       if (!infoscreen_frame[i - start])
2192         infoscreen_step[i - start]++;
2193       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2194         j++;
2195     }
2196
2197     j++;
2198
2199     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2200                                TILEX, TILEY);
2201     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2202                                  graphic, sync_frame, USE_MASKING);
2203
2204     if (init)
2205       DrawInfoScreen_HelpText(element, action, direction, i - start);
2206
2207     i++;
2208   }
2209
2210   redraw_mask |= REDRAW_FIELD;
2211
2212   FrameCounter++;
2213 }
2214
2215 static char *getHelpText(int element, int action, int direction)
2216 {
2217   char token[MAX_LINE_LEN];
2218
2219   strcpy(token, element_info[element].token_name);
2220
2221   if (action != -1)
2222     strcat(token, element_action_info[action].suffix);
2223
2224   if (direction != -1)
2225     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2226
2227   return getHashEntry(helptext_info, token);
2228 }
2229
2230 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2231 {
2232   int font_nr = FONT_INFO_ELEMENTS;
2233   int font_width = getFontWidth(font_nr);
2234   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
2235   int sy = mSY + 65 + 2 * 32 + 1;
2236   int ystep = TILEY + 4;
2237   int pad_x = sx - SX;
2238   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
2239   int max_lines_per_text = 2;    
2240   char *text = NULL;
2241
2242   if (action != -1 && direction != -1)          /* element.action.direction */
2243     text = getHelpText(element, action, direction);
2244
2245   if (text == NULL && action != -1)             /* element.action */
2246     text = getHelpText(element, action, -1);
2247
2248   if (text == NULL && direction != -1)          /* element.direction */
2249     text = getHelpText(element, -1, direction);
2250
2251   if (text == NULL)                             /* base element */
2252     text = getHelpText(element, -1, -1);
2253
2254   if (text == NULL)                             /* not found */
2255     text = "No description available";
2256
2257   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2258     sy += getFontHeight(font_nr) / 2;
2259
2260   DrawTextBuffer(sx, sy + ypos * ystep, text, font_nr,
2261                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2262                  TRUE, FALSE, FALSE);
2263 }
2264
2265 void DrawInfoScreen_TitleScreen()
2266 {
2267   DrawTitleScreen();
2268 }
2269
2270 void HandleInfoScreen_TitleScreen(int button)
2271 {
2272   HandleTitleScreen(0, 0, 0, 0, button);
2273 }
2274
2275 void DrawInfoScreen_Elements()
2276 {
2277   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2278
2279   FadeOut(REDRAW_FIELD);
2280
2281   LoadHelpAnimInfo();
2282   LoadHelpTextInfo();
2283
2284   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2285
2286   FadeIn(REDRAW_FIELD);
2287
2288   InitAnimation();
2289 }
2290
2291 void HandleInfoScreen_Elements(int button)
2292 {
2293   static unsigned int info_delay = 0;
2294   static int num_anims;
2295   static int num_pages;
2296   static int page;
2297   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
2298   int i;
2299
2300   if (button == MB_MENU_INITIALIZE)
2301   {
2302     boolean new_element = TRUE;
2303
2304     num_anims = 0;
2305
2306     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2307     {
2308       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2309         new_element = TRUE;
2310       else if (new_element)
2311       {
2312         num_anims++;
2313         new_element = FALSE;
2314       }
2315     }
2316
2317     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
2318     page = 0;
2319   }
2320
2321   if (button == MB_MENU_LEAVE)
2322   {
2323     PlaySound(SND_MENU_ITEM_SELECTING);
2324
2325     info_mode = INFO_MODE_MAIN;
2326     DrawInfoScreen();
2327
2328     return;
2329   }
2330   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2331   {
2332     if (button != MB_MENU_INITIALIZE)
2333     {
2334       PlaySound(SND_MENU_ITEM_SELECTING);
2335
2336       page++;
2337     }
2338
2339     if (page >= num_pages)
2340     {
2341       FadeSoundsAndMusic();
2342
2343       info_mode = INFO_MODE_MAIN;
2344       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2345
2346       return;
2347     }
2348
2349     if (page > 0)
2350       FadeSetNextScreen();
2351
2352     if (button != MB_MENU_INITIALIZE)
2353       FadeOut(REDRAW_FIELD);
2354
2355     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
2356
2357     if (button != MB_MENU_INITIALIZE)
2358       FadeIn(REDRAW_FIELD);
2359   }
2360   else
2361   {
2362     if (DelayReached(&info_delay, GameFrameDelay))
2363       if (page < num_pages)
2364         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
2365
2366     PlayMenuSoundIfLoop();
2367   }
2368 }
2369
2370 void DrawInfoScreen_Music()
2371 {
2372   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
2373
2374   FadeOut(REDRAW_FIELD);
2375
2376   ClearField();
2377   DrawHeadline();
2378
2379   LoadMusicInfo();
2380
2381   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
2382
2383   FadeIn(REDRAW_FIELD);
2384 }
2385
2386 void HandleInfoScreen_Music(int button)
2387 {
2388   static struct MusicFileInfo *list = NULL;
2389   int ystart1 = mSY - SY + 100;
2390   int ystart2 = mSY - SY + 150;
2391   int ybottom = mSY - SY + SYSIZE - 20;
2392   int dy = 30;
2393
2394   if (button == MB_MENU_INITIALIZE)
2395   {
2396     list = music_file_info;
2397
2398     if (list == NULL)
2399     {
2400       FadeSoundsAndMusic();
2401
2402       ClearField();
2403       DrawHeadline();
2404
2405       DrawTextSCentered(ystart1, FONT_TEXT_1,
2406                         "No music info for this level set.");
2407
2408       DrawTextSCentered(ybottom, FONT_TEXT_4,
2409                         "Press any key or button for info menu");
2410
2411       return;
2412     }
2413   }
2414
2415   if (button == MB_MENU_LEAVE)
2416   {
2417     PlaySound(SND_MENU_ITEM_SELECTING);
2418
2419     FadeSoundsAndMusic();
2420
2421     info_mode = INFO_MODE_MAIN;
2422     DrawInfoScreen();
2423
2424     return;
2425   }
2426   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2427   {
2428     int y = 0;
2429
2430     if (button != MB_MENU_INITIALIZE)
2431     {
2432       PlaySound(SND_MENU_ITEM_SELECTING);
2433
2434       if (list != NULL)
2435         list = list->next;
2436     }
2437
2438     if (list == NULL)
2439     {
2440       FadeSoundsAndMusic();
2441
2442       info_mode = INFO_MODE_MAIN;
2443       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2444
2445       return;
2446     }
2447
2448     FadeSoundsAndMusic();
2449
2450     if (list != music_file_info)
2451       FadeSetNextScreen();
2452
2453     if (button != MB_MENU_INITIALIZE)
2454       FadeOut(REDRAW_FIELD);
2455
2456     ClearField();
2457     DrawHeadline();
2458
2459     if (list->is_sound)
2460     {
2461       int sound = list->music;
2462
2463       if (sound_info[sound].loop)
2464         PlaySoundLoop(sound);
2465       else
2466         PlaySound(sound);
2467
2468       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Sounds:");
2469     }
2470     else
2471     {
2472       PlayMusic(list->music);
2473
2474       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Music:");
2475     }
2476
2477     if (!strEqual(list->title, UNKNOWN_NAME))
2478     {
2479       if (!strEqual(list->title_header, UNKNOWN_NAME))
2480         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->title_header);
2481
2482       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->title);
2483     }
2484
2485     if (!strEqual(list->artist, UNKNOWN_NAME))
2486     {
2487       if (!strEqual(list->artist_header, UNKNOWN_NAME))
2488         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->artist_header);
2489       else
2490         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "by");
2491
2492       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->artist);
2493     }
2494
2495     if (!strEqual(list->album, UNKNOWN_NAME))
2496     {
2497       if (!strEqual(list->album_header, UNKNOWN_NAME))
2498         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->album_header);
2499       else
2500         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the album");
2501
2502       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->album);
2503     }
2504
2505     if (!strEqual(list->year, UNKNOWN_NAME))
2506     {
2507       if (!strEqual(list->year_header, UNKNOWN_NAME))
2508         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->year_header);
2509       else
2510         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the year");
2511
2512       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->year);
2513     }
2514
2515     DrawTextSCentered(ybottom, FONT_TEXT_4,
2516                       "Press any key or button for next page");
2517
2518     if (button != MB_MENU_INITIALIZE)
2519       FadeIn(REDRAW_FIELD);
2520   }
2521
2522   if (list != NULL && list->is_sound && sound_info[list->music].loop)
2523     PlaySoundLoop(list->music);
2524 }
2525
2526 static void DrawInfoScreen_CreditsScreen(int screen_nr)
2527 {
2528   int ystart1 = mSY - SY + 100;
2529   int ystart2 = mSY - SY + 150;
2530   int ybottom = mSY - SY + SYSIZE - 20;
2531   int ystep = 30;
2532
2533   ClearField();
2534   DrawHeadline();
2535
2536   DrawTextSCentered(ystart1, FONT_TEXT_1, "Credits:");
2537
2538   if (screen_nr == 0)
2539   {
2540     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2541                       "Special thanks to");
2542     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2543                       "Peter Liepa");
2544     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2545                       "for creating");
2546     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2547                       "\"Boulder Dash\"");
2548     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2549                       "in the year");
2550     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2551                       "1984");
2552     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2553                       "published by");
2554     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2555                       "First Star Software");
2556   }
2557   else if (screen_nr == 1)
2558   {
2559     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2560                       "Special thanks to");
2561     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2562                       "Klaus Heinz & Volker Wertich");
2563     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2564                       "for creating");
2565     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2566                       "\"Emerald Mine\"");
2567     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2568                       "in the year");
2569     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2570                       "1987");
2571     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2572                       "published by");
2573     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2574                       "Kingsoft");
2575   }
2576   else if (screen_nr == 2)
2577   {
2578     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2579                       "Special thanks to");
2580     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2581                       "Michael Stopp & Philip Jespersen");
2582     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2583                       "for creating");
2584     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2585                       "\"Supaplex\"");
2586     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2587                       "in the year");
2588     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2589                       "1991");
2590     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2591                       "published by");
2592     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2593                       "Digital Integration");
2594   }
2595   else if (screen_nr == 3)
2596   {
2597     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2598                       "Special thanks to");
2599     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2600                       "Hiroyuki Imabayashi");
2601     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2602                       "for creating");
2603     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2604                       "\"Sokoban\"");
2605     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2606                       "in the year");
2607     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2608                       "1982");
2609     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2610                       "published by");
2611     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2612                       "Thinking Rabbit");
2613   }
2614   else if (screen_nr == 4)
2615   {
2616     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2617                       "Special thanks to");
2618     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2619                       "Alan Bond");
2620     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2621                       "and");
2622     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2623                       "J\xfcrgen Bonhagen");
2624     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2625                       "for the continuous creation");
2626     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2627                       "of outstanding level sets");
2628   }
2629   else if (screen_nr == 5)
2630   {
2631     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2632                       "Thanks to");
2633     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2634                       "Peter Elzner");
2635     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2636                       "for ideas and inspiration by");
2637     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2638                       "Diamond Caves");
2639
2640     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2641                       "Thanks to");
2642     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_3,
2643                       "Steffest");
2644     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
2645                       "for ideas and inspiration by");
2646     DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
2647                       "DX-Boulderdash");
2648   }
2649   else if (screen_nr == 6)
2650   {
2651     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2652                       "Thanks to");
2653     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2654                       "David Tritscher");
2655 #if 1
2656     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2657                       "for the code base used for the");
2658     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_2,
2659                       "native Emerald Mine engine");
2660 #else
2661     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2662                       "for the new Emerald Mine engine");
2663 #endif
2664   }
2665   else if (screen_nr == 7)
2666   {
2667     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2668                       "Thanks to");
2669     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2670                       "Guido Schulz");
2671     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2672                       "for the initial DOS port");
2673
2674     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2675                       "Thanks to");
2676     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2677                       "Karl H\xf6rnell");
2678     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2679                       "for some additional toons");
2680   }
2681   else if (screen_nr == 8)
2682   {
2683     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2684                       "And not to forget:");
2685     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
2686                       "Many thanks to");
2687     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
2688                       "All those who contributed");
2689     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2690                       "levels to this game");
2691     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_3,
2692                       "since 1995");
2693   }
2694
2695   DrawTextSCentered(ybottom, FONT_TEXT_4,
2696                     "Press any key or button for next page");
2697 }
2698
2699 void DrawInfoScreen_Credits()
2700 {
2701   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
2702
2703   FadeSoundsAndMusic();
2704
2705   FadeOut(REDRAW_FIELD);
2706
2707   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
2708
2709   FadeIn(REDRAW_FIELD);
2710 }
2711
2712 void HandleInfoScreen_Credits(int button)
2713 {
2714   static int screen_nr = 0;
2715   int num_screens = 9;
2716
2717   if (button == MB_MENU_INITIALIZE)
2718   {
2719     screen_nr = 0;
2720
2721     // DrawInfoScreen_CreditsScreen(screen_nr);
2722   }
2723
2724   if (button == MB_MENU_LEAVE)
2725   {
2726     PlaySound(SND_MENU_ITEM_SELECTING);
2727
2728     info_mode = INFO_MODE_MAIN;
2729     DrawInfoScreen();
2730
2731     return;
2732   }
2733   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2734   {
2735     if (button != MB_MENU_INITIALIZE)
2736     {
2737       PlaySound(SND_MENU_ITEM_SELECTING);
2738
2739       screen_nr++;
2740     }
2741
2742     if (screen_nr >= num_screens)
2743     {
2744       FadeSoundsAndMusic();
2745
2746       info_mode = INFO_MODE_MAIN;
2747       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2748
2749       return;
2750     }
2751
2752     if (screen_nr > 0)
2753       FadeSetNextScreen();
2754
2755     if (button != MB_MENU_INITIALIZE)
2756       FadeOut(REDRAW_FIELD);
2757
2758     DrawInfoScreen_CreditsScreen(screen_nr);
2759
2760     if (button != MB_MENU_INITIALIZE)
2761       FadeIn(REDRAW_FIELD);
2762   }
2763   else
2764   {
2765     PlayMenuSoundIfLoop();
2766   }
2767 }
2768
2769 void DrawInfoScreen_Program()
2770 {
2771   int ystart1 = mSY - SY + 100;
2772   int ystart2 = mSY - SY + 150;
2773   int ybottom = mSY - SY + SYSIZE - 20;
2774   int ystep = 30;
2775
2776   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
2777
2778   FadeOut(REDRAW_FIELD);
2779
2780   ClearField();
2781   DrawHeadline();
2782
2783   DrawTextSCentered(ystart1, FONT_TEXT_1, "Program Information:");
2784
2785   DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2786                     "This game is Freeware!");
2787   DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
2788                     "If you like it, send e-mail to:");
2789   DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
2790                     PROGRAM_EMAIL_STRING);
2791   DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2792                     "More information and levels:");
2793   DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2794                     PROGRAM_WEBSITE_STRING);
2795   DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
2796                     "If you have created new levels,");
2797   DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_2,
2798                     "send them to me to include them!");
2799   DrawTextSCentered(ystart2 + 9 * ystep, FONT_TEXT_2,
2800                     ":-)");
2801
2802   DrawTextSCentered(ybottom, FONT_TEXT_4,
2803                     "Press any key or button for info menu");
2804
2805   FadeIn(REDRAW_FIELD);
2806 }
2807
2808 void HandleInfoScreen_Program(int button)
2809 {
2810   if (button == MB_MENU_LEAVE)
2811   {
2812     PlaySound(SND_MENU_ITEM_SELECTING);
2813
2814     info_mode = INFO_MODE_MAIN;
2815     DrawInfoScreen();
2816
2817     return;
2818   }
2819   else if (button == MB_MENU_CHOICE)
2820   {
2821     PlaySound(SND_MENU_ITEM_SELECTING);
2822
2823     FadeSoundsAndMusic();
2824
2825     info_mode = INFO_MODE_MAIN;
2826     DrawAndFadeInInfoScreen(REDRAW_FIELD);
2827   }
2828   else
2829   {
2830     PlayMenuSoundIfLoop();
2831   }
2832 }
2833
2834 void DrawInfoScreen_Version()
2835 {
2836   int font_header = FONT_TEXT_3;
2837   int font_text = FONT_TEXT_2;
2838   int xstep = getFontWidth(font_text);
2839   int ystep = getFontHeight(font_text);
2840   int ystart1 = mSY - SY + 100;
2841   int ystart2 = mSY - SY + 150;
2842   int ybottom = mSY - SY + SYSIZE - 20;
2843   int xstart1 = mSX - SX + 2 * xstep;
2844   int xstart2 = mSX - SX + 18 * xstep;
2845   int xstart3 = mSX - SX + 28 * xstep;
2846   SDL_version sdl_version_compiled;
2847   const SDL_version *sdl_version_linked;
2848   int driver_name_len = 10;
2849 #if defined(TARGET_SDL2)
2850   SDL_version sdl_version_linked_ext;
2851   const char *driver_name = NULL;
2852 #else
2853   char driver_name[driver_name_len];
2854 #endif
2855
2856   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
2857
2858   FadeOut(REDRAW_FIELD);
2859
2860   ClearField();
2861   DrawHeadline();
2862
2863   DrawTextSCentered(ystart1, FONT_TEXT_1, "Version Information:");
2864
2865   DrawTextF(xstart1, ystart2, font_header, "Name");
2866   DrawTextF(xstart2, ystart2, font_text, PROGRAM_TITLE_STRING);
2867
2868   ystart2 += ystep;
2869   DrawTextF(xstart1, ystart2, font_header, "Version");
2870   DrawTextF(xstart2, ystart2, font_text, getProgramVersionString());
2871
2872   ystart2 += ystep;
2873   DrawTextF(xstart1, ystart2, font_header, "Platform");
2874   DrawTextF(xstart2, ystart2, font_text, PLATFORM_STRING);
2875
2876   ystart2 += ystep;
2877   DrawTextF(xstart1, ystart2, font_header, "Target");
2878   DrawTextF(xstart2, ystart2, font_text, TARGET_STRING);
2879
2880   ystart2 += ystep;
2881   DrawTextF(xstart1, ystart2, font_header, "Compile time");
2882   DrawTextF(xstart2, ystart2, font_text, getCompileDateString());
2883
2884   ystart2 += 3 * ystep;
2885   DrawTextF(xstart1, ystart2, font_header, "Library");
2886   DrawTextF(xstart2, ystart2, font_header, "compiled");
2887   DrawTextF(xstart3, ystart2, font_header, "linked");
2888
2889   SDL_VERSION(&sdl_version_compiled);
2890 #if defined(TARGET_SDL2)
2891   SDL_GetVersion(&sdl_version_linked_ext);
2892   sdl_version_linked = &sdl_version_linked_ext;
2893 #else
2894   sdl_version_linked = SDL_Linked_Version();
2895 #endif
2896
2897   ystart2 += 2 * ystep;
2898   DrawTextF(xstart1, ystart2, font_text, "SDL");
2899   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2900             sdl_version_compiled.major,
2901             sdl_version_compiled.minor,
2902             sdl_version_compiled.patch);
2903   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2904             sdl_version_linked->major,
2905             sdl_version_linked->minor,
2906             sdl_version_linked->patch);
2907
2908   SDL_IMAGE_VERSION(&sdl_version_compiled);
2909   sdl_version_linked = IMG_Linked_Version();
2910
2911   ystart2 += ystep;
2912   DrawTextF(xstart1, ystart2, font_text, "SDL_image");
2913   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2914             sdl_version_compiled.major,
2915             sdl_version_compiled.minor,
2916             sdl_version_compiled.patch);
2917   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2918             sdl_version_linked->major,
2919             sdl_version_linked->minor,
2920             sdl_version_linked->patch);
2921
2922   SDL_MIXER_VERSION(&sdl_version_compiled);
2923   sdl_version_linked = Mix_Linked_Version();
2924
2925   ystart2 += ystep;
2926   DrawTextF(xstart1, ystart2, font_text, "SDL_mixer");
2927   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2928             sdl_version_compiled.major,
2929             sdl_version_compiled.minor,
2930             sdl_version_compiled.patch);
2931   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2932             sdl_version_linked->major,
2933             sdl_version_linked->minor,
2934             sdl_version_linked->patch);
2935
2936   SDL_NET_VERSION(&sdl_version_compiled);
2937   sdl_version_linked = SDLNet_Linked_Version();
2938
2939   ystart2 += ystep;
2940   DrawTextF(xstart1, ystart2, font_text, "SDL_net");
2941   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2942             sdl_version_compiled.major,
2943             sdl_version_compiled.minor,
2944             sdl_version_compiled.patch);
2945   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2946             sdl_version_linked->major,
2947             sdl_version_linked->minor,
2948             sdl_version_linked->patch);
2949
2950   ystart2 += 3 * ystep;
2951   DrawTextF(xstart1, ystart2, font_header, "Driver");
2952   DrawTextF(xstart2, ystart2, font_header, "Requested");
2953   DrawTextF(xstart3, ystart2, font_header, "Used");
2954
2955 #if defined(TARGET_SDL2)
2956   driver_name = getStringCopyNStatic(SDL_GetVideoDriver(0), driver_name_len);
2957 #else
2958   SDL_VideoDriverName(driver_name, driver_name_len);
2959 #endif
2960
2961   ystart2 += 2 * ystep;
2962   DrawTextF(xstart1, ystart2, font_text, "SDL_VideoDriver");
2963   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_videodriver);
2964   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
2965
2966 #if defined(TARGET_SDL2)
2967   driver_name = getStringCopyNStatic(SDL_GetAudioDriver(0), driver_name_len);
2968 #else
2969   SDL_AudioDriverName(driver_name, driver_name_len);
2970 #endif
2971
2972   ystart2 += ystep;
2973   DrawTextF(xstart1, ystart2, font_text, "SDL_AudioDriver");
2974   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_audiodriver);
2975   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
2976
2977   DrawTextSCentered(ybottom, FONT_TEXT_4,
2978                     "Press any key or button for info menu");
2979
2980   FadeIn(REDRAW_FIELD);
2981 }
2982
2983 void HandleInfoScreen_Version(int button)
2984 {
2985   if (button == MB_MENU_LEAVE)
2986   {
2987     PlaySound(SND_MENU_ITEM_SELECTING);
2988
2989     info_mode = INFO_MODE_MAIN;
2990     DrawInfoScreen();
2991
2992     return;
2993   }
2994   else if (button == MB_MENU_CHOICE)
2995   {
2996     PlaySound(SND_MENU_ITEM_SELECTING);
2997
2998     FadeSoundsAndMusic();
2999
3000     info_mode = INFO_MODE_MAIN;
3001     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3002   }
3003   else
3004   {
3005     PlayMenuSoundIfLoop();
3006   }
3007 }
3008
3009 void DrawInfoScreen_LevelSet()
3010 {
3011   struct TitleMessageInfo *tmi = &readme;
3012   char *filename = getLevelSetInfoFilename();
3013
3014   /* if chars set to "-1", automatically determine by text and font width */
3015   if (tmi->chars == -1)
3016     tmi->chars = tmi->width / getFontWidth(tmi->font);
3017   else
3018     tmi->width = tmi->chars * getFontWidth(tmi->font);
3019
3020   /* if lines set to "-1", automatically determine by text and font height */
3021   if (tmi->lines == -1)
3022     tmi->lines = tmi->height / getFontHeight(tmi->font);
3023   else
3024     tmi->height = tmi->lines * getFontHeight(tmi->font);
3025
3026   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3027
3028   FadeOut(REDRAW_FIELD);
3029
3030   ClearField();
3031   DrawHeadline();
3032
3033   DrawTextCentered(mSY + 100, FONT_TEXT_1, "Level Set Information:");
3034
3035   if (filename != NULL)
3036     DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3037                  filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3038                  tmi->autowrap, tmi->centered, tmi->parse_comments);
3039   else
3040     DrawTextCentered(mSY + ALIGNED_TEXT_YPOS(tmi), FONT_TEXT_2,
3041                      "No information for this level set.");
3042
3043   DrawTextCentered(mSY + SYSIZE - 20, FONT_TEXT_4,
3044                    "Press any key or button for info menu");
3045
3046   FadeIn(REDRAW_FIELD);
3047 }
3048
3049 void HandleInfoScreen_LevelSet(int button)
3050 {
3051   if (button == MB_MENU_LEAVE)
3052   {
3053     PlaySound(SND_MENU_ITEM_SELECTING);
3054
3055     info_mode = INFO_MODE_MAIN;
3056     DrawInfoScreen();
3057
3058     return;
3059   }
3060   else if (button == MB_MENU_CHOICE)
3061   {
3062     PlaySound(SND_MENU_ITEM_SELECTING);
3063
3064     FadeSoundsAndMusic();
3065
3066     info_mode = INFO_MODE_MAIN;
3067     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3068   }
3069   else
3070   {
3071     PlayMenuSoundIfLoop();
3072   }
3073 }
3074
3075 static void DrawInfoScreenExt(int fade_mask, boolean do_fading)
3076 {
3077   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
3078
3079   if (info_mode == INFO_MODE_TITLE)
3080     DrawInfoScreen_TitleScreen();
3081   else if (info_mode == INFO_MODE_ELEMENTS)
3082     DrawInfoScreen_Elements();
3083   else if (info_mode == INFO_MODE_MUSIC)
3084     DrawInfoScreen_Music();
3085   else if (info_mode == INFO_MODE_CREDITS)
3086     DrawInfoScreen_Credits();
3087   else if (info_mode == INFO_MODE_PROGRAM)
3088     DrawInfoScreen_Program();
3089   else if (info_mode == INFO_MODE_VERSION)
3090     DrawInfoScreen_Version();
3091   else if (info_mode == INFO_MODE_LEVELSET)
3092     DrawInfoScreen_LevelSet();
3093   else
3094     DrawInfoScreen_Main(fade_mask, do_fading);
3095
3096   if (info_mode != INFO_MODE_MAIN &&
3097       info_mode != INFO_MODE_TITLE &&
3098       info_mode != INFO_MODE_MUSIC)
3099   {
3100     PlayMenuSound();
3101     PlayMenuMusic();
3102   }
3103 }
3104
3105 void DrawAndFadeInInfoScreen(int fade_mask)
3106 {
3107   DrawInfoScreenExt(fade_mask, TRUE);
3108 }
3109
3110 void DrawInfoScreen()
3111 {
3112   DrawInfoScreenExt(REDRAW_FIELD, FALSE);
3113 }
3114
3115 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3116 {
3117   if (info_mode == INFO_MODE_TITLE)
3118     HandleInfoScreen_TitleScreen(button);
3119   else if (info_mode == INFO_MODE_ELEMENTS)
3120     HandleInfoScreen_Elements(button);
3121   else if (info_mode == INFO_MODE_MUSIC)
3122     HandleInfoScreen_Music(button);
3123   else if (info_mode == INFO_MODE_CREDITS)
3124     HandleInfoScreen_Credits(button);
3125   else if (info_mode == INFO_MODE_PROGRAM)
3126     HandleInfoScreen_Program(button);
3127   else if (info_mode == INFO_MODE_VERSION)
3128     HandleInfoScreen_Version(button);
3129   else if (info_mode == INFO_MODE_LEVELSET)
3130     HandleInfoScreen_LevelSet(button);
3131   else
3132     HandleInfoScreen_Main(mx, my, dx, dy, button);
3133 }
3134
3135
3136 /* ========================================================================= */
3137 /* type name functions                                                       */
3138 /* ========================================================================= */
3139
3140 void HandleTypeName(int newxpos, Key key)
3141 {
3142   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3143   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3144   struct TextPosInfo *pos = mci->pos_input;
3145   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3146   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3147   static int xpos = 0;
3148   int font_nr = pos->font;
3149   int font_active_nr = FONT_ACTIVE(font_nr);
3150   int font_width = getFontWidth(font_active_nr);
3151   char key_char = getValidConfigValueChar(getCharFromKey(key));
3152   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3153   boolean is_active = TRUE;
3154
3155   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3156
3157   if (newxpos)
3158   {
3159     strcpy(last_player_name, setup.player_name);
3160
3161     xpos = newxpos;
3162
3163 #if defined(TARGET_SDL2)
3164     SDL_StartTextInput();
3165 #endif
3166   }
3167   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3168   {
3169     setup.player_name[xpos] = key_char;
3170     setup.player_name[xpos + 1] = 0;
3171
3172     xpos++;
3173   }
3174   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3175   {
3176     xpos--;
3177
3178     setup.player_name[xpos] = 0;
3179   }
3180   else if (key == KSYM_Return && xpos > 0)
3181   {
3182     SaveSetup();
3183
3184     is_active = FALSE;
3185
3186     game_status = GAME_MODE_MAIN;
3187   }
3188   else if (key == KSYM_Escape)
3189   {
3190     strcpy(setup.player_name, last_player_name);
3191
3192     is_active = FALSE;
3193
3194     game_status = GAME_MODE_MAIN;
3195   }
3196
3197   if (is_active)
3198   {
3199     pos->width = (strlen(setup.player_name) + 1) * font_width;
3200     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3201
3202     DrawText(startx, starty, setup.player_name, font_active_nr);
3203     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3204   }
3205   else
3206   {
3207     pos->width = strlen(setup.player_name) * font_width;
3208     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3209
3210     DrawText(startx, starty, setup.player_name, font_nr);
3211
3212 #if defined(TARGET_SDL2)
3213     SDL_StopTextInput();
3214 #endif
3215   }
3216 }
3217
3218
3219 /* ========================================================================= */
3220 /* tree menu functions                                                       */
3221 /* ========================================================================= */
3222
3223 static void DrawChooseTree(TreeInfo **ti_ptr)
3224 {
3225   UnmapAllGadgets();
3226
3227   FreeScreenGadgets();
3228   CreateScreenGadgets();
3229
3230   CloseDoor(DOOR_CLOSE_2);
3231
3232   FadeOut(REDRAW_FIELD);
3233
3234   ClearField();
3235
3236   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3237   MapScreenTreeGadgets(*ti_ptr);
3238
3239   FadeIn(REDRAW_FIELD);
3240
3241   InitAnimation();
3242 }
3243
3244 static void AdjustScrollbar(int id, int items_max, int items_visible,
3245                             int item_position)
3246 {
3247   struct GadgetInfo *gi = screen_gadget[id];
3248
3249   if (item_position > items_max - items_visible)
3250     item_position = items_max - items_visible;
3251
3252   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
3253                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3254                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
3255 }
3256
3257 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
3258 {
3259   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
3260                   first_entry);
3261 }
3262
3263 static void drawChooseTreeList(int first_entry, int num_page_entries,
3264                                TreeInfo *ti)
3265 {
3266   int i;
3267   char *title_string = NULL;
3268   int yoffset_sets = MENU_TITLE1_YPOS;
3269   int yoffset_setup = 16;
3270   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
3271                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
3272   int last_game_status = game_status;   /* save current game status */
3273
3274   title_string = ti->infotext;
3275
3276   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3277
3278   /* clear tree list area, but not title or scrollbar */
3279   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3280                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
3281                  NUM_MENU_ENTRIES_ON_SCREEN * 32);
3282
3283   for (i = 0; i < num_page_entries; i++)
3284   {
3285     TreeInfo *node, *node_first;
3286     int entry_pos = first_entry + i;
3287     int xpos = MENU_SCREEN_START_XPOS;
3288     int ypos = MENU_SCREEN_START_YPOS + i;
3289     int startx = mSX + xpos * 32;
3290     int starty = mSY + ypos * 32;
3291     int font_nr = FONT_TEXT_1;
3292     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3293     int startx_text = startx + font_xoffset;
3294     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3295     int text_size = startx_scrollbar - startx_text;
3296     int max_buffer_len = text_size / getFontWidth(font_nr);
3297     char buffer[max_buffer_len + 1];
3298
3299     node_first = getTreeInfoFirstGroupEntry(ti);
3300     node = getTreeInfoFromPos(node_first, entry_pos);
3301
3302     strncpy(buffer, node->name, max_buffer_len);
3303     buffer[max_buffer_len] = '\0';
3304
3305     DrawText(startx, starty, buffer, font_nr + node->color);
3306
3307     if (node->parent_link)
3308       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3309     else if (node->level_group)
3310       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3311     else
3312       initCursor(i, IMG_MENU_BUTTON);
3313   }
3314
3315   game_status = last_game_status;       /* restore current game status */
3316
3317   redraw_mask |= REDRAW_FIELD;
3318 }
3319
3320 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3321 {
3322   TreeInfo *node, *node_first;
3323   int x, last_redraw_mask = redraw_mask;
3324   int ypos = MENU_TITLE2_YPOS;
3325   int font_nr = FONT_TITLE_2;
3326
3327   if (ti->type == TREE_TYPE_LEVEL_NR)
3328     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
3329
3330   if (ti->type != TREE_TYPE_LEVEL_DIR)
3331     return;
3332
3333   node_first = getTreeInfoFirstGroupEntry(ti);
3334   node = getTreeInfoFromPos(node_first, entry_pos);
3335
3336   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3337
3338   if (node->parent_link)
3339     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
3340                       node->node_parent->name);
3341   else if (node->level_group)
3342     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
3343                       node->name);
3344   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3345     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
3346                       node->levels, (node->levels > 1 ? "levels" : "level"),
3347                       node->class_desc);
3348
3349   /* let BackToFront() redraw only what is needed */
3350   redraw_mask = last_redraw_mask | REDRAW_TILES;
3351   for (x = 0; x < SCR_FIELDX; x++)
3352     MarkTileDirty(x, 1);
3353 }
3354
3355 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3356                              TreeInfo **ti_ptr)
3357 {
3358   TreeInfo *ti = *ti_ptr;
3359   int x = 0;
3360   int y = ti->cl_cursor;
3361   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3362   int num_entries = numTreeInfoInGroup(ti);
3363   int num_page_entries;
3364   int last_game_status = game_status;   /* save current game status */
3365   boolean position_set_by_scrollbar = (dx == 999);
3366
3367   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3368     num_page_entries = num_entries;
3369   else
3370     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3371
3372   game_status = last_game_status;       /* restore current game status */
3373
3374   if (button == MB_MENU_INITIALIZE)
3375   {
3376     int num_entries = numTreeInfoInGroup(ti);
3377     int entry_pos = posTreeInfo(ti);
3378
3379     if (ti->cl_first == -1)
3380     {
3381       /* only on initialization */
3382       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3383       ti->cl_cursor = entry_pos - ti->cl_first;
3384     }
3385     else if (ti->cl_cursor >= num_page_entries ||
3386              (num_entries > num_page_entries &&
3387               num_entries - ti->cl_first < num_page_entries))
3388     {
3389       /* only after change of list size (by custom graphic configuration) */
3390       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3391       ti->cl_cursor = entry_pos - ti->cl_first;
3392     }
3393
3394     if (position_set_by_scrollbar)
3395       ti->cl_first = dy;
3396     else
3397       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3398                                 ti->cl_first, ti);
3399
3400     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3401     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3402     drawChooseTreeCursor(ti->cl_cursor, TRUE);
3403
3404     return;
3405   }
3406   else if (button == MB_MENU_LEAVE)
3407   {
3408     FadeSetLeaveMenu();
3409
3410     PlaySound(SND_MENU_ITEM_SELECTING);
3411
3412     if (ti->node_parent)
3413     {
3414       *ti_ptr = ti->node_parent;
3415       DrawChooseTree(ti_ptr);
3416     }
3417     else if (game_status == GAME_MODE_SETUP)
3418     {
3419       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
3420           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3421         execSetupGame();
3422       else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3423                setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
3424                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
3425         execSetupGraphics();
3426       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
3427                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
3428                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
3429         execSetupSound();
3430       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
3431                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
3432                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
3433         execSetupTouch();
3434       else
3435         execSetupArtwork();
3436     }
3437     else
3438     {
3439       if (game_status == GAME_MODE_LEVELNR)
3440         level_nr = atoi(level_number_current->identifier);
3441
3442       game_status = GAME_MODE_MAIN;
3443
3444       DrawMainMenuExt(REDRAW_FIELD, FALSE);
3445     }
3446
3447     return;
3448   }
3449
3450   if (mx || my)         /* mouse input */
3451   {
3452     int last_game_status = game_status; /* save current game status */
3453
3454     x = (mx - mSX) / 32;
3455     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
3456
3457     game_status = last_game_status;     /* restore current game status */
3458   }
3459   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
3460   {
3461     /* move cursor instead of scrolling when already at start/end of list */
3462     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
3463       dy = -1;
3464     else if (dy == +1 * SCROLL_LINE &&
3465              ti->cl_first + num_page_entries == num_entries)
3466       dy = 1;
3467
3468     /* handle scrolling screen one line or page */
3469     if (ti->cl_cursor + dy < 0 ||
3470         ti->cl_cursor + dy > num_page_entries - 1)
3471     {
3472       boolean redraw = FALSE;
3473
3474       if (ABS(dy) == SCROLL_PAGE)
3475         step = num_page_entries - 1;
3476
3477       if (dy < 0 && ti->cl_first > 0)
3478       {
3479         /* scroll page/line up */
3480
3481         ti->cl_first -= step;
3482         if (ti->cl_first < 0)
3483           ti->cl_first = 0;
3484
3485         redraw = TRUE;
3486       }
3487       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
3488       {
3489         /* scroll page/line down */
3490
3491         ti->cl_first += step;
3492         if (ti->cl_first + num_page_entries > num_entries)
3493           ti->cl_first = MAX(0, num_entries - num_page_entries);
3494
3495         redraw = TRUE;
3496       }
3497
3498       if (redraw)
3499       {
3500         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3501         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3502         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3503
3504         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3505                                   ti->cl_first, ti);
3506       }
3507
3508
3509       return;
3510     }
3511
3512     /* handle moving cursor one line */
3513     y = ti->cl_cursor + dy;
3514   }
3515
3516   if (dx == 1)
3517   {
3518     TreeInfo *node_first, *node_cursor;
3519     int entry_pos = ti->cl_first + y;
3520
3521     node_first = getTreeInfoFirstGroupEntry(ti);
3522     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3523
3524     if (node_cursor->node_group)
3525     {
3526       FadeSetEnterMenu();
3527
3528       PlaySound(SND_MENU_ITEM_SELECTING);
3529
3530       node_cursor->cl_first = ti->cl_first;
3531       node_cursor->cl_cursor = ti->cl_cursor;
3532       *ti_ptr = node_cursor->node_group;
3533       DrawChooseTree(ti_ptr);
3534
3535       return;
3536     }
3537   }
3538   else if (dx == -1 && ti->node_parent)
3539   {
3540     FadeSetLeaveMenu();
3541
3542     PlaySound(SND_MENU_ITEM_SELECTING);
3543
3544     *ti_ptr = ti->node_parent;
3545     DrawChooseTree(ti_ptr);
3546
3547     return;
3548   }
3549
3550   if (!anyScrollbarGadgetActive() &&
3551       IN_VIS_FIELD(x, y) &&
3552       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
3553       y >= 0 && y < num_page_entries)
3554   {
3555     if (button)
3556     {
3557       if (y != ti->cl_cursor)
3558       {
3559         PlaySound(SND_MENU_ITEM_ACTIVATING);
3560
3561         drawChooseTreeCursor(ti->cl_cursor, FALSE);
3562         drawChooseTreeCursor(y, TRUE);
3563         drawChooseTreeInfo(ti->cl_first + y, ti);
3564
3565         ti->cl_cursor = y;
3566       }
3567     }
3568     else
3569     {
3570       TreeInfo *node_first, *node_cursor;
3571       int entry_pos = ti->cl_first + y;
3572
3573       PlaySound(SND_MENU_ITEM_SELECTING);
3574
3575       node_first = getTreeInfoFirstGroupEntry(ti);
3576       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3577
3578       if (node_cursor->node_group)
3579       {
3580         FadeSetEnterMenu();
3581
3582         node_cursor->cl_first = ti->cl_first;
3583         node_cursor->cl_cursor = ti->cl_cursor;
3584         *ti_ptr = node_cursor->node_group;
3585         DrawChooseTree(ti_ptr);
3586       }
3587       else if (node_cursor->parent_link)
3588       {
3589         FadeSetLeaveMenu();
3590
3591         *ti_ptr = node_cursor->node_parent;
3592         DrawChooseTree(ti_ptr);
3593       }
3594       else
3595       {
3596         FadeSetEnterMenu();
3597
3598         node_cursor->cl_first = ti->cl_first;
3599         node_cursor->cl_cursor = ti->cl_cursor;
3600         *ti_ptr = node_cursor;
3601
3602         if (ti->type == TREE_TYPE_LEVEL_DIR)
3603         {
3604           LoadLevelSetup_SeriesInfo();
3605
3606           SaveLevelSetup_LastSeries();
3607           SaveLevelSetup_SeriesInfo();
3608           TapeErase();
3609         }
3610
3611         if (game_status == GAME_MODE_SETUP)
3612         {
3613           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
3614               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3615             execSetupGame();
3616           else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3617                    setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
3618                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
3619             execSetupGraphics();
3620           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
3621                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
3622                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
3623             execSetupSound();
3624           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
3625                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
3626                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
3627             execSetupTouch();
3628           else
3629             execSetupArtwork();
3630         }
3631         else
3632         {
3633           if (game_status == GAME_MODE_LEVELNR)
3634             level_nr = atoi(level_number_current->identifier);
3635
3636           game_status = GAME_MODE_MAIN;
3637
3638           DrawMainMenu();
3639         }
3640       }
3641     }
3642   }
3643 }
3644
3645 void DrawChooseLevelSet()
3646 {
3647   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
3648
3649   DrawChooseTree(&leveldir_current);
3650
3651   PlayMenuSound();
3652   PlayMenuMusic();
3653 }
3654
3655 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
3656 {
3657   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
3658 }
3659
3660 void DrawChooseLevelNr()
3661 {
3662   int i;
3663
3664   if (level_number != NULL)
3665   {
3666     freeTreeInfo(level_number);
3667
3668     level_number = NULL;
3669   }
3670
3671   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
3672   {
3673     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
3674     char identifier[32], name[32];
3675     int value = i;
3676
3677     /* temporarily load level info to get level name */
3678     LoadLevelInfoOnly(i);
3679
3680     ti->node_top = &level_number;
3681     ti->sort_priority = 10000 + value;
3682     ti->color = (level.no_valid_file ? FC_BLUE :
3683                  LevelStats_getSolved(i) ? FC_GREEN :
3684                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
3685
3686     sprintf(identifier, "%d", value);
3687     sprintf(name, "%03d: %s", value,
3688             (level.no_valid_file ? "(no file)" : level.name));
3689
3690     setString(&ti->identifier, identifier);
3691     setString(&ti->name, name);
3692     setString(&ti->name_sorting, name);
3693
3694     pushTreeInfo(&level_number, ti);
3695   }
3696
3697   /* sort level number values to start with lowest level number */
3698   sortTreeInfo(&level_number);
3699
3700   /* set current level number to current level number */
3701   level_number_current =
3702     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
3703
3704   /* if that also fails, set current level number to first available level */
3705   if (level_number_current == NULL)
3706     level_number_current = level_number;
3707
3708   SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
3709
3710   DrawChooseTree(&level_number_current);
3711
3712   PlayMenuSound();
3713   PlayMenuMusic();
3714 }
3715
3716 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
3717 {
3718   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
3719 }
3720
3721 void DrawHallOfFame(int highlight_position)
3722 {
3723   UnmapAllGadgets();
3724   FadeSoundsAndMusic();
3725
3726   /* (this is needed when called from GameEnd() after winning a game) */
3727   KeyboardAutoRepeatOn();
3728   ActivateJoystick();
3729
3730   /* (this is needed when called from GameEnd() after winning a game) */
3731   SetDrawDeactivationMask(REDRAW_NONE);
3732   SetDrawBackgroundMask(REDRAW_FIELD);
3733
3734   CloseDoor(DOOR_CLOSE_2);
3735
3736   if (highlight_position < 0) 
3737     LoadScore(level_nr);
3738
3739   FadeSetEnterScreen();
3740
3741   FadeOut(REDRAW_FIELD);
3742
3743   InitAnimation();
3744
3745   PlayMenuSound();
3746   PlayMenuMusic();
3747
3748   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
3749
3750   FadeIn(REDRAW_FIELD);
3751 }
3752
3753 static void drawHallOfFameList(int first_entry, int highlight_position)
3754 {
3755   int i, j;
3756
3757   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
3758   ClearField();
3759
3760   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
3761   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
3762                     "HighScores of Level %d", level_nr);
3763
3764   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
3765   {
3766     int entry = first_entry + i;
3767     boolean active = (entry == highlight_position);
3768     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
3769     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
3770     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
3771     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
3772     int dx1 = 3 * getFontWidth(font_nr1);
3773     int dx2 = dx1 + getFontWidth(font_nr1);
3774     int dx3 = SXSIZE - 2 * (mSX - SX) - 5 * getFontWidth(font_nr4);
3775     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
3776     int sy = mSY + 64 + i * 32;
3777
3778     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
3779     DrawText(mSX + dx1, sy, ".", font_nr1);
3780
3781     for (j = 0; j < num_dots; j++)
3782       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
3783
3784     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
3785       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
3786
3787     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
3788   }
3789
3790   redraw_mask |= REDRAW_FIELD;
3791 }
3792
3793 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
3794 {
3795   static int first_entry = 0;
3796   static int highlight_position = 0;
3797   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3798
3799   if (button == MB_MENU_INITIALIZE)
3800   {
3801     first_entry = 0;
3802     highlight_position = mx;
3803     drawHallOfFameList(first_entry, highlight_position);
3804
3805     return;
3806   }
3807
3808   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
3809     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
3810
3811   if (dy < 0)
3812   {
3813     if (first_entry > 0)
3814     {
3815       first_entry -= step;
3816       if (first_entry < 0)
3817         first_entry = 0;
3818
3819       drawHallOfFameList(first_entry, highlight_position);
3820     }
3821   }
3822   else if (dy > 0)
3823   {
3824     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
3825     {
3826       first_entry += step;
3827       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
3828         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
3829
3830       drawHallOfFameList(first_entry, highlight_position);
3831     }
3832   }
3833   else if (button == MB_MENU_LEAVE)
3834   {
3835     PlaySound(SND_MENU_ITEM_SELECTING);
3836
3837     FadeSound(SND_BACKGROUND_SCORES);
3838
3839     game_status = GAME_MODE_MAIN;
3840
3841     DrawMainMenu();
3842   }
3843   else if (button == MB_MENU_CHOICE)
3844   {
3845     PlaySound(SND_MENU_ITEM_SELECTING);
3846
3847     FadeSound(SND_BACKGROUND_SCORES);
3848
3849     game_status = GAME_MODE_MAIN;
3850
3851     DrawAndFadeInMainMenu(REDRAW_FIELD);
3852   }
3853
3854   if (game_status == GAME_MODE_SCORES)
3855     PlayMenuSoundIfLoop();
3856 }
3857
3858
3859 /* ========================================================================= */
3860 /* setup screen functions                                                    */
3861 /* ========================================================================= */
3862
3863 static struct TokenInfo *setup_info;
3864 static int num_setup_info;      /* number of setup entries shown on screen */
3865 static int max_setup_info;      /* total number of setup entries in list */
3866
3867 static char *screen_mode_text;
3868 static char *window_size_text;
3869 static char *scaling_type_text;
3870 static char *scroll_delay_text;
3871 static char *game_speed_text;
3872 static char *graphics_set_name;
3873 static char *sounds_set_name;
3874 static char *music_set_name;
3875 static char *volume_simple_text;
3876 static char *volume_loops_text;
3877 static char *volume_music_text;
3878 static char *touch_controls_text;
3879 static char *move_distance_text;
3880 static char *drop_distance_text;
3881
3882 static void execSetupMain()
3883 {
3884   setup_mode = SETUP_MODE_MAIN;
3885
3886   DrawSetupScreen();
3887 }
3888
3889 static void execSetupGame_setGameSpeeds()
3890 {
3891   if (game_speeds == NULL)
3892   {
3893     int i;
3894
3895     for (i = 0; game_speeds_list[i].value != -1; i++)
3896     {
3897       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
3898       char identifier[32], name[32];
3899       int value = game_speeds_list[i].value;
3900       char *text = game_speeds_list[i].text;
3901
3902       ti->node_top = &game_speeds;
3903       ti->sort_priority = 10000 - value;
3904
3905       sprintf(identifier, "%d", value);
3906       sprintf(name, "%s", text);
3907
3908       setString(&ti->identifier, identifier);
3909       setString(&ti->name, name);
3910       setString(&ti->name_sorting, name);
3911       setString(&ti->infotext, "Game Speed");
3912
3913       pushTreeInfo(&game_speeds, ti);
3914     }
3915
3916     /* sort game speed values to start with slowest game speed */
3917     sortTreeInfo(&game_speeds);
3918
3919     /* set current game speed to configured game speed value */
3920     game_speed_current =
3921       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
3922
3923     /* if that fails, set current game speed to reliable default value */
3924     if (game_speed_current == NULL)
3925       game_speed_current =
3926         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
3927
3928     /* if that also fails, set current game speed to first available speed */
3929     if (game_speed_current == NULL)
3930       game_speed_current = game_speeds;
3931   }
3932
3933   setup.game_frame_delay = atoi(game_speed_current->identifier);
3934
3935   /* needed for displaying game speed text instead of identifier */
3936   game_speed_text = game_speed_current->name;
3937 }
3938
3939 static void execSetupGame_setScrollDelays()
3940 {
3941   if (scroll_delays == NULL)
3942   {
3943     int i;
3944
3945     for (i = 0; scroll_delays_list[i].value != -1; i++)
3946     {
3947       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
3948       char identifier[32], name[32];
3949       int value = scroll_delays_list[i].value;
3950       char *text = scroll_delays_list[i].text;
3951
3952       ti->node_top = &scroll_delays;
3953       ti->sort_priority = value;
3954
3955       sprintf(identifier, "%d", value);
3956       sprintf(name, "%s", text);
3957
3958       setString(&ti->identifier, identifier);
3959       setString(&ti->name, name);
3960       setString(&ti->name_sorting, name);
3961       setString(&ti->infotext, "Scaling Type");
3962
3963       pushTreeInfo(&scroll_delays, ti);
3964     }
3965
3966     /* sort scaling type values to start with lowest scaling type value */
3967     sortTreeInfo(&scroll_delays);
3968
3969     /* set current scaling type value to configured scaling type value */
3970     scroll_delay_current =
3971       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
3972
3973     /* if that fails, set current scaling type to reliable default value */
3974     if (scroll_delay_current == NULL)
3975       scroll_delay_current =
3976         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
3977
3978     /* if that also fails, set current scaling type to first available value */
3979     if (scroll_delay_current == NULL)
3980       scroll_delay_current = scroll_delays;
3981   }
3982
3983   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
3984
3985   /* needed for displaying scaling type text instead of identifier */
3986   scroll_delay_text = scroll_delay_current->name;
3987 }
3988
3989 static void execSetupGame()
3990 {
3991   execSetupGame_setGameSpeeds();
3992   execSetupGame_setScrollDelays();
3993
3994   setup_mode = SETUP_MODE_GAME;
3995
3996   DrawSetupScreen();
3997 }
3998
3999 static void execSetupChooseGameSpeed()
4000 {
4001   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4002
4003   DrawSetupScreen();
4004 }
4005
4006 static void execSetupChooseScrollDelay()
4007 {
4008   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4009
4010   DrawSetupScreen();
4011 }
4012
4013 static void execSetupEditor()
4014 {
4015   setup_mode = SETUP_MODE_EDITOR;
4016
4017   DrawSetupScreen();
4018 }
4019
4020 static void execSetupGraphics_setWindowSizes(boolean update_list)
4021 {
4022   if (window_sizes != NULL && update_list)
4023   {
4024     freeTreeInfo(window_sizes);
4025
4026     window_sizes = NULL;
4027   }
4028
4029   if (window_sizes == NULL)
4030   {
4031     boolean current_window_size_found = FALSE;
4032     int i;
4033
4034     for (i = 0; window_sizes_list[i].value != -1; i++)
4035     {
4036       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4037       char identifier[32], name[32];
4038       int value = window_sizes_list[i].value;
4039       char *text = window_sizes_list[i].text;
4040
4041       ti->node_top = &window_sizes;
4042       ti->sort_priority = value;
4043
4044       sprintf(identifier, "%d", value);
4045       sprintf(name, "%s", text);
4046
4047       setString(&ti->identifier, identifier);
4048       setString(&ti->name, name);
4049       setString(&ti->name_sorting, name);
4050       setString(&ti->infotext, "Window Scaling");
4051
4052       pushTreeInfo(&window_sizes, ti);
4053
4054       if (value == setup.window_scaling_percent)
4055         current_window_size_found = TRUE;
4056     }
4057
4058     if (!current_window_size_found)
4059     {
4060       // add entry for non-preset window scaling value
4061
4062       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4063       char identifier[32], name[32];
4064       int value = setup.window_scaling_percent;
4065
4066       ti->node_top = &window_sizes;
4067       ti->sort_priority = value;
4068
4069       sprintf(identifier, "%d", value);
4070       sprintf(name, "%d %% (Current)", value);
4071
4072       setString(&ti->identifier, identifier);
4073       setString(&ti->name, name);
4074       setString(&ti->name_sorting, name);
4075       setString(&ti->infotext, "Window Scaling");
4076
4077       pushTreeInfo(&window_sizes, ti);
4078     }
4079
4080     /* sort window size values to start with lowest window size value */
4081     sortTreeInfo(&window_sizes);
4082
4083     /* set current window size value to configured window size value */
4084     window_size_current =
4085       getTreeInfoFromIdentifier(window_sizes,
4086                                 i_to_a(setup.window_scaling_percent));
4087
4088     /* if that fails, set current window size to reliable default value */
4089     if (window_size_current == NULL)
4090       window_size_current =
4091         getTreeInfoFromIdentifier(window_sizes,
4092                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
4093
4094     /* if that also fails, set current window size to first available value */
4095     if (window_size_current == NULL)
4096       window_size_current = window_sizes;
4097   }
4098
4099   setup.window_scaling_percent = atoi(window_size_current->identifier);
4100
4101   /* needed for displaying window size text instead of identifier */
4102   window_size_text = window_size_current->name;
4103 }
4104
4105 static void execSetupGraphics_setScalingTypes()
4106 {
4107   if (scaling_types == NULL)
4108   {
4109     int i;
4110
4111     for (i = 0; scaling_types_list[i].value != NULL; i++)
4112     {
4113       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4114       char identifier[32], name[32];
4115       char *value = scaling_types_list[i].value;
4116       char *text = scaling_types_list[i].text;
4117
4118       ti->node_top = &scaling_types;
4119       ti->sort_priority = i;
4120
4121       sprintf(identifier, "%s", value);
4122       sprintf(name, "%s", text);
4123
4124       setString(&ti->identifier, identifier);
4125       setString(&ti->name, name);
4126       setString(&ti->name_sorting, name);
4127       setString(&ti->infotext, "Anti-Aliasing");
4128
4129       pushTreeInfo(&scaling_types, ti);
4130     }
4131
4132     /* sort scaling type values to start with lowest scaling type value */
4133     sortTreeInfo(&scaling_types);
4134
4135     /* set current scaling type value to configured scaling type value */
4136     scaling_type_current =
4137       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
4138
4139     /* if that fails, set current scaling type to reliable default value */
4140     if (scaling_type_current == NULL)
4141       scaling_type_current =
4142         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
4143
4144     /* if that also fails, set current scaling type to first available value */
4145     if (scaling_type_current == NULL)
4146       scaling_type_current = scaling_types;
4147   }
4148
4149   setup.window_scaling_quality = scaling_type_current->identifier;
4150
4151   /* needed for displaying scaling type text instead of identifier */
4152   scaling_type_text = scaling_type_current->name;
4153 }
4154
4155 static void execSetupGraphics_setScreenModes()
4156 {
4157   // if (screen_modes == NULL && video.fullscreen_available)
4158   if (screen_modes == NULL && video.fullscreen_modes != NULL)
4159   {
4160     int i;
4161
4162     for (i = 0; video.fullscreen_modes[i].width != -1; i++)
4163     {
4164       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4165       char identifier[32], name[32];
4166       int x = video.fullscreen_modes[i].width;
4167       int y = video.fullscreen_modes[i].height;
4168       int xx, yy;
4169
4170       get_aspect_ratio_from_screen_mode(&video.fullscreen_modes[i], &xx, &yy);
4171
4172       ti->node_top = &screen_modes;
4173       ti->sort_priority = x * 10000 + y;
4174
4175       sprintf(identifier, "%dx%d", x, y);
4176       sprintf(name, "%d x %d [%d:%d]", x, y, xx, yy);
4177
4178       setString(&ti->identifier, identifier);
4179       setString(&ti->name, name);
4180       setString(&ti->name_sorting, name);
4181       setString(&ti->infotext, "Fullscreen Mode");
4182
4183       pushTreeInfo(&screen_modes, ti);
4184     }
4185
4186     /* sort fullscreen modes to start with lowest available screen resolution */
4187     sortTreeInfo(&screen_modes);
4188
4189     /* set current screen mode for fullscreen mode to configured setup value */
4190     screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4191                                                     setup.fullscreen_mode);
4192
4193     /* if that fails, set current screen mode to reliable default value */
4194     if (screen_mode_current == NULL)
4195       screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4196                                                       DEFAULT_FULLSCREEN_MODE);
4197
4198     /* if that also fails, set current screen mode to first available mode */
4199     if (screen_mode_current == NULL)
4200       screen_mode_current = screen_modes;
4201
4202     if (screen_mode_current == NULL)
4203       video.fullscreen_available = FALSE;
4204   }
4205
4206   // if (video.fullscreen_available)
4207   if (screen_mode_current != NULL)
4208   {
4209     setup.fullscreen_mode = screen_mode_current->identifier;
4210
4211     /* needed for displaying screen mode name instead of identifier */
4212     screen_mode_text = screen_mode_current->name;
4213   }
4214 }
4215
4216 static void execSetupGraphics()
4217 {
4218   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
4219   {
4220     // update "setup.window_scaling_percent" from list selection
4221     execSetupGraphics_setWindowSizes(FALSE);
4222   }
4223   else
4224   {
4225     // update list selection from "setup.window_scaling_percent"
4226     execSetupGraphics_setWindowSizes(TRUE);
4227   }
4228
4229   execSetupGraphics_setScalingTypes();
4230   execSetupGraphics_setScreenModes();
4231
4232   setup_mode = SETUP_MODE_GRAPHICS;
4233
4234   DrawSetupScreen();
4235
4236 #if defined(TARGET_SDL2)
4237   // window scaling may have changed at this point
4238   ToggleFullscreenOrChangeWindowScalingIfNeeded();
4239
4240   // window scaling quality may have changed at this point
4241   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
4242     SDLSetWindowScalingQuality(setup.window_scaling_quality);
4243 #endif
4244 }
4245
4246 #if !defined(PLATFORM_ANDROID)
4247 #if defined(TARGET_SDL2)
4248 static void execSetupChooseWindowSize()
4249 {
4250   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
4251
4252   DrawSetupScreen();
4253 }
4254
4255 static void execSetupChooseScalingType()
4256 {
4257   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
4258
4259   DrawSetupScreen();
4260 }
4261 #else
4262 static void execSetupChooseScreenMode()
4263 {
4264   if (!video.fullscreen_available)
4265     return;
4266
4267   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4268
4269   DrawSetupScreen();
4270 }
4271 #endif
4272 #endif
4273
4274 static void execSetupChooseVolumeSimple()
4275 {
4276   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
4277
4278   DrawSetupScreen();
4279 }
4280
4281 static void execSetupChooseVolumeLoops()
4282 {
4283   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
4284
4285   DrawSetupScreen();
4286 }
4287
4288 static void execSetupChooseVolumeMusic()
4289 {
4290   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
4291
4292   DrawSetupScreen();
4293 }
4294
4295 static void execSetupSound()
4296 {
4297   if (volumes_simple == NULL)
4298   {
4299     int i;
4300
4301     for (i = 0; volumes_list[i].value != -1; i++)
4302     {
4303       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4304       char identifier[32], name[32];
4305       int value = volumes_list[i].value;
4306       char *text = volumes_list[i].text;
4307
4308       ti->node_top = &volumes_simple;
4309       ti->sort_priority = value;
4310
4311       sprintf(identifier, "%d", value);
4312       sprintf(name, "%s", text);
4313
4314       setString(&ti->identifier, identifier);
4315       setString(&ti->name, name);
4316       setString(&ti->name_sorting, name);
4317       setString(&ti->infotext, "Sound Volume");
4318
4319       pushTreeInfo(&volumes_simple, ti);
4320     }
4321
4322     /* sort volume values to start with lowest volume value */
4323     sortTreeInfo(&volumes_simple);
4324
4325     /* set current volume value to configured volume value */
4326     volume_simple_current =
4327       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
4328
4329     /* if that fails, set current volume to reliable default value */
4330     if (volume_simple_current == NULL)
4331       volume_simple_current =
4332         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
4333
4334     /* if that also fails, set current volume to first available value */
4335     if (volume_simple_current == NULL)
4336       volume_simple_current = volumes_simple;
4337   }
4338
4339   if (volumes_loops == NULL)
4340   {
4341     int i;
4342
4343     for (i = 0; volumes_list[i].value != -1; i++)
4344     {
4345       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4346       char identifier[32], name[32];
4347       int value = volumes_list[i].value;
4348       char *text = volumes_list[i].text;
4349
4350       ti->node_top = &volumes_loops;
4351       ti->sort_priority = value;
4352
4353       sprintf(identifier, "%d", value);
4354       sprintf(name, "%s", text);
4355
4356       setString(&ti->identifier, identifier);
4357       setString(&ti->name, name);
4358       setString(&ti->name_sorting, name);
4359       setString(&ti->infotext, "Loops Volume");
4360
4361       pushTreeInfo(&volumes_loops, ti);
4362     }
4363
4364     /* sort volume values to start with lowest volume value */
4365     sortTreeInfo(&volumes_loops);
4366
4367     /* set current volume value to configured volume value */
4368     volume_loops_current =
4369       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
4370
4371     /* if that fails, set current volume to reliable default value */
4372     if (volume_loops_current == NULL)
4373       volume_loops_current =
4374         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
4375
4376     /* if that also fails, set current volume to first available value */
4377     if (volume_loops_current == NULL)
4378       volume_loops_current = volumes_loops;
4379   }
4380
4381   if (volumes_music == NULL)
4382   {
4383     int i;
4384
4385     for (i = 0; volumes_list[i].value != -1; i++)
4386     {
4387       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4388       char identifier[32], name[32];
4389       int value = volumes_list[i].value;
4390       char *text = volumes_list[i].text;
4391
4392       ti->node_top = &volumes_music;
4393       ti->sort_priority = value;
4394
4395       sprintf(identifier, "%d", value);
4396       sprintf(name, "%s", text);
4397
4398       setString(&ti->identifier, identifier);
4399       setString(&ti->name, name);
4400       setString(&ti->name_sorting, name);
4401       setString(&ti->infotext, "Music Volume");
4402
4403       pushTreeInfo(&volumes_music, ti);
4404     }
4405
4406     /* sort volume values to start with lowest volume value */
4407     sortTreeInfo(&volumes_music);
4408
4409     /* set current volume value to configured volume value */
4410     volume_music_current =
4411       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
4412
4413     /* if that fails, set current volume to reliable default value */
4414     if (volume_music_current == NULL)
4415       volume_music_current =
4416         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
4417
4418     /* if that also fails, set current volume to first available value */
4419     if (volume_music_current == NULL)
4420       volume_music_current = volumes_music;
4421   }
4422
4423   setup.volume_simple = atoi(volume_simple_current->identifier);
4424   setup.volume_loops  = atoi(volume_loops_current->identifier);
4425   setup.volume_music  = atoi(volume_music_current->identifier);
4426
4427   /* needed for displaying volume text instead of identifier */
4428   volume_simple_text = volume_simple_current->name;
4429   volume_loops_text = volume_loops_current->name;
4430   volume_music_text = volume_music_current->name;
4431
4432   setup_mode = SETUP_MODE_SOUND;
4433
4434   DrawSetupScreen();
4435 }
4436
4437 static void execSetupChooseTouchControls()
4438 {
4439   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
4440
4441   DrawSetupScreen();
4442 }
4443
4444 static void execSetupChooseMoveDistance()
4445 {
4446   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
4447
4448   DrawSetupScreen();
4449 }
4450
4451 static void execSetupChooseDropDistance()
4452 {
4453   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
4454
4455   DrawSetupScreen();
4456 }
4457
4458 static void execSetupTouch()
4459 {
4460   if (touch_controls == NULL)
4461   {
4462     int i;
4463
4464     for (i = 0; touch_controls_list[i].value != NULL; i++)
4465     {
4466       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4467       char identifier[32], name[32];
4468       char *value = touch_controls_list[i].value;
4469       char *text = touch_controls_list[i].text;
4470
4471       ti->node_top = &touch_controls;
4472       ti->sort_priority = i;
4473
4474       sprintf(identifier, "%s", value);
4475       sprintf(name, "%s", text);
4476
4477       setString(&ti->identifier, identifier);
4478       setString(&ti->name, name);
4479       setString(&ti->name_sorting, name);
4480       setString(&ti->infotext, "Control Type");
4481
4482       pushTreeInfo(&touch_controls, ti);
4483     }
4484
4485     /* sort touch control values to start with lowest touch control value */
4486     sortTreeInfo(&touch_controls);
4487
4488     /* set current touch control value to configured touch control value */
4489     touch_control_current =
4490       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
4491
4492     /* if that fails, set current touch control to reliable default value */
4493     if (touch_control_current == NULL)
4494       touch_control_current =
4495         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
4496
4497     /* if that also fails, set current touch control to first available value */
4498     if (touch_control_current == NULL)
4499       touch_control_current = touch_controls;
4500   }
4501
4502   if (move_distances == NULL)
4503   {
4504     int i;
4505
4506     for (i = 0; distances_list[i].value != -1; i++)
4507     {
4508       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4509       char identifier[32], name[32];
4510       int value = distances_list[i].value;
4511       char *text = distances_list[i].text;
4512
4513       ti->node_top = &move_distances;
4514       ti->sort_priority = value;
4515
4516       sprintf(identifier, "%d", value);
4517       sprintf(name, "%s", text);
4518
4519       setString(&ti->identifier, identifier);
4520       setString(&ti->name, name);
4521       setString(&ti->name_sorting, name);
4522       setString(&ti->infotext, "Move Distance");
4523
4524       pushTreeInfo(&move_distances, ti);
4525     }
4526
4527     /* sort distance values to start with lowest distance value */
4528     sortTreeInfo(&move_distances);
4529
4530     /* set current distance value to configured distance value */
4531     move_distance_current =
4532       getTreeInfoFromIdentifier(move_distances,
4533                                 i_to_a(setup.touch.move_distance));
4534
4535     /* if that fails, set current distance to reliable default value */
4536     if (move_distance_current == NULL)
4537       move_distance_current =
4538         getTreeInfoFromIdentifier(move_distances, i_to_a(1));
4539
4540     /* if that also fails, set current distance to first available value */
4541     if (move_distance_current == NULL)
4542       move_distance_current = move_distances;
4543   }
4544
4545   if (drop_distances == NULL)
4546   {
4547     int i;
4548
4549     for (i = 0; distances_list[i].value != -1; i++)
4550     {
4551       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4552       char identifier[32], name[32];
4553       int value = distances_list[i].value;
4554       char *text = distances_list[i].text;
4555
4556       ti->node_top = &drop_distances;
4557       ti->sort_priority = value;
4558
4559       sprintf(identifier, "%d", value);
4560       sprintf(name, "%s", text);
4561
4562       setString(&ti->identifier, identifier);
4563       setString(&ti->name, name);
4564       setString(&ti->name_sorting, name);
4565       setString(&ti->infotext, "Drop Distance");
4566
4567       pushTreeInfo(&drop_distances, ti);
4568     }
4569
4570     /* sort distance values to start with lowest distance value */
4571     sortTreeInfo(&drop_distances);
4572
4573     /* set current distance value to configured distance value */
4574     drop_distance_current =
4575       getTreeInfoFromIdentifier(drop_distances,
4576                                 i_to_a(setup.touch.drop_distance));
4577
4578     /* if that fails, set current distance to reliable default value */
4579     if (drop_distance_current == NULL)
4580       drop_distance_current =
4581         getTreeInfoFromIdentifier(drop_distances, i_to_a(1));
4582
4583     /* if that also fails, set current distance to first available value */
4584     if (drop_distance_current == NULL)
4585       drop_distance_current = drop_distances;
4586   }
4587
4588   setup.touch.control_type = touch_control_current->identifier;
4589   setup.touch.move_distance = atoi(move_distance_current->identifier);
4590   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
4591
4592   /* needed for displaying volume text instead of identifier */
4593   touch_controls_text = touch_control_current->name;
4594   move_distance_text = move_distance_current->name;
4595   drop_distance_text = drop_distance_current->name;
4596
4597   setup_mode = SETUP_MODE_TOUCH;
4598
4599   DrawSetupScreen();
4600 }
4601
4602 static void execSetupArtwork()
4603 {
4604 #if 0
4605   printf("::: '%s', '%s', '%s'\n",
4606          artwork.gfx_current->subdir,
4607          artwork.gfx_current->fullpath,
4608          artwork.gfx_current->basepath);
4609 #endif
4610
4611   setup.graphics_set = artwork.gfx_current->identifier;
4612   setup.sounds_set = artwork.snd_current->identifier;
4613   setup.music_set = artwork.mus_current->identifier;
4614
4615   /* needed if last screen (setup choice) changed graphics, sounds or music */
4616   ReloadCustomArtwork(0);
4617
4618   /* needed for displaying artwork name instead of artwork identifier */
4619   graphics_set_name = artwork.gfx_current->name;
4620   sounds_set_name = artwork.snd_current->name;
4621   music_set_name = artwork.mus_current->name;
4622
4623   setup_mode = SETUP_MODE_ARTWORK;
4624
4625   DrawSetupScreen();
4626 }
4627
4628 static void execSetupChooseGraphics()
4629 {
4630   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4631
4632   DrawSetupScreen();
4633 }
4634
4635 static void execSetupChooseSounds()
4636 {
4637   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4638
4639   DrawSetupScreen();
4640 }
4641
4642 static void execSetupChooseMusic()
4643 {
4644   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4645
4646   DrawSetupScreen();
4647 }
4648
4649 #if !defined(PLATFORM_ANDROID)
4650 static void execSetupInput()
4651 {
4652   setup_mode = SETUP_MODE_INPUT;
4653
4654   DrawSetupScreen();
4655 }
4656 #endif
4657
4658 static void execSetupShortcuts()
4659 {
4660   setup_mode = SETUP_MODE_SHORTCUTS;
4661
4662   DrawSetupScreen();
4663 }
4664
4665 static void execSetupShortcuts1()
4666 {
4667   setup_mode = SETUP_MODE_SHORTCUTS_1;
4668
4669   DrawSetupScreen();
4670 }
4671
4672 static void execSetupShortcuts2()
4673 {
4674   setup_mode = SETUP_MODE_SHORTCUTS_2;
4675
4676   DrawSetupScreen();
4677 }
4678
4679 static void execSetupShortcuts3()
4680 {
4681   setup_mode = SETUP_MODE_SHORTCUTS_3;
4682
4683   DrawSetupScreen();
4684 }
4685
4686 static void execSetupShortcuts4()
4687 {
4688   setup_mode = SETUP_MODE_SHORTCUTS_4;
4689
4690   DrawSetupScreen();
4691 }
4692
4693 static void execSetupShortcuts5()
4694 {
4695   setup_mode = SETUP_MODE_SHORTCUTS_5;
4696
4697   DrawSetupScreen();
4698 }
4699
4700 static void execExitSetup()
4701 {
4702   game_status = GAME_MODE_MAIN;
4703
4704   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4705 }
4706
4707 static void execSaveAndExitSetup()
4708 {
4709   SaveSetup();
4710   execExitSetup();
4711 }
4712
4713 static struct TokenInfo setup_info_main[] =
4714 {
4715   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4716   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4717   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4718   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4719   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4720 #if !defined(PLATFORM_ANDROID)
4721   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4722   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
4723 #else
4724   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
4725 #endif
4726   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
4727   { TYPE_EMPTY,         NULL,                   ""                      },
4728   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4729   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4730
4731   { 0,                  NULL,                   NULL                    }
4732 };
4733
4734 static struct TokenInfo setup_info_game[] =
4735 {
4736   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4737   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4738   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4739   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4740   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4741   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4742   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4743   { TYPE_STRING,        &game_speed_text,       ""                      },
4744 #if 1
4745   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
4746   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4747 #endif
4748   { TYPE_EMPTY,         NULL,                   ""                      },
4749   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4750
4751   { 0,                  NULL,                   NULL                    }
4752 };
4753
4754 static struct TokenInfo setup_info_editor[] =
4755 {
4756 #if 0
4757   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4758   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4759   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4760   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4761   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4762   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4763   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4764   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4765 #endif
4766   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4767   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4768   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4769 #if 0
4770   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4771 #endif
4772   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4773   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4774   { TYPE_EMPTY,         NULL,                   ""                      },
4775 #if 0
4776   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4777   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4778   { TYPE_EMPTY,         NULL,                   ""                      },
4779 #endif
4780   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4781   { TYPE_EMPTY,         NULL,                   ""                      },
4782   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4783
4784   { 0,                  NULL,                   NULL                    }
4785 };
4786
4787 static struct TokenInfo setup_info_graphics[] =
4788 {
4789 #if !defined(PLATFORM_ANDROID)
4790   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4791 #if defined(TARGET_SDL2)
4792   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
4793   { TYPE_STRING,        &window_size_text,      ""                      },
4794   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
4795   { TYPE_STRING,        &scaling_type_text,     ""                      },
4796 #else
4797   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4798   { TYPE_STRING,        &screen_mode_text,      ""                      },
4799 #endif
4800 #endif
4801 #if 0
4802   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
4803   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4804 #endif
4805   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4806   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4807   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4808   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4809   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4810   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4811   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
4812   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
4813   { TYPE_EMPTY,         NULL,                   ""                      },
4814   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4815
4816   { 0,                  NULL,                   NULL                    }
4817 };
4818
4819 static struct TokenInfo setup_info_sound[] =
4820 {
4821   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4822   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4823   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4824   { TYPE_EMPTY,         NULL,                   ""                      },
4825   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
4826   { TYPE_STRING,        &volume_simple_text,    ""                      },
4827   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
4828   { TYPE_STRING,        &volume_loops_text,     ""                      },
4829   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
4830   { TYPE_STRING,        &volume_music_text,     ""                      },
4831   { TYPE_EMPTY,         NULL,                   ""                      },
4832   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4833
4834   { 0,                  NULL,                   NULL                    }
4835 };
4836
4837 static struct TokenInfo setup_info_artwork[] =
4838 {
4839   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
4840   { TYPE_STRING,        &graphics_set_name,     ""                      },
4841   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
4842   { TYPE_STRING,        &sounds_set_name,       ""                      },
4843   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
4844   { TYPE_STRING,        &music_set_name,        ""                      },
4845   { TYPE_EMPTY,         NULL,                   ""                      },
4846   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
4847   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
4848   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
4849   { TYPE_EMPTY,         NULL,                   ""                      },
4850   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4851
4852   { 0,                  NULL,                   NULL                    }
4853 };
4854
4855 static struct TokenInfo setup_info_input[] =
4856 {
4857   { TYPE_SWITCH,        NULL,                   "Player:"               },
4858   { TYPE_SWITCH,        NULL,                   "Device:"               },
4859   { TYPE_ENTER_MENU,    NULL,                   ""                      },
4860   { TYPE_EMPTY,         NULL,                   ""                      },
4861   { TYPE_EMPTY,         NULL,                   ""                      },
4862   { TYPE_EMPTY,         NULL,                   ""                      },
4863   { TYPE_EMPTY,         NULL,                   ""                      },
4864   { TYPE_EMPTY,         NULL,                   ""                      },
4865   { TYPE_EMPTY,         NULL,                   ""                      },
4866   { TYPE_EMPTY,         NULL,                   ""                      },
4867   { TYPE_EMPTY,         NULL,                   ""                      },
4868   { TYPE_EMPTY,         NULL,                   ""                      },
4869   { TYPE_EMPTY,         NULL,                   ""                      },
4870   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4871
4872   { 0,                  NULL,                   NULL                    }
4873 };
4874
4875 static struct TokenInfo setup_info_touch[] =
4876 {
4877   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
4878   { TYPE_STRING,        &touch_controls_text,   ""                      },
4879   { TYPE_EMPTY,         NULL,                   ""                      },
4880   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
4881   { TYPE_STRING,        &move_distance_text,    ""                      },
4882   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
4883   { TYPE_STRING,        &drop_distance_text,    ""                      },
4884   { TYPE_EMPTY,         NULL,                   ""                      },
4885   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4886
4887   { 0,                  NULL,                   NULL                    }
4888 };
4889
4890 static struct TokenInfo setup_info_shortcuts[] =
4891 {
4892   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
4893   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
4894   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
4895   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
4896   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
4897   { TYPE_EMPTY,         NULL,                   ""                      },
4898   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4899
4900   { 0,                  NULL,                   NULL                    }
4901 };
4902
4903 static struct TokenInfo setup_info_shortcuts_1[] =
4904 {
4905   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
4906   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
4907   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
4908   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
4909   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
4910   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
4911   { TYPE_EMPTY,         NULL,                   ""                      },
4912   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
4913   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
4914   { TYPE_EMPTY,         NULL,                   ""                      },
4915   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4916
4917   { 0,                  NULL,                   NULL                    }
4918 };
4919
4920 static struct TokenInfo setup_info_shortcuts_2[] =
4921 {
4922   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
4923   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
4924   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
4925   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
4926   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
4927   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
4928   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
4929   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
4930   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
4931   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
4932   { TYPE_EMPTY,         NULL,                   ""                      },
4933   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4934
4935   { 0,                  NULL,                   NULL                    }
4936 };
4937
4938 static struct TokenInfo setup_info_shortcuts_3[] =
4939 {
4940   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
4941   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
4942   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
4943   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
4944   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
4945   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
4946   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
4947   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
4948   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
4949   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
4950   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
4951   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
4952   { TYPE_EMPTY,         NULL,                   ""                      },
4953   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4954
4955   { 0,                  NULL,                   NULL                    }
4956 };
4957
4958 static struct TokenInfo setup_info_shortcuts_4[] =
4959 {
4960   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
4961   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
4962   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
4963   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
4964   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
4965   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
4966   { TYPE_EMPTY,         NULL,                   ""                      },
4967   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4968
4969   { 0,                  NULL,                   NULL                    }
4970 };
4971
4972 static struct TokenInfo setup_info_shortcuts_5[] =
4973 {
4974   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
4975   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
4976   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
4977   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
4978   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
4979   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
4980   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
4981   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
4982   { TYPE_EMPTY,         NULL,                   ""                      },
4983   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4984
4985   { 0,                  NULL,                   NULL                    }
4986 };
4987
4988 static Key getSetupKey()
4989 {
4990   Key key = KSYM_UNDEFINED;
4991   boolean got_key_event = FALSE;
4992
4993   while (!got_key_event)
4994   {
4995     if (PendingEvent())         /* got event */
4996     {
4997       Event event;
4998
4999       NextEvent(&event);
5000
5001       switch (event.type)
5002       {
5003         case EVENT_KEYPRESS:
5004           {
5005             key = GetEventKey((KeyEvent *)&event, TRUE);
5006
5007             /* press 'Escape' or 'Enter' to keep the existing key binding */
5008             if (key == KSYM_Escape || key == KSYM_Return)
5009               key = KSYM_UNDEFINED;     /* keep old value */
5010
5011             got_key_event = TRUE;
5012           }
5013           break;
5014
5015         case EVENT_KEYRELEASE:
5016           key_joystick_mapping = 0;
5017           break;
5018
5019         default:
5020           HandleOtherEvents(&event);
5021           break;
5022       }
5023     }
5024
5025     DoAnimation();
5026     BackToFront();
5027
5028     /* don't eat all CPU time */
5029     Delay(10);
5030   }
5031
5032   return key;
5033 }
5034
5035 static int getSetupTextFont(int type)
5036 {
5037   if (type & (TYPE_SWITCH       |
5038               TYPE_YES_NO       |
5039               TYPE_YES_NO_AUTO  |
5040               TYPE_STRING       |
5041               TYPE_ECS_AGA      |
5042               TYPE_KEYTEXT      |
5043               TYPE_ENTER_LIST))
5044     return FONT_MENU_2;
5045   else
5046     return FONT_MENU_1;
5047 }
5048
5049 static int getSetupValueFont(int type, void *value)
5050 {
5051   if (type & TYPE_KEY)
5052     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
5053   else if (type & TYPE_STRING)
5054     return FONT_VALUE_2;
5055   else if (type & TYPE_ECS_AGA)
5056     return FONT_VALUE_1;
5057   else if (type & TYPE_BOOLEAN_STYLE)
5058     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
5059   else if (type & TYPE_YES_NO_AUTO)
5060     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
5061             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
5062   else
5063     return FONT_VALUE_1;
5064 }
5065
5066 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
5067 {
5068   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5069   struct TokenInfo *si = &setup_info[si_pos];
5070   boolean font_draw_xoffset_modified = FALSE;
5071   int font_draw_xoffset_old = -1;
5072   int xoffset = (num_setup_info < max_setup_info ? -1 : 0);
5073   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
5074   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
5075   int xpos = menu_screen_value_xpos;
5076   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
5077   int startx = mSX + xpos * 32;
5078   int starty = mSY + ypos * 32;
5079   int font_nr, font_width;
5080   int type = si->type;
5081   void *value = si->value;
5082   char *value_string = getSetupValue(type, value);
5083   int i;
5084
5085   if (value_string == NULL)
5086     return;
5087
5088   if (type & TYPE_KEY)
5089   {
5090     xpos = MENU_SCREEN_START_XPOS;
5091
5092     if (type & TYPE_QUERY)
5093       value_string = "<press key>";
5094   }
5095   else if (type & TYPE_STRING)
5096   {
5097     int max_value_len = (SCR_FIELDX - 2) * 2;
5098
5099     xpos = MENU_SCREEN_START_XPOS;
5100
5101     if (strlen(value_string) > max_value_len)
5102       value_string[max_value_len] = '\0';
5103   }
5104   else if (type & TYPE_YES_NO_AUTO)
5105   {
5106     xpos = menu_screen_value_xpos - 1;
5107   }
5108
5109   startx = mSX + xpos * 32;
5110   starty = mSY + ypos * 32;
5111   font_nr = getSetupValueFont(type, value);
5112   font_width = getFontWidth(font_nr);
5113
5114   /* downward compatibility correction for Juergen Bonhagen's menu settings */
5115   if (setup_mode != SETUP_MODE_INPUT)
5116   {
5117     int max_menu_text_length_big = (menu_screen_value_xpos -
5118                                     MENU_SCREEN_START_XPOS);
5119     int max_menu_text_length_medium = max_menu_text_length_big * 2;
5120     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
5121     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
5122     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
5123     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
5124     int text_font_nr = getSetupTextFont(FONT_MENU_2);
5125     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
5126     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
5127     boolean correct_font_draw_xoffset = FALSE;
5128
5129     if (xpos == MENU_SCREEN_START_XPOS &&
5130         startx + font1_xoffset < text_startx + text_font_xoffset)
5131       correct_font_draw_xoffset = TRUE;
5132
5133     if (xpos == menu_screen_value_xpos &&
5134         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
5135       correct_font_draw_xoffset = TRUE;
5136
5137     /* check if setup value would overlap with setup text when printed */
5138     /* (this can happen for extreme/wrong values for font draw offset) */
5139     if (correct_font_draw_xoffset)
5140     {
5141       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
5142       font_draw_xoffset_modified = TRUE;
5143
5144       if (type & TYPE_KEY)
5145         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
5146       else if (!(type & TYPE_STRING))
5147         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
5148           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
5149     }
5150   }
5151
5152   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
5153     DrawText(startx + i * font_width, starty, " ", font_nr);
5154
5155   DrawText(startx, starty, value_string, font_nr);
5156
5157   if (font_draw_xoffset_modified)
5158     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
5159 }
5160
5161 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
5162 {
5163   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5164   struct TokenInfo *si = &setup_info[si_pos];
5165
5166   if (si->type & TYPE_BOOLEAN_STYLE)
5167   {
5168     *(boolean *)si->value ^= TRUE;
5169   }
5170   else if (si->type & TYPE_YES_NO_AUTO)
5171   {
5172     *(int *)si->value =
5173       (dx == -1 ?
5174        (*(int *)si->value == AUTO ? TRUE :
5175         *(int *)si->value == TRUE ? FALSE : AUTO) :
5176        (*(int *)si->value == TRUE ? AUTO :
5177         *(int *)si->value == AUTO ? FALSE : TRUE));
5178   }
5179   else if (si->type & TYPE_KEY)
5180   {
5181     Key key;
5182
5183     si->type |= TYPE_QUERY;
5184     drawSetupValue(screen_pos, setup_info_pos_raw);
5185     si->type &= ~TYPE_QUERY;
5186
5187     key = getSetupKey();
5188     if (key != KSYM_UNDEFINED)
5189       *(Key *)si->value = key;
5190   }
5191
5192   drawSetupValue(screen_pos, setup_info_pos_raw);
5193
5194   // fullscreen state may have changed at this point
5195   if (si->value == &setup.fullscreen)
5196     ToggleFullscreenOrChangeWindowScalingIfNeeded();
5197 }
5198
5199 static void DrawCursorAndText_Setup(int screen_pos, int setup_info_pos_raw,
5200                                     boolean active)
5201 {
5202   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5203   struct TokenInfo *si = &setup_info[si_pos];
5204   int xpos = MENU_SCREEN_START_XPOS;
5205   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
5206   int font_nr = getSetupTextFont(si->type);
5207
5208   if (setup_info == setup_info_input)
5209     font_nr = FONT_MENU_1;
5210
5211   if (active)
5212     font_nr = FONT_ACTIVE(font_nr);
5213
5214   DrawText(mSX + xpos * 32, mSY + ypos * 32, si->text, font_nr);
5215
5216   if (si->type & ~TYPE_SKIP_ENTRY)
5217     drawCursor(screen_pos, active);
5218 }
5219
5220 static void drawSetupInfoList(struct TokenInfo *setup_info,
5221                               int first_entry, int num_page_entries)
5222 {
5223   int i;
5224
5225   if (num_page_entries > NUM_MENU_ENTRIES_ON_SCREEN)
5226     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
5227
5228   if (num_page_entries > max_setup_info)
5229     num_page_entries = max_setup_info;
5230
5231   if (first_entry + num_page_entries > max_setup_info)
5232     first_entry = 0;
5233
5234   /* clear tree list area, but not title or scrollbar */
5235   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
5236                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
5237                  NUM_MENU_ENTRIES_ON_SCREEN * 32);
5238
5239   for (i = 0; i < num_page_entries; i++)
5240   {
5241     int setup_info_pos = first_entry + i;
5242     struct TokenInfo *si = &setup_info[setup_info_pos];
5243     void *value_ptr = si->value;
5244
5245     /* set some entries to "unchangeable" according to other variables */
5246     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
5247         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
5248         (value_ptr == &setup.sound_music  && !audio.music_available) ||
5249         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
5250         (value_ptr == &screen_mode_text   && !video.fullscreen_available) ||
5251         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
5252         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
5253       si->type |= TYPE_GHOSTED;
5254
5255     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5256       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5257     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5258       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5259     else if (si->type & ~TYPE_SKIP_ENTRY)
5260       initCursor(i, IMG_MENU_BUTTON);
5261
5262     DrawCursorAndText_Setup(i, setup_info_pos, FALSE);
5263
5264     if (si->type & TYPE_VALUE)
5265       drawSetupValue(i, setup_info_pos);
5266   }
5267 }
5268
5269 static void DrawSetupScreen_Generic()
5270 {
5271   boolean redraw_all = FALSE;
5272   char *title_string = NULL;
5273   int i;
5274
5275   UnmapAllGadgets();
5276
5277   FreeScreenGadgets();
5278   CreateScreenGadgets();
5279
5280   CloseDoor(DOOR_CLOSE_2);
5281
5282   if (redraw_mask & REDRAW_ALL)
5283     redraw_all = TRUE;
5284
5285   FadeOut(REDRAW_FIELD);
5286
5287   ClearField();
5288
5289   if (setup_mode == SETUP_MODE_MAIN)
5290   {
5291     setup_info = setup_info_main;
5292     title_string = "Setup";
5293   }
5294   else if (setup_mode == SETUP_MODE_GAME)
5295   {
5296     setup_info = setup_info_game;
5297     title_string = "Setup Game";
5298   }
5299   else if (setup_mode == SETUP_MODE_EDITOR)
5300   {
5301     setup_info = setup_info_editor;
5302     title_string = "Setup Editor";
5303   }
5304   else if (setup_mode == SETUP_MODE_GRAPHICS)
5305   {
5306     setup_info = setup_info_graphics;
5307     title_string = "Setup Graphics";
5308   }
5309   else if (setup_mode == SETUP_MODE_SOUND)
5310   {
5311     setup_info = setup_info_sound;
5312     title_string = "Setup Sound";
5313   }
5314   else if (setup_mode == SETUP_MODE_ARTWORK)
5315   {
5316     setup_info = setup_info_artwork;
5317     title_string = "Custom Artwork";
5318   }
5319   else if (setup_mode == SETUP_MODE_TOUCH)
5320   {
5321     setup_info = setup_info_touch;
5322     title_string = "Setup Touch Ctrls";
5323   }
5324   else if (setup_mode == SETUP_MODE_SHORTCUTS)
5325   {
5326     setup_info = setup_info_shortcuts;
5327     title_string = "Setup Shortcuts";
5328   }
5329   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
5330   {
5331     setup_info = setup_info_shortcuts_1;
5332     title_string = "Setup Shortcuts";
5333   }
5334   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
5335   {
5336     setup_info = setup_info_shortcuts_2;
5337     title_string = "Setup Shortcuts";
5338   }
5339   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
5340   {
5341     setup_info = setup_info_shortcuts_3;
5342     title_string = "Setup Shortcuts";
5343   }
5344   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
5345   {
5346     setup_info = setup_info_shortcuts_4;
5347     title_string = "Setup Shortcuts";
5348   }
5349   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
5350   {
5351     setup_info = setup_info_shortcuts_5;
5352     title_string = "Setup Shortcuts";
5353   }
5354
5355   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
5356
5357   // determine maximal number of setup entries that can be displayed on screen
5358   num_setup_info = 0;
5359   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5360     num_setup_info++;
5361
5362   // determine maximal number of setup entries available for this setup screen
5363   max_setup_info = 0;
5364   for (i = 0; setup_info[i].type != 0; i++)
5365     max_setup_info++;
5366
5367   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
5368
5369   MapScreenGadgets(max_setup_info);
5370
5371   if (redraw_all)
5372     redraw_mask = REDRAW_ALL;
5373
5374   FadeIn(redraw_mask);
5375
5376   InitAnimation();
5377 }
5378
5379 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
5380 {
5381   static int choice_store[MAX_SETUP_MODES];
5382   static int first_entry_store[MAX_SETUP_MODES];
5383   int choice = choice_store[setup_mode];                /* starts with 0 */
5384   int first_entry = first_entry_store[setup_mode];      /* starts with 0 */
5385   int x = 0;
5386   int y = choice - first_entry;
5387   int y_old = y;
5388   boolean position_set_by_scrollbar = (dx == 999);
5389   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
5390   int num_page_entries;
5391
5392   num_page_entries = MIN(max_setup_info, NUM_MENU_ENTRIES_ON_SCREEN);
5393
5394   if (button == MB_MENU_INITIALIZE)
5395   {
5396     /* advance to first valid menu entry */
5397     while (choice < num_setup_info &&
5398            setup_info[choice].type & TYPE_SKIP_ENTRY)
5399       choice++;
5400
5401     if (position_set_by_scrollbar)
5402       first_entry = first_entry_store[setup_mode] = dy;
5403     else
5404       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5405                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5406
5407     drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5408
5409     if (choice < first_entry)
5410     {
5411       choice = first_entry;
5412
5413       if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5414         choice++;
5415     }
5416     else if (choice > first_entry + num_page_entries - 1)
5417     {
5418       choice = first_entry + num_page_entries - 1;
5419
5420       if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5421         choice--;
5422     }
5423
5424     choice_store[setup_mode] = choice;
5425
5426     DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5427
5428     return;
5429   }
5430   else if (button == MB_MENU_LEAVE)
5431   {
5432     int i;
5433
5434     PlaySound(SND_MENU_ITEM_SELECTING);
5435
5436     for (i = 0; setup_info[i].type != 0; i++)
5437     {
5438       if (setup_info[i].type & TYPE_LEAVE_MENU)
5439       {
5440         void (*menu_callback_function)(void) = setup_info[i].value;
5441
5442         FadeSetLeaveMenu();
5443
5444         menu_callback_function();
5445
5446         break;  /* absolutely needed because function changes 'setup_info'! */
5447       }
5448     }
5449
5450     return;
5451   }
5452
5453   if (mx || my)         /* mouse input */
5454   {
5455     x = (mx - mSX) / 32;
5456     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5457   }
5458   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
5459   {
5460     /* move cursor instead of scrolling when already at start/end of list */
5461     if (dy == -1 * SCROLL_LINE && first_entry == 0)
5462       dy = -1;
5463     else if (dy == +1 * SCROLL_LINE &&
5464              first_entry + num_page_entries == max_setup_info)
5465       dy = 1;
5466
5467     /* handle scrolling screen one line or page */
5468     if (y + dy < 0 ||
5469         y + dy > num_page_entries - 1)
5470     {
5471       boolean redraw = FALSE;
5472
5473       if (ABS(dy) == SCROLL_PAGE)
5474         step = num_page_entries - 1;
5475
5476       if (dy < 0 && first_entry > 0)
5477       {
5478         /* scroll page/line up */
5479
5480         first_entry -= step;
5481         if (first_entry < 0)
5482           first_entry = 0;
5483
5484         redraw = TRUE;
5485       }
5486       else if (dy > 0 && first_entry + num_page_entries < max_setup_info)
5487       {
5488         /* scroll page/line down */
5489
5490         first_entry += step;
5491         if (first_entry + num_page_entries > max_setup_info)
5492           first_entry = MAX(0, max_setup_info - num_page_entries);
5493
5494         redraw = TRUE;
5495       }
5496
5497       if (redraw)
5498       {
5499         choice += first_entry - first_entry_store[setup_mode];
5500
5501         if (choice < first_entry)
5502         {
5503           choice = first_entry;
5504
5505           if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5506             choice++;
5507         }
5508         else if (choice > first_entry + num_page_entries - 1)
5509         {
5510           choice = first_entry + num_page_entries - 1;
5511
5512           if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5513             choice--;
5514         }
5515         else if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5516         {
5517           choice += SIGN(dy);
5518
5519           if (choice < first_entry ||
5520               choice > first_entry + num_page_entries - 1)
5521           first_entry += SIGN(dy);
5522         }
5523
5524         first_entry_store[setup_mode] = first_entry;
5525         choice_store[setup_mode] = choice;
5526
5527         drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5528
5529         DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5530
5531         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5532                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5533       }
5534
5535       return;
5536     }
5537
5538     if (dx)
5539     {
5540       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
5541
5542       if (setup_info[choice].type & menu_navigation_type ||
5543           setup_info[choice].type & TYPE_BOOLEAN_STYLE ||
5544           setup_info[choice].type & TYPE_YES_NO_AUTO)
5545         button = MB_MENU_CHOICE;
5546     }
5547     else if (dy)
5548       y += dy;
5549
5550     /* jump to next non-empty menu entry (up or down) */
5551     while (first_entry + y > 0 &&
5552            first_entry + y < max_setup_info - 1 &&
5553            setup_info[first_entry + y].type & TYPE_SKIP_ENTRY)
5554       y += dy;
5555
5556     if (!IN_VIS_FIELD(x, y))
5557     {
5558       choice += y - y_old;
5559
5560       if (choice < first_entry)
5561         first_entry = choice;
5562       else if (choice > first_entry + num_page_entries - 1)
5563         first_entry = choice - num_page_entries + 1;
5564
5565       if (first_entry >= 0 &&
5566           first_entry + num_page_entries <= max_setup_info)
5567       {
5568         first_entry_store[setup_mode] = first_entry;
5569
5570         if (choice < first_entry)
5571           choice = first_entry;
5572         else if (choice > first_entry + num_page_entries - 1)
5573           choice = first_entry + num_page_entries - 1;
5574
5575         choice_store[setup_mode] = choice;
5576
5577         drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5578
5579         DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5580
5581         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5582                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5583       }
5584
5585       return;
5586     }
5587   }
5588
5589   if (!anyScrollbarGadgetActive() &&
5590       IN_VIS_FIELD(x, y) &&
5591       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
5592       y >= 0 && y < num_page_entries)
5593   {
5594     if (button)
5595     {
5596       if (first_entry + y != choice &&
5597           setup_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
5598       {
5599         PlaySound(SND_MENU_ITEM_ACTIVATING);
5600
5601         DrawCursorAndText_Setup(choice - first_entry, choice, FALSE);
5602         DrawCursorAndText_Setup(y, first_entry + y, TRUE);
5603
5604         choice = choice_store[setup_mode] = first_entry + y;
5605       }
5606     }
5607     else if (!(setup_info[first_entry + y].type & TYPE_GHOSTED))
5608     {
5609       PlaySound(SND_MENU_ITEM_SELECTING);
5610
5611       /* when selecting key headline, execute function for key value change */
5612       if (setup_info[first_entry + y].type & TYPE_KEYTEXT &&
5613           setup_info[first_entry + y + 1].type & TYPE_KEY)
5614         y++;
5615
5616       /* when selecting string value, execute function for list selection */
5617       if (setup_info[first_entry + y].type & TYPE_STRING && y > 0 &&
5618           setup_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
5619         y--;
5620
5621       if (setup_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
5622       {
5623         void (*menu_callback_function)(void) =
5624           setup_info[first_entry + y].value;
5625
5626         FadeSetFromType(setup_info[first_entry + y].type);
5627
5628         menu_callback_function();
5629       }
5630       else
5631       {
5632         if (setup_info[first_entry + y].type & TYPE_VALUE)
5633           changeSetupValue(y, first_entry + y, dx);
5634       }
5635     }
5636   }
5637 }
5638
5639 void DrawSetupScreen_Input()
5640 {
5641   int i;
5642
5643   FadeOut(REDRAW_FIELD);
5644
5645   ClearField();
5646
5647   setup_info = setup_info_input;
5648
5649   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5650
5651   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5652                     "Joysticks deactivated on this screen");
5653
5654   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5655   {
5656     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5657       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5658     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5659       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5660     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5661       initCursor(i, IMG_MENU_BUTTON);
5662
5663     DrawCursorAndText_Setup(i, -1, FALSE);
5664   }
5665
5666   /* create gadgets for setup input menu screen */
5667   FreeScreenGadgets();
5668   CreateScreenGadgets();
5669
5670   /* map gadgets for setup input menu screen */
5671   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5672
5673   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5674
5675   FadeIn(REDRAW_FIELD);
5676
5677   InitAnimation();
5678 }
5679
5680 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5681 {
5682   if (device_name == NULL)
5683     return;
5684
5685   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5686     device_nr = 0;
5687
5688   if (strlen(device_name) > 1)
5689   {
5690     char c1 = device_name[strlen(device_name) - 1];
5691     char c2 = device_name[strlen(device_name) - 2];
5692
5693     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5694       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5695   }
5696   else
5697     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5698             strlen(device_name));
5699 }
5700
5701 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5702 {
5703   int i;
5704   static struct SetupKeyboardInfo custom_key;
5705   static struct
5706   {
5707     Key *key;
5708     char *text;
5709   } custom[] =
5710   {
5711     { &custom_key.left,  "Joystick Left"  },
5712     { &custom_key.right, "Joystick Right" },
5713     { &custom_key.up,    "Joystick Up"    },
5714     { &custom_key.down,  "Joystick Down"  },
5715     { &custom_key.snap,  "Button 1"       },
5716     { &custom_key.drop,  "Button 2"       }
5717   };
5718   static char *joystick_name[MAX_PLAYERS] =
5719   {
5720     "Joystick1",
5721     "Joystick2",
5722     "Joystick3",
5723     "Joystick4"
5724   };
5725   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5726
5727   InitJoysticks();
5728
5729   custom_key = setup.input[player_nr].key;
5730
5731   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5732            FONT_INPUT_1_ACTIVE);
5733
5734   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5735                              TILEX, TILEY);
5736   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5737                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5738
5739   if (setup.input[player_nr].use_joystick)
5740   {
5741     char *device_name = setup.input[player_nr].joy.device_name;
5742     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5743     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5744
5745     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5746     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5747   }
5748   else
5749   {
5750     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5751     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5752   }
5753
5754   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5755
5756   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5757   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5758   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5759   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5760
5761   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5762   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5763   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5764   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5765   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5766   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5767
5768   for (i = 0; i < 6; i++)
5769   {
5770     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5771
5772     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5773              "              ", FONT_VALUE_1);
5774     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5775              (setup.input[player_nr].use_joystick ?
5776               custom[i].text :
5777               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5778   }
5779 }
5780
5781 static int input_player_nr = 0;
5782
5783 void HandleSetupScreen_Input_Player(int step, int direction)
5784 {
5785   int old_player_nr = input_player_nr;
5786   int new_player_nr;
5787
5788   new_player_nr = old_player_nr + step * direction;
5789   if (new_player_nr < 0)
5790     new_player_nr = 0;
5791   if (new_player_nr > MAX_PLAYERS - 1)
5792     new_player_nr = MAX_PLAYERS - 1;
5793
5794   if (new_player_nr != old_player_nr)
5795   {
5796     input_player_nr = new_player_nr;
5797
5798     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5799   }
5800 }
5801
5802 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5803 {
5804   static int choice = 0;
5805   int x = 0;
5806   int y = choice;
5807   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5808   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5809   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5810   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5811
5812   if (button == MB_MENU_INITIALIZE)
5813   {
5814     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5815
5816     DrawCursorAndText_Setup(choice, -1, TRUE);
5817
5818     return;
5819   }
5820   else if (button == MB_MENU_LEAVE)
5821   {
5822     setup_mode = SETUP_MODE_MAIN;
5823     DrawSetupScreen();
5824     InitJoysticks();
5825
5826     return;
5827   }
5828
5829   if (mx || my)         /* mouse input */
5830   {
5831     x = (mx - mSX) / 32;
5832     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5833   }
5834   else if (dx || dy)    /* keyboard input */
5835   {
5836     if (dx && choice == 0)
5837       x = (dx < 0 ? 10 : 12);
5838     else if ((dx && choice == 1) ||
5839              (dx == +1 && choice == 2) ||
5840              (dx == -1 && choice == pos_end))
5841       button = MB_MENU_CHOICE;
5842     else if (dy)
5843       y = choice + dy;
5844
5845     if (y >= pos_empty1 && y <= pos_empty2)
5846       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
5847   }
5848
5849   if (y == 0 && dx != 0 && button)
5850   {
5851     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
5852   }
5853   else if (IN_VIS_FIELD(x, y) &&
5854            y >= pos_start && y <= pos_end &&
5855            !(y >= pos_empty1 && y <= pos_empty2))
5856   {
5857     if (button)
5858     {
5859       if (y != choice)
5860       {
5861         DrawCursorAndText_Setup(choice, -1, FALSE);
5862         DrawCursorAndText_Setup(y, -1, TRUE);
5863
5864         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
5865
5866         choice = y;
5867       }
5868     }
5869     else
5870     {
5871       if (y == 1)
5872       {
5873         char *device_name = setup.input[input_player_nr].joy.device_name;
5874
5875         if (!setup.input[input_player_nr].use_joystick)
5876         {
5877           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
5878
5879           setJoystickDeviceToNr(device_name, new_device_nr);
5880           setup.input[input_player_nr].use_joystick = TRUE;
5881         }
5882         else
5883         {
5884           int device_nr = getJoystickNrFromDeviceName(device_name);
5885           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
5886
5887           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
5888             setup.input[input_player_nr].use_joystick = FALSE;
5889           else
5890             setJoystickDeviceToNr(device_name, new_device_nr);
5891         }
5892
5893         drawPlayerSetupInputInfo(input_player_nr, FALSE);
5894       }
5895       else if (y == 2)
5896       {
5897         if (setup.input[input_player_nr].use_joystick)
5898         {
5899           InitJoysticks();
5900           CalibrateJoystick(input_player_nr);
5901         }
5902         else
5903           CustomizeKeyboard(input_player_nr);
5904       }
5905       else if (y == pos_end)
5906       {
5907         InitJoysticks();
5908
5909         FadeSetLeaveMenu();
5910
5911         setup_mode = SETUP_MODE_MAIN;
5912         DrawSetupScreen();
5913       }
5914     }
5915   }
5916 }
5917
5918 void CustomizeKeyboard(int player_nr)
5919 {
5920   int i;
5921   int step_nr;
5922   boolean finished = FALSE;
5923   static struct SetupKeyboardInfo custom_key;
5924   static struct
5925   {
5926     Key *key;
5927     char *text;
5928   } customize_step[] =
5929   {
5930     { &custom_key.left,  "Move Left"    },
5931     { &custom_key.right, "Move Right"   },
5932     { &custom_key.up,    "Move Up"      },
5933     { &custom_key.down,  "Move Down"    },
5934     { &custom_key.snap,  "Snap Field"   },
5935     { &custom_key.drop,  "Drop Element" }
5936   };
5937
5938   /* read existing key bindings from player setup */
5939   custom_key = setup.input[player_nr].key;
5940
5941   FadeSetEnterMenu();
5942   FadeOut(REDRAW_FIELD);
5943
5944   ClearField();
5945
5946   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
5947
5948   step_nr = 0;
5949   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5950            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5951   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5952            "Key:", FONT_INPUT_1_ACTIVE);
5953   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5954            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
5955
5956   FadeIn(REDRAW_FIELD);
5957
5958   InitAnimation();
5959
5960   while (!finished)
5961   {
5962     if (PendingEvent())         /* got event */
5963     {
5964       Event event;
5965
5966       NextEvent(&event);
5967
5968       switch (event.type)
5969       {
5970         case EVENT_KEYPRESS:
5971           {
5972             Key key = GetEventKey((KeyEvent *)&event, FALSE);
5973
5974             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
5975             {
5976               if (key == KSYM_Escape)
5977                 FadeSkipNextFadeIn();
5978
5979               finished = TRUE;
5980               break;
5981             }
5982
5983             /* all keys configured -- wait for "Escape" or "Return" key */
5984             if (step_nr == 6)
5985               break;
5986
5987             /* press 'Enter' to keep the existing key binding */
5988             if (key == KSYM_Return)
5989               key = *customize_step[step_nr].key;
5990
5991             /* check if key already used */
5992             for (i = 0; i < step_nr; i++)
5993               if (*customize_step[i].key == key)
5994                 break;
5995             if (i < step_nr)
5996               break;
5997
5998             /* got new key binding */
5999             *customize_step[step_nr].key = key;
6000             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6001                      "             ", FONT_VALUE_1);
6002             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6003                      getKeyNameFromKey(key), FONT_VALUE_1);
6004             step_nr++;
6005
6006             /* un-highlight last query */
6007             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
6008                      customize_step[step_nr - 1].text, FONT_MENU_1);
6009             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
6010                      "Key:", FONT_MENU_1);
6011
6012             /* press 'Enter' to leave */
6013             if (step_nr == 6)
6014             {
6015               DrawText(mSX + 16, mSY + 15 * 32 + 16,
6016                        "Press Enter", FONT_TITLE_1);
6017               break;
6018             }
6019
6020             /* query next key binding */
6021             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6022                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6023             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6024                      "Key:", FONT_INPUT_1_ACTIVE);
6025             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6026                      getKeyNameFromKey(*customize_step[step_nr].key),
6027                      FONT_VALUE_OLD);
6028           }
6029           break;
6030
6031         case EVENT_KEYRELEASE:
6032           key_joystick_mapping = 0;
6033           break;
6034
6035         default:
6036           HandleOtherEvents(&event);
6037           break;
6038       }
6039     }
6040
6041     DoAnimation();
6042     BackToFront();
6043
6044     /* don't eat all CPU time */
6045     Delay(10);
6046   }
6047
6048   /* write new key bindings back to player setup */
6049   setup.input[player_nr].key = custom_key;
6050
6051   StopAnimation();
6052   DrawSetupScreen_Input();
6053 }
6054
6055 static boolean CalibrateJoystickMain(int player_nr)
6056 {
6057   int new_joystick_xleft = JOYSTICK_XMIDDLE;
6058   int new_joystick_xright = JOYSTICK_XMIDDLE;
6059   int new_joystick_yupper = JOYSTICK_YMIDDLE;
6060   int new_joystick_ylower = JOYSTICK_YMIDDLE;
6061   int new_joystick_xmiddle, new_joystick_ymiddle;
6062
6063   int joystick_fd = joystick.fd[player_nr];
6064   int x, y, last_x, last_y, xpos = 8, ypos = 3;
6065   boolean check[3][3];
6066   int check_remaining = 3 * 3;
6067   int joy_x, joy_y;
6068   int joy_value;
6069   int result = -1;
6070
6071   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
6072     return FALSE;
6073
6074   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
6075     return FALSE;
6076
6077   FadeSetEnterMenu();
6078   FadeOut(REDRAW_FIELD);
6079
6080   ClearField();
6081
6082   for (y = 0; y < 3; y++)
6083   {
6084     for (x = 0; x < 3; x++)
6085     {
6086       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
6087       check[x][y] = FALSE;
6088     }
6089   }
6090
6091   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
6092   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
6093   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
6094   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
6095   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
6096   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
6097   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
6098
6099   joy_value = Joystick(player_nr);
6100   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6101   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6102
6103   /* eventually uncalibrated center position (joystick could be uncentered) */
6104   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6105     return FALSE;
6106
6107   new_joystick_xmiddle = joy_x;
6108   new_joystick_ymiddle = joy_y;
6109
6110   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
6111
6112   FadeIn(REDRAW_FIELD);
6113
6114   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
6115   InitAnimation();
6116
6117   while (result < 0)
6118   {
6119     if (PendingEvent())         /* got event */
6120     {
6121       Event event;
6122
6123       NextEvent(&event);
6124
6125       switch (event.type)
6126       {
6127         case EVENT_KEYPRESS:
6128           switch (GetEventKey((KeyEvent *)&event, TRUE))
6129           {
6130             case KSYM_Return:
6131               if (check_remaining == 0)
6132                 result = 1;
6133               break;
6134
6135             case KSYM_Escape:
6136               FadeSkipNextFadeIn();
6137               result = 0;
6138               break;
6139
6140             default:
6141               break;
6142           }
6143           break;
6144
6145         case EVENT_KEYRELEASE:
6146           key_joystick_mapping = 0;
6147           break;
6148
6149         default:
6150           HandleOtherEvents(&event);
6151           break;
6152       }
6153     }
6154
6155     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6156       return FALSE;
6157
6158     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
6159     new_joystick_xright = MAX(new_joystick_xright, joy_x);
6160     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
6161     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
6162
6163     setup.input[player_nr].joy.xleft = new_joystick_xleft;
6164     setup.input[player_nr].joy.yupper = new_joystick_yupper;
6165     setup.input[player_nr].joy.xright = new_joystick_xright;
6166     setup.input[player_nr].joy.ylower = new_joystick_ylower;
6167     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
6168     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
6169
6170     CheckJoystickData();
6171
6172     joy_value = Joystick(player_nr);
6173
6174     if (joy_value & JOY_BUTTON && check_remaining == 0)
6175       result = 1;
6176
6177     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6178     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6179
6180     if (x != last_x || y != last_y)
6181     {
6182       DrawFixedGraphic(xpos + last_x, ypos + last_y,
6183                        IMG_MENU_CALIBRATE_YELLOW, 0);
6184       DrawFixedGraphic(xpos + x,      ypos + y,
6185                        IMG_MENU_CALIBRATE_RED,    0);
6186
6187       last_x = x;
6188       last_y = y;
6189
6190       if (check_remaining > 0 && !check[x+1][y+1])
6191       {
6192         check[x+1][y+1] = TRUE;
6193         check_remaining--;
6194       }
6195     }
6196
6197     DoAnimation();
6198     BackToFront();
6199
6200     /* don't eat all CPU time */
6201     Delay(10);
6202   }
6203
6204   /* calibrated center position (joystick should now be centered) */
6205   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6206     return FALSE;
6207
6208   new_joystick_xmiddle = joy_x;
6209   new_joystick_ymiddle = joy_y;
6210
6211   StopAnimation();
6212
6213   /* wait until the last pressed button was released */
6214   while (Joystick(player_nr) & JOY_BUTTON)
6215   {
6216     if (PendingEvent())         /* got event */
6217     {
6218       Event event;
6219
6220       NextEvent(&event);
6221       HandleOtherEvents(&event);
6222
6223       Delay(10);
6224     }
6225   }
6226
6227   return TRUE;
6228 }
6229
6230 void CalibrateJoystick(int player_nr)
6231 {
6232   if (!CalibrateJoystickMain(player_nr))
6233   {
6234     char *device_name = setup.input[player_nr].joy.device_name;
6235     int nr = getJoystickNrFromDeviceName(device_name) + 1;
6236     int xpos = mSX - SX;
6237     int ypos = mSY - SY;
6238
6239     ClearField();
6240
6241     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
6242     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
6243     BackToFront();
6244
6245     Delay(2000);                /* show error message for a short time */
6246
6247     ClearEventQueue();
6248   }
6249
6250   DrawSetupScreen_Input();
6251 }
6252
6253 void DrawSetupScreen()
6254 {
6255   DeactivateJoystick();
6256
6257   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6258
6259   if (setup_mode == SETUP_MODE_INPUT)
6260     DrawSetupScreen_Input();
6261   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6262     DrawChooseTree(&game_speed_current);
6263   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6264     DrawChooseTree(&scroll_delay_current);
6265   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6266     DrawChooseTree(&screen_mode_current);
6267   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6268     DrawChooseTree(&window_size_current);
6269   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6270     DrawChooseTree(&scaling_type_current);
6271   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6272     DrawChooseTree(&artwork.gfx_current);
6273   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6274     DrawChooseTree(&artwork.snd_current);
6275   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6276     DrawChooseTree(&artwork.mus_current);
6277   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6278     DrawChooseTree(&volume_simple_current);
6279   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6280     DrawChooseTree(&volume_loops_current);
6281   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6282     DrawChooseTree(&volume_music_current);
6283   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6284     DrawChooseTree(&touch_control_current);
6285   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6286     DrawChooseTree(&move_distance_current);
6287   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6288     DrawChooseTree(&drop_distance_current);
6289   else
6290     DrawSetupScreen_Generic();
6291
6292   PlayMenuSound();
6293   PlayMenuMusic();
6294 }
6295
6296 void RedrawSetupScreenAfterFullscreenToggle()
6297 {
6298   if (setup_mode == SETUP_MODE_GRAPHICS ||
6299       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6300   {
6301     // update list selection from "setup.window_scaling_percent"
6302     execSetupGraphics_setWindowSizes(TRUE);
6303
6304     DrawSetupScreen();
6305   }
6306 }
6307
6308 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
6309 {
6310   if (setup_mode == SETUP_MODE_INPUT)
6311     HandleSetupScreen_Input(mx, my, dx, dy, button);
6312   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6313     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
6314   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6315     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
6316   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6317     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
6318   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6319     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
6320   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6321     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
6322   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6323     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
6324   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6325     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
6326   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6327     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
6328   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6329     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
6330   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6331     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
6332   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6333     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
6334   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6335     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
6336   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6337     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
6338   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6339     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
6340   else
6341     HandleSetupScreen_Generic(mx, my, dx, dy, button);
6342 }
6343
6344 void HandleGameActions()
6345 {
6346   if (game_status != GAME_MODE_PLAYING)
6347     return;
6348
6349   GameActions();        /* main game loop */
6350
6351   if (tape.auto_play && !tape.playing)
6352     AutoPlayTape();     /* continue automatically playing next tape */
6353 }
6354
6355
6356 /* ---------- new screen button stuff -------------------------------------- */
6357
6358 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
6359 {
6360   switch (gadget_id)
6361   {
6362     case SCREEN_CTRL_ID_PREV_LEVEL:
6363       *x = mSX + GDI_ACTIVE_POS(menu.main.button.prev_level.x);
6364       *y = mSY + GDI_ACTIVE_POS(menu.main.button.prev_level.y);
6365       break;
6366
6367     case SCREEN_CTRL_ID_NEXT_LEVEL:
6368       *x = mSX + GDI_ACTIVE_POS(menu.main.button.next_level.x);
6369       *y = mSY + GDI_ACTIVE_POS(menu.main.button.next_level.y);
6370       break;
6371
6372     case SCREEN_CTRL_ID_PREV_PLAYER:
6373       *x = mSX + TILEX * 10;
6374       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6375       break;
6376
6377     case SCREEN_CTRL_ID_NEXT_PLAYER:
6378       *x = mSX + TILEX * 12;
6379       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6380       break;
6381
6382     default:
6383       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
6384   }
6385 }
6386
6387 static struct
6388 {
6389   int gfx_unpressed, gfx_pressed;
6390   void (*get_gadget_position)(int *, int *, int);
6391   int gadget_id;
6392   int screen_mask;
6393   char *infotext;
6394 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
6395 {
6396   {
6397     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
6398     getScreenMenuButtonPos,
6399     SCREEN_CTRL_ID_PREV_LEVEL,
6400     SCREEN_MASK_MAIN,
6401     "last level"
6402   },
6403   {
6404     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
6405     getScreenMenuButtonPos,
6406     SCREEN_CTRL_ID_NEXT_LEVEL,
6407     SCREEN_MASK_MAIN,
6408     "next level"
6409   },
6410   {
6411     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
6412     getScreenMenuButtonPos,
6413     SCREEN_CTRL_ID_PREV_PLAYER,
6414     SCREEN_MASK_INPUT,
6415     "last player"
6416   },
6417   {
6418     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
6419     getScreenMenuButtonPos,
6420     SCREEN_CTRL_ID_NEXT_PLAYER,
6421     SCREEN_MASK_INPUT,
6422     "next player"
6423   },
6424 };
6425
6426 static struct
6427 {
6428   int gfx_unpressed, gfx_pressed;
6429   int x, y;
6430   int gadget_id;
6431   char *infotext;
6432 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
6433 {
6434   {
6435     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
6436     -1, -1,     /* these values are not constant, but can change at runtime */
6437     SCREEN_CTRL_ID_SCROLL_UP,
6438     "scroll up"
6439   },
6440   {
6441     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
6442     -1, -1,     /* these values are not constant, but can change at runtime */
6443     SCREEN_CTRL_ID_SCROLL_DOWN,
6444     "scroll down"
6445   }
6446 };
6447
6448 static struct
6449 {
6450   int gfx_unpressed, gfx_pressed;
6451   int x, y;
6452   int width, height;
6453   int type;
6454   int gadget_id;
6455   char *infotext;
6456 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
6457 {
6458   {
6459     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
6460     -1, -1,     /* these values are not constant, but can change at runtime */
6461     -1, -1,     /* these values are not constant, but can change at runtime */
6462     GD_TYPE_SCROLLBAR_VERTICAL,
6463     SCREEN_CTRL_ID_SCROLL_VERTICAL,
6464     "scroll level series vertically"
6465   }
6466 };
6467
6468 static void CreateScreenMenubuttons()
6469 {
6470   struct GadgetInfo *gi;
6471   unsigned int event_mask;
6472   int i;
6473
6474   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6475   {
6476     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6477     int gfx_unpressed, gfx_pressed;
6478     int x, y, width, height;
6479     int gd_x1, gd_x2, gd_y1, gd_y2;
6480     int id = menubutton_info[i].gadget_id;
6481
6482     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6483
6484     menubutton_info[i].get_gadget_position(&x, &y, id);
6485
6486     width = SC_MENUBUTTON_XSIZE;
6487     height = SC_MENUBUTTON_YSIZE;
6488
6489     gfx_unpressed = menubutton_info[i].gfx_unpressed;
6490     gfx_pressed   = menubutton_info[i].gfx_pressed;
6491     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6492     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6493     gd_x1 = graphic_info[gfx_unpressed].src_x;
6494     gd_y1 = graphic_info[gfx_unpressed].src_y;
6495     gd_x2 = graphic_info[gfx_pressed].src_x;
6496     gd_y2 = graphic_info[gfx_pressed].src_y;
6497
6498     gi = CreateGadget(GDI_CUSTOM_ID, id,
6499                       GDI_CUSTOM_TYPE_ID, i,
6500                       GDI_INFO_TEXT, menubutton_info[i].infotext,
6501                       GDI_X, x,
6502                       GDI_Y, y,
6503                       GDI_WIDTH, width,
6504                       GDI_HEIGHT, height,
6505                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6506                       GDI_STATE, GD_BUTTON_UNPRESSED,
6507                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6508                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6509                       GDI_DIRECT_DRAW, FALSE,
6510                       GDI_EVENT_MASK, event_mask,
6511                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6512                       GDI_END);
6513
6514     if (gi == NULL)
6515       Error(ERR_EXIT, "cannot create gadget");
6516
6517     screen_gadget[id] = gi;
6518   }
6519 }
6520
6521 static void CreateScreenScrollbuttons()
6522 {
6523   struct GadgetInfo *gi;
6524   unsigned int event_mask;
6525   int i;
6526
6527   /* these values are not constant, but can change at runtime */
6528   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
6529   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
6530   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
6531   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
6532
6533   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6534   {
6535     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6536     int gfx_unpressed, gfx_pressed;
6537     int x, y, width, height;
6538     int gd_x1, gd_x2, gd_y1, gd_y2;
6539     int id = scrollbutton_info[i].gadget_id;
6540
6541     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6542
6543     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
6544     y = mSY + scrollbutton_info[i].y;
6545     width = SC_SCROLLBUTTON_XSIZE;
6546     height = SC_SCROLLBUTTON_YSIZE;
6547
6548     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
6549       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
6550                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
6551
6552     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
6553     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
6554     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6555     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6556     gd_x1 = graphic_info[gfx_unpressed].src_x;
6557     gd_y1 = graphic_info[gfx_unpressed].src_y;
6558     gd_x2 = graphic_info[gfx_pressed].src_x;
6559     gd_y2 = graphic_info[gfx_pressed].src_y;
6560
6561     gi = CreateGadget(GDI_CUSTOM_ID, id,
6562                       GDI_CUSTOM_TYPE_ID, i,
6563                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6564                       GDI_X, x,
6565                       GDI_Y, y,
6566                       GDI_WIDTH, width,
6567                       GDI_HEIGHT, height,
6568                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6569                       GDI_STATE, GD_BUTTON_UNPRESSED,
6570                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6571                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6572                       GDI_DIRECT_DRAW, FALSE,
6573                       GDI_EVENT_MASK, event_mask,
6574                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6575                       GDI_END);
6576
6577     if (gi == NULL)
6578       Error(ERR_EXIT, "cannot create gadget");
6579
6580     screen_gadget[id] = gi;
6581   }
6582 }
6583
6584 static void CreateScreenScrollbars()
6585 {
6586   int i;
6587
6588   /* these values are not constant, but can change at runtime */
6589   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6590   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6591   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6592   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6593
6594   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6595   {
6596     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6597     int gfx_unpressed, gfx_pressed;
6598     int x, y, width, height;
6599     int gd_x1, gd_x2, gd_y1, gd_y2;
6600     struct GadgetInfo *gi;
6601     int items_max, items_visible, item_position;
6602     unsigned int event_mask;
6603     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6604     int id = scrollbar_info[i].gadget_id;
6605
6606     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6607
6608     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6609     y = mSY + scrollbar_info[i].y;
6610     width  = scrollbar_info[i].width;
6611     height = scrollbar_info[i].height;
6612
6613     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6614       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6615
6616     items_max = num_page_entries;
6617     items_visible = num_page_entries;
6618     item_position = 0;
6619
6620     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6621     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6622     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6623     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6624     gd_x1 = graphic_info[gfx_unpressed].src_x;
6625     gd_y1 = graphic_info[gfx_unpressed].src_y;
6626     gd_x2 = graphic_info[gfx_pressed].src_x;
6627     gd_y2 = graphic_info[gfx_pressed].src_y;
6628
6629     gi = CreateGadget(GDI_CUSTOM_ID, id,
6630                       GDI_CUSTOM_TYPE_ID, i,
6631                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6632                       GDI_X, x,
6633                       GDI_Y, y,
6634                       GDI_WIDTH, width,
6635                       GDI_HEIGHT, height,
6636                       GDI_TYPE, scrollbar_info[i].type,
6637                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6638                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6639                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6640                       GDI_WHEEL_AREA_X, SX,
6641                       GDI_WHEEL_AREA_Y, SY,
6642                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6643                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6644                       GDI_STATE, GD_BUTTON_UNPRESSED,
6645                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6646                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6647                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6648                       GDI_DIRECT_DRAW, FALSE,
6649                       GDI_EVENT_MASK, event_mask,
6650                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6651                       GDI_END);
6652
6653     if (gi == NULL)
6654       Error(ERR_EXIT, "cannot create gadget");
6655
6656     screen_gadget[id] = gi;
6657   }
6658 }
6659
6660 void CreateScreenGadgets()
6661 {
6662   int last_game_status = game_status;   /* save current game status */
6663
6664   CreateScreenMenubuttons();
6665
6666   CreateScreenScrollbuttons();
6667   CreateScreenScrollbars();
6668
6669   game_status = last_game_status;       /* restore current game status */
6670 }
6671
6672 void FreeScreenGadgets()
6673 {
6674   int i;
6675
6676   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6677     FreeGadget(screen_gadget[i]);
6678 }
6679
6680 void MapScreenMenuGadgets(int screen_mask)
6681 {
6682   int i;
6683
6684   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6685     if (screen_mask & menubutton_info[i].screen_mask)
6686       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6687 }
6688
6689 void MapScreenGadgets(int num_entries)
6690 {
6691   int i;
6692
6693   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6694     return;
6695
6696   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6697     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6698
6699   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6700     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6701 }
6702
6703 void MapScreenTreeGadgets(TreeInfo *ti)
6704 {
6705   MapScreenGadgets(numTreeInfoInGroup(ti));
6706 }
6707
6708 static void HandleScreenGadgets(struct GadgetInfo *gi)
6709 {
6710   int id = gi->custom_id;
6711   int button = gi->event.button;
6712   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6713
6714   switch (id)
6715   {
6716     case SCREEN_CTRL_ID_PREV_LEVEL:
6717       HandleMainMenu_SelectLevel(step, -1);
6718       break;
6719
6720     case SCREEN_CTRL_ID_NEXT_LEVEL:
6721       HandleMainMenu_SelectLevel(step, +1);
6722       break;
6723
6724     case SCREEN_CTRL_ID_PREV_PLAYER:
6725       HandleSetupScreen_Input_Player(step, -1);
6726       break;
6727
6728     case SCREEN_CTRL_ID_NEXT_PLAYER:
6729       HandleSetupScreen_Input_Player(step, +1);
6730       break;
6731
6732     case SCREEN_CTRL_ID_SCROLL_UP:
6733       if (game_status == GAME_MODE_LEVELS)
6734         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6735       else if (game_status == GAME_MODE_LEVELNR)
6736         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6737       else if (game_status == GAME_MODE_SETUP)
6738         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6739       break;
6740
6741     case SCREEN_CTRL_ID_SCROLL_DOWN:
6742       if (game_status == GAME_MODE_LEVELS)
6743         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6744       else if (game_status == GAME_MODE_LEVELNR)
6745         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6746       else if (game_status == GAME_MODE_SETUP)
6747         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6748       break;
6749
6750     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
6751       if (game_status == GAME_MODE_LEVELS)
6752         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6753       else if (game_status == GAME_MODE_LEVELNR)
6754         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6755       else if (game_status == GAME_MODE_SETUP)
6756         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6757       break;
6758
6759     default:
6760       break;
6761   }
6762 }