rnd-20070321-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 1
1463   FadeIn(redraw_mask);
1464 #else
1465 #if 1
1466   if (!do_fading)
1467     BackToFront();
1468   else if (fading.anim_mode == ANIM_CROSSFADE)
1469     FadeCross(redraw_mask);
1470   else
1471     FadeIn(redraw_mask);
1472 #else
1473   if (do_fading)
1474     FadeIn(redraw_mask);
1475   else
1476     BackToFront();
1477 #endif
1478 #endif
1479
1480 #if 1
1481   FadeSetEnterMenu();
1482 #else
1483   fading = title_default;
1484 #endif
1485
1486   SetMouseCursor(CURSOR_DEFAULT);
1487
1488   InitAnimation();
1489
1490   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1491 }
1492
1493 void DrawAndFadeInMainMenu(int redraw_mask)
1494 {
1495   DrawMainMenuExt(redraw_mask, TRUE);
1496 }
1497
1498 void DrawMainMenu()
1499 {
1500   DrawMainMenuExt(REDRAW_ALL, FALSE);
1501 }
1502
1503 #if 0
1504 static void gotoTopLevelDir()
1505 {
1506   /* move upwards to top level directory */
1507   while (leveldir_current->node_parent)
1508   {
1509     /* write a "path" into level tree for easy navigation to last level */
1510     if (leveldir_current->node_parent->node_group->cl_first == -1)
1511     {
1512       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1513       int leveldir_pos = posTreeInfo(leveldir_current);
1514       int num_page_entries;
1515       int cl_first, cl_cursor;
1516
1517       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
1518         num_page_entries = num_leveldirs;
1519       else
1520         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1521
1522       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1523       cl_cursor = leveldir_pos - cl_first;
1524
1525       leveldir_current->node_parent->node_group->cl_first = cl_first;
1526       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1527     }
1528
1529     leveldir_current = leveldir_current->node_parent;
1530   }
1531 }
1532 #endif
1533
1534 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1535 {
1536   static unsigned long title_delay = 0;
1537   static int title_screen_nr = 0;
1538   static int last_sound = -1, last_music = -1;
1539   boolean return_to_main_menu = FALSE;
1540   boolean use_fading_main_menu = TRUE;
1541 #if 0
1542 #if 1
1543   boolean use_cross_fading = FALSE;
1544 #else
1545   boolean use_cross_fading = !show_title_initial;               /* default */
1546 #endif
1547 #endif
1548   struct TitleControlInfo *tci;
1549   struct TitleFadingInfo fading_last = fading;
1550   struct TitleFadingInfo fading_next;
1551   int sound, music;
1552
1553   if (button == MB_MENU_INITIALIZE)
1554   {
1555 #if 0
1556     boolean use_cross_fading = (fading.anim_mode == ANIM_CROSSFADE);
1557 #endif
1558
1559 #if 0
1560     int last_game_status = game_status; /* save current game status */
1561 #endif
1562
1563     title_delay = 0;
1564     title_screen_nr = 0;
1565     tci = &title_controls[title_screen_nr];
1566
1567     last_sound = SND_UNDEFINED;
1568     last_music = MUS_UNDEFINED;
1569
1570 #if 0
1571     /* determine number of title screens to display (images and messages) */
1572     InitializeTitleControls();
1573 #endif
1574
1575     if (game_status == GAME_MODE_INFO)
1576     {
1577       if (num_title_screens == 0)
1578       {
1579         DrawInfoScreen_NotAvailable("Title screen information:",
1580                                     "No title screen for this level set.");
1581
1582         /* use default settings for fading, but always disable auto delay */
1583         fading = title_default;
1584         fading.auto_delay = -1;
1585
1586         return;
1587       }
1588
1589       FadeSoundsAndMusic();
1590
1591 #if 1
1592       FadeOut(REDRAW_ALL);
1593 #endif
1594     }
1595
1596     if (tci->is_image)
1597       DrawTitleScreenImage(tci->local_nr, tci->initial);
1598     else
1599       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1600
1601     fading = fading_next = getTitleFading(tci);
1602
1603     if (fading_last.fade_mode != FADE_MODE_CROSSFADE &&
1604         fading_next.fade_mode == FADE_MODE_CROSSFADE)
1605       fading.fade_mode = FADE_MODE_FADE;
1606
1607 #if 1
1608     sound = getTitleSound(tci);
1609     music = getTitleMusic(tci);
1610
1611     if (sound != last_sound)
1612       PlayMenuSoundExt(sound);
1613     if (music != last_music)
1614       PlayMenuMusicExt(music);
1615
1616     last_sound = sound;
1617     last_music = music;
1618 #endif
1619
1620     SetMouseCursor(CURSOR_NONE);
1621
1622 #if 1
1623     FadeIn(REDRAW_ALL);
1624 #endif
1625
1626     fading = fading_next;
1627
1628     DelayReached(&title_delay, 0);      /* reset delay counter */
1629
1630     return;
1631   }
1632
1633   if (fading.auto_delay > -1 && DelayReached(&title_delay, fading.auto_delay))
1634     button = MB_MENU_CHOICE;
1635
1636   if (button == MB_MENU_LEAVE)
1637   {
1638     return_to_main_menu = TRUE;
1639     use_fading_main_menu = FALSE;
1640   }
1641   else if (button == MB_MENU_CHOICE)
1642   {
1643 #if 0
1644     boolean use_cross_fading = (fading.anim_mode == ANIM_CROSSFADE);
1645 #endif
1646
1647     if (game_status == GAME_MODE_INFO && num_title_screens == 0)
1648     {
1649 #if 0
1650       FadeOut(REDRAW_FIELD);
1651 #endif
1652
1653       FadeSetStartItem();
1654
1655       info_mode = INFO_MODE_MAIN;
1656       DrawAndFadeInInfoScreen(REDRAW_FIELD);
1657
1658       return;
1659     }
1660
1661     title_screen_nr++;
1662     tci = &title_controls[title_screen_nr];
1663
1664     if (title_screen_nr < num_title_screens)
1665     {
1666 #if 1
1667 #if 0
1668       boolean use_cross_fading = (fading.anim_mode == ANIM_CROSSFADE);
1669 #endif
1670 #else
1671       int anim_mode;
1672
1673       if (tci->is_image)
1674         anim_mode =
1675           graphic_info[getTitleScreenGraphic(tci->local_nr,
1676                                              tci->initial)].anim_mode;
1677       else
1678         anim_mode = ANIM_FADE;  /* ??? */
1679
1680       use_cross_fading = (anim_mode == ANIM_FADE ? FALSE :
1681                           anim_mode == ANIM_CROSSFADE ? TRUE :
1682                           use_cross_fading);
1683 #endif
1684
1685       sound = getTitleSound(tci);
1686       music = getTitleMusic(tci);
1687
1688       if (sound == SND_UNDEFINED || sound != last_sound)
1689         FadeSounds();
1690       if (music == MUS_UNDEFINED || music != last_music)
1691         FadeMusic();
1692
1693 #if 1
1694       FadeOut(REDRAW_ALL);
1695 #endif
1696
1697       if (tci->is_image)
1698         DrawTitleScreenImage(tci->local_nr, tci->initial);
1699       else
1700         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1701
1702       fading_next = getTitleFading(tci);
1703
1704 #if 1
1705       sound = getTitleSound(tci);
1706       music = getTitleMusic(tci);
1707
1708       if (sound != last_sound)
1709         PlayMenuSoundExt(sound);
1710       if (music != last_music)
1711         PlayMenuMusicExt(music);
1712
1713       last_sound = sound;
1714       last_music = music;
1715 #endif
1716
1717 #if 0
1718       printf("::: %d -> %d\n", fading.fade_mode, fading_next.fade_mode);
1719 #endif
1720
1721       /* last screen already faded out, next screen has no animation */
1722       if (fading.fade_mode      != FADE_MODE_CROSSFADE &&
1723           fading_next.fade_mode == FADE_MODE_NONE)
1724         fading = fading_next;
1725
1726 #if 1
1727       FadeIn(REDRAW_ALL);
1728 #endif
1729
1730       fading = fading_next;
1731
1732       DelayReached(&title_delay, 0);    /* reset delay counter */
1733     }
1734     else
1735     {
1736       FadeSoundsAndMusic();
1737
1738 #if 0
1739 #if 1
1740       {
1741 #if 0
1742         boolean use_cross_fading = (fading.anim_mode == ANIM_CROSSFADE);
1743 #endif
1744
1745 #if 1
1746         FadeOut(REDRAW_ALL);
1747 #endif
1748       }
1749 #else
1750       FadeOut(REDRAW_ALL);
1751 #endif
1752 #endif
1753
1754       return_to_main_menu = TRUE;
1755     }
1756   }
1757
1758   if (return_to_main_menu)
1759   {
1760 #if 0
1761     RedrawBackground();
1762 #endif
1763
1764     SetMouseCursor(CURSOR_DEFAULT);
1765
1766     if (game_status == GAME_MODE_INFO)
1767     {
1768 #if 0
1769       OpenDoor(DOOR_CLOSE_1 | DOOR_CLOSE_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1770 #endif
1771
1772       info_mode = INFO_MODE_MAIN;
1773       DrawInfoScreenExt(REDRAW_ALL, use_fading_main_menu);
1774     }
1775     else        /* default: return to main menu */
1776     {
1777 #if 0
1778       OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1779 #endif
1780
1781       game_status = GAME_MODE_MAIN;
1782       DrawMainMenuExt(REDRAW_ALL, use_fading_main_menu);
1783     }
1784   }
1785 }
1786
1787 void HandleMainMenu_SelectLevel(int step, int direction)
1788 {
1789   int old_level_nr = level_nr;
1790   int new_level_nr;
1791
1792   new_level_nr = old_level_nr + step * direction;
1793   if (new_level_nr < leveldir_current->first_level)
1794     new_level_nr = leveldir_current->first_level;
1795   if (new_level_nr > leveldir_current->last_level)
1796     new_level_nr = leveldir_current->last_level;
1797
1798   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1799   {
1800     /* skipping levels is only allowed when trying to skip single level */
1801     if (setup.skip_levels && step == 1 &&
1802         Request("Level still unsolved ! Skip despite handicap ?", REQ_ASK))
1803     {
1804       leveldir_current->handicap_level++;
1805       SaveLevelSetup_SeriesInfo();
1806     }
1807
1808     new_level_nr = leveldir_current->handicap_level;
1809   }
1810
1811   if (new_level_nr != old_level_nr)
1812   {
1813     struct MainControlInfo *mci= getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1814
1815     PlaySound(SND_MENU_ITEM_SELECTING);
1816
1817     level_nr = new_level_nr;
1818
1819 #if 1
1820     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1821              int2str(level_nr, 3), mci->pos_text->font);
1822 #else
1823     DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3), FONT_VALUE_1);
1824 #endif
1825
1826     LoadLevel(level_nr);
1827     DrawPreviewLevel(TRUE);
1828
1829     TapeErase();
1830     LoadTape(level_nr);
1831     DrawCompleteVideoDisplay();
1832
1833     /* needed because DrawPreviewLevel() takes some time */
1834     BackToFront();
1835     SyncDisplay();
1836   }
1837 }
1838
1839 #if 1
1840
1841 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1842 {
1843   static int choice = MAIN_CONTROL_GAME;
1844   int pos = choice;
1845   int i;
1846
1847   if (button == MB_MENU_INITIALIZE)
1848   {
1849     DrawCursorAndText_Main(choice, TRUE);
1850
1851     return;
1852   }
1853
1854   if (mx || my)         /* mouse input */
1855   {
1856     pos = -1;
1857
1858     for (i = 0; main_controls[i].nr != -1; i++)
1859     {
1860       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1861           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1862           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1863       {
1864         pos = main_controls[i].nr;
1865
1866         break;
1867       }
1868     }
1869   }
1870   else if (dx || dy)    /* keyboard input */
1871   {
1872     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
1873                    choice == MAIN_CONTROL_SETUP))
1874       button = MB_MENU_CHOICE;
1875     else if (dy)
1876       pos = choice + dy;
1877   }
1878
1879   if (pos == MAIN_CONTROL_LEVELS && dx != 0 && button)
1880   {
1881     HandleMainMenu_SelectLevel(1, dx < 0 ? -1 : +1);
1882   }
1883   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
1884   {
1885     if (button)
1886     {
1887       if (pos != choice)
1888       {
1889         PlaySound(SND_MENU_ITEM_ACTIVATING);
1890
1891         DrawCursorAndText_Main(choice, FALSE);
1892         DrawCursorAndText_Main(pos, TRUE);
1893
1894         choice = pos;
1895       }
1896     }
1897     else
1898     {
1899       PlaySound(SND_MENU_ITEM_SELECTING);
1900
1901       if (pos == MAIN_CONTROL_NAME)
1902       {
1903         game_status = GAME_MODE_PSEUDO_TYPENAME;
1904
1905         HandleTypeName(strlen(setup.player_name), 0);
1906       }
1907       else if (pos == MAIN_CONTROL_LEVELS)
1908       {
1909         if (leveldir_first)
1910         {
1911           game_status = GAME_MODE_LEVELS;
1912
1913           SaveLevelSetup_LastSeries();
1914           SaveLevelSetup_SeriesInfo();
1915
1916 #if 0
1917           gotoTopLevelDir();
1918 #endif
1919
1920           DrawChooseLevel();
1921         }
1922       }
1923       else if (pos == MAIN_CONTROL_SCORES)
1924       {
1925         game_status = GAME_MODE_SCORES;
1926
1927         DrawHallOfFame(-1);
1928       }
1929       else if (pos == MAIN_CONTROL_EDITOR)
1930       {
1931         if (leveldir_current->readonly &&
1932             !strEqual(setup.player_name, "Artsoft"))
1933           Request("This level is read only !", REQ_CONFIRM);
1934
1935         game_status = GAME_MODE_EDITOR;
1936
1937         FadeSetStartItem();
1938
1939         DrawLevelEd();
1940       }
1941       else if (pos == MAIN_CONTROL_INFO)
1942       {
1943         game_status = GAME_MODE_INFO;
1944         info_mode = INFO_MODE_MAIN;
1945
1946 #if 0
1947         fading = menu.navigation;
1948 #endif
1949
1950         DrawInfoScreen();
1951       }
1952       else if (pos == MAIN_CONTROL_GAME)
1953       {
1954         StartGameActions(options.network, setup.autorecord, NEW_RANDOMIZE);
1955       }
1956       else if (pos == MAIN_CONTROL_SETUP)
1957       {
1958         game_status = GAME_MODE_SETUP;
1959         setup_mode = SETUP_MODE_MAIN;
1960
1961         DrawSetupScreen();
1962       }
1963       else if (pos == MAIN_CONTROL_QUIT)
1964       {
1965         SaveLevelSetup_LastSeries();
1966         SaveLevelSetup_SeriesInfo();
1967
1968         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
1969           game_status = GAME_MODE_QUIT;
1970       }
1971     }
1972   }
1973
1974   if (game_status == GAME_MODE_MAIN)
1975   {
1976     DrawPreviewLevel(FALSE);
1977     DoAnimation();
1978   }
1979 }
1980
1981 #else
1982
1983 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1984 {
1985   static int choice = 5;
1986   int x = 0;
1987   int y = choice;
1988
1989   if (button == MB_MENU_INITIALIZE)
1990   {
1991     drawCursor(choice, TRUE);
1992
1993     return;
1994   }
1995
1996   if (mx || my)         /* mouse input */
1997   {
1998     x = (mx - mSX) / 32;
1999     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2000   }
2001   else if (dx || dy)    /* keyboard input */
2002   {
2003     if (dx && choice == 1)
2004       x = (dx < 0 ? 10 : 14);
2005     else if (dx > 0)
2006     {
2007       if (choice == 4 || choice == 6)
2008         button = MB_MENU_CHOICE;
2009     }
2010     else if (dy)
2011       y = choice + dy;
2012   }
2013
2014   if (y == 1 && dx != 0 && button)
2015   {
2016     HandleMainMenu_SelectLevel(1, dx < 0 ? -1 : +1);
2017   }
2018   else if (IN_VIS_FIELD(x, y) &&
2019            y >= 0 && y <= 7 && (y != 1 || x < 10))
2020   {
2021     if (button)
2022     {
2023       if (y != choice)
2024       {
2025         drawCursor(choice, FALSE);
2026         drawCursor(y, TRUE);
2027
2028         choice = y;
2029       }
2030     }
2031     else
2032     {
2033       if (y == 0)
2034       {
2035         game_status = GAME_MODE_PSEUDO_TYPENAME;
2036         HandleTypeName(strlen(setup.player_name), 0);
2037       }
2038       else if (y == 1)
2039       {
2040         if (leveldir_first)
2041         {
2042           game_status = GAME_MODE_LEVELS;
2043           SaveLevelSetup_LastSeries();
2044           SaveLevelSetup_SeriesInfo();
2045
2046 #if 0
2047           gotoTopLevelDir();
2048 #endif
2049
2050           DrawChooseLevel();
2051         }
2052       }
2053       else if (y == 2)
2054       {
2055         game_status = GAME_MODE_SCORES;
2056         DrawHallOfFame(-1);
2057       }
2058       else if (y == 3)
2059       {
2060         if (leveldir_current->readonly &&
2061             !strEqual(setup.player_name, "Artsoft"))
2062           Request("This level is read only !", REQ_CONFIRM);
2063         game_status = GAME_MODE_EDITOR;
2064         DrawLevelEd();
2065       }
2066       else if (y == 4)
2067       {
2068         game_status = GAME_MODE_INFO;
2069         info_mode = INFO_MODE_MAIN;
2070         DrawInfoScreen();
2071       }
2072       else if (y == 5)
2073       {
2074         StartGameActions(options.network, setup.autorecord, NEW_RANDOMIZE);
2075       }
2076       else if (y == 6)
2077       {
2078         game_status = GAME_MODE_SETUP;
2079         setup_mode = SETUP_MODE_MAIN;
2080
2081         DrawSetupScreen();
2082       }
2083       else if (y == 7)
2084       {
2085         SaveLevelSetup_LastSeries();
2086         SaveLevelSetup_SeriesInfo();
2087
2088         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
2089           game_status = GAME_MODE_QUIT;
2090       }
2091     }
2092   }
2093
2094   if (game_status == GAME_MODE_MAIN)
2095   {
2096     DrawPreviewLevel(FALSE);
2097     DoAnimation();
2098   }
2099 }
2100
2101 #endif
2102
2103
2104 /* ========================================================================= */
2105 /* info screen functions                                                     */
2106 /* ========================================================================= */
2107
2108 static struct TokenInfo *info_info;
2109 static int num_info_info;
2110
2111 static void execInfoTitleScreen()
2112 {
2113 #if 0
2114   FadeSetStartItem();
2115 #endif
2116
2117   info_mode = INFO_MODE_TITLE;
2118   DrawInfoScreen();
2119 }
2120
2121 static void execInfoElements()
2122 {
2123 #if 0
2124   FadeSetStartItem();
2125 #endif
2126
2127   info_mode = INFO_MODE_ELEMENTS;
2128   DrawInfoScreen();
2129 }
2130
2131 static void execInfoMusic()
2132 {
2133 #if 0
2134   FadeSetStartItem();
2135 #endif
2136
2137   info_mode = INFO_MODE_MUSIC;
2138   DrawInfoScreen();
2139 }
2140
2141 static void execInfoCredits()
2142 {
2143 #if 0
2144   FadeSetStartItem();
2145 #endif
2146
2147   info_mode = INFO_MODE_CREDITS;
2148   DrawInfoScreen();
2149 }
2150
2151 static void execInfoProgram()
2152 {
2153 #if 0
2154   FadeSetStartItem();
2155 #endif
2156
2157   info_mode = INFO_MODE_PROGRAM;
2158   DrawInfoScreen();
2159 }
2160
2161 static void execInfoVersion()
2162 {
2163 #if 0
2164   FadeSetStartItem();
2165 #endif
2166
2167   info_mode = INFO_MODE_VERSION;
2168   DrawInfoScreen();
2169 }
2170
2171 static void execInfoLevelSet()
2172 {
2173 #if 0
2174   FadeSetStartItem();
2175 #endif
2176
2177   info_mode = INFO_MODE_LEVELSET;
2178   DrawInfoScreen();
2179 }
2180
2181 static void execExitInfo()
2182 {
2183 #if 0
2184   FadeSetLeaveMenu();
2185 #endif
2186
2187   game_status = GAME_MODE_MAIN;
2188 #if 1
2189   DrawMainMenuExt(REDRAW_FIELD, FALSE);
2190 #else
2191   DrawMainMenu();
2192 #endif
2193 }
2194
2195 static struct TokenInfo info_info_main[] =
2196 {
2197   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
2198   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
2199   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
2200   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
2201   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
2202   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
2203   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
2204   { TYPE_EMPTY,         NULL,                   ""                      },
2205   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
2206
2207   { 0,                  NULL,                   NULL                    }
2208 };
2209
2210 static void DrawCursorAndText_Info(int pos, boolean active)
2211 {
2212   int xpos = MENU_SCREEN_START_XPOS;
2213   int ypos = MENU_SCREEN_START_YPOS + pos;
2214   int font_nr = FONT_MENU_1;
2215
2216   if (active)
2217     font_nr = FONT_ACTIVE(font_nr);
2218
2219   DrawText(mSX + xpos * 32, mSY + ypos * 32, info_info[pos].text, font_nr);
2220
2221   if (info_info[pos].type & ~TYPE_SKIP_ENTRY)
2222     drawCursor(pos, active);
2223 }
2224
2225 static void DrawInfoScreen_Main(int redraw_mask, boolean do_fading)
2226 {
2227   int i;
2228
2229   UnmapAllGadgets();
2230   CloseDoor(DOOR_CLOSE_2);
2231
2232 #if 1
2233   FadeOut(redraw_mask);
2234 #endif
2235
2236 #if 1
2237   if (redraw_mask == REDRAW_ALL)
2238   {
2239     RedrawBackground();
2240     OpenDoor(DOOR_CLOSE_1 | DOOR_CLOSE_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2241   }
2242 #endif
2243
2244   ClearWindow();
2245
2246   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2247
2248   info_info = info_info_main;
2249   num_info_info = 0;
2250
2251   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2252   {
2253 #if 0
2254     int xpos = MENU_SCREEN_START_XPOS;
2255     int ypos = MENU_SCREEN_START_YPOS + i;
2256     int font_nr = FONT_MENU_1;
2257 #endif
2258
2259     if (info_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2260       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2261     else if (info_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2262       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2263     else if (info_info[i].type & ~TYPE_SKIP_ENTRY)
2264       initCursor(i, IMG_MENU_BUTTON);
2265
2266 #if 1
2267     DrawCursorAndText_Info(i, FALSE);
2268 #else
2269     DrawText(mSX + xpos * 32, mSY + ypos * 32, info_info[i].text, font_nr);
2270 #endif
2271
2272     num_info_info++;
2273   }
2274
2275   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2276
2277   PlayMenuSound();
2278   PlayMenuMusic();
2279
2280   DrawMaskedBorder(redraw_mask);
2281
2282 #if 1
2283   FadeIn(redraw_mask);
2284 #else
2285 #if 1
2286   if (!do_fading)
2287     BackToFront();
2288   else if (fading.anim_mode == ANIM_CROSSFADE)
2289     FadeCross(redraw_mask);
2290   else
2291     FadeIn(redraw_mask);
2292 #else
2293   if (do_fading)
2294     FadeIn(redraw_mask);
2295   else
2296     BackToFront();
2297 #endif
2298 #endif
2299
2300   InitAnimation();
2301 }
2302
2303 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2304 {
2305   static int choice_store[MAX_INFO_MODES];
2306   int choice = choice_store[info_mode];         /* always starts with 0 */
2307   int x = 0;
2308   int y = choice;
2309
2310   if (button == MB_MENU_INITIALIZE)
2311   {
2312     /* advance to first valid menu entry */
2313     while (choice < num_info_info &&
2314            info_info[choice].type & TYPE_SKIP_ENTRY)
2315       choice++;
2316     choice_store[info_mode] = choice;
2317
2318 #if 1
2319     DrawCursorAndText_Info(choice, TRUE);
2320 #else
2321     drawCursor(choice, TRUE);
2322 #endif
2323
2324     return;
2325   }
2326   else if (button == MB_MENU_LEAVE)
2327   {
2328     for (y = 0; y < num_info_info; y++)
2329     {
2330       if (info_info[y].type & TYPE_LEAVE_MENU)
2331       {
2332         void (*menu_callback_function)(void) = info_info[y].value;
2333
2334         FadeSetLeaveMenu();
2335
2336         menu_callback_function();
2337
2338         break;  /* absolutely needed because function changes 'info_info'! */
2339       }
2340     }
2341
2342     return;
2343   }
2344
2345   if (mx || my)         /* mouse input */
2346   {
2347     x = (mx - mSX) / 32;
2348     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2349   }
2350   else if (dx || dy)    /* keyboard input */
2351   {
2352     if (dx)
2353     {
2354       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2355
2356       if (info_info[choice].type & menu_navigation_type ||
2357           info_info[choice].type & TYPE_ENTER_SCREEN ||
2358           info_info[choice].type & TYPE_BOOLEAN_STYLE)
2359         button = MB_MENU_CHOICE;
2360     }
2361     else if (dy)
2362       y = choice + dy;
2363
2364     /* jump to next non-empty menu entry (up or down) */
2365     while (y > 0 && y < num_info_info - 1 &&
2366            info_info[y].type & TYPE_SKIP_ENTRY)
2367       y += dy;
2368   }
2369
2370   if (IN_VIS_FIELD(x, y) &&
2371       y >= 0 && y < num_info_info && info_info[y].type & ~TYPE_SKIP_ENTRY)
2372   {
2373     if (button)
2374     {
2375       if (y != choice)
2376       {
2377         PlaySound(SND_MENU_ITEM_ACTIVATING);
2378
2379 #if 1
2380         DrawCursorAndText_Info(choice, FALSE);
2381         DrawCursorAndText_Info(y, TRUE);
2382 #else
2383         drawCursor(choice, FALSE);
2384         drawCursor(y, TRUE);
2385 #endif
2386
2387         choice = choice_store[info_mode] = y;
2388       }
2389     }
2390     else if (!(info_info[y].type & TYPE_GHOSTED))
2391     {
2392       PlaySound(SND_MENU_ITEM_SELECTING);
2393
2394       if (info_info[y].type & TYPE_ENTER_OR_LEAVE)
2395       {
2396         void (*menu_callback_function)(void) = info_info[choice].value;
2397
2398         FadeSetFromType(info_info[y].type);
2399
2400         menu_callback_function();
2401       }
2402     }
2403   }
2404 }
2405
2406 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2407 {
2408 #if 1
2409   int ystart1 = mSY - SY + 100;
2410   int ystart2 = mSY - SY + 150;
2411   int ybottom = mSY - SY + SYSIZE - 20;
2412 #else
2413   int ystart1 = 100;
2414   int ystart2 = 150;
2415   int ybottom = SYSIZE - 20;
2416 #endif
2417
2418   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
2419
2420 #if 1
2421   FadeOut(REDRAW_FIELD);
2422 #endif
2423
2424   ClearWindow();
2425   DrawHeadline();
2426
2427   DrawTextSCentered(ystart1, FONT_TEXT_1, text_title);
2428   DrawTextSCentered(ystart2, FONT_TEXT_2, text_error);
2429
2430   DrawTextSCentered(ybottom, FONT_TEXT_4,
2431                     "Press any key or button for info menu");
2432
2433 #if 1
2434   FadeIn(REDRAW_FIELD);
2435 #endif
2436 }
2437
2438 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2439 {
2440   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2441   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2442   int xstart = mSX + 16;
2443   int ystart1 = mSY - SY + 100;
2444   int ystart2 = mSY + 64 + 2 * 32;
2445   int ybottom = mSY - SY + SYSIZE - 20;
2446   int ystep = TILEY + 4;
2447   int element, action, direction;
2448   int graphic;
2449   int delay;
2450   int sync_frame;
2451   int i, j;
2452
2453   if (init)
2454   {
2455     for (i = 0; i < MAX_INFO_ELEMENTS_ON_SCREEN; i++)
2456       infoscreen_step[i] = infoscreen_frame[i] = 0;
2457
2458     ClearWindow();
2459     DrawHeadline();
2460
2461     DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Elements:");
2462
2463     DrawTextSCentered(ybottom, FONT_TEXT_4,
2464                       "Press any key or button for next page");
2465
2466     FrameCounter = 0;
2467   }
2468
2469   i = j = 0;
2470   while (helpanim_info[j].element != HELPANIM_LIST_END)
2471   {
2472     if (i >= start + MAX_INFO_ELEMENTS_ON_SCREEN ||
2473         i >= max_anims)
2474       break;
2475     else if (i < start)
2476     {
2477       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2478         j++;
2479
2480       j++;
2481       i++;
2482
2483       continue;
2484     }
2485
2486     j += infoscreen_step[i - start];
2487
2488     element = helpanim_info[j].element;
2489     action = helpanim_info[j].action;
2490     direction = helpanim_info[j].direction;
2491
2492     if (element < 0)
2493       element = EL_UNKNOWN;
2494
2495     if (action != -1 && direction != -1)
2496       graphic = el_act_dir2img(element, action, direction);
2497     else if (action != -1)
2498       graphic = el_act2img(element, action);
2499     else if (direction != -1)
2500       graphic = el_dir2img(element, direction);
2501     else
2502       graphic = el2img(element);
2503
2504     delay = helpanim_info[j++].delay;
2505
2506     if (delay == -1)
2507       delay = 1000000;
2508
2509     if (infoscreen_frame[i - start] == 0)
2510     {
2511       sync_frame = 0;
2512       infoscreen_frame[i - start] = delay - 1;
2513     }
2514     else
2515     {
2516       sync_frame = delay - infoscreen_frame[i - start];
2517       infoscreen_frame[i - start]--;
2518     }
2519
2520     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2521     {
2522       if (!infoscreen_frame[i - start])
2523         infoscreen_step[i - start] = 0;
2524     }
2525     else
2526     {
2527       if (!infoscreen_frame[i - start])
2528         infoscreen_step[i - start]++;
2529       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2530         j++;
2531     }
2532
2533     j++;
2534
2535     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2536                                TILEX, TILEY);
2537     DrawGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2538                             graphic, sync_frame, USE_MASKING);
2539
2540     if (init)
2541       DrawInfoScreen_HelpText(element, action, direction, i - start);
2542
2543     i++;
2544   }
2545
2546   redraw_mask |= REDRAW_FIELD;
2547
2548   FrameCounter++;
2549 }
2550
2551 static char *getHelpText(int element, int action, int direction)
2552 {
2553   char token[MAX_LINE_LEN];
2554
2555   strcpy(token, element_info[element].token_name);
2556
2557   if (action != -1)
2558     strcat(token, element_action_info[action].suffix);
2559
2560   if (direction != -1)
2561     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2562
2563   return getHashEntry(helptext_info, token);
2564 }
2565
2566 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2567 {
2568 #if 1
2569   int font_nr = FONT_INFO_ELEMENTS;
2570 #else
2571   int font_nr = FONT_LEVEL_NUMBER;
2572 #endif
2573   int font_width = getFontWidth(font_nr);
2574   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
2575   int sy = mSY + 65 + 2 * 32 + 1;
2576   int ystep = TILEY + 4;
2577   int pad_x = sx - SX;
2578   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
2579   int max_lines_per_text = 2;    
2580   char *text = NULL;
2581
2582   if (action != -1 && direction != -1)          /* element.action.direction */
2583     text = getHelpText(element, action, direction);
2584
2585   if (text == NULL && action != -1)             /* element.action */
2586     text = getHelpText(element, action, -1);
2587
2588   if (text == NULL && direction != -1)          /* element.direction */
2589     text = getHelpText(element, -1, direction);
2590
2591   if (text == NULL)                             /* base element */
2592     text = getHelpText(element, -1, -1);
2593
2594   if (text == NULL)                             /* not found */
2595     text = "No description available";
2596
2597   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2598     sy += getFontHeight(font_nr) / 2;
2599
2600 #if 1
2601   DrawTextBuffer(sx, sy + ypos * ystep, text, font_nr,
2602                  max_chars_per_line, -1, max_lines_per_text, -1,
2603                  TRUE, FALSE, FALSE);
2604 #else
2605   DrawTextWrapped(sx, sy + ypos * ystep, text, font_nr,
2606                   max_chars_per_line, max_lines_per_text);
2607 #endif
2608 }
2609
2610 void DrawInfoScreen_TitleScreen()
2611 {
2612   DrawTitleScreen();
2613 }
2614
2615 void HandleInfoScreen_TitleScreen(int button)
2616 {
2617   HandleTitleScreen(0, 0, 0, 0, button);
2618 }
2619
2620 void DrawInfoScreen_Elements()
2621 {
2622   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2623
2624 #if 1
2625   FadeOut(REDRAW_FIELD);
2626 #endif
2627
2628   LoadHelpAnimInfo();
2629   LoadHelpTextInfo();
2630
2631   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2632
2633 #if 1
2634   FadeIn(REDRAW_FIELD);
2635 #endif
2636
2637   InitAnimation();
2638 }
2639
2640 void HandleInfoScreen_Elements(int button)
2641 {
2642   static unsigned long info_delay = 0;
2643   static int num_anims;
2644   static int num_pages;
2645   static int page;
2646   int anims_per_page = MAX_INFO_ELEMENTS_ON_SCREEN;
2647   int i;
2648
2649   if (button == MB_MENU_INITIALIZE)
2650   {
2651     boolean new_element = TRUE;
2652
2653     num_anims = 0;
2654
2655     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2656     {
2657       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2658         new_element = TRUE;
2659       else if (new_element)
2660       {
2661         num_anims++;
2662         new_element = FALSE;
2663       }
2664     }
2665
2666     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
2667     page = 0;
2668   }
2669
2670   if (button == MB_MENU_LEAVE)
2671   {
2672     PlaySound(SND_MENU_ITEM_SELECTING);
2673
2674     info_mode = INFO_MODE_MAIN;
2675     DrawInfoScreen();
2676
2677     return;
2678   }
2679   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2680   {
2681     if (button != MB_MENU_INITIALIZE)
2682     {
2683       PlaySound(SND_MENU_ITEM_SELECTING);
2684
2685       page++;
2686     }
2687
2688     if (page >= num_pages)
2689     {
2690       FadeSoundsAndMusic();
2691
2692 #if 0
2693       FadeOut(REDRAW_FIELD);
2694 #endif
2695
2696       info_mode = INFO_MODE_MAIN;
2697       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2698
2699       return;
2700     }
2701
2702     if (button != MB_MENU_INITIALIZE)
2703       FadeCrossSaveBackbuffer();
2704
2705     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
2706
2707     if (button != MB_MENU_INITIALIZE)
2708       FadeCross(REDRAW_FIELD);
2709   }
2710   else
2711   {
2712     if (DelayReached(&info_delay, GameFrameDelay))
2713       if (page < num_pages)
2714         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
2715
2716     PlayMenuSoundIfLoop();
2717   }
2718 }
2719
2720 void DrawInfoScreen_Music()
2721 {
2722   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
2723
2724 #if 1
2725   FadeOut(REDRAW_FIELD);
2726 #endif
2727
2728   ClearWindow();
2729   DrawHeadline();
2730
2731   LoadMusicInfo();
2732
2733   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
2734
2735 #if 1
2736   FadeIn(REDRAW_FIELD);
2737 #endif
2738 }
2739
2740 void HandleInfoScreen_Music(int button)
2741 {
2742   static struct MusicFileInfo *list = NULL;
2743   int ystart1 = mSY - SY + 100;
2744   int ystart2 = mSY - SY + 150;
2745   int ybottom = mSY - SY + SYSIZE - 20;
2746   int dy = 30;
2747
2748   if (button == MB_MENU_INITIALIZE)
2749   {
2750     list = music_file_info;
2751
2752     if (list == NULL)
2753     {
2754       FadeSoundsAndMusic();
2755
2756       ClearWindow();
2757       DrawHeadline();
2758
2759       DrawTextSCentered(ystart1, FONT_TEXT_1,
2760                         "No music info for this level set.");
2761
2762       DrawTextSCentered(ybottom, FONT_TEXT_4,
2763                         "Press any key or button for info menu");
2764
2765       return;
2766     }
2767   }
2768
2769   if (button == MB_MENU_LEAVE)
2770   {
2771     PlaySound(SND_MENU_ITEM_SELECTING);
2772
2773     FadeSoundsAndMusic();
2774
2775     info_mode = INFO_MODE_MAIN;
2776     DrawInfoScreen();
2777
2778     return;
2779   }
2780   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2781   {
2782     int y = 0;
2783
2784     if (button != MB_MENU_INITIALIZE)
2785     {
2786       PlaySound(SND_MENU_ITEM_SELECTING);
2787
2788       if (list != NULL)
2789         list = list->next;
2790     }
2791
2792     if (list == NULL)
2793     {
2794       FadeSoundsAndMusic();
2795
2796 #if 0
2797       FadeOut(REDRAW_FIELD);
2798 #endif
2799
2800       info_mode = INFO_MODE_MAIN;
2801       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2802
2803       return;
2804     }
2805
2806     FadeSoundsAndMusic();
2807
2808     if (button != MB_MENU_INITIALIZE)
2809       FadeCrossSaveBackbuffer();
2810
2811     ClearWindow();
2812     DrawHeadline();
2813
2814     if (list->is_sound)
2815     {
2816       int sound = list->music;
2817
2818       if (sound_info[sound].loop)
2819         PlaySoundLoop(sound);
2820       else
2821         PlaySound(sound);
2822
2823       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Sounds:");
2824     }
2825     else
2826     {
2827       PlayMusic(list->music);
2828
2829       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Music:");
2830     }
2831
2832     if (!strEqual(list->title, UNKNOWN_NAME))
2833     {
2834       if (!strEqual(list->title_header, UNKNOWN_NAME))
2835         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->title_header);
2836
2837       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->title);
2838     }
2839
2840     if (!strEqual(list->artist, UNKNOWN_NAME))
2841     {
2842       if (!strEqual(list->artist_header, UNKNOWN_NAME))
2843         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->artist_header);
2844       else
2845         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "by");
2846
2847       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->artist);
2848     }
2849
2850     if (!strEqual(list->album, UNKNOWN_NAME))
2851     {
2852       if (!strEqual(list->album_header, UNKNOWN_NAME))
2853         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->album_header);
2854       else
2855         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the album");
2856
2857       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->album);
2858     }
2859
2860     if (!strEqual(list->year, UNKNOWN_NAME))
2861     {
2862       if (!strEqual(list->year_header, UNKNOWN_NAME))
2863         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->year_header);
2864       else
2865         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the year");
2866
2867       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->year);
2868     }
2869
2870     DrawTextSCentered(ybottom, FONT_TEXT_4,
2871                       "Press any key or button for next page");
2872
2873     if (button != MB_MENU_INITIALIZE)
2874       FadeCross(REDRAW_FIELD);
2875   }
2876
2877   if (list != NULL && list->is_sound && sound_info[list->music].loop)
2878     PlaySoundLoop(list->music);
2879 }
2880
2881 static boolean DrawInfoScreen_CreditsScreen(int screen_nr)
2882 {
2883   int ystart1 = mSY - SY + 100;
2884   int ystart2 = mSY - SY + 150;
2885   int ybottom = mSY - SY + SYSIZE - 20;
2886   int ystep = 30;
2887
2888   if (screen_nr > 8)
2889     return FALSE;
2890
2891   ClearWindow();
2892   DrawHeadline();
2893
2894   DrawTextSCentered(ystart1, FONT_TEXT_1, "Credits:");
2895
2896   if (screen_nr == 0)
2897   {
2898     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2899                       "Special thanks to");
2900     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2901                       "Peter Liepa");
2902     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2903                       "for creating");
2904     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2905                       "\"Boulder Dash\"");
2906     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2907                       "in the year");
2908     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2909                       "1984");
2910     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2911                       "published by");
2912     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2913                       "First Star Software");
2914   }
2915   else if (screen_nr == 1)
2916   {
2917     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2918                       "Special thanks to");
2919     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2920                       "Klaus Heinz & Volker Wertich");
2921     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2922                       "for creating");
2923     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2924                       "\"Emerald Mine\"");
2925     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2926                       "in the year");
2927     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2928                       "1987");
2929     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2930                       "published by");
2931     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2932                       "Kingsoft");
2933   }
2934   else if (screen_nr == 2)
2935   {
2936     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2937                       "Special thanks to");
2938     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2939                       "Michael Stopp & Philip Jespersen");
2940     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2941                       "for creating");
2942     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2943                       "\"Supaplex\"");
2944     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2945                       "in the year");
2946     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2947                       "1991");
2948     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2949                       "published by");
2950     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2951                       "Digital Integration");
2952   }
2953   else if (screen_nr == 3)
2954   {
2955     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2956                       "Special thanks to");
2957     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2958                       "Hiroyuki Imabayashi");
2959     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2960                       "for creating");
2961     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2962                       "\"Sokoban\"");
2963     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2964                       "in the year");
2965     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2966                       "1982");
2967     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2968                       "published by");
2969     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2970                       "Thinking Rabbit");
2971   }
2972   else if (screen_nr == 4)
2973   {
2974     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2975                       "Special thanks to");
2976     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2977                       "Alan Bond");
2978     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2979                       "and");
2980     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2981                       "Jürgen Bonhagen");
2982     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2983                       "for the continuous creation");
2984     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2985                       "of outstanding level sets");
2986   }
2987   else if (screen_nr == 5)
2988   {
2989     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2990                       "Thanks to");
2991     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2992                       "Peter Elzner");
2993     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2994                       "for ideas and inspiration by");
2995     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2996                       "Diamond Caves");
2997
2998     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2999                       "Thanks to");
3000     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_3,
3001                       "Steffest");
3002     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
3003                       "for ideas and inspiration by");
3004     DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
3005                       "DX-Boulderdash");
3006   }
3007   else if (screen_nr == 6)
3008   {
3009     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3010                       "Thanks to");
3011     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3012                       "David Tritscher");
3013     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3014                       "for the new Emerald Mine engine");
3015   }
3016   else if (screen_nr == 7)
3017   {
3018     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3019                       "Thanks to");
3020     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3021                       "Guido Schulz");
3022     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3023                       "for the initial DOS port");
3024
3025     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3026                       "Thanks to");
3027     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
3028                       "Karl Hörnell");
3029     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
3030                       "for some additional toons");
3031   }
3032   else if (screen_nr == 8)
3033   {
3034     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3035                       "And not to forget:");
3036     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
3037                       "Many thanks to");
3038     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
3039                       "All those who contributed");
3040     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3041                       "levels to this game");
3042     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_3,
3043                       "since 1995");
3044   }
3045 #if 0
3046   else
3047   {
3048     return FALSE;
3049   }
3050 #endif
3051
3052   DrawTextSCentered(ybottom, FONT_TEXT_4,
3053                     "Press any key or button for next page");
3054
3055   return TRUE;
3056 }
3057
3058 void DrawInfoScreen_Credits()
3059 {
3060   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3061
3062   FadeSoundsAndMusic();
3063
3064 #if 1
3065   FadeOut(REDRAW_FIELD);
3066 #endif
3067
3068   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3069
3070 #if 1
3071   FadeIn(REDRAW_FIELD);
3072 #endif
3073 }
3074
3075 void HandleInfoScreen_Credits(int button)
3076 {
3077   static int screen_nr = 0;
3078
3079   if (button == MB_MENU_INITIALIZE)
3080   {
3081     screen_nr = 0;
3082
3083     DrawInfoScreen_CreditsScreen(screen_nr);
3084   }
3085   else if (button == MB_MENU_LEAVE)
3086   {
3087     PlaySound(SND_MENU_ITEM_SELECTING);
3088
3089     info_mode = INFO_MODE_MAIN;
3090     DrawInfoScreen();
3091
3092     return;
3093   }
3094   else if (button == MB_MENU_CHOICE)
3095   {
3096     boolean show_screen;
3097
3098     PlaySound(SND_MENU_ITEM_SELECTING);
3099
3100     screen_nr++;
3101
3102     FadeCrossSaveBackbuffer();
3103
3104     show_screen = DrawInfoScreen_CreditsScreen(screen_nr);
3105   
3106     if (show_screen)
3107     {
3108       FadeCross(REDRAW_FIELD);
3109     }
3110     else
3111     {
3112       FadeSoundsAndMusic();
3113
3114 #if 0
3115       FadeOut(REDRAW_FIELD);
3116 #endif
3117
3118       info_mode = INFO_MODE_MAIN;
3119       DrawAndFadeInInfoScreen(REDRAW_FIELD);
3120     }
3121   }
3122   else
3123   {
3124     PlayMenuSoundIfLoop();
3125   }
3126 }
3127
3128 void DrawInfoScreen_Program()
3129 {
3130   int ystart1 = mSY - SY + 100;
3131   int ystart2 = mSY - SY + 150;
3132   int ybottom = mSY - SY + SYSIZE - 20;
3133   int ystep = 30;
3134
3135   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3136
3137 #if 1
3138   FadeOut(REDRAW_FIELD);
3139 #endif
3140
3141   ClearWindow();
3142   DrawHeadline();
3143
3144   DrawTextSCentered(ystart1, FONT_TEXT_1, "Program Information:");
3145
3146   DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3147                     "This game is Freeware!");
3148   DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
3149                     "If you like it, send e-mail to:");
3150   DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
3151                     PROGRAM_EMAIL_STRING);
3152   DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_2,
3153                     "or SnailMail to:");
3154   DrawTextSCentered(ystart2 + 4 * ystep + 0, FONT_TEXT_3,
3155                     "Holger Schemel");
3156   DrawTextSCentered(ystart2 + 4 * ystep + 20, FONT_TEXT_3,
3157                     "Detmolder Strasse 189");
3158   DrawTextSCentered(ystart2 + 4 * ystep + 40, FONT_TEXT_3,
3159                     "33604 Bielefeld");
3160   DrawTextSCentered(ystart2 + 4 * ystep + 60, FONT_TEXT_3,
3161                     "Germany");
3162   DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
3163                     "More information and levels:");
3164   DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
3165                     PROGRAM_WEBSITE_STRING);
3166   DrawTextSCentered(ystart2 + 9 * ystep, FONT_TEXT_2,
3167                     "If you have created new levels,");
3168   DrawTextSCentered(ystart2 + 10 * ystep, FONT_TEXT_2,
3169                     "send them to me to include them!");
3170   DrawTextSCentered(ystart2 + 11 * ystep, FONT_TEXT_2,
3171                     ":-)");
3172
3173   DrawTextSCentered(ybottom, FONT_TEXT_4,
3174                     "Press any key or button for info menu");
3175
3176 #if 1
3177   FadeIn(REDRAW_FIELD);
3178 #endif
3179 }
3180
3181 void HandleInfoScreen_Program(int button)
3182 {
3183   if (button == MB_MENU_LEAVE)
3184   {
3185     PlaySound(SND_MENU_ITEM_SELECTING);
3186
3187     info_mode = INFO_MODE_MAIN;
3188     DrawInfoScreen();
3189
3190     return;
3191   }
3192   else if (button == MB_MENU_CHOICE)
3193   {
3194     PlaySound(SND_MENU_ITEM_SELECTING);
3195
3196     FadeSoundsAndMusic();
3197
3198 #if 0
3199     FadeOut(REDRAW_FIELD);
3200 #endif
3201
3202     info_mode = INFO_MODE_MAIN;
3203     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3204   }
3205   else
3206   {
3207     PlayMenuSoundIfLoop();
3208   }
3209 }
3210
3211 void DrawInfoScreen_Version()
3212 {
3213   int font_header = FONT_TEXT_3;
3214   int font_text = FONT_TEXT_2;
3215   int xstep = getFontWidth(font_text);
3216   int ystep = getFontHeight(font_text);
3217   int ystart1 = mSY - SY + 100;
3218   int ystart2 = mSY - SY + 150;
3219   int ybottom = mSY - SY + SYSIZE - 20;
3220   int xstart1 = mSX + 2 * xstep;
3221   int xstart2 = mSX + 19 * xstep;
3222 #if defined(TARGET_SDL)
3223   int xstart3 = mSX + 29 * xstep;
3224   SDL_version sdl_version_compiled;
3225   const SDL_version *sdl_version_linked;
3226   int driver_name_len = 8;
3227   char driver_name[driver_name_len];
3228 #endif
3229
3230   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3231
3232 #if 1
3233   FadeOut(REDRAW_FIELD);
3234 #endif
3235
3236   ClearWindow();
3237   DrawHeadline();
3238
3239   DrawTextSCentered(ystart1, FONT_TEXT_1, "Version Information:");
3240
3241   DrawTextF(xstart1, ystart2, font_header, "Name");
3242   DrawTextF(xstart2, ystart2, font_text, PROGRAM_TITLE_STRING);
3243
3244   ystart2 += ystep;
3245   DrawTextF(xstart1, ystart2, font_header, "Version");
3246   DrawTextF(xstart2, ystart2, font_text, getProgramFullVersionString());
3247
3248   ystart2 += ystep;
3249   DrawTextF(xstart1, ystart2, font_header, "Platform");
3250   DrawTextF(xstart2, ystart2, font_text, PLATFORM_STRING);
3251
3252   ystart2 += ystep;
3253   DrawTextF(xstart1, ystart2, font_header, "Target");
3254   DrawTextF(xstart2, ystart2, font_text, TARGET_STRING);
3255
3256   ystart2 += ystep;
3257   DrawTextF(xstart1, ystart2, font_header, "Compile time");
3258   DrawTextF(xstart2, ystart2, font_text, getCompileDateString());
3259
3260 #if defined(TARGET_SDL)
3261   ystart2 += 3 * ystep;
3262   DrawTextF(xstart1, ystart2, font_header, "Library");
3263   DrawTextF(xstart2, ystart2, font_header, "compiled");
3264   DrawTextF(xstart3, ystart2, font_header, "linked");
3265
3266   SDL_VERSION(&sdl_version_compiled);
3267   sdl_version_linked = SDL_Linked_Version();
3268
3269   ystart2 += 2 * ystep;
3270   DrawTextF(xstart1, ystart2, font_text, "SDL");
3271   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3272             sdl_version_compiled.major,
3273             sdl_version_compiled.minor,
3274             sdl_version_compiled.patch);
3275   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3276             sdl_version_linked->major,
3277             sdl_version_linked->minor,
3278             sdl_version_linked->patch);
3279
3280   SDL_IMAGE_VERSION(&sdl_version_compiled);
3281   sdl_version_linked = IMG_Linked_Version();
3282
3283   ystart2 += ystep;
3284   DrawTextF(xstart1, ystart2, font_text, "SDL_image");
3285   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3286             sdl_version_compiled.major,
3287             sdl_version_compiled.minor,
3288             sdl_version_compiled.patch);
3289   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3290             sdl_version_linked->major,
3291             sdl_version_linked->minor,
3292             sdl_version_linked->patch);
3293
3294   SDL_MIXER_VERSION(&sdl_version_compiled);
3295   sdl_version_linked = Mix_Linked_Version();
3296
3297   ystart2 += ystep;
3298   DrawTextF(xstart1, ystart2, font_text, "SDL_mixer");
3299   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3300             sdl_version_compiled.major,
3301             sdl_version_compiled.minor,
3302             sdl_version_compiled.patch);
3303   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3304             sdl_version_linked->major,
3305             sdl_version_linked->minor,
3306             sdl_version_linked->patch);
3307
3308   SDL_NET_VERSION(&sdl_version_compiled);
3309   sdl_version_linked = SDLNet_Linked_Version();
3310
3311   ystart2 += ystep;
3312   DrawTextF(xstart1, ystart2, font_text, "SDL_net");
3313   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3314             sdl_version_compiled.major,
3315             sdl_version_compiled.minor,
3316             sdl_version_compiled.patch);
3317   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3318             sdl_version_linked->major,
3319             sdl_version_linked->minor,
3320             sdl_version_linked->patch);
3321
3322   ystart2 += 3 * ystep;
3323   DrawTextF(xstart1, ystart2, font_header, "Driver");
3324   DrawTextF(xstart2, ystart2, font_header, "Requested");
3325   DrawTextF(xstart3, ystart2, font_header, "Used");
3326
3327   SDL_VideoDriverName(driver_name, driver_name_len);
3328
3329   ystart2 += 2 * ystep;
3330   DrawTextF(xstart1, ystart2, font_text, "SDL_VideoDriver");
3331   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_videodriver);
3332   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3333
3334   SDL_AudioDriverName(driver_name, driver_name_len);
3335
3336   ystart2 += ystep;
3337   DrawTextF(xstart1, ystart2, font_text, "SDL_AudioDriver");
3338   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_audiodriver);
3339   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3340 #endif
3341
3342   DrawTextSCentered(ybottom, FONT_TEXT_4,
3343                     "Press any key or button for info menu");
3344
3345 #if 1
3346   FadeIn(REDRAW_FIELD);
3347 #endif
3348 }
3349
3350 void HandleInfoScreen_Version(int button)
3351 {
3352   if (button == MB_MENU_LEAVE)
3353   {
3354     PlaySound(SND_MENU_ITEM_SELECTING);
3355
3356     info_mode = INFO_MODE_MAIN;
3357     DrawInfoScreen();
3358
3359     return;
3360   }
3361   else if (button == MB_MENU_CHOICE)
3362   {
3363     PlaySound(SND_MENU_ITEM_SELECTING);
3364
3365     FadeSoundsAndMusic();
3366
3367 #if 0
3368     FadeOut(REDRAW_FIELD);
3369 #endif
3370
3371     info_mode = INFO_MODE_MAIN;
3372     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3373   }
3374   else
3375   {
3376     PlayMenuSoundIfLoop();
3377   }
3378 }
3379
3380 void DrawInfoScreen_LevelSet()
3381 {
3382   struct TitleMessageInfo *tmi = &readme;
3383   char *filename = getLevelSetInfoFilename();
3384
3385   /* if chars set to "-1", automatically determine by text and font width */
3386   if (tmi->chars == -1)
3387     tmi->chars = tmi->width / getFontWidth(tmi->font);
3388   else
3389     tmi->width = tmi->chars * getFontWidth(tmi->font);
3390
3391   /* if lines set to "-1", automatically determine by text and font height */
3392   if (tmi->lines == -1)
3393     tmi->lines = tmi->height / getFontHeight(tmi->font);
3394   else
3395     tmi->height = tmi->lines * getFontHeight(tmi->font);
3396
3397   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3398
3399 #if 1
3400   FadeOut(REDRAW_FIELD);
3401 #endif
3402
3403   ClearWindow();
3404   DrawHeadline();
3405
3406   DrawTextCentered(mSY + 100, FONT_TEXT_1, "Level Set Information:");
3407
3408   if (filename != NULL)
3409     DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3410                  filename, tmi->font, tmi->chars, -1, tmi->lines, -1,
3411                  tmi->autowrap, tmi->centered, tmi->parse_comments);
3412   else
3413     DrawTextCentered(mSY + ALIGNED_TEXT_YPOS(tmi), FONT_TEXT_2,
3414                      "No information for this level set.");
3415
3416   DrawTextCentered(mSY + SYSIZE - 20, FONT_TEXT_4,
3417                    "Press any key or button for info menu");
3418
3419 #if 1
3420   FadeIn(REDRAW_FIELD);
3421 #endif
3422 }
3423
3424 void HandleInfoScreen_LevelSet(int button)
3425 {
3426   if (button == MB_MENU_LEAVE)
3427   {
3428     PlaySound(SND_MENU_ITEM_SELECTING);
3429
3430     info_mode = INFO_MODE_MAIN;
3431     DrawInfoScreen();
3432
3433     return;
3434   }
3435   else if (button == MB_MENU_CHOICE)
3436   {
3437     PlaySound(SND_MENU_ITEM_SELECTING);
3438
3439     FadeSoundsAndMusic();
3440
3441 #if 0
3442     FadeOut(REDRAW_FIELD);
3443 #endif
3444
3445     info_mode = INFO_MODE_MAIN;
3446     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3447   }
3448   else
3449   {
3450     PlayMenuSoundIfLoop();
3451   }
3452 }
3453
3454 static void DrawInfoScreenExt(int redraw_mask, boolean do_fading)
3455 {
3456   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
3457
3458   if (info_mode == INFO_MODE_TITLE)
3459     DrawInfoScreen_TitleScreen();
3460   else if (info_mode == INFO_MODE_ELEMENTS)
3461     DrawInfoScreen_Elements();
3462   else if (info_mode == INFO_MODE_MUSIC)
3463     DrawInfoScreen_Music();
3464   else if (info_mode == INFO_MODE_CREDITS)
3465     DrawInfoScreen_Credits();
3466   else if (info_mode == INFO_MODE_PROGRAM)
3467     DrawInfoScreen_Program();
3468   else if (info_mode == INFO_MODE_VERSION)
3469     DrawInfoScreen_Version();
3470   else if (info_mode == INFO_MODE_LEVELSET)
3471     DrawInfoScreen_LevelSet();
3472   else
3473     DrawInfoScreen_Main(redraw_mask, do_fading);
3474
3475   if (info_mode != INFO_MODE_MAIN &&
3476       info_mode != INFO_MODE_TITLE &&
3477       info_mode != INFO_MODE_MUSIC)
3478   {
3479     PlayMenuSound();
3480     PlayMenuMusic();
3481   }
3482 }
3483
3484 void DrawAndFadeInInfoScreen(int redraw_mask)
3485 {
3486   DrawInfoScreenExt(redraw_mask, TRUE);
3487 }
3488
3489 void DrawInfoScreen()
3490 {
3491   DrawInfoScreenExt(REDRAW_FIELD, FALSE);
3492 }
3493
3494 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3495 {
3496   if (info_mode == INFO_MODE_TITLE)
3497     HandleInfoScreen_TitleScreen(button);
3498   else if (info_mode == INFO_MODE_ELEMENTS)
3499     HandleInfoScreen_Elements(button);
3500   else if (info_mode == INFO_MODE_MUSIC)
3501     HandleInfoScreen_Music(button);
3502   else if (info_mode == INFO_MODE_CREDITS)
3503     HandleInfoScreen_Credits(button);
3504   else if (info_mode == INFO_MODE_PROGRAM)
3505     HandleInfoScreen_Program(button);
3506   else if (info_mode == INFO_MODE_VERSION)
3507     HandleInfoScreen_Version(button);
3508   else if (info_mode == INFO_MODE_LEVELSET)
3509     HandleInfoScreen_LevelSet(button);
3510   else
3511     HandleInfoScreen_Main(mx, my, dx, dy, button);
3512
3513   DoAnimation();
3514 }
3515
3516
3517 /* ========================================================================= */
3518 /* type name functions                                                       */
3519 /* ========================================================================= */
3520
3521 void HandleTypeName(int newxpos, Key key)
3522 {
3523   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3524   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3525 #if 1
3526   struct TextPosInfo *pos = mci->pos_input;
3527   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3528   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3529 #endif
3530 #if 1
3531   static int xpos = 0;
3532 #else
3533   static int xpos = 0, ypos = 2;
3534 #endif
3535   int font_nr = pos->font;
3536   int font_active_nr = FONT_ACTIVE(font_nr);
3537   int font_width = getFontWidth(font_active_nr);
3538 #if 1
3539 #if 0
3540   int startx = mSX + mci->pos_input->x;
3541   int starty = mSY + mci->pos_input->y;
3542 #endif
3543 #else
3544   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
3545   int startx = mSX + 32 + name_width;
3546   int starty = mSY + ypos * 32;
3547 #endif
3548   char key_char = getValidConfigValueChar(getCharFromKey(key));
3549   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3550   boolean is_active = TRUE;
3551
3552   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3553
3554   if (newxpos)
3555   {
3556     strcpy(last_player_name, setup.player_name);
3557
3558     xpos = newxpos;
3559
3560 #if 0
3561     /* add one character width for added cursor character */
3562     pos->width += font_width;
3563     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3564
3565     DrawText(startx, starty, setup.player_name, font_active_nr);
3566     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3567 #endif
3568   }
3569   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3570   {
3571     setup.player_name[xpos] = key_char;
3572     setup.player_name[xpos + 1] = 0;
3573
3574     xpos++;
3575
3576 #if 0
3577     /* add one character width for added name text character */
3578     pos->width += font_width;
3579     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3580
3581     DrawText(startx, starty, setup.player_name, font_active_nr);
3582     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3583 #endif
3584   }
3585   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3586   {
3587     xpos--;
3588
3589     setup.player_name[xpos] = 0;
3590
3591 #if 0
3592     /* remove one character width for removed name text character */
3593     pos->width -= font_width;
3594     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3595
3596     DrawText(startx, starty, setup.player_name, font_active_nr);
3597     DrawText(startx + xpos * font_width, starty, "_ ", font_active_nr);
3598 #endif
3599   }
3600   else if (key == KSYM_Return && xpos > 0)
3601   {
3602 #if 0
3603     /* remove one character width for removed cursor text character */
3604     pos->width -= font_width;
3605     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3606
3607     DrawText(startx, starty, setup.player_name, font_nr);
3608     DrawText(startx + xpos * font_width, starty, " ", font_active_nr);
3609 #endif
3610
3611     SaveSetup();
3612
3613     is_active = FALSE;
3614
3615     game_status = GAME_MODE_MAIN;
3616   }
3617   else if (key == KSYM_Escape)
3618   {
3619     strcpy(setup.player_name, last_player_name);
3620
3621     is_active = FALSE;
3622
3623     game_status = GAME_MODE_MAIN;
3624   }
3625
3626   if (is_active)
3627   {
3628     pos->width = (strlen(setup.player_name) + 1) * font_width;
3629     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3630
3631     DrawText(startx, starty, setup.player_name, font_active_nr);
3632     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3633   }
3634   else
3635   {
3636     pos->width = strlen(setup.player_name) * font_width;
3637     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3638
3639     DrawText(startx, starty, setup.player_name, font_nr);
3640   }
3641 }
3642
3643
3644 /* ========================================================================= */
3645 /* tree menu functions                                                       */
3646 /* ========================================================================= */
3647
3648 static void DrawChooseTree(TreeInfo **ti_ptr)
3649 {
3650   UnmapAllGadgets();
3651
3652   FreeScreenGadgets();
3653   CreateScreenGadgets();
3654
3655   CloseDoor(DOOR_CLOSE_2);
3656
3657 #if 1
3658   FadeOut(REDRAW_FIELD);
3659 #endif
3660
3661   ClearWindow();
3662
3663   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3664   MapScreenTreeGadgets(*ti_ptr);
3665
3666 #if 1
3667   FadeIn(REDRAW_FIELD);
3668 #endif
3669
3670   InitAnimation();
3671 }
3672
3673 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
3674 {
3675   struct GadgetInfo *gi = screen_gadget[id];
3676   int items_max, items_visible, item_position;
3677
3678   items_max = numTreeInfoInGroup(ti);
3679   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
3680   item_position = first_entry;
3681
3682   if (item_position > items_max - items_visible)
3683     item_position = items_max - items_visible;
3684
3685   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
3686                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3687                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
3688 }
3689
3690 static void drawChooseTreeList(int first_entry, int num_page_entries,
3691                                TreeInfo *ti)
3692 {
3693   int i;
3694   char *title_string = NULL;
3695   int yoffset_sets = MENU_TITLE1_YPOS;
3696   int yoffset_setup = 16;
3697   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? yoffset_sets :
3698                  yoffset_setup);
3699   int last_game_status = game_status;   /* save current game status */
3700
3701   title_string = ti->infotext;
3702
3703   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3704
3705   /* force LEVELS font on artwork setup screen */
3706   game_status = GAME_MODE_LEVELS;
3707
3708 #if 1
3709   /* clear tree list area, but not title or scrollbar */
3710   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3711                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
3712                  NUM_MENU_ENTRIES_ON_SCREEN * 32);
3713 #else
3714   /* clear tree list area, but not title or scrollbar */
3715   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3716                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
3717                  MAX_MENU_ENTRIES_ON_SCREEN * 32);
3718 #endif
3719
3720   for (i = 0; i < num_page_entries; i++)
3721   {
3722     TreeInfo *node, *node_first;
3723     int entry_pos = first_entry + i;
3724     int xpos = MENU_SCREEN_START_XPOS;
3725     int ypos = MENU_SCREEN_START_YPOS + i;
3726     int startx = mSX + xpos * 32;
3727     int starty = mSY + ypos * 32;
3728     int font_nr = FONT_TEXT_1;
3729     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3730     int startx_text = startx + font_xoffset;
3731     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3732     int text_size = startx_scrollbar - startx_text;
3733     int max_buffer_len = text_size / getFontWidth(font_nr);
3734     char buffer[max_buffer_len + 1];
3735
3736     node_first = getTreeInfoFirstGroupEntry(ti);
3737     node = getTreeInfoFromPos(node_first, entry_pos);
3738
3739     strncpy(buffer, node->name, max_buffer_len);
3740     buffer[max_buffer_len] = '\0';
3741
3742     DrawText(startx, starty, buffer, font_nr + node->color);
3743
3744     if (node->parent_link)
3745       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3746     else if (node->level_group)
3747       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3748     else
3749       initCursor(i, IMG_MENU_BUTTON);
3750   }
3751
3752   game_status = last_game_status;       /* restore current game status */
3753
3754   redraw_mask |= REDRAW_FIELD;
3755 }
3756
3757 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3758 {
3759   TreeInfo *node, *node_first;
3760   int x, last_redraw_mask = redraw_mask;
3761   int ypos = MENU_TITLE2_YPOS;
3762   int font_nr = FONT_TITLE_2;
3763
3764   if (ti->type != TREE_TYPE_LEVEL_DIR)
3765     return;
3766
3767   node_first = getTreeInfoFirstGroupEntry(ti);
3768   node = getTreeInfoFromPos(node_first, entry_pos);
3769
3770   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3771
3772   if (node->parent_link)
3773     DrawTextFCentered(ypos, font_nr, "leave group \"%s\"",
3774                       node->class_desc);
3775   else if (node->level_group)
3776     DrawTextFCentered(ypos, font_nr, "enter group \"%s\"",
3777                       node->class_desc);
3778   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3779     DrawTextFCentered(ypos, font_nr, "%3d levels (%s)",
3780                       node->levels, node->class_desc);
3781
3782   /* let BackToFront() redraw only what is needed */
3783   redraw_mask = last_redraw_mask | REDRAW_TILES;
3784   for (x = 0; x < SCR_FIELDX; x++)
3785     MarkTileDirty(x, 1);
3786 }
3787
3788 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3789                              TreeInfo **ti_ptr)
3790 {
3791   TreeInfo *ti = *ti_ptr;
3792   int x = 0;
3793   int y = ti->cl_cursor;
3794   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3795   int num_entries = numTreeInfoInGroup(ti);
3796   int num_page_entries;
3797   int last_game_status = game_status;   /* save current game status */
3798   boolean position_set_by_scrollbar = (dx == 999);
3799
3800   /* force LEVELS draw offset on choose level and artwork setup screen */
3801   game_status = GAME_MODE_LEVELS;
3802
3803   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3804     num_page_entries = num_entries;
3805   else
3806     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3807
3808   game_status = last_game_status;       /* restore current game status */
3809
3810   if (button == MB_MENU_INITIALIZE)
3811   {
3812     int num_entries = numTreeInfoInGroup(ti);
3813     int entry_pos = posTreeInfo(ti);
3814
3815     if (ti->cl_first == -1)
3816     {
3817       /* only on initialization */
3818       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3819       ti->cl_cursor = entry_pos - ti->cl_first;
3820     }
3821     else if (ti->cl_cursor >= num_page_entries ||
3822              (num_entries > num_page_entries &&
3823               num_entries - ti->cl_first < num_page_entries))
3824     {
3825       /* only after change of list size (by custom graphic configuration) */
3826       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3827       ti->cl_cursor = entry_pos - ti->cl_first;
3828     }
3829
3830     if (position_set_by_scrollbar)
3831       ti->cl_first = dy;
3832     else
3833       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3834                                 ti->cl_first, ti);
3835
3836     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3837     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3838     drawChooseTreeCursor(ti->cl_cursor, TRUE);
3839
3840     return;
3841   }
3842   else if (button == MB_MENU_LEAVE)
3843   {
3844     FadeSetLeaveMenu();
3845
3846     PlaySound(SND_MENU_ITEM_SELECTING);
3847
3848     if (ti->node_parent)
3849     {
3850       *ti_ptr = ti->node_parent;
3851       DrawChooseTree(ti_ptr);
3852     }
3853     else if (game_status == GAME_MODE_SETUP)
3854     {
3855       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
3856         execSetupGame();
3857       else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
3858         execSetupGraphics();
3859       else
3860         execSetupArtwork();
3861     }
3862     else
3863     {
3864       game_status = GAME_MODE_MAIN;
3865 #if 1
3866       DrawMainMenuExt(REDRAW_FIELD, FALSE);
3867 #else
3868       DrawMainMenu();
3869 #endif
3870     }
3871
3872     return;
3873   }
3874
3875   if (mx || my)         /* mouse input */
3876   {
3877     int last_game_status = game_status; /* save current game status */
3878
3879     /* force LEVELS draw offset on artwork setup screen */
3880     game_status = GAME_MODE_LEVELS;
3881
3882     x = (mx - mSX) / 32;
3883     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
3884
3885     game_status = last_game_status;     /* restore current game status */
3886   }
3887   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
3888   {
3889     /* move cursor instead of scrolling when already at start/end of list */
3890     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
3891       dy = -1;
3892     else if (dy == +1 * SCROLL_LINE &&
3893              ti->cl_first + num_page_entries == num_entries)
3894       dy = 1;
3895
3896     /* handle scrolling screen one line or page */
3897     if (ti->cl_cursor + dy < 0 ||
3898         ti->cl_cursor + dy > num_page_entries - 1)
3899     {
3900       if (ABS(dy) == SCROLL_PAGE)
3901         step = num_page_entries - 1;
3902
3903       if (dy < 0 && ti->cl_first > 0)
3904       {
3905         /* scroll page/line up */
3906
3907         ti->cl_first -= step;
3908         if (ti->cl_first < 0)
3909           ti->cl_first = 0;
3910
3911         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3912         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3913         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3914
3915         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3916                                   ti->cl_first, ti);
3917       }
3918       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
3919       {
3920         /* scroll page/line down */
3921
3922         ti->cl_first += step;
3923         if (ti->cl_first + num_page_entries > num_entries)
3924           ti->cl_first = MAX(0, num_entries - num_page_entries);
3925
3926         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3927         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3928         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3929
3930         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3931                                   ti->cl_first, ti);
3932       }
3933
3934       return;
3935     }
3936
3937     /* handle moving cursor one line */
3938     y = ti->cl_cursor + dy;
3939   }
3940
3941   if (dx == 1)
3942   {
3943     TreeInfo *node_first, *node_cursor;
3944     int entry_pos = ti->cl_first + y;
3945
3946     node_first = getTreeInfoFirstGroupEntry(ti);
3947     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3948
3949     if (node_cursor->node_group)
3950     {
3951       FadeSetEnterMenu();
3952
3953       PlaySound(SND_MENU_ITEM_SELECTING);
3954
3955       node_cursor->cl_first = ti->cl_first;
3956       node_cursor->cl_cursor = ti->cl_cursor;
3957       *ti_ptr = node_cursor->node_group;
3958       DrawChooseTree(ti_ptr);
3959
3960       return;
3961     }
3962   }
3963   else if (dx == -1 && ti->node_parent)
3964   {
3965     FadeSetLeaveMenu();
3966
3967     PlaySound(SND_MENU_ITEM_SELECTING);
3968
3969     *ti_ptr = ti->node_parent;
3970     DrawChooseTree(ti_ptr);
3971
3972     return;
3973   }
3974
3975   if (!anyScrollbarGadgetActive() &&
3976       IN_VIS_FIELD(x, y) &&
3977       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
3978       y >= 0 && y < num_page_entries)
3979   {
3980     if (button)
3981     {
3982       if (y != ti->cl_cursor)
3983       {
3984         PlaySound(SND_MENU_ITEM_ACTIVATING);
3985
3986         drawChooseTreeCursor(ti->cl_cursor, FALSE);
3987         drawChooseTreeCursor(y, TRUE);
3988         drawChooseTreeInfo(ti->cl_first + y, ti);
3989
3990         ti->cl_cursor = y;
3991       }
3992     }
3993     else
3994     {
3995       TreeInfo *node_first, *node_cursor;
3996       int entry_pos = ti->cl_first + y;
3997
3998       PlaySound(SND_MENU_ITEM_SELECTING);
3999
4000       node_first = getTreeInfoFirstGroupEntry(ti);
4001       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4002
4003       if (node_cursor->node_group)
4004       {
4005         FadeSetEnterMenu();
4006
4007         node_cursor->cl_first = ti->cl_first;
4008         node_cursor->cl_cursor = ti->cl_cursor;
4009         *ti_ptr = node_cursor->node_group;
4010         DrawChooseTree(ti_ptr);
4011       }
4012       else if (node_cursor->parent_link)
4013       {
4014         FadeSetLeaveMenu();
4015
4016         *ti_ptr = node_cursor->node_parent;
4017         DrawChooseTree(ti_ptr);
4018       }
4019       else
4020       {
4021         FadeSetStartItem();
4022
4023         node_cursor->cl_first = ti->cl_first;
4024         node_cursor->cl_cursor = ti->cl_cursor;
4025         *ti_ptr = node_cursor;
4026
4027         if (ti->type == TREE_TYPE_LEVEL_DIR)
4028         {
4029           LoadLevelSetup_SeriesInfo();
4030
4031           SaveLevelSetup_LastSeries();
4032           SaveLevelSetup_SeriesInfo();
4033           TapeErase();
4034         }
4035
4036         if (game_status == GAME_MODE_SETUP)
4037         {
4038           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
4039             execSetupGame();
4040           else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
4041             execSetupGraphics();
4042           else
4043             execSetupArtwork();
4044         }
4045         else
4046         {
4047           game_status = GAME_MODE_MAIN;
4048           DrawMainMenu();
4049         }
4050       }
4051     }
4052   }
4053 }
4054
4055 void DrawChooseLevel()
4056 {
4057   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
4058
4059   DrawChooseTree(&leveldir_current);
4060
4061   PlayMenuSound();
4062   PlayMenuMusic();
4063 }
4064
4065 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
4066 {
4067   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4068
4069   DoAnimation();
4070 }
4071
4072 void DrawHallOfFame(int highlight_position)
4073 {
4074   UnmapAllGadgets();
4075   FadeSoundsAndMusic();
4076
4077   /* (this is needed when called from GameEnd() after winning a game) */
4078   KeyboardAutoRepeatOn();
4079   ActivateJoystick();
4080
4081   /* (this is needed when called from GameEnd() after winning a game) */
4082   SetDrawDeactivationMask(REDRAW_NONE);
4083   SetDrawBackgroundMask(REDRAW_FIELD);
4084
4085   CloseDoor(DOOR_CLOSE_2);
4086
4087   if (highlight_position < 0) 
4088     LoadScore(level_nr);
4089
4090   FadeSetStartItem();
4091
4092 #if 1
4093   FadeOut(REDRAW_FIELD);
4094 #endif
4095
4096   InitAnimation();
4097
4098   PlayMenuSound();
4099   PlayMenuMusic();
4100
4101   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
4102
4103 #if 1
4104   FadeIn(REDRAW_FIELD);
4105 #endif
4106 }
4107
4108 static void drawHallOfFameList(int first_entry, int highlight_position)
4109 {
4110   int i;
4111
4112   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4113   ClearWindow();
4114
4115   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4116   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4117                     "HighScores of Level %d", level_nr);
4118
4119   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4120   {
4121     int entry = first_entry + i;
4122     boolean active = (entry == highlight_position);
4123     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4124     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4125     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4126     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4127     int dx1 = 3 * getFontWidth(font_nr1);
4128     int dx2 = dx1 + getFontWidth(font_nr1);
4129     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
4130     int sy = mSY + 64 + i * 32;
4131
4132     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4133     DrawText(mSX + dx1, sy, ".", font_nr1);
4134     DrawText(mSX + dx2, sy, ".........................", font_nr3);
4135
4136     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4137       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4138
4139     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4140   }
4141
4142   redraw_mask |= REDRAW_FIELD;
4143 }
4144
4145 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4146 {
4147   static int first_entry = 0;
4148   static int highlight_position = 0;
4149   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4150
4151   if (button == MB_MENU_INITIALIZE)
4152   {
4153     first_entry = 0;
4154     highlight_position = mx;
4155     drawHallOfFameList(first_entry, highlight_position);
4156
4157     return;
4158   }
4159
4160   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
4161     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4162
4163   if (dy < 0)
4164   {
4165     if (first_entry > 0)
4166     {
4167       first_entry -= step;
4168       if (first_entry < 0)
4169         first_entry = 0;
4170
4171       drawHallOfFameList(first_entry, highlight_position);
4172     }
4173   }
4174   else if (dy > 0)
4175   {
4176     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4177     {
4178       first_entry += step;
4179       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4180         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4181
4182       drawHallOfFameList(first_entry, highlight_position);
4183     }
4184   }
4185   else if (button == MB_MENU_LEAVE)
4186   {
4187     PlaySound(SND_MENU_ITEM_SELECTING);
4188
4189     FadeSound(SND_BACKGROUND_SCORES);
4190
4191     game_status = GAME_MODE_MAIN;
4192
4193     DrawMainMenu();
4194   }
4195   else if (button == MB_MENU_CHOICE)
4196   {
4197     PlaySound(SND_MENU_ITEM_SELECTING);
4198
4199     FadeSound(SND_BACKGROUND_SCORES);
4200
4201 #if 0
4202     FadeOut(REDRAW_FIELD);
4203 #endif
4204
4205     game_status = GAME_MODE_MAIN;
4206
4207     DrawAndFadeInMainMenu(REDRAW_FIELD);
4208   }
4209
4210   if (game_status == GAME_MODE_SCORES)
4211     PlayMenuSoundIfLoop();
4212
4213   DoAnimation();
4214 }
4215
4216
4217 /* ========================================================================= */
4218 /* setup screen functions                                                    */
4219 /* ========================================================================= */
4220
4221 static struct TokenInfo *setup_info;
4222 static int num_setup_info;
4223
4224 static char *screen_mode_text;
4225 static char *game_speed_text;
4226 static char *graphics_set_name;
4227 static char *sounds_set_name;
4228 static char *music_set_name;
4229
4230 static void execSetupMain()
4231 {
4232 #if 0
4233   FadeSetLeaveMenu();
4234 #endif
4235
4236   setup_mode = SETUP_MODE_MAIN;
4237   DrawSetupScreen();
4238 }
4239
4240 static void execSetupGame()
4241 {
4242 #if 0
4243   FadeSetEnterMenu();
4244 #endif
4245
4246   if (game_speeds == NULL)
4247   {
4248     int i;
4249
4250     for (i = 0; game_speeds_list[i].value != -1; i++)
4251     {
4252       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4253       char identifier[32], name[32];
4254       int value = game_speeds_list[i].value;
4255       char *text = game_speeds_list[i].text;
4256
4257       ti->node_top = &game_speeds;
4258       ti->sort_priority = 10000 - value;
4259
4260       sprintf(identifier, "%d", value);
4261       sprintf(name, "%s", text);
4262
4263       setString(&ti->identifier, identifier);
4264       setString(&ti->name, name);
4265       setString(&ti->name_sorting, name);
4266       setString(&ti->infotext, "Game Speed");
4267
4268       pushTreeInfo(&game_speeds, ti);
4269     }
4270
4271     /* sort game speed values to start with slowest game speed */
4272     sortTreeInfo(&game_speeds);
4273
4274     /* set current game speed to configured game speed value */
4275     game_speed_current =
4276       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4277
4278     /* if that fails, set current game speed to reliable default value */
4279     if (game_speed_current == NULL)
4280       game_speed_current =
4281         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4282
4283     /* if that also fails, set current game speed to first available speed */
4284     if (game_speed_current == NULL)
4285       game_speed_current = game_speeds;
4286   }
4287
4288   setup.game_frame_delay = atoi(game_speed_current->identifier);
4289
4290   /* needed for displaying game speed text instead of identifier */
4291   game_speed_text = game_speed_current->name;
4292
4293   setup_mode = SETUP_MODE_GAME;
4294   DrawSetupScreen();
4295 }
4296
4297 static void execSetupChooseGameSpeed()
4298 {
4299 #if 0
4300   FadeSetEnterMenu();
4301 #endif
4302
4303   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4304   DrawSetupScreen();
4305 }
4306
4307 static void execSetupEditor()
4308 {
4309 #if 0
4310   FadeSetEnterMenu();
4311 #endif
4312
4313   setup_mode = SETUP_MODE_EDITOR;
4314   DrawSetupScreen();
4315 }
4316
4317 static void execSetupGraphics()
4318 {
4319 #if 0
4320   FadeSetEnterMenu();
4321 #endif
4322
4323   if (video.fullscreen_available && screen_modes == NULL)
4324   {
4325     int i;
4326
4327     for (i = 0; video.fullscreen_modes[i].width != -1; i++)
4328     {
4329       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4330       char identifier[32], name[32];
4331       int x = video.fullscreen_modes[i].width;
4332       int y = video.fullscreen_modes[i].height;
4333       int xx, yy;
4334
4335       get_aspect_ratio_from_screen_mode(&video.fullscreen_modes[i], &xx, &yy);
4336
4337       ti->node_top = &screen_modes;
4338       ti->sort_priority = x * 10000 + y;
4339
4340       sprintf(identifier, "%dx%d", x, y);
4341       sprintf(name, "%d x %d [%d:%d]", x, y, xx, yy);
4342
4343       setString(&ti->identifier, identifier);
4344       setString(&ti->name, name);
4345       setString(&ti->name_sorting, name);
4346       setString(&ti->infotext, "Fullscreen Mode");
4347
4348       pushTreeInfo(&screen_modes, ti);
4349     }
4350
4351     /* sort fullscreen modes to start with lowest available screen resolution */
4352     sortTreeInfo(&screen_modes);
4353
4354     /* set current screen mode for fullscreen mode to configured setup value */
4355     screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4356                                                     setup.fullscreen_mode);
4357
4358     /* if that fails, set current screen mode to reliable default value */
4359     if (screen_mode_current == NULL)
4360       screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4361                                                       DEFAULT_FULLSCREEN_MODE);
4362
4363     /* if that also fails, set current screen mode to first available mode */
4364     if (screen_mode_current == NULL)
4365       screen_mode_current = screen_modes;
4366
4367     if (screen_mode_current == NULL)
4368       video.fullscreen_available = FALSE;
4369   }
4370
4371   if (video.fullscreen_available)
4372   {
4373     setup.fullscreen_mode = screen_mode_current->identifier;
4374
4375     /* needed for displaying screen mode name instead of identifier */
4376     screen_mode_text = screen_mode_current->name;
4377   }
4378
4379   setup_mode = SETUP_MODE_GRAPHICS;
4380   DrawSetupScreen();
4381 }
4382
4383 static void execSetupChooseScreenMode()
4384 {
4385 #if 0
4386   FadeSetEnterMenu();
4387 #endif
4388
4389   if (!video.fullscreen_available)
4390     return;
4391
4392   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4393   DrawSetupScreen();
4394 }
4395
4396 static void execSetupSound()
4397 {
4398 #if 0
4399   FadeSetEnterMenu();
4400 #endif
4401
4402   setup_mode = SETUP_MODE_SOUND;
4403   DrawSetupScreen();
4404 }
4405
4406 static void execSetupArtwork()
4407 {
4408 #if 0
4409   FadeSetEnterMenu();
4410 #endif
4411
4412   setup.graphics_set = artwork.gfx_current->identifier;
4413   setup.sounds_set = artwork.snd_current->identifier;
4414   setup.music_set = artwork.mus_current->identifier;
4415
4416   /* needed if last screen (setup choice) changed graphics, sounds or music */
4417   ReloadCustomArtwork(0);
4418
4419   /* needed for displaying artwork name instead of artwork identifier */
4420   graphics_set_name = artwork.gfx_current->name;
4421   sounds_set_name = artwork.snd_current->name;
4422   music_set_name = artwork.mus_current->name;
4423
4424   setup_mode = SETUP_MODE_ARTWORK;
4425   DrawSetupScreen();
4426 }
4427
4428 static void execSetupChooseGraphics()
4429 {
4430 #if 0
4431   FadeSetEnterMenu();
4432 #endif
4433
4434   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4435   DrawSetupScreen();
4436 }
4437
4438 static void execSetupChooseSounds()
4439 {
4440 #if 0
4441   FadeSetEnterMenu();
4442 #endif
4443
4444   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4445   DrawSetupScreen();
4446 }
4447
4448 static void execSetupChooseMusic()
4449 {
4450 #if 0
4451   FadeSetEnterMenu();
4452 #endif
4453
4454   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4455   DrawSetupScreen();
4456 }
4457
4458 static void execSetupInput()
4459 {
4460 #if 0
4461   FadeSetEnterMenu();
4462 #endif
4463
4464   setup_mode = SETUP_MODE_INPUT;
4465   DrawSetupScreen();
4466 }
4467
4468 static void execSetupShortcut1()
4469 {
4470 #if 0
4471   FadeSetEnterMenu();
4472 #endif
4473
4474   setup_mode = SETUP_MODE_SHORTCUT_1;
4475   DrawSetupScreen();
4476 }
4477
4478 static void execSetupShortcut2()
4479 {
4480 #if 0
4481   FadeSetEnterMenu();
4482 #endif
4483
4484   setup_mode = SETUP_MODE_SHORTCUT_2;
4485   DrawSetupScreen();
4486 }
4487
4488 static void execExitSetup()
4489 {
4490 #if 0
4491   FadeSetLeaveMenu();
4492 #endif
4493
4494   game_status = GAME_MODE_MAIN;
4495 #if 1
4496   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4497 #else
4498   DrawMainMenu();
4499 #endif
4500 }
4501
4502 static void execSaveAndExitSetup()
4503 {
4504   SaveSetup();
4505   execExitSetup();
4506 }
4507
4508 static struct TokenInfo setup_info_main[] =
4509 {
4510   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4511   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4512   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4513   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4514   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4515   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4516   { TYPE_ENTER_MENU,    execSetupShortcut1,     "Key Shortcuts 1"       },
4517   { TYPE_ENTER_MENU,    execSetupShortcut2,     "Key Shortcuts 2"       },
4518   { TYPE_EMPTY,         NULL,                   ""                      },
4519   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4520   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4521
4522   { 0,                  NULL,                   NULL                    }
4523 };
4524
4525 static struct TokenInfo setup_info_game[] =
4526 {
4527   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4528   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4529   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4530   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4531   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4532   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4533   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4534   { TYPE_STRING,        &game_speed_text,       ""                      },
4535   { TYPE_EMPTY,         NULL,                   ""                      },
4536   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4537
4538   { 0,                  NULL,                   NULL                    }
4539 };
4540
4541 static struct TokenInfo setup_info_editor[] =
4542 {
4543 #if 0
4544   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4545   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4546   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4547   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4548   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4549   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4550   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4551   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4552 #endif
4553   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4554   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4555   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4556 #if 0
4557   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4558 #endif
4559   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4560   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4561   { TYPE_EMPTY,         NULL,                   ""                      },
4562 #if 0
4563   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4564   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4565   { TYPE_EMPTY,         NULL,                   ""                      },
4566 #endif
4567   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4568   { TYPE_EMPTY,         NULL,                   ""                      },
4569   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4570
4571   { 0,                  NULL,                   NULL                    }
4572 };
4573
4574 static struct TokenInfo setup_info_graphics[] =
4575 {
4576   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4577   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4578   { TYPE_STRING,        &screen_mode_text,      ""                      },
4579   { TYPE_SWITCH,        &setup.scroll_delay,    "Delayed Scrolling:"    },
4580 #if 0
4581   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scrolling:"       },
4582   { TYPE_SWITCH,        &setup.double_buffering,"Double-Buffering:"     },
4583 #endif
4584   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4585   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4586   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4587   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4588   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4589   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4590   { TYPE_EMPTY,         NULL,                   ""                      },
4591   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4592
4593   { 0,                  NULL,                   NULL                    }
4594 };
4595
4596 static struct TokenInfo setup_info_sound[] =
4597 {
4598   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4599   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4600   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4601   { TYPE_EMPTY,         NULL,                   ""                      },
4602   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4603
4604   { 0,                  NULL,                   NULL                    }
4605 };
4606
4607 static struct TokenInfo setup_info_artwork[] =
4608 {
4609   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
4610   { TYPE_STRING,        &graphics_set_name,     ""                      },
4611   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
4612   { TYPE_STRING,        &sounds_set_name,       ""                      },
4613   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
4614   { TYPE_STRING,        &music_set_name,        ""                      },
4615   { TYPE_EMPTY,         NULL,                   ""                      },
4616 #if 1
4617   { TYPE_YES_NO, &setup.override_level_graphics,"Override Level Graphics:" },
4618   { TYPE_YES_NO, &setup.override_level_sounds,  "Override Level Sounds:"   },
4619   { TYPE_YES_NO, &setup.override_level_music,   "Override Level Music:"    },
4620 #else
4621   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
4622   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
4623   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
4624   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
4625 #endif
4626   { TYPE_EMPTY,         NULL,                   ""                      },
4627   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4628
4629   { 0,                  NULL,                   NULL                    }
4630 };
4631
4632 static struct TokenInfo setup_info_input[] =
4633 {
4634   { TYPE_SWITCH,        NULL,                   "Player:"               },
4635   { TYPE_SWITCH,        NULL,                   "Device:"               },
4636   { TYPE_ENTER_MENU,    NULL,                   ""                      },
4637   { TYPE_EMPTY,         NULL,                   ""                      },
4638   { TYPE_EMPTY,         NULL,                   ""                      },
4639   { TYPE_EMPTY,         NULL,                   ""                      },
4640   { TYPE_EMPTY,         NULL,                   ""                      },
4641   { TYPE_EMPTY,         NULL,                   ""                      },
4642   { TYPE_EMPTY,         NULL,                   ""                      },
4643   { TYPE_EMPTY,         NULL,                   ""                      },
4644   { TYPE_EMPTY,         NULL,                   ""                      },
4645   { TYPE_EMPTY,         NULL,                   ""                      },
4646   { TYPE_EMPTY,         NULL,                   ""                      },
4647   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4648
4649   { 0,                  NULL,                   NULL                    }
4650 };
4651
4652 static struct TokenInfo setup_info_shortcut_1[] =
4653 {
4654   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
4655   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
4656   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
4657   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
4658   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
4659   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
4660   { TYPE_EMPTY,         NULL,                   ""                      },
4661   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
4662   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
4663   { TYPE_EMPTY,         NULL,                   ""                      },
4664   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4665
4666   { 0,                  NULL,                   NULL                    }
4667 };
4668
4669 static struct TokenInfo setup_info_shortcut_2[] =
4670 {
4671   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
4672   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
4673   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
4674   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
4675   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
4676   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
4677   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
4678   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
4679   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
4680   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
4681   { TYPE_EMPTY,         NULL,                   ""                      },
4682   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4683
4684   { 0,                  NULL,                   NULL                    }
4685 };
4686
4687 static Key getSetupKey()
4688 {
4689   Key key = KSYM_UNDEFINED;
4690   boolean got_key_event = FALSE;
4691
4692   while (!got_key_event)
4693   {
4694     if (PendingEvent())         /* got event */
4695     {
4696       Event event;
4697
4698       NextEvent(&event);
4699
4700       switch (event.type)
4701       {
4702         case EVENT_KEYPRESS:
4703           {
4704             key = GetEventKey((KeyEvent *)&event, TRUE);
4705
4706             /* press 'Escape' or 'Enter' to keep the existing key binding */
4707             if (key == KSYM_Escape || key == KSYM_Return)
4708               key = KSYM_UNDEFINED;     /* keep old value */
4709
4710             got_key_event = TRUE;
4711           }
4712           break;
4713
4714         case EVENT_KEYRELEASE:
4715           key_joystick_mapping = 0;
4716           break;
4717
4718         default:
4719           HandleOtherEvents(&event);
4720           break;
4721       }
4722     }
4723
4724     DoAnimation();
4725     BackToFront();
4726
4727     /* don't eat all CPU time */
4728     Delay(10);
4729   }
4730
4731   return key;
4732 }
4733
4734 static int getSetupTextFont(int type)
4735 {
4736   if (type & (TYPE_SWITCH |
4737               TYPE_YES_NO |
4738               TYPE_STRING |
4739               TYPE_ECS_AGA |
4740               TYPE_KEYTEXT |
4741               TYPE_ENTER_LIST))
4742     return FONT_MENU_2;
4743   else
4744     return FONT_MENU_1;
4745 }
4746
4747 static int getSetupValueFont(int type, void *value)
4748 {
4749   if (type & TYPE_KEY)
4750     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
4751   else if (type & TYPE_STRING)
4752     return FONT_VALUE_2;
4753   else if (type & TYPE_ECS_AGA)
4754     return FONT_VALUE_1;
4755   else if (type & TYPE_BOOLEAN_STYLE)
4756     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
4757   else
4758     return FONT_VALUE_1;
4759 }
4760
4761 static void drawSetupValue(int pos)
4762 {
4763   boolean font_draw_xoffset_modified = FALSE;
4764   int font_draw_xoffset_old = -1;
4765   int xpos = MENU_SCREEN_VALUE_XPOS;
4766   int ypos = MENU_SCREEN_START_YPOS + pos;
4767   int startx = mSX + xpos * 32;
4768   int starty = mSY + ypos * 32;
4769   int font_nr, font_width;
4770   int type = setup_info[pos].type;
4771   void *value = setup_info[pos].value;
4772   char *value_string = getSetupValue(type, value);
4773   int i;
4774
4775   if (value_string == NULL)
4776     return;
4777
4778   if (type & TYPE_KEY)
4779   {
4780     xpos = MENU_SCREEN_START_XPOS;
4781
4782     if (type & TYPE_QUERY)
4783     {
4784       value_string = "<press key>";
4785     }
4786   }
4787   else if (type & TYPE_STRING)
4788   {
4789     int max_value_len = (SCR_FIELDX - 2) * 2;
4790
4791     xpos = MENU_SCREEN_START_XPOS;
4792
4793     if (strlen(value_string) > max_value_len)
4794       value_string[max_value_len] = '\0';
4795   }
4796
4797   startx = mSX + xpos * 32;
4798   starty = mSY + ypos * 32;
4799   font_nr = getSetupValueFont(type, value);
4800   font_width = getFontWidth(font_nr);
4801
4802   /* downward compatibility correction for Juergen Bonhagen's menu settings */
4803   if (setup_mode != SETUP_MODE_INPUT)
4804   {
4805     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
4806     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4807     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
4808     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
4809     int text_font_nr = getSetupTextFont(FONT_MENU_2);
4810     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
4811     int text_width = MAX_MENU_TEXT_LENGTH_MEDIUM * getFontWidth(text_font_nr);
4812     boolean correct_font_draw_xoffset = FALSE;
4813
4814     if (xpos == MENU_SCREEN_START_XPOS &&
4815         startx + font1_xoffset < text_startx + text_font_xoffset)
4816       correct_font_draw_xoffset = TRUE;
4817
4818     if (xpos == MENU_SCREEN_VALUE_XPOS &&
4819         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
4820       correct_font_draw_xoffset = TRUE;
4821
4822     /* check if setup value would overlap with setup text when printed */
4823     /* (this can happen for extreme/wrong values for font draw offset) */
4824     if (correct_font_draw_xoffset)
4825     {
4826       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
4827       font_draw_xoffset_modified = TRUE;
4828
4829       if (type & TYPE_KEY)
4830         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
4831       else if (!(type & TYPE_STRING))
4832         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
4833           MAX_MENU_TEXT_LENGTH_MEDIUM * (16 - getFontWidth(text_font_nr));
4834     }
4835   }
4836
4837   for (i = 0; i <= MENU_SCREEN_MAX_XPOS - xpos; i++)
4838     DrawText(startx + i * font_width, starty, " ", font_nr);
4839
4840   DrawText(startx, starty, value_string, font_nr);
4841
4842   if (font_draw_xoffset_modified)
4843     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
4844 }
4845
4846 static void changeSetupValue(int pos)
4847 {
4848   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
4849   {
4850     *(boolean *)setup_info[pos].value ^= TRUE;
4851   }
4852   else if (setup_info[pos].type & TYPE_KEY)
4853   {
4854     Key key;
4855
4856     setup_info[pos].type |= TYPE_QUERY;
4857     drawSetupValue(pos);
4858     setup_info[pos].type &= ~TYPE_QUERY;
4859
4860     key = getSetupKey();
4861     if (key != KSYM_UNDEFINED)
4862       *(Key *)setup_info[pos].value = key;
4863   }
4864
4865   drawSetupValue(pos);
4866 }
4867
4868 static void DrawCursorAndText_Setup(int pos, boolean active)
4869 {
4870   int xpos = MENU_SCREEN_START_XPOS;
4871   int ypos = MENU_SCREEN_START_YPOS + pos;
4872   int font_nr = getSetupTextFont(setup_info[pos].type);
4873
4874   if (setup_info == setup_info_input)
4875     font_nr = FONT_MENU_1;
4876
4877   if (active)
4878     font_nr = FONT_ACTIVE(font_nr);
4879
4880   DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[pos].text, font_nr);
4881
4882   if (setup_info[pos].type & ~TYPE_SKIP_ENTRY)
4883     drawCursor(pos, active);
4884 }
4885
4886 static void DrawSetupScreen_Generic()
4887 {
4888   char *title_string = NULL;
4889   int i;
4890
4891   UnmapAllGadgets();
4892   CloseDoor(DOOR_CLOSE_2);
4893
4894 #if 1
4895   FadeOut(REDRAW_FIELD);
4896 #endif
4897
4898   ClearWindow();
4899
4900   if (setup_mode == SETUP_MODE_MAIN)
4901   {
4902     setup_info = setup_info_main;
4903     title_string = "Setup";
4904   }
4905   else if (setup_mode == SETUP_MODE_GAME)
4906   {
4907     setup_info = setup_info_game;
4908     title_string = "Setup Game";
4909   }
4910   else if (setup_mode == SETUP_MODE_EDITOR)
4911   {
4912     setup_info = setup_info_editor;
4913     title_string = "Setup Editor";
4914   }
4915   else if (setup_mode == SETUP_MODE_GRAPHICS)
4916   {
4917     setup_info = setup_info_graphics;
4918     title_string = "Setup Graphics";
4919   }
4920   else if (setup_mode == SETUP_MODE_SOUND)
4921   {
4922     setup_info = setup_info_sound;
4923     title_string = "Setup Sound";
4924   }
4925   else if (setup_mode == SETUP_MODE_ARTWORK)
4926   {
4927     setup_info = setup_info_artwork;
4928     title_string = "Custom Artwork";
4929   }
4930   else if (setup_mode == SETUP_MODE_SHORTCUT_1)
4931   {
4932     setup_info = setup_info_shortcut_1;
4933     title_string = "Setup Shortcuts";
4934   }
4935   else if (setup_mode == SETUP_MODE_SHORTCUT_2)
4936   {
4937     setup_info = setup_info_shortcut_2;
4938     title_string = "Setup Shortcuts";
4939   }
4940
4941   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
4942
4943   num_setup_info = 0;
4944   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4945   {
4946     void *value_ptr = setup_info[i].value;
4947 #if 1
4948 #else
4949     int xpos = MENU_SCREEN_START_XPOS;
4950     int ypos = MENU_SCREEN_START_YPOS + i;
4951     int font_nr;
4952 #endif
4953
4954     /* set some entries to "unchangeable" according to other variables */
4955     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
4956         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
4957         (value_ptr == &setup.sound_music  && !audio.music_available) ||
4958         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
4959         (value_ptr == &screen_mode_text   && !video.fullscreen_available))
4960       setup_info[i].type |= TYPE_GHOSTED;
4961
4962     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
4963       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4964     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
4965       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4966     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
4967       initCursor(i, IMG_MENU_BUTTON);
4968
4969 #if 1
4970     DrawCursorAndText_Setup(i, FALSE);
4971 #else
4972     font_nr = getSetupTextFont(setup_info[i].type);
4973
4974     DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[i].text, font_nr);
4975 #endif
4976
4977     if (setup_info[i].type & TYPE_VALUE)
4978       drawSetupValue(i);
4979
4980     num_setup_info++;
4981   }
4982
4983 #if 0
4984   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
4985                     "Joysticks deactivated in setup menu");
4986 #endif
4987
4988 #if 1
4989   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4990 #endif
4991
4992 #if 1
4993   FadeIn(REDRAW_FIELD);
4994 #endif
4995
4996   InitAnimation();
4997 #if 0
4998   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4999 #endif
5000 }
5001
5002 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
5003 {
5004   static int choice_store[MAX_SETUP_MODES];
5005   int choice = choice_store[setup_mode];        /* always starts with 0 */
5006   int x = 0;
5007   int y = choice;
5008
5009   if (button == MB_MENU_INITIALIZE)
5010   {
5011     /* advance to first valid menu entry */
5012     while (choice < num_setup_info &&
5013            setup_info[choice].type & TYPE_SKIP_ENTRY)
5014       choice++;
5015     choice_store[setup_mode] = choice;
5016
5017 #if 1
5018     DrawCursorAndText_Setup(choice, TRUE);
5019 #else
5020     drawCursor(choice, TRUE);
5021 #endif
5022
5023     return;
5024   }
5025   else if (button == MB_MENU_LEAVE)
5026   {
5027     PlaySound(SND_MENU_ITEM_SELECTING);
5028
5029     for (y = 0; y < num_setup_info; y++)
5030     {
5031       if (setup_info[y].type & TYPE_LEAVE_MENU)
5032       {
5033         void (*menu_callback_function)(void) = setup_info[y].value;
5034
5035         FadeSetLeaveMenu();
5036
5037         menu_callback_function();
5038
5039         break;  /* absolutely needed because function changes 'setup_info'! */
5040       }
5041     }
5042
5043     return;
5044   }
5045
5046   if (mx || my)         /* mouse input */
5047   {
5048     x = (mx - mSX) / 32;
5049     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5050   }
5051   else if (dx || dy)    /* keyboard input */
5052   {
5053     if (dx)
5054     {
5055       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
5056
5057       if (setup_info[choice].type & menu_navigation_type ||
5058           setup_info[choice].type & TYPE_BOOLEAN_STYLE)
5059         button = MB_MENU_CHOICE;
5060     }
5061     else if (dy)
5062       y = choice + dy;
5063
5064     /* jump to next non-empty menu entry (up or down) */
5065     while (y > 0 && y < num_setup_info - 1 &&
5066            setup_info[y].type & TYPE_SKIP_ENTRY)
5067       y += dy;
5068   }
5069
5070   if (IN_VIS_FIELD(x, y) && y >= 0 && y < num_setup_info)
5071   {
5072     if (button)
5073     {
5074       if (y != choice && setup_info[y].type & ~TYPE_SKIP_ENTRY)
5075       {
5076         PlaySound(SND_MENU_ITEM_ACTIVATING);
5077
5078 #if 1
5079         DrawCursorAndText_Setup(choice, FALSE);
5080         DrawCursorAndText_Setup(y, TRUE);
5081 #else
5082         drawCursor(choice, FALSE);
5083         drawCursor(y, TRUE);
5084 #endif
5085
5086         choice = choice_store[setup_mode] = y;
5087       }
5088     }
5089     else if (!(setup_info[y].type & TYPE_GHOSTED))
5090     {
5091       PlaySound(SND_MENU_ITEM_SELECTING);
5092
5093       /* when selecting key headline, execute function for key value change */
5094       if (setup_info[y].type & TYPE_KEYTEXT &&
5095           setup_info[y + 1].type & TYPE_KEY)
5096         y++;
5097
5098       /* when selecting string value, execute function for list selection */
5099       if (setup_info[y].type & TYPE_STRING && y > 0 &&
5100           setup_info[y - 1].type & TYPE_ENTER_LIST)
5101         y--;
5102
5103       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE)
5104       {
5105         void (*menu_callback_function)(void) = setup_info[y].value;
5106
5107         FadeSetFromType(setup_info[y].type);
5108
5109         menu_callback_function();
5110       }
5111       else
5112       {
5113         if (setup_info[y].type & TYPE_VALUE)
5114           changeSetupValue(y);
5115       }
5116     }
5117   }
5118 }
5119
5120 void DrawSetupScreen_Input()
5121 {
5122 #if 1
5123   int i;
5124 #endif
5125
5126 #if 1
5127   FadeOut(REDRAW_FIELD);
5128 #endif
5129
5130   ClearWindow();
5131
5132 #if 1
5133   setup_info = setup_info_input;
5134 #endif
5135
5136   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5137
5138 #if 1
5139   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5140   {
5141     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5142       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5143     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5144       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5145     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5146       initCursor(i, IMG_MENU_BUTTON);
5147
5148     DrawCursorAndText_Setup(i, FALSE);
5149   }
5150 #else
5151   initCursor(0,  IMG_MENU_BUTTON);
5152   initCursor(1,  IMG_MENU_BUTTON);
5153   initCursor(2,  IMG_MENU_BUTTON_ENTER_MENU);
5154   initCursor(13, IMG_MENU_BUTTON_LEAVE_MENU);
5155
5156   DrawText(mSX + 32, mSY +  2 * 32, "Player:", FONT_MENU_1);
5157   DrawText(mSX + 32, mSY +  3 * 32, "Device:", FONT_MENU_1);
5158   DrawText(mSX + 32, mSY + 15 * 32, "Back",   FONT_MENU_1);
5159 #endif
5160
5161 #if 0
5162   DeactivateJoystickForCalibration();
5163 #endif
5164 #if 1
5165   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5166                     "Joysticks deactivated on this screen");
5167 #endif
5168
5169   /* create gadgets for setup input menu screen */
5170   FreeScreenGadgets();
5171   CreateScreenGadgets();
5172
5173   /* map gadgets for setup input menu screen */
5174   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5175
5176   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5177
5178 #if 1
5179   FadeIn(REDRAW_FIELD);
5180 #endif
5181
5182   InitAnimation();
5183 }
5184
5185 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5186 {
5187   if (device_name == NULL)
5188     return;
5189
5190   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5191     device_nr = 0;
5192
5193   if (strlen(device_name) > 1)
5194   {
5195     char c1 = device_name[strlen(device_name) - 1];
5196     char c2 = device_name[strlen(device_name) - 2];
5197
5198     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5199       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5200   }
5201   else
5202     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5203             strlen(device_name));
5204 }
5205
5206 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5207 {
5208   int i;
5209   static struct SetupKeyboardInfo custom_key;
5210   static struct
5211   {
5212     Key *key;
5213     char *text;
5214   } custom[] =
5215   {
5216     { &custom_key.left,  "Joystick Left"  },
5217     { &custom_key.right, "Joystick Right" },
5218     { &custom_key.up,    "Joystick Up"    },
5219     { &custom_key.down,  "Joystick Down"  },
5220     { &custom_key.snap,  "Button 1"       },
5221     { &custom_key.drop,  "Button 2"       }
5222   };
5223   static char *joystick_name[MAX_PLAYERS] =
5224   {
5225     "Joystick1",
5226     "Joystick2",
5227     "Joystick3",
5228     "Joystick4"
5229   };
5230   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5231
5232   InitJoysticks();
5233
5234   custom_key = setup.input[player_nr].key;
5235
5236   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5237            FONT_INPUT_1_ACTIVE);
5238
5239   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5240                              TILEX, TILEY);
5241   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5242                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5243
5244   if (setup.input[player_nr].use_joystick)
5245   {
5246     char *device_name = setup.input[player_nr].joy.device_name;
5247     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5248     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5249
5250     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5251     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5252   }
5253   else
5254   {
5255     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5256     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5257   }
5258
5259   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5260
5261   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5262   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5263   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5264   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5265
5266   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5267   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5268   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5269   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5270   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5271   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5272
5273   for (i = 0; i < 6; i++)
5274   {
5275     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5276
5277     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5278              "              ", FONT_VALUE_1);
5279     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5280              (setup.input[player_nr].use_joystick ?
5281               custom[i].text :
5282               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5283   }
5284 }
5285
5286 static int input_player_nr = 0;
5287
5288 void HandleSetupScreen_Input_Player(int step, int direction)
5289 {
5290   int old_player_nr = input_player_nr;
5291   int new_player_nr;
5292
5293   new_player_nr = old_player_nr + step * direction;
5294   if (new_player_nr < 0)
5295     new_player_nr = 0;
5296   if (new_player_nr > MAX_PLAYERS - 1)
5297     new_player_nr = MAX_PLAYERS - 1;
5298
5299   if (new_player_nr != old_player_nr)
5300   {
5301     input_player_nr = new_player_nr;
5302
5303     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5304   }
5305 }
5306
5307 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5308 {
5309   static int choice = 0;
5310   int x = 0;
5311   int y = choice;
5312   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5313   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5314   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5315   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5316
5317   if (button == MB_MENU_INITIALIZE)
5318   {
5319     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5320
5321 #if 1
5322     DrawCursorAndText_Setup(choice, TRUE);
5323 #else
5324     drawCursor(choice, TRUE);
5325 #endif
5326
5327     return;
5328   }
5329   else if (button == MB_MENU_LEAVE)
5330   {
5331     setup_mode = SETUP_MODE_MAIN;
5332     DrawSetupScreen();
5333     InitJoysticks();
5334
5335     return;
5336   }
5337
5338   if (mx || my)         /* mouse input */
5339   {
5340     x = (mx - mSX) / 32;
5341     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5342   }
5343   else if (dx || dy)    /* keyboard input */
5344   {
5345     if (dx && choice == 0)
5346       x = (dx < 0 ? 10 : 12);
5347     else if ((dx && choice == 1) ||
5348              (dx == +1 && choice == 2) ||
5349              (dx == -1 && choice == pos_end))
5350       button = MB_MENU_CHOICE;
5351     else if (dy)
5352       y = choice + dy;
5353
5354     if (y >= pos_empty1 && y <= pos_empty2)
5355       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
5356   }
5357
5358   if (y == 0 && dx != 0 && button)
5359   {
5360     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
5361   }
5362   else if (IN_VIS_FIELD(x, y) &&
5363            y >= pos_start && y <= pos_end &&
5364            !(y >= pos_empty1 && y <= pos_empty2))
5365   {
5366     if (button)
5367     {
5368       if (y != choice)
5369       {
5370 #if 1
5371         DrawCursorAndText_Setup(choice, FALSE);
5372         DrawCursorAndText_Setup(y, TRUE);
5373
5374         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
5375 #else
5376         drawCursor(choice, FALSE);
5377         drawCursor(y, TRUE);
5378 #endif
5379
5380         choice = y;
5381       }
5382     }
5383     else
5384     {
5385       if (y == 1)
5386       {
5387         char *device_name = setup.input[input_player_nr].joy.device_name;
5388
5389         if (!setup.input[input_player_nr].use_joystick)
5390         {
5391           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
5392
5393           setJoystickDeviceToNr(device_name, new_device_nr);
5394           setup.input[input_player_nr].use_joystick = TRUE;
5395         }
5396         else
5397         {
5398           int device_nr = getJoystickNrFromDeviceName(device_name);
5399           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
5400
5401           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
5402             setup.input[input_player_nr].use_joystick = FALSE;
5403           else
5404             setJoystickDeviceToNr(device_name, new_device_nr);
5405         }
5406
5407         drawPlayerSetupInputInfo(input_player_nr, FALSE);
5408       }
5409       else if (y == 2)
5410       {
5411         if (setup.input[input_player_nr].use_joystick)
5412         {
5413           InitJoysticks();
5414           CalibrateJoystick(input_player_nr);
5415         }
5416         else
5417           CustomizeKeyboard(input_player_nr);
5418       }
5419       else if (y == pos_end)
5420       {
5421         InitJoysticks();
5422
5423         FadeSetLeaveMenu();
5424
5425         setup_mode = SETUP_MODE_MAIN;
5426         DrawSetupScreen();
5427       }
5428     }
5429   }
5430 }
5431
5432 void CustomizeKeyboard(int player_nr)
5433 {
5434   int i;
5435   int step_nr;
5436   boolean finished = FALSE;
5437   static struct SetupKeyboardInfo custom_key;
5438   static struct
5439   {
5440     Key *key;
5441     char *text;
5442   } customize_step[] =
5443   {
5444     { &custom_key.left,  "Move Left"    },
5445     { &custom_key.right, "Move Right"   },
5446     { &custom_key.up,    "Move Up"      },
5447     { &custom_key.down,  "Move Down"    },
5448     { &custom_key.snap,  "Snap Field"   },
5449     { &custom_key.drop,  "Drop Element" }
5450   };
5451
5452   /* read existing key bindings from player setup */
5453   custom_key = setup.input[player_nr].key;
5454
5455   FadeSetEnterMenu();
5456   FadeOut(REDRAW_FIELD);
5457
5458   ClearWindow();
5459
5460   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
5461
5462 #if 0
5463   BackToFront();
5464   InitAnimation();
5465 #endif
5466
5467   step_nr = 0;
5468   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5469            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5470   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5471            "Key:", FONT_INPUT_1_ACTIVE);
5472   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5473            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
5474
5475 #if 1
5476   FadeIn(REDRAW_FIELD);
5477
5478   InitAnimation();
5479 #endif
5480
5481   while (!finished)
5482   {
5483     if (PendingEvent())         /* got event */
5484     {
5485       Event event;
5486
5487       NextEvent(&event);
5488
5489       switch (event.type)
5490       {
5491         case EVENT_KEYPRESS:
5492           {
5493             Key key = GetEventKey((KeyEvent *)&event, FALSE);
5494
5495             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
5496             {
5497               if (key == KSYM_Escape)
5498                 FadeSkipNextFadeIn();
5499
5500               finished = TRUE;
5501               break;
5502             }
5503
5504             /* all keys configured -- wait for "Escape" or "Return" key */
5505             if (step_nr == 6)
5506               break;
5507
5508             /* press 'Enter' to keep the existing key binding */
5509             if (key == KSYM_Return)
5510               key = *customize_step[step_nr].key;
5511
5512             /* check if key already used */
5513             for (i = 0; i < step_nr; i++)
5514               if (*customize_step[i].key == key)
5515                 break;
5516             if (i < step_nr)
5517               break;
5518
5519             /* got new key binding */
5520             *customize_step[step_nr].key = key;
5521             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5522                      "             ", FONT_VALUE_1);
5523             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5524                      getKeyNameFromKey(key), FONT_VALUE_1);
5525             step_nr++;
5526
5527             /* un-highlight last query */
5528             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
5529                      customize_step[step_nr - 1].text, FONT_MENU_1);
5530             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
5531                      "Key:", FONT_MENU_1);
5532
5533             /* press 'Enter' to leave */
5534             if (step_nr == 6)
5535             {
5536               DrawText(mSX + 16, mSY + 15 * 32 + 16,
5537                        "Press Enter", FONT_TITLE_1);
5538               break;
5539             }
5540
5541             /* query next key binding */
5542             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5543                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5544             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5545                      "Key:", FONT_INPUT_1_ACTIVE);
5546             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5547                      getKeyNameFromKey(*customize_step[step_nr].key),
5548                      FONT_VALUE_OLD);
5549           }
5550           break;
5551
5552         case EVENT_KEYRELEASE:
5553           key_joystick_mapping = 0;
5554           break;
5555
5556         default:
5557           HandleOtherEvents(&event);
5558           break;
5559       }
5560     }
5561
5562     DoAnimation();
5563     BackToFront();
5564
5565     /* don't eat all CPU time */
5566     Delay(10);
5567   }
5568
5569   /* write new key bindings back to player setup */
5570   setup.input[player_nr].key = custom_key;
5571
5572   StopAnimation();
5573   DrawSetupScreen_Input();
5574 }
5575
5576 static boolean CalibrateJoystickMain(int player_nr)
5577 {
5578   int new_joystick_xleft = JOYSTICK_XMIDDLE;
5579   int new_joystick_xright = JOYSTICK_XMIDDLE;
5580   int new_joystick_yupper = JOYSTICK_YMIDDLE;
5581   int new_joystick_ylower = JOYSTICK_YMIDDLE;
5582   int new_joystick_xmiddle, new_joystick_ymiddle;
5583
5584   int joystick_fd = joystick.fd[player_nr];
5585   int x, y, last_x, last_y, xpos = 8, ypos = 3;
5586   boolean check[3][3];
5587   int check_remaining = 3 * 3;
5588   int joy_x, joy_y;
5589   int joy_value;
5590   int result = -1;
5591
5592   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
5593     return FALSE;
5594
5595   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
5596     return FALSE;
5597
5598   FadeSetEnterMenu();
5599   FadeOut(REDRAW_FIELD);
5600
5601   ClearWindow();
5602
5603   for (y = 0; y < 3; y++)
5604   {
5605     for (x = 0; x < 3; x++)
5606     {
5607       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
5608       check[x][y] = FALSE;
5609     }
5610   }
5611
5612   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
5613   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
5614   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
5615   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
5616   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
5617   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
5618   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
5619
5620   joy_value = Joystick(player_nr);
5621   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5622   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5623
5624   /* eventually uncalibrated center position (joystick could be uncentered) */
5625   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5626     return FALSE;
5627
5628   new_joystick_xmiddle = joy_x;
5629   new_joystick_ymiddle = joy_y;
5630
5631   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
5632
5633 #if 1
5634   FadeIn(REDRAW_FIELD);
5635 #else
5636   BackToFront();
5637 #endif
5638
5639   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
5640   InitAnimation();
5641
5642   while (result < 0)
5643   {
5644     if (PendingEvent())         /* got event */
5645     {
5646       Event event;
5647
5648       NextEvent(&event);
5649
5650       switch (event.type)
5651       {
5652         case EVENT_KEYPRESS:
5653           switch (GetEventKey((KeyEvent *)&event, TRUE))
5654           {
5655             case KSYM_Return:
5656               if (check_remaining == 0)
5657                 result = 1;
5658               break;
5659
5660             case KSYM_Escape:
5661               FadeSkipNextFadeIn();
5662               result = 0;
5663               break;
5664
5665             default:
5666               break;
5667           }
5668           break;
5669
5670         case EVENT_KEYRELEASE:
5671           key_joystick_mapping = 0;
5672           break;
5673
5674         default:
5675           HandleOtherEvents(&event);
5676           break;
5677       }
5678     }
5679
5680     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5681       return FALSE;
5682
5683     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
5684     new_joystick_xright = MAX(new_joystick_xright, joy_x);
5685     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
5686     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
5687
5688     setup.input[player_nr].joy.xleft = new_joystick_xleft;
5689     setup.input[player_nr].joy.yupper = new_joystick_yupper;
5690     setup.input[player_nr].joy.xright = new_joystick_xright;
5691     setup.input[player_nr].joy.ylower = new_joystick_ylower;
5692     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
5693     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
5694
5695     CheckJoystickData();
5696
5697     joy_value = Joystick(player_nr);
5698
5699     if (joy_value & JOY_BUTTON && check_remaining == 0)
5700       result = 1;
5701
5702     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5703     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5704
5705     if (x != last_x || y != last_y)
5706     {
5707       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
5708       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
5709
5710       last_x = x;
5711       last_y = y;
5712
5713       if (check_remaining > 0 && !check[x+1][y+1])
5714       {
5715         check[x+1][y+1] = TRUE;
5716         check_remaining--;
5717       }
5718
5719 #if 0
5720 #ifdef DEBUG
5721       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
5722              setup.input[player_nr].joy.xleft,
5723              setup.input[player_nr].joy.xmiddle,
5724              setup.input[player_nr].joy.xright);
5725       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
5726              setup.input[player_nr].joy.yupper,
5727              setup.input[player_nr].joy.ymiddle,
5728              setup.input[player_nr].joy.ylower);
5729 #endif
5730 #endif
5731
5732     }
5733
5734     DoAnimation();
5735     BackToFront();
5736
5737     /* don't eat all CPU time */
5738     Delay(10);
5739   }
5740
5741   /* calibrated center position (joystick should now be centered) */
5742   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5743     return FALSE;
5744
5745   new_joystick_xmiddle = joy_x;
5746   new_joystick_ymiddle = joy_y;
5747
5748   StopAnimation();
5749
5750 #if 0
5751   DrawSetupScreen_Input();
5752 #endif
5753
5754   /* wait until the last pressed button was released */
5755   while (Joystick(player_nr) & JOY_BUTTON)
5756   {
5757     if (PendingEvent())         /* got event */
5758     {
5759       Event event;
5760
5761       NextEvent(&event);
5762       HandleOtherEvents(&event);
5763
5764       Delay(10);
5765     }
5766   }
5767
5768   return TRUE;
5769 }
5770
5771 void CalibrateJoystick(int player_nr)
5772 {
5773   if (!CalibrateJoystickMain(player_nr))
5774   {
5775     char *device_name = setup.input[player_nr].joy.device_name;
5776     int nr = getJoystickNrFromDeviceName(device_name) + 1;
5777     int xpos = mSX - SX;
5778     int ypos = mSY - SY;
5779
5780     ClearWindow();
5781
5782     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
5783     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
5784     BackToFront();
5785
5786     Delay(2000);                /* show error message for a short time */
5787
5788     ClearEventQueue();
5789   }
5790
5791 #if 1
5792   DrawSetupScreen_Input();
5793 #endif
5794 }
5795
5796 void DrawSetupScreen()
5797 {
5798   DeactivateJoystick();
5799
5800   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
5801
5802   if (setup_mode == SETUP_MODE_INPUT)
5803     DrawSetupScreen_Input();
5804   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5805     DrawChooseTree(&game_speed_current);
5806   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5807     DrawChooseTree(&screen_mode_current);
5808   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5809     DrawChooseTree(&artwork.gfx_current);
5810   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5811     DrawChooseTree(&artwork.snd_current);
5812   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5813     DrawChooseTree(&artwork.mus_current);
5814   else
5815     DrawSetupScreen_Generic();
5816
5817   PlayMenuSound();
5818   PlayMenuMusic();
5819 }
5820
5821 void RedrawSetupScreenAfterFullscreenToggle()
5822 {
5823   if (setup_mode == SETUP_MODE_GRAPHICS)
5824     DrawSetupScreen();
5825 }
5826
5827 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
5828 {
5829   if (setup_mode == SETUP_MODE_INPUT)
5830     HandleSetupScreen_Input(mx, my, dx, dy, button);
5831   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5832     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
5833   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5834     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
5835   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5836     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
5837   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5838     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
5839   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5840     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
5841   else
5842     HandleSetupScreen_Generic(mx, my, dx, dy, button);
5843
5844   DoAnimation();
5845 }
5846
5847 void HandleGameActions()
5848 {
5849   if (game_status != GAME_MODE_PLAYING)
5850     return;
5851
5852   GameActions();        /* main game loop */
5853
5854   if (tape.auto_play && !tape.playing)
5855     AutoPlayTape();     /* continue automatically playing next tape */
5856 }
5857
5858
5859 /* ---------- new screen button stuff -------------------------------------- */
5860
5861 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
5862 {
5863   switch (gadget_id)
5864   {
5865 #if 1
5866     case SCREEN_CTRL_ID_PREV_LEVEL:
5867       *x = mSX + menu.main.button.prev_level.x;
5868       *y = mSY + menu.main.button.prev_level.y;
5869       break;
5870
5871     case SCREEN_CTRL_ID_NEXT_LEVEL:
5872       *x = mSX + menu.main.button.next_level.x;
5873       *y = mSY + menu.main.button.next_level.y;
5874       break;
5875 #else
5876     case SCREEN_CTRL_ID_PREV_LEVEL:
5877       *x = mSX + TILEX * getPrevlevelButtonPos();
5878       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5879       break;
5880
5881     case SCREEN_CTRL_ID_NEXT_LEVEL:
5882       *x = mSX + TILEX * getNextLevelButtonPos();
5883       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5884       break;
5885 #endif
5886
5887     case SCREEN_CTRL_ID_PREV_PLAYER:
5888       *x = mSX + TILEX * 10;
5889       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5890       break;
5891
5892     case SCREEN_CTRL_ID_NEXT_PLAYER:
5893       *x = mSX + TILEX * 12;
5894       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5895       break;
5896
5897     default:
5898       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
5899   }
5900 }
5901
5902 static struct
5903 {
5904   int gfx_unpressed, gfx_pressed;
5905   void (*get_gadget_position)(int *, int *, int);
5906   int gadget_id;
5907   int screen_mask;
5908   char *infotext;
5909 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
5910 {
5911   {
5912     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
5913     getScreenMenuButtonPos,
5914     SCREEN_CTRL_ID_PREV_LEVEL,
5915     SCREEN_MASK_MAIN,
5916     "last level"
5917   },
5918   {
5919     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
5920     getScreenMenuButtonPos,
5921     SCREEN_CTRL_ID_NEXT_LEVEL,
5922     SCREEN_MASK_MAIN,
5923     "next level"
5924   },
5925   {
5926     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
5927     getScreenMenuButtonPos,
5928     SCREEN_CTRL_ID_PREV_PLAYER,
5929     SCREEN_MASK_INPUT,
5930     "last player"
5931   },
5932   {
5933     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
5934     getScreenMenuButtonPos,
5935     SCREEN_CTRL_ID_NEXT_PLAYER,
5936     SCREEN_MASK_INPUT,
5937     "next player"
5938   },
5939 };
5940
5941 static struct
5942 {
5943   int gfx_unpressed, gfx_pressed;
5944   int x, y;
5945   int gadget_id;
5946   char *infotext;
5947 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
5948 {
5949   {
5950     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
5951     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
5952     SCREEN_CTRL_ID_SCROLL_UP,
5953     "scroll up"
5954   },
5955   {
5956     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
5957     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
5958     SCREEN_CTRL_ID_SCROLL_DOWN,
5959     "scroll down"
5960   }
5961 };
5962
5963 static struct
5964 {
5965 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5966   Bitmap **gfx_unpressed, **gfx_pressed;
5967 #else
5968   int gfx_unpressed, gfx_pressed;
5969 #endif
5970   int x, y;
5971   int width, height;
5972   int type;
5973   int gadget_id;
5974   char *infotext;
5975 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
5976 {
5977   {
5978 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5979     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
5980 #else
5981     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
5982 #endif
5983     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
5984     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
5985     GD_TYPE_SCROLLBAR_VERTICAL,
5986     SCREEN_CTRL_ID_SCROLL_VERTICAL,
5987     "scroll level series vertically"
5988   }
5989 };
5990
5991 static void CreateScreenMenubuttons()
5992 {
5993   struct GadgetInfo *gi;
5994   unsigned long event_mask;
5995   int i;
5996
5997   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
5998   {
5999     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6000     int gfx_unpressed, gfx_pressed;
6001     int x, y, width, height;
6002     int gd_x1, gd_x2, gd_y1, gd_y2;
6003     int id = menubutton_info[i].gadget_id;
6004
6005     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6006
6007     menubutton_info[i].get_gadget_position(&x, &y, id);
6008
6009     width = SC_MENUBUTTON_XSIZE;
6010     height = SC_MENUBUTTON_YSIZE;
6011
6012     gfx_unpressed = menubutton_info[i].gfx_unpressed;
6013     gfx_pressed   = menubutton_info[i].gfx_pressed;
6014     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6015     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6016     gd_x1 = graphic_info[gfx_unpressed].src_x;
6017     gd_y1 = graphic_info[gfx_unpressed].src_y;
6018     gd_x2 = graphic_info[gfx_pressed].src_x;
6019     gd_y2 = graphic_info[gfx_pressed].src_y;
6020
6021     gi = CreateGadget(GDI_CUSTOM_ID, id,
6022                       GDI_CUSTOM_TYPE_ID, i,
6023                       GDI_INFO_TEXT, menubutton_info[i].infotext,
6024                       GDI_X, x,
6025                       GDI_Y, y,
6026                       GDI_WIDTH, width,
6027                       GDI_HEIGHT, height,
6028                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6029                       GDI_STATE, GD_BUTTON_UNPRESSED,
6030                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6031                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6032                       GDI_DIRECT_DRAW, FALSE,
6033                       GDI_EVENT_MASK, event_mask,
6034                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6035                       GDI_END);
6036
6037     if (gi == NULL)
6038       Error(ERR_EXIT, "cannot create gadget");
6039
6040     screen_gadget[id] = gi;
6041   }
6042 }
6043
6044 static void CreateScreenScrollbuttons()
6045 {
6046   struct GadgetInfo *gi;
6047   unsigned long event_mask;
6048   int i;
6049
6050   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6051   {
6052     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6053     int gfx_unpressed, gfx_pressed;
6054     int x, y, width, height;
6055     int gd_x1, gd_x2, gd_y1, gd_y2;
6056     int id = scrollbutton_info[i].gadget_id;
6057
6058     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6059
6060     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
6061     y = mSY + scrollbutton_info[i].y;
6062     width = SC_SCROLLBUTTON_XSIZE;
6063     height = SC_SCROLLBUTTON_YSIZE;
6064
6065     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
6066       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
6067                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
6068
6069     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
6070     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
6071     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6072     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6073     gd_x1 = graphic_info[gfx_unpressed].src_x;
6074     gd_y1 = graphic_info[gfx_unpressed].src_y;
6075     gd_x2 = graphic_info[gfx_pressed].src_x;
6076     gd_y2 = graphic_info[gfx_pressed].src_y;
6077
6078     gi = CreateGadget(GDI_CUSTOM_ID, id,
6079                       GDI_CUSTOM_TYPE_ID, i,
6080                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6081                       GDI_X, x,
6082                       GDI_Y, y,
6083                       GDI_WIDTH, width,
6084                       GDI_HEIGHT, height,
6085                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6086                       GDI_STATE, GD_BUTTON_UNPRESSED,
6087                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6088                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6089                       GDI_DIRECT_DRAW, FALSE,
6090                       GDI_EVENT_MASK, event_mask,
6091                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6092                       GDI_END);
6093
6094     if (gi == NULL)
6095       Error(ERR_EXIT, "cannot create gadget");
6096
6097     screen_gadget[id] = gi;
6098   }
6099 }
6100
6101 static void CreateScreenScrollbars()
6102 {
6103   int i;
6104
6105   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6106   {
6107     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6108 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6109     int gfx_unpressed, gfx_pressed;
6110 #endif
6111     int x, y, width, height;
6112     int gd_x1, gd_x2, gd_y1, gd_y2;
6113     struct GadgetInfo *gi;
6114     int items_max, items_visible, item_position;
6115     unsigned long event_mask;
6116     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6117     int id = scrollbar_info[i].gadget_id;
6118
6119     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6120
6121     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6122     y = mSY + scrollbar_info[i].y;
6123     width  = scrollbar_info[i].width;
6124     height = scrollbar_info[i].height;
6125
6126     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6127       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6128
6129     items_max = num_page_entries;
6130     items_visible = num_page_entries;
6131     item_position = 0;
6132
6133 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6134     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
6135     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
6136     gd_x1 = 0;
6137     gd_y1 = 0;
6138     gd_x2 = 0;
6139     gd_y2 = 0;
6140 #else
6141     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6142     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6143     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6144     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6145     gd_x1 = graphic_info[gfx_unpressed].src_x;
6146     gd_y1 = graphic_info[gfx_unpressed].src_y;
6147     gd_x2 = graphic_info[gfx_pressed].src_x;
6148     gd_y2 = graphic_info[gfx_pressed].src_y;
6149 #endif
6150
6151     gi = CreateGadget(GDI_CUSTOM_ID, id,
6152                       GDI_CUSTOM_TYPE_ID, i,
6153                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6154                       GDI_X, x,
6155                       GDI_Y, y,
6156                       GDI_WIDTH, width,
6157                       GDI_HEIGHT, height,
6158                       GDI_TYPE, scrollbar_info[i].type,
6159                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6160                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6161                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6162 #if 1
6163                       GDI_WHEEL_AREA_X, SX,
6164                       GDI_WHEEL_AREA_Y, SY,
6165                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6166                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6167 #else
6168                       GDI_WHEEL_AREA_X, 0,
6169                       GDI_WHEEL_AREA_Y, 0,
6170                       GDI_WHEEL_AREA_WIDTH, WIN_XSIZE,
6171                       GDI_WHEEL_AREA_HEIGHT, WIN_YSIZE,
6172 #endif
6173                       GDI_STATE, GD_BUTTON_UNPRESSED,
6174                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6175                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6176                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6177                       GDI_DIRECT_DRAW, FALSE,
6178                       GDI_EVENT_MASK, event_mask,
6179                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6180                       GDI_END);
6181
6182     if (gi == NULL)
6183       Error(ERR_EXIT, "cannot create gadget");
6184
6185     screen_gadget[id] = gi;
6186   }
6187 }
6188
6189 void CreateScreenGadgets()
6190 {
6191   int last_game_status = game_status;   /* save current game status */
6192
6193 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6194   int i;
6195
6196   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6197   {
6198     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
6199
6200     /* copy pointers to clip mask and GC */
6201     scrollbar_bitmap[i]->clip_mask =
6202       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
6203     scrollbar_bitmap[i]->stored_clip_gc =
6204       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
6205
6206     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
6207                scrollbar_bitmap[i],
6208                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
6209                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
6210                TILEX, TILEY, 0, 0);
6211   }
6212 #endif
6213
6214   CreateScreenMenubuttons();
6215
6216   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
6217   game_status = GAME_MODE_LEVELS;
6218
6219   CreateScreenScrollbuttons();
6220   CreateScreenScrollbars();
6221
6222   game_status = last_game_status;       /* restore current game status */
6223 }
6224
6225 void FreeScreenGadgets()
6226 {
6227   int i;
6228
6229 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6230   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6231   {
6232     /* prevent freeing clip mask and GC twice */
6233     scrollbar_bitmap[i]->clip_mask = None;
6234     scrollbar_bitmap[i]->stored_clip_gc = None;
6235
6236     FreeBitmap(scrollbar_bitmap[i]);
6237   }
6238 #endif
6239
6240   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6241     FreeGadget(screen_gadget[i]);
6242 }
6243
6244 void MapScreenMenuGadgets(int screen_mask)
6245 {
6246   int i;
6247
6248   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6249     if (screen_mask & menubutton_info[i].screen_mask)
6250       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6251 }
6252
6253 void MapScreenTreeGadgets(TreeInfo *ti)
6254 {
6255   int num_entries = numTreeInfoInGroup(ti);
6256   int i;
6257
6258   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6259     return;
6260
6261   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6262     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6263
6264   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6265     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6266 }
6267
6268 static void HandleScreenGadgets(struct GadgetInfo *gi)
6269 {
6270   int id = gi->custom_id;
6271   int button = gi->event.button;
6272   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6273
6274   switch (id)
6275   {
6276     case SCREEN_CTRL_ID_PREV_LEVEL:
6277       HandleMainMenu_SelectLevel(step, -1);
6278       break;
6279
6280     case SCREEN_CTRL_ID_NEXT_LEVEL:
6281       HandleMainMenu_SelectLevel(step, +1);
6282       break;
6283
6284     case SCREEN_CTRL_ID_PREV_PLAYER:
6285       HandleSetupScreen_Input_Player(step, -1);
6286       break;
6287
6288     case SCREEN_CTRL_ID_NEXT_PLAYER:
6289       HandleSetupScreen_Input_Player(step, +1);
6290       break;
6291
6292     case SCREEN_CTRL_ID_SCROLL_UP:
6293       if (game_status == GAME_MODE_LEVELS)
6294         HandleChooseLevel(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6295       else if (game_status == GAME_MODE_SETUP)
6296         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6297       break;
6298
6299     case SCREEN_CTRL_ID_SCROLL_DOWN:
6300       if (game_status == GAME_MODE_LEVELS)
6301         HandleChooseLevel(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6302       else if (game_status == GAME_MODE_SETUP)
6303         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6304       break;
6305
6306     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
6307       if (game_status == GAME_MODE_LEVELS)
6308         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6309       else if (game_status == GAME_MODE_SETUP)
6310         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6311       break;
6312
6313     default:
6314       break;
6315   }
6316 }