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