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