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