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