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