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