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