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