small code cleanup
[rocksndiamonds.git] / src / screens.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // screens.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "screens.h"
15 #include "events.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "editor.h"
19 #include "files.h"
20 #include "tape.h"
21 #include "anim.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26
27 #define DEBUG_JOYSTICKS         0
28
29
30 /* screens on the info screen */
31 #define INFO_MODE_MAIN                  0
32 #define INFO_MODE_TITLE                 1
33 #define INFO_MODE_ELEMENTS              2
34 #define INFO_MODE_MUSIC                 3
35 #define INFO_MODE_CREDITS               4
36 #define INFO_MODE_PROGRAM               5
37 #define INFO_MODE_VERSION               6
38 #define INFO_MODE_LEVELSET              7
39
40 #define MAX_INFO_MODES                  8
41
42 /* screens on the setup screen */
43 /* (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h) */
44 /* (should also match corresponding entries in src/conf_gfx.c) */
45 #define SETUP_MODE_MAIN                 0
46 #define SETUP_MODE_GAME                 1
47 #define SETUP_MODE_EDITOR               2
48 #define SETUP_MODE_GRAPHICS             3
49 #define SETUP_MODE_SOUND                4
50 #define SETUP_MODE_ARTWORK              5
51 #define SETUP_MODE_INPUT                6
52 #define SETUP_MODE_TOUCH                7
53 #define SETUP_MODE_SHORTCUTS            8
54 #define SETUP_MODE_SHORTCUTS_1          9
55 #define SETUP_MODE_SHORTCUTS_2          10
56 #define SETUP_MODE_SHORTCUTS_3          11
57 #define SETUP_MODE_SHORTCUTS_4          12
58 #define SETUP_MODE_SHORTCUTS_5          13
59
60 /* sub-screens on the setup screen (generic) */
61 #define SETUP_MODE_CHOOSE_ARTWORK       14
62 #define SETUP_MODE_CHOOSE_OTHER         15
63
64 /* sub-screens on the setup screen (specific) */
65 #define SETUP_MODE_CHOOSE_GAME_SPEED    16
66 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  17
67 #define SETUP_MODE_CHOOSE_SNAPSHOT_MODE 18
68 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   19
69 #define SETUP_MODE_CHOOSE_SCALING_TYPE  20
70 #define SETUP_MODE_CHOOSE_RENDERING     21
71 #define SETUP_MODE_CHOOSE_VSYNC         22
72 #define SETUP_MODE_CHOOSE_GRAPHICS      23
73 #define SETUP_MODE_CHOOSE_SOUNDS        24
74 #define SETUP_MODE_CHOOSE_MUSIC         25
75 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 26
76 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  27
77 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  28
78 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 29
79 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 30
80 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 31
81 #define SETUP_MODE_CHOOSE_TRANSPARENCY  32
82 #define SETUP_MODE_CHOOSE_GRID_XSIZE_0  33
83 #define SETUP_MODE_CHOOSE_GRID_YSIZE_0  34
84 #define SETUP_MODE_CHOOSE_GRID_XSIZE_1  35
85 #define SETUP_MODE_CHOOSE_GRID_YSIZE_1  36
86 #define SETUP_MODE_CONFIG_VIRT_BUTTONS  37
87
88 #define MAX_SETUP_MODES                 38
89
90 #define MAX_MENU_MODES                  MAX(MAX_INFO_MODES, MAX_SETUP_MODES)
91
92 /* setup screen titles */
93 #define STR_SETUP_MAIN                  "Setup"
94 #define STR_SETUP_GAME                  "Game & Menu"
95 #define STR_SETUP_EDITOR                "Editor"
96 #define STR_SETUP_GRAPHICS              "Graphics"
97 #define STR_SETUP_SOUND                 "Sound & Music"
98 #define STR_SETUP_ARTWORK               "Custom Artwork"
99 #define STR_SETUP_INPUT                 "Input Devices"
100 #define STR_SETUP_TOUCH                 "Touch Controls"
101 #define STR_SETUP_SHORTCUTS             "Key Shortcuts"
102 #define STR_SETUP_EXIT                  "Exit"
103 #define STR_SETUP_SAVE_AND_EXIT         "Save and Exit"
104
105 #define STR_SETUP_CHOOSE_GAME_SPEED     "Game Speed"
106 #define STR_SETUP_CHOOSE_SCROLL_DELAY   "Scroll Delay"
107 #define STR_SETUP_CHOOSE_SNAPSHOT_MODE  "Snapshot Mode"
108 #define STR_SETUP_CHOOSE_WINDOW_SIZE    "Window Scaling"
109 #define STR_SETUP_CHOOSE_SCALING_TYPE   "Anti-Aliasing"
110 #define STR_SETUP_CHOOSE_RENDERING      "Rendering Mode"
111 #define STR_SETUP_CHOOSE_VSYNC          "VSync Mode"
112 #define STR_SETUP_CHOOSE_VOLUME_SIMPLE  "Sound Volume"
113 #define STR_SETUP_CHOOSE_VOLUME_LOOPS   "Loops Volume"
114 #define STR_SETUP_CHOOSE_VOLUME_MUSIC   "Music Volume"
115 #define STR_SETUP_CHOOSE_TOUCH_CONTROL  "Control Type"
116 #define STR_SETUP_CHOOSE_MOVE_DISTANCE  "Move Distance"
117 #define STR_SETUP_CHOOSE_DROP_DISTANCE  "Drop Distance"
118 #define STR_SETUP_CHOOSE_TRANSPARENCY   "Transparency"
119 #define STR_SETUP_CHOOSE_GRID_XSIZE_0   "Horiz. Buttons"
120 #define STR_SETUP_CHOOSE_GRID_YSIZE_0   "Vert. Buttons"
121 #define STR_SETUP_CHOOSE_GRID_XSIZE_1   "Horiz. Buttons"
122 #define STR_SETUP_CHOOSE_GRID_YSIZE_1   "Vert. Buttons"
123
124 /* for input setup functions */
125 #define SETUPINPUT_SCREEN_POS_START     0
126 #define SETUPINPUT_SCREEN_POS_EMPTY1    3
127 #define SETUPINPUT_SCREEN_POS_EMPTY2    12
128 #define SETUPINPUT_SCREEN_POS_END       13
129
130 #define MENU_SETUP_FONT_TITLE           FONT_TEXT_1
131 #define MENU_SETUP_FONT_TEXT            FONT_TITLE_2
132
133 #define MAX_SETUP_TEXT_INPUT_LEN        28
134
135 /* for various menu stuff  */
136 #define MENU_SCREEN_START_XPOS          1
137 #define MENU_SCREEN_START_YPOS          2
138 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
139 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
140 #define MENU_TITLE1_YPOS                8
141 #define MENU_TITLE2_YPOS                46
142 #define MENU_INFO_FONT_TITLE            FONT_TEXT_1
143 #define MENU_INFO_FONT_HEAD             FONT_TEXT_2
144 #define MENU_INFO_FONT_TEXT             FONT_TEXT_3
145 #define MENU_INFO_FONT_FOOT             FONT_TEXT_4
146 #define MENU_INFO_SPACE_HEAD            (menu.headline2_spacing_info[info_mode])
147 #define MENU_SCREEN_INFO_SPACE_LEFT     (menu.left_spacing_info[info_mode])
148 #define MENU_SCREEN_INFO_SPACE_RIGHT    (menu.right_spacing_info[info_mode])
149 #define MENU_SCREEN_INFO_SPACE_TOP      (menu.top_spacing_info[info_mode])
150 #define MENU_SCREEN_INFO_SPACE_BOTTOM   (menu.bottom_spacing_info[info_mode])
151 #define MENU_SCREEN_INFO_YSTART1        MENU_SCREEN_INFO_SPACE_TOP
152 #define MENU_SCREEN_INFO_YSTART2        (MENU_SCREEN_INFO_YSTART1 +            \
153                                          getMenuTextStep(MENU_INFO_SPACE_HEAD, \
154                                                          MENU_INFO_FONT_TITLE))
155 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
156 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - MENU_SCREEN_INFO_SPACE_BOTTOM)
157 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
158                                          MENU_SCREEN_INFO_YSTART2 -     \
159                                          TILEY / 2)
160 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
161 #define STD_INFO_ELEMENTS_ON_SCREEN     (MENU_SCREEN_INFO_YSIZE /       \
162                                          MENU_SCREEN_INFO_YSTEP)
163 #define NUM_INFO_ELEMENTS_FROM_CONF     \
164   (menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] > 0 ?             \
165    menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] :                 \
166    MAX_MENU_ENTRIES_ON_SCREEN)
167 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MIN(STD_INFO_ELEMENTS_ON_SCREEN, \
168                                                 MAX_INFO_ELEMENTS_ON_SCREEN), \
169                                             NUM_INFO_ELEMENTS_FROM_CONF)
170 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
171 #define MAX_MENU_TEXT_LENGTH_BIG        13
172 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
173
174 /* screen gadget identifiers */
175 #define SCREEN_CTRL_ID_PREV_LEVEL       0
176 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
177 #define SCREEN_CTRL_ID_PREV_PLAYER      2
178 #define SCREEN_CTRL_ID_NEXT_PLAYER      3
179 #define SCREEN_CTRL_ID_INSERT_SOLUTION  4
180 #define SCREEN_CTRL_ID_PLAY_SOLUTION    5
181 #define SCREEN_CTRL_ID_SCROLL_UP        6
182 #define SCREEN_CTRL_ID_SCROLL_DOWN      7
183 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  8
184 #define SCREEN_CTRL_ID_NETWORK_SERVER   9
185
186 #define NUM_SCREEN_GADGETS              10
187
188 #define NUM_SCREEN_MENUBUTTONS          6
189 #define NUM_SCREEN_SCROLLBUTTONS        2
190 #define NUM_SCREEN_SCROLLBARS           1
191 #define NUM_SCREEN_TEXTINPUT            1
192
193 #define SCREEN_MASK_MAIN                (1 << 0)
194 #define SCREEN_MASK_MAIN_HAS_SOLUTION   (1 << 1)
195 #define SCREEN_MASK_INPUT               (1 << 2)
196
197 /* graphic position and size values for buttons and scrollbars */
198 #define SC_MENUBUTTON_XSIZE             TILEX
199 #define SC_MENUBUTTON_YSIZE             TILEY
200
201 #define SC_SCROLLBUTTON_XSIZE           TILEX
202 #define SC_SCROLLBUTTON_YSIZE           TILEY
203
204 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
205
206 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
207 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
208                                          SC_SCROLLBUTTON_YSIZE)
209
210 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
211 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
212
213 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
214 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
215                                          SC_SCROLLBUTTON_YSIZE)
216
217 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
218 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
219                                          SC_SCROLL_VERTICAL_YSIZE)
220
221 #define SC_BORDER_SIZE                  14
222
223
224 /* forward declarations of internal functions */
225 static void HandleScreenGadgets(struct GadgetInfo *);
226 static void HandleSetupScreen_Generic(int, int, int, int, int);
227 static void HandleSetupScreen_Input(int, int, int, int, int);
228 static void CustomizeKeyboard(int);
229 static void ConfigureJoystick(int);
230 static void ConfigureVirtualButtons(void);
231 static void execSetupGame(void);
232 static void execSetupGraphics(void);
233 static void execSetupSound(void);
234 static void execSetupTouch(void);
235 static void execSetupArtwork(void);
236 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
237
238 static void DrawChooseLevelSet(void);
239 static void DrawChooseLevelNr(void);
240 static void DrawInfoScreen(void);
241 static void DrawSetupScreen(void);
242
243 static void DrawInfoScreen_NotAvailable(char *, char *);
244 static void DrawInfoScreen_HelpAnim(int, int, boolean);
245 static void DrawInfoScreen_HelpText(int, int, int, int);
246 static void HandleInfoScreen_Main(int, int, int, int, int);
247 static void HandleInfoScreen_TitleScreen(int);
248 static void HandleInfoScreen_Elements(int);
249 static void HandleInfoScreen_Music(int);
250 static void HandleInfoScreen_Credits(int);
251 static void HandleInfoScreen_Program(int);
252 static void HandleInfoScreen_Version(int);
253
254 static void MapScreenMenuGadgets(int);
255 static void MapScreenGadgets(int);
256 static void MapScreenTreeGadgets(TreeInfo *);
257
258 static void UpdateScreenMenuGadgets(int, boolean);
259
260 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
261
262 static int info_mode = INFO_MODE_MAIN;
263 static int setup_mode = SETUP_MODE_MAIN;
264
265 static TreeInfo *window_sizes = NULL;
266 static TreeInfo *window_size_current = NULL;
267
268 static TreeInfo *scaling_types = NULL;
269 static TreeInfo *scaling_type_current = NULL;
270
271 static TreeInfo *rendering_modes = NULL;
272 static TreeInfo *rendering_mode_current = NULL;
273
274 static TreeInfo *vsync_modes = NULL;
275 static TreeInfo *vsync_mode_current = NULL;
276
277 static TreeInfo *scroll_delays = NULL;
278 static TreeInfo *scroll_delay_current = NULL;
279
280 static TreeInfo *snapshot_modes = NULL;
281 static TreeInfo *snapshot_mode_current = NULL;
282
283 static TreeInfo *game_speeds = NULL;
284 static TreeInfo *game_speed_current = NULL;
285
286 static TreeInfo *volumes_simple = NULL;
287 static TreeInfo *volume_simple_current = NULL;
288
289 static TreeInfo *volumes_loops = NULL;
290 static TreeInfo *volume_loops_current = NULL;
291
292 static TreeInfo *volumes_music = NULL;
293 static TreeInfo *volume_music_current = NULL;
294
295 static TreeInfo *touch_controls = NULL;
296 static TreeInfo *touch_control_current = NULL;
297
298 static TreeInfo *move_distances = NULL;
299 static TreeInfo *move_distance_current = NULL;
300
301 static TreeInfo *drop_distances = NULL;
302 static TreeInfo *drop_distance_current = NULL;
303
304 static TreeInfo *transparencies = NULL;
305 static TreeInfo *transparency_current = NULL;
306
307 static TreeInfo *grid_sizes[2][2] = { { NULL, NULL }, { NULL, NULL } };
308 static TreeInfo *grid_size_current[2][2] = { { NULL, NULL }, { NULL, NULL } };
309
310 static TreeInfo *level_number = NULL;
311 static TreeInfo *level_number_current = NULL;
312
313 static struct
314 {
315   int value;
316   char *text;
317 } window_sizes_list[] =
318 {
319   {     50,     "50 %"                          },
320   {     80,     "80 %"                          },
321   {     90,     "90 %"                          },
322   {     100,    "100 % (Default)"               },
323   {     110,    "110 %"                         },
324   {     120,    "120 %"                         },
325   {     130,    "130 %"                         },
326   {     140,    "140 %"                         },
327   {     150,    "150 %"                         },
328   {     200,    "200 %"                         },
329   {     250,    "250 %"                         },
330   {     300,    "300 %"                         },
331
332   {     -1,     NULL                            },
333 };
334
335 static struct
336 {
337   char *value;
338   char *text;
339 } scaling_types_list[] =
340 {
341   {     SCALING_QUALITY_NEAREST, "Off"          },
342   {     SCALING_QUALITY_LINEAR,  "Linear"       },
343   {     SCALING_QUALITY_BEST,    "Anisotropic"  },
344
345   {     NULL,                    NULL           },
346 };
347
348 static struct
349 {
350   char *value;
351   char *text;
352 } rendering_modes_list[] =
353 {
354   {     STR_SPECIAL_RENDERING_OFF,      "Off (May show artifacts, fast)" },
355   {     STR_SPECIAL_RENDERING_BITMAP,   "Bitmap/Texture mode (slower)"   },
356 #if DEBUG
357   // this mode may work under certain conditions, but does not work on Windows
358   {     STR_SPECIAL_RENDERING_TARGET,   "Target Texture mode (slower)"   },
359 #endif
360   {     STR_SPECIAL_RENDERING_DOUBLE,   "Double Texture mode (slower)"   },
361
362   {     NULL,                            NULL                            },
363 };
364
365 static struct
366 {
367   char *value;
368   char *text;
369 } vsync_modes_list[] =
370 {
371   {     STR_VSYNC_MODE_OFF,             "Off"           },
372   {     STR_VSYNC_MODE_NORMAL,          "Normal"        },
373   {     STR_VSYNC_MODE_ADAPTIVE,        "Adaptive"      },
374
375   {     NULL,                            NULL           },
376 };
377
378 static struct
379 {
380   int value;
381   char *text;
382 } game_speeds_list[] =
383 {
384 #if 1
385   {     30,     "Very Slow"                     },
386   {     25,     "Slow"                          },
387   {     20,     "Normal"                        },
388   {     15,     "Fast"                          },
389   {     10,     "Very Fast"                     },
390 #else
391   {     1000,   "1/1s (Extremely Slow)"         },
392   {     500,    "1/2s"                          },
393   {     200,    "1/5s"                          },
394   {     100,    "1/10s"                         },
395   {     50,     "1/20s"                         },
396   {     29,     "1/35s (Original Supaplex)"     },
397   {     25,     "1/40s"                         },
398   {     20,     "1/50s (Normal Speed)"          },
399   {     14,     "1/70s (Maximum Supaplex)"      },
400   {     10,     "1/100s"                        },
401   {     5,      "1/200s"                        },
402   {     2,      "1/500s"                        },
403   {     1,      "1/1000s (Extremely Fast)"      },
404 #endif
405
406   {     -1,     NULL                            },
407 };
408
409 static struct
410 {
411   int value;
412   char *text;
413 } scroll_delays_list[] =
414 {
415   {     0,      "0 Tiles (No Scroll Delay)"     },
416   {     1,      "1 Tile"                        },
417   {     2,      "2 Tiles"                       },
418   {     3,      "3 Tiles (Default)"             },
419   {     4,      "4 Tiles"                       },
420   {     5,      "5 Tiles"                       },
421   {     6,      "6 Tiles"                       },
422   {     7,      "7 Tiles"                       },
423   {     8,      "8 Tiles (Maximum Scroll Delay)"},
424
425   {     -1,     NULL                            },
426 };
427
428 static struct
429 {
430   char *value;
431   char *text;
432 } snapshot_modes_list[] =
433 {
434   {     STR_SNAPSHOT_MODE_OFF,                  "Off"           },
435   {     STR_SNAPSHOT_MODE_EVERY_STEP,           "Every Step"    },
436   {     STR_SNAPSHOT_MODE_EVERY_MOVE,           "Every Move"    },
437   {     STR_SNAPSHOT_MODE_EVERY_COLLECT,        "Every Collect" },
438
439   {     NULL,                                   NULL            },
440 };
441
442 static struct
443 {
444   int value;
445   char *text;
446 } volumes_list[] =
447 {
448   {     0,      "0 %"                           },
449   {     1,      "1 %"                           },
450   {     2,      "2 %"                           },
451   {     5,      "5 %"                           },
452   {     10,     "10 %"                          },
453   {     20,     "20 %"                          },
454   {     30,     "30 %"                          },
455   {     40,     "40 %"                          },
456   {     50,     "50 %"                          },
457   {     60,     "60 %"                          },
458   {     70,     "70 %"                          },
459   {     80,     "80 %"                          },
460   {     90,     "90 %"                          },
461   {     100,    "100 %"                         },
462
463   {     -1,     NULL                            },
464 };
465
466 static struct
467 {
468   char *value;
469   char *text;
470 } touch_controls_list[] =
471 {
472   {     TOUCH_CONTROL_OFF,              "Off"                   },
473   {     TOUCH_CONTROL_VIRTUAL_BUTTONS,  "Virtual Buttons"       },
474   {     TOUCH_CONTROL_WIPE_GESTURES,    "Wipe Gestures"         },
475   {     TOUCH_CONTROL_FOLLOW_FINGER,    "Follow Finger"         },
476
477   {     NULL,                           NULL                    },
478 };
479
480 static struct
481 {
482   int value;
483   char *text;
484 } distances_list[] =
485 {
486   {     1,      "1 %"                           },
487   {     2,      "2 %"                           },
488   {     3,      "3 %"                           },
489   {     4,      "4 %"                           },
490   {     5,      "5 %"                           },
491   {     10,     "10 %"                          },
492   {     15,     "15 %"                          },
493   {     20,     "20 %"                          },
494   {     25,     "25 %"                          },
495
496   {     -1,     NULL                            },
497 };
498
499 static struct
500 {
501   int value;
502   char *text;
503 } transparencies_list[] =
504 {
505   {     0,      "0 % (Opaque)"                  },
506   {     10,     "10 %"                          },
507   {     20,     "20 %"                          },
508   {     30,     "30 %"                          },
509   {     40,     "40 %"                          },
510   {     50,     "50 %"                          },
511   {     60,     "60 %"                          },
512   {     70,     "70 %"                          },
513   {     80,     "80 %"                          },
514   {     90,     "90 %"                          },
515   {     100,    "100 % (Invisible)"             },
516
517   {     -1,     NULL                            },
518 };
519
520 static struct
521 {
522   int value;
523   char *text;
524 } grid_sizes_list[] =
525 {
526   {     3,      "3"                             },
527   {     4,      "4"                             },
528   {     5,      "5"                             },
529   {     6,      "6"                             },
530   {     7,      "7"                             },
531   {     8,      "8"                             },
532   {     9,      "9"                             },
533   {     10,     "10"                            },
534   {     11,     "11"                            },
535   {     12,     "12"                            },
536   {     13,     "13"                            },
537   {     14,     "14"                            },
538   {     15,     "15"                            },
539   {     16,     "16"                            },
540   {     17,     "17"                            },
541   {     18,     "18"                            },
542   {     19,     "19"                            },
543   {     20,     "20"                            },
544   {     21,     "21"                            },
545   {     22,     "22"                            },
546   {     23,     "23"                            },
547   {     24,     "24"                            },
548   {     25,     "25"                            },
549   {     26,     "26"                            },
550   {     27,     "27"                            },
551   {     28,     "28"                            },
552   {     29,     "29"                            },
553   {     30,     "30"                            },
554   {     31,     "31"                            },
555   {     32,     "32"                            },
556
557   {     -1,     NULL                            },
558 };
559
560 #define DRAW_MODE(s)            ((s) >= GAME_MODE_MAIN &&               \
561                                  (s) <= GAME_MODE_SETUP ? (s) :         \
562                                  (s) == GAME_MODE_PSEUDO_TYPENAME ?     \
563                                  GAME_MODE_MAIN : GAME_MODE_DEFAULT)
564
565 /* (there are no draw offset definitions needed for INFO_MODE_TITLE) */
566 #define DRAW_MODE_INFO(i)       ((i) >= INFO_MODE_TITLE &&              \
567                                  (i) <= INFO_MODE_LEVELSET ? (i) :      \
568                                  INFO_MODE_MAIN)
569
570 #define DRAW_MODE_SETUP(i)      ((i) >= SETUP_MODE_MAIN &&              \
571                                  (i) <= SETUP_MODE_SHORTCUTS_5 ? (i) :  \
572                                  (i) >= SETUP_MODE_CHOOSE_GRAPHICS &&   \
573                                  (i) <= SETUP_MODE_CHOOSE_MUSIC ?       \
574                                  SETUP_MODE_CHOOSE_ARTWORK :            \
575                                  SETUP_MODE_CHOOSE_OTHER)
576
577 #define DRAW_XOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
578                                  menu.draw_xoffset[GAME_MODE_INFO] :    \
579                                  menu.draw_xoffset_info[DRAW_MODE_INFO(i)])
580 #define DRAW_YOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
581                                  menu.draw_yoffset[GAME_MODE_INFO] :    \
582                                  menu.draw_yoffset_info[DRAW_MODE_INFO(i)])
583 #define EXTRA_SPACING_INFO(i)   (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ? \
584                                  menu.extra_spacing[GAME_MODE_INFO] :   \
585                                  menu.extra_spacing_info[DRAW_MODE_INFO(i)])
586
587 #define DRAW_XOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
588                                  menu.draw_xoffset[GAME_MODE_SETUP] :   \
589                                  menu.draw_xoffset_setup[DRAW_MODE_SETUP(i)])
590 #define DRAW_YOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
591                                  menu.draw_yoffset[GAME_MODE_SETUP] :   \
592                                  menu.draw_yoffset_setup[DRAW_MODE_SETUP(i)])
593 #define EXTRA_SPACING_SETUP(i)  (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
594                                  menu.extra_spacing[GAME_MODE_SETUP] :  \
595                                  menu.extra_spacing_setup[DRAW_MODE_SETUP(i)])
596
597 #define DRAW_XOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
598                                  DRAW_XOFFSET_INFO(info_mode) :         \
599                                  (s) == GAME_MODE_SETUP ?               \
600                                  DRAW_XOFFSET_SETUP(setup_mode) :       \
601                                  menu.draw_xoffset[DRAW_MODE(s)])
602 #define DRAW_YOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
603                                  DRAW_YOFFSET_INFO(info_mode) :         \
604                                  (s) == GAME_MODE_SETUP ?               \
605                                  DRAW_YOFFSET_SETUP(setup_mode) :       \
606                                  menu.draw_yoffset[DRAW_MODE(s)])
607 #define EXTRA_SPACING(s)        ((s) == GAME_MODE_INFO ?                \
608                                  EXTRA_SPACING_INFO(info_mode) :        \
609                                  (s) == GAME_MODE_SETUP ?               \
610                                  EXTRA_SPACING_SETUP(setup_mode) :      \
611                                  menu.extra_spacing[DRAW_MODE(s)])
612
613 #define mSX                     (SX + DRAW_XOFFSET(game_status))
614 #define mSY                     (SY + DRAW_YOFFSET(game_status))
615
616 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
617                                     menu.list_size[game_status] :       \
618                                     MAX_MENU_ENTRIES_ON_SCREEN)
619
620 #define IN_VIS_MENU(x, y)       IN_FIELD(x, y, SCR_FIELDX,              \
621                                          NUM_MENU_ENTRIES_ON_SCREEN)
622
623
624 /* title display and control definitions */
625
626 #define MAX_NUM_TITLE_SCREENS   (2 * MAX_NUM_TITLE_IMAGES +             \
627                                  2 * MAX_NUM_TITLE_MESSAGES)
628
629 #define NO_DIRECT_LEVEL_SELECT  (-1)
630
631
632 static int num_title_screens = 0;
633
634 struct TitleControlInfo
635 {
636   boolean is_image;
637   boolean initial;
638   boolean first;
639   int local_nr;
640   int sort_priority;
641 };
642
643 struct TitleControlInfo title_controls[MAX_NUM_TITLE_SCREENS];
644
645 /* main menu display and control definitions */
646
647 #define MAIN_CONTROL_NAME                       0
648 #define MAIN_CONTROL_LEVELS                     1
649 #define MAIN_CONTROL_SCORES                     2
650 #define MAIN_CONTROL_EDITOR                     3
651 #define MAIN_CONTROL_INFO                       4
652 #define MAIN_CONTROL_GAME                       5
653 #define MAIN_CONTROL_SETUP                      6
654 #define MAIN_CONTROL_QUIT                       7
655 #define MAIN_CONTROL_PREV_LEVEL                 8
656 #define MAIN_CONTROL_NEXT_LEVEL                 9
657 #define MAIN_CONTROL_FIRST_LEVEL                10
658 #define MAIN_CONTROL_LAST_LEVEL                 11
659 #define MAIN_CONTROL_LEVEL_NUMBER               12
660 #define MAIN_CONTROL_LEVEL_INFO_1               13
661 #define MAIN_CONTROL_LEVEL_INFO_2               14
662 #define MAIN_CONTROL_LEVEL_NAME                 15
663 #define MAIN_CONTROL_LEVEL_AUTHOR               16
664 #define MAIN_CONTROL_LEVEL_YEAR                 17
665 #define MAIN_CONTROL_LEVEL_IMPORTED_FROM        18
666 #define MAIN_CONTROL_LEVEL_IMPORTED_BY          19
667 #define MAIN_CONTROL_LEVEL_TESTED_BY            20
668 #define MAIN_CONTROL_TITLE_1                    21
669 #define MAIN_CONTROL_TITLE_2                    22
670 #define MAIN_CONTROL_TITLE_3                    23
671
672 static char str_main_text_name[10];
673 static char str_main_text_first_level[10];
674 static char str_main_text_last_level[10];
675 static char str_main_text_level_number[10];
676
677 static char network_server_hostname[MAX_SETUP_TEXT_INPUT_LEN + 1];
678
679 static char *main_text_name                     = str_main_text_name;
680 static char *main_text_first_level              = str_main_text_first_level;
681 static char *main_text_last_level               = str_main_text_last_level;
682 static char *main_text_level_number             = str_main_text_level_number;
683 static char *main_text_levels                   = "Levelset";
684 static char *main_text_scores                   = "Hall Of Fame";
685 static char *main_text_editor                   = "Level Creator";
686 static char *main_text_info                     = "Info Screen";
687 static char *main_text_game                     = "Start Game";
688 static char *main_text_setup                    = "Setup";
689 static char *main_text_quit                     = "Quit";
690 static char *main_text_level_name               = level.name;
691 static char *main_text_level_author             = level.author;
692 static char *main_text_level_year               = NULL;
693 static char *main_text_level_imported_from      = NULL;
694 static char *main_text_level_imported_by        = NULL;
695 static char *main_text_level_tested_by          = NULL;
696 static char *main_text_title_1                  = NULL;
697 static char *main_text_title_2                  = NULL;
698 static char *main_text_title_3                  = NULL;
699
700 struct MainControlInfo
701 {
702   int nr;
703
704   struct MenuPosInfo *pos_button;
705   int button_graphic;
706
707   struct TextPosInfo *pos_text;
708   char **text;
709
710   struct TextPosInfo *pos_input;
711   char **input;
712 };
713
714 static struct MainControlInfo main_controls[] =
715 {
716   {
717     MAIN_CONTROL_NAME,
718     &menu.main.button.name,             IMG_MENU_BUTTON_NAME,
719     &menu.main.text.name,               &main_text_name,
720     &menu.main.input.name,              &setup.player_name,
721   },
722   {
723     MAIN_CONTROL_LEVELS,
724     &menu.main.button.levels,           IMG_MENU_BUTTON_LEVELS,
725     &menu.main.text.levels,             &main_text_levels,
726     NULL,                               NULL,
727   },
728   {
729     MAIN_CONTROL_SCORES,
730     &menu.main.button.scores,           IMG_MENU_BUTTON_SCORES,
731     &menu.main.text.scores,             &main_text_scores,
732     NULL,                               NULL,
733   },
734   {
735     MAIN_CONTROL_EDITOR,
736     &menu.main.button.editor,           IMG_MENU_BUTTON_EDITOR,
737     &menu.main.text.editor,             &main_text_editor,
738     NULL,                               NULL,
739   },
740   {
741     MAIN_CONTROL_INFO,
742     &menu.main.button.info,             IMG_MENU_BUTTON_INFO,
743     &menu.main.text.info,               &main_text_info,
744     NULL,                               NULL,
745   },
746   {
747     MAIN_CONTROL_GAME,
748     &menu.main.button.game,             IMG_MENU_BUTTON_GAME,
749     &menu.main.text.game,               &main_text_game,
750     NULL,                               NULL,
751   },
752   {
753     MAIN_CONTROL_SETUP,
754     &menu.main.button.setup,            IMG_MENU_BUTTON_SETUP,
755     &menu.main.text.setup,              &main_text_setup,
756     NULL,                               NULL,
757   },
758   {
759     MAIN_CONTROL_QUIT,
760     &menu.main.button.quit,             IMG_MENU_BUTTON_QUIT,
761     &menu.main.text.quit,               &main_text_quit,
762     NULL,                               NULL,
763   },
764 #if 0
765   /* (these two buttons are real gadgets) */
766   {
767     MAIN_CONTROL_PREV_LEVEL,
768     &menu.main.button.prev_level,       IMG_MENU_BUTTON_PREV_LEVEL,
769     NULL,                               NULL,
770     NULL,                               NULL,
771   },
772   {
773     MAIN_CONTROL_NEXT_LEVEL,
774     &menu.main.button.next_level,       IMG_MENU_BUTTON_NEXT_LEVEL,
775     NULL,                               NULL,
776     NULL,                               NULL,
777   },
778 #endif
779   {
780     MAIN_CONTROL_FIRST_LEVEL,
781     &menu.main.button.first_level,      IMG_MENU_BUTTON_FIRST_LEVEL,
782     &menu.main.text.first_level,        &main_text_first_level,
783     NULL,                               NULL,
784   },
785   {
786     MAIN_CONTROL_LAST_LEVEL,
787     &menu.main.button.last_level,       IMG_MENU_BUTTON_LAST_LEVEL,
788     &menu.main.text.last_level,         &main_text_last_level,
789     NULL,                               NULL,
790   },
791   {
792     MAIN_CONTROL_LEVEL_NUMBER,
793     &menu.main.button.level_number,     IMG_MENU_BUTTON_LEVEL_NUMBER,
794     &menu.main.text.level_number,       &main_text_level_number,
795     NULL,                               NULL,
796   },
797   {
798     MAIN_CONTROL_LEVEL_INFO_1,
799     NULL,                               -1,
800     &menu.main.text.level_info_1,       NULL,
801     NULL,                               NULL,
802   },
803   {
804     MAIN_CONTROL_LEVEL_INFO_2,
805     NULL,                               -1,
806     &menu.main.text.level_info_2,       NULL,
807     NULL,                               NULL,
808   },
809   {
810     MAIN_CONTROL_LEVEL_NAME,
811     NULL,                               -1,
812     &menu.main.text.level_name,         &main_text_level_name,
813     NULL,                               NULL,
814   },
815   {
816     MAIN_CONTROL_LEVEL_AUTHOR,
817     NULL,                               -1,
818     &menu.main.text.level_author,       &main_text_level_author,
819     NULL,                               NULL,
820   },
821   {
822     MAIN_CONTROL_LEVEL_YEAR,
823     NULL,                               -1,
824     &menu.main.text.level_year,         &main_text_level_year,
825     NULL,                               NULL,
826   },
827   {
828     MAIN_CONTROL_LEVEL_IMPORTED_FROM,
829     NULL,                               -1,
830     &menu.main.text.level_imported_from, &main_text_level_imported_from,
831     NULL,                               NULL,
832   },
833   {
834     MAIN_CONTROL_LEVEL_IMPORTED_BY,
835     NULL,                               -1,
836     &menu.main.text.level_imported_by,  &main_text_level_imported_by,
837     NULL,                               NULL,
838   },
839   {
840     MAIN_CONTROL_LEVEL_TESTED_BY,
841     NULL,                               -1,
842     &menu.main.text.level_tested_by,    &main_text_level_tested_by,
843     NULL,                               NULL,
844   },
845   {
846     MAIN_CONTROL_TITLE_1,
847     NULL,                               -1,
848     &menu.main.text.title_1,            &main_text_title_1,
849     NULL,                               NULL,
850   },
851   {
852     MAIN_CONTROL_TITLE_2,
853     NULL,                               -1,
854     &menu.main.text.title_2,            &main_text_title_2,
855     NULL,                               NULL,
856   },
857   {
858     MAIN_CONTROL_TITLE_3,
859     NULL,                               -1,
860     &menu.main.text.title_3,            &main_text_title_3,
861     NULL,                               NULL,
862   },
863
864   {
865     -1,
866     NULL,                               -1,
867     NULL,                               NULL,
868     NULL,                               NULL,
869   }
870 };
871
872
873 static int getTitleScreenGraphic(int nr, boolean initial)
874 {
875   return (initial ? IMG_TITLESCREEN_INITIAL_1 : IMG_TITLESCREEN_1) + nr;
876 }
877
878 static struct TitleMessageInfo *getTitleMessageInfo(int nr, boolean initial)
879 {
880   return (initial ? &titlemessage_initial[nr] : &titlemessage[nr]);
881 }
882
883 #if 0
884 static int getTitleScreenGameMode(boolean initial)
885 {
886   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
887 }
888 #endif
889
890 static int getTitleMessageGameMode(boolean initial)
891 {
892   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
893 }
894
895 static int getTitleAnimMode(struct TitleControlInfo *tci)
896 {
897   int base = (tci->initial ? GAME_MODE_TITLE_INITIAL_1 : GAME_MODE_TITLE_1);
898
899   return base + tci->local_nr;
900 }
901
902 #if 0
903 static int getTitleScreenBackground(boolean initial)
904 {
905   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
906 }
907 #endif
908
909 #if 0
910 static int getTitleMessageBackground(int nr, boolean initial)
911 {
912   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
913 }
914 #endif
915
916 static int getTitleBackground(int nr, boolean initial, boolean is_image)
917 {
918   int base = (is_image ?
919               (initial ? IMG_BACKGROUND_TITLESCREEN_INITIAL_1 :
920                          IMG_BACKGROUND_TITLESCREEN_1) :
921               (initial ? IMG_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
922                          IMG_BACKGROUND_TITLEMESSAGE_1));
923   int graphic_global = (initial ? IMG_BACKGROUND_TITLE_INITIAL :
924                                   IMG_BACKGROUND_TITLE);
925   int graphic_local = base + nr;
926
927   if (graphic_info[graphic_local].bitmap != NULL)
928     return graphic_local;
929
930   if (graphic_info[graphic_global].bitmap != NULL)
931     return graphic_global;
932
933   return IMG_UNDEFINED;
934 }
935
936 static int getTitleSound(struct TitleControlInfo *tci)
937 {
938   boolean is_image = tci->is_image;
939   int initial = tci->initial;
940   int nr = tci->local_nr;
941   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
942   int base = (is_image ?
943               (initial ? SND_BACKGROUND_TITLESCREEN_INITIAL_1 :
944                          SND_BACKGROUND_TITLESCREEN_1) :
945               (initial ? SND_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
946                          SND_BACKGROUND_TITLEMESSAGE_1));
947   int sound_global = menu.sound[mode];
948   int sound_local = base + nr;
949
950 #if 0
951   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
952          nr, initial, is_image,
953          sound_global, getSoundListEntry(sound_global)->filename,
954          sound_local, getSoundListEntry(sound_local)->filename);
955 #endif
956
957   if (!strEqual(getSoundListEntry(sound_local)->filename, UNDEFINED_FILENAME))
958     return sound_local;
959
960   if (!strEqual(getSoundListEntry(sound_global)->filename, UNDEFINED_FILENAME))
961     return sound_global;
962
963   return SND_UNDEFINED;
964 }
965
966 static int getTitleMusic(struct TitleControlInfo *tci)
967 {
968   boolean is_image = tci->is_image;
969   int initial = tci->initial;
970   int nr = tci->local_nr;
971   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
972   int base = (is_image ?
973               (initial ? MUS_BACKGROUND_TITLESCREEN_INITIAL_1 :
974                          MUS_BACKGROUND_TITLESCREEN_1) :
975               (initial ? MUS_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
976                          MUS_BACKGROUND_TITLEMESSAGE_1));
977   int music_global = menu.music[mode];
978   int music_local = base + nr;
979
980 #if 0
981   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
982          nr, initial, is_image,
983          music_global, getMusicListEntry(music_global)->filename,
984          music_local, getMusicListEntry(music_local)->filename);
985 #endif
986
987   if (!strEqual(getMusicListEntry(music_local)->filename, UNDEFINED_FILENAME))
988     return music_local;
989
990   if (!strEqual(getMusicListEntry(music_global)->filename, UNDEFINED_FILENAME))
991     return music_global;
992
993   return MUS_UNDEFINED;
994 }
995
996 static struct TitleFadingInfo getTitleFading(struct TitleControlInfo *tci)
997 {
998   boolean is_image = tci->is_image;
999   boolean initial = tci->initial;
1000   boolean first = tci->first;
1001   int nr = tci->local_nr;
1002   struct TitleMessageInfo tmi;
1003   struct TitleFadingInfo ti;
1004
1005   tmi = (is_image ? (initial ? (first ?
1006                                 titlescreen_initial_first[nr] :
1007                                 titlescreen_initial[nr])
1008                              : (first ?
1009                                 titlescreen_first[nr] :
1010                                 titlescreen[nr]))
1011                   : (initial ? (first ?
1012                                 titlemessage_initial_first[nr] :
1013                                 titlemessage_initial[nr])
1014                              : (first ?
1015                                 titlemessage_first[nr] :
1016                                 titlemessage[nr])));
1017
1018   ti.fade_mode  = tmi.fade_mode;
1019   ti.fade_delay = tmi.fade_delay;
1020   ti.post_delay = tmi.post_delay;
1021   ti.auto_delay = tmi.auto_delay;
1022
1023   return ti;
1024 }
1025
1026 static int compareTitleControlInfo(const void *object1, const void *object2)
1027 {
1028   const struct TitleControlInfo *tci1 = (struct TitleControlInfo *)object1;
1029   const struct TitleControlInfo *tci2 = (struct TitleControlInfo *)object2;
1030   int compare_result;
1031
1032   if (tci1->initial != tci2->initial)
1033     compare_result = (tci1->initial ? -1 : +1);
1034   else if (tci1->sort_priority != tci2->sort_priority)
1035     compare_result = tci1->sort_priority - tci2->sort_priority;
1036   else if (tci1->is_image != tci2->is_image)
1037     compare_result = (tci1->is_image ? -1 : +1);
1038   else
1039     compare_result = tci1->local_nr - tci2->local_nr;
1040
1041   return compare_result;
1042 }
1043
1044 static void InitializeTitleControlsExt_AddTitleInfo(boolean is_image,
1045                                                     boolean initial,
1046                                                     int nr, int sort_priority)
1047 {
1048   title_controls[num_title_screens].is_image = is_image;
1049   title_controls[num_title_screens].initial = initial;
1050   title_controls[num_title_screens].local_nr = nr;
1051   title_controls[num_title_screens].sort_priority = sort_priority;
1052
1053   title_controls[num_title_screens].first = FALSE;      /* will be set later */
1054
1055   num_title_screens++;
1056 }
1057
1058 static void InitializeTitleControls_CheckTitleInfo(boolean initial)
1059 {
1060   int i;
1061
1062   for (i = 0; i < MAX_NUM_TITLE_IMAGES; i++)
1063   {
1064     int graphic = getTitleScreenGraphic(i, initial);
1065     Bitmap *bitmap = graphic_info[graphic].bitmap;
1066     int sort_priority = graphic_info[graphic].sort_priority;
1067
1068     if (bitmap != NULL)
1069       InitializeTitleControlsExt_AddTitleInfo(TRUE, initial, i, sort_priority);
1070   }
1071
1072   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
1073   {
1074     struct TitleMessageInfo *tmi = getTitleMessageInfo(i, initial);
1075     char *filename = getLevelSetTitleMessageFilename(i, initial);
1076     int sort_priority = tmi->sort_priority;
1077
1078     if (filename != NULL)
1079       InitializeTitleControlsExt_AddTitleInfo(FALSE, initial, i, sort_priority);
1080   }
1081 }
1082
1083 static void InitializeTitleControls(boolean show_title_initial)
1084 {
1085   num_title_screens = 0;
1086
1087   /* 1st step: initialize title screens for game start (only when starting) */
1088   if (show_title_initial)
1089     InitializeTitleControls_CheckTitleInfo(TRUE);
1090
1091   /* 2nd step: initialize title screens for current level set */
1092   InitializeTitleControls_CheckTitleInfo(FALSE);
1093
1094   /* sort title screens according to sort_priority and title number */
1095   qsort(title_controls, num_title_screens, sizeof(struct TitleControlInfo),
1096         compareTitleControlInfo);
1097
1098   /* mark first title screen */
1099   title_controls[0].first = TRUE;
1100 }
1101
1102 static boolean visibleMenuPos(struct MenuPosInfo *pos)
1103 {
1104   return (pos != NULL && pos->x != -1 && pos->y != -1);
1105 }
1106
1107 static boolean visibleTextPos(struct TextPosInfo *pos)
1108 {
1109   return (pos != NULL && pos->x != -1 && pos->y != -1);
1110 }
1111
1112 static void InitializeMainControls(void)
1113 {
1114   boolean local_team_mode = (!network.enabled && setup.team_mode);
1115   int i;
1116
1117   /* set main control text values to dynamically determined values */
1118   sprintf(main_text_name,         "%s",   local_team_mode ? "Team:" : "Name:");
1119
1120   strcpy(main_text_first_level,  int2str(leveldir_current->first_level,
1121                                          menu.main.text.first_level.size));
1122   strcpy(main_text_last_level,   int2str(leveldir_current->last_level,
1123                                          menu.main.text.last_level.size));
1124   strcpy(main_text_level_number, int2str(level_nr,
1125                                          menu.main.text.level_number.size));
1126
1127   main_text_level_year          = leveldir_current->year;
1128   main_text_level_imported_from = leveldir_current->imported_from;
1129   main_text_level_imported_by   = leveldir_current->imported_by;
1130   main_text_level_tested_by     = leveldir_current->tested_by;
1131
1132   main_text_title_1 = getConfigProgramTitleString();
1133   main_text_title_2 = getConfigProgramCopyrightString();
1134   main_text_title_3 = getConfigProgramCompanyString();
1135
1136   /* set main control screen positions to dynamically determined values */
1137   for (i = 0; main_controls[i].nr != -1; i++)
1138   {
1139     struct MainControlInfo *mci = &main_controls[i];
1140     int nr                         = mci->nr;
1141     struct MenuPosInfo *pos_button = mci->pos_button;
1142     struct TextPosInfo *pos_text   = mci->pos_text;
1143     struct TextPosInfo *pos_input  = mci->pos_input;
1144     char *text                     = (mci->text  ? *mci->text  : NULL);
1145     char *input                    = (mci->input ? *mci->input : NULL);
1146     int button_graphic             = mci->button_graphic;
1147     int font_text                  = (pos_text  ? pos_text->font  : -1);
1148     int font_input                 = (pos_input ? pos_input->font : -1);
1149
1150     int font_text_width   = (font_text  != -1 ? getFontWidth(font_text)   : 0);
1151     int font_text_height  = (font_text  != -1 ? getFontHeight(font_text)  : 0);
1152     int font_input_width  = (font_input != -1 ? getFontWidth(font_input)  : 0);
1153     int font_input_height = (font_input != -1 ? getFontHeight(font_input) : 0);
1154     int text_chars  = (text  != NULL ? strlen(text)  : 0);
1155     int input_chars = (input != NULL ? strlen(input) : 0);
1156
1157     int button_width =
1158       (button_graphic != -1 ? graphic_info[button_graphic].width  : 0);
1159     int button_height =
1160       (button_graphic != -1 ? graphic_info[button_graphic].height : 0);
1161     int text_width   = font_text_width * text_chars;
1162     int text_height  = font_text_height;
1163     int input_width  = font_input_width * input_chars;
1164     int input_height = font_input_height;
1165
1166     if (nr == MAIN_CONTROL_NAME)
1167     {
1168       menu.main.input.name.width  = input_width;
1169       menu.main.input.name.height = input_height;
1170     }
1171
1172     if (pos_button != NULL)             /* (x/y may be -1/-1 here) */
1173     {
1174       pos_button->width  = button_width;
1175       pos_button->height = button_height;
1176     }
1177
1178     if (pos_text != NULL)               /* (x/y may be -1/-1 here) */
1179     {
1180       /* calculate text size -- needed for text alignment */
1181       boolean calculate_text_size = (text != NULL);
1182
1183       if (pos_text->width == -1 || calculate_text_size)
1184         pos_text->width = text_width;
1185       if (pos_text->height == -1 || calculate_text_size)
1186         pos_text->height = text_height;
1187
1188       if (visibleMenuPos(pos_button))
1189       {
1190         if (pos_text->x == -1)
1191           pos_text->x = pos_button->x + pos_button->width;
1192         if (pos_text->y == -1)
1193           pos_text->y =
1194             pos_button->y + (pos_button->height - pos_text->height) / 2;
1195       }
1196     }
1197
1198     if (pos_input != NULL)              /* (x/y may be -1/-1 here) */
1199     {
1200       if (visibleTextPos(pos_text))
1201       {
1202         if (pos_input->x == -1)
1203           pos_input->x = pos_text->x + pos_text->width;
1204         if (pos_input->y == -1)
1205           pos_input->y = pos_text->y;
1206       }
1207
1208       if (pos_input->width == -1)
1209         pos_input->width = input_width;
1210       if (pos_input->height == -1)
1211         pos_input->height = input_height;
1212     }
1213   }
1214 }
1215
1216 static void DrawPressedGraphicThruMask(int dst_x, int dst_y,
1217                                        int graphic, boolean pressed)
1218 {
1219   struct GraphicInfo *g = &graphic_info[graphic];
1220   Bitmap *src_bitmap;
1221   int src_x, src_y;
1222   int xoffset = (pressed ? g->pressed_xoffset : 0);
1223   int yoffset = (pressed ? g->pressed_yoffset : 0);
1224
1225   getFixedGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
1226
1227   BlitBitmapMasked(src_bitmap, drawto, src_x + xoffset, src_y + yoffset,
1228                    g->width, g->height, dst_x, dst_y);
1229 }
1230
1231 static void DrawCursorAndText_Main_Ext(int nr, boolean active_text,
1232                                        boolean active_input,
1233                                        boolean pressed_button)
1234 {
1235   int i;
1236
1237   for (i = 0; main_controls[i].nr != -1; i++)
1238   {
1239     struct MainControlInfo *mci = &main_controls[i];
1240
1241     if (mci->nr == nr || nr == -1)
1242     {
1243       struct MenuPosInfo *pos_button = mci->pos_button;
1244       struct TextPosInfo *pos_text   = mci->pos_text;
1245       struct TextPosInfo *pos_input  = mci->pos_input;
1246       char *text                     = (mci->text  ? *mci->text  : NULL);
1247       char *input                    = (mci->input ? *mci->input : NULL);
1248       int button_graphic             = mci->button_graphic;
1249       int font_text                  = (pos_text  ? pos_text->font  : -1);
1250       int font_input                 = (pos_input ? pos_input->font : -1);
1251
1252       if (active_text)
1253       {
1254         button_graphic = BUTTON_ACTIVE(button_graphic);
1255         font_text = FONT_ACTIVE(font_text);
1256       }
1257
1258       if (active_input)
1259       {
1260         font_input = FONT_ACTIVE(font_input);
1261       }
1262
1263       if (visibleMenuPos(pos_button))
1264       {
1265         struct MenuPosInfo *pos = pos_button;
1266         int x = mSX + pos->x;
1267         int y = mSY + pos->y;
1268
1269         DrawBackgroundForGraphic(x, y, pos->width, pos->height, button_graphic);
1270         DrawPressedGraphicThruMask(x, y, button_graphic, pressed_button);
1271       }
1272
1273       if (visibleTextPos(pos_text) && text != NULL)
1274       {
1275         struct TextPosInfo *pos = pos_text;
1276         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1277         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1278
1279 #if 1
1280         /* (check why/if this is needed) */
1281         DrawBackgroundForFont(x, y, pos->width, pos->height, font_text);
1282 #endif
1283         DrawText(x, y, text, font_text);
1284       }
1285
1286       if (visibleTextPos(pos_input) && input != NULL)
1287       {
1288         struct TextPosInfo *pos = pos_input;
1289         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1290         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1291
1292 #if 1
1293         /* (check why/if this is needed) */
1294         DrawBackgroundForFont(x, y, pos->width, pos->height, font_input);
1295 #endif
1296         DrawText(x, y, input, font_input);
1297       }
1298     }
1299   }
1300 }
1301
1302 static void DrawCursorAndText_Main(int nr, boolean active_text,
1303                                    boolean pressed_button)
1304 {
1305   DrawCursorAndText_Main_Ext(nr, active_text, FALSE, pressed_button);
1306 }
1307
1308 #if 0
1309 static void DrawCursorAndText_Main_Input(int nr, boolean active_text,
1310                                          boolean pressed_button)
1311 {
1312   DrawCursorAndText_Main_Ext(nr, active_text, TRUE, pressed_button);
1313 }
1314 #endif
1315
1316 static struct MainControlInfo *getMainControlInfo(int nr)
1317 {
1318   int i;
1319
1320   for (i = 0; main_controls[i].nr != -1; i++)
1321     if (main_controls[i].nr == nr)
1322       return &main_controls[i];
1323
1324   return NULL;
1325 }
1326
1327 static boolean insideMenuPosRect(struct MenuPosInfo *rect, int x, int y)
1328 {
1329   if (rect == NULL)
1330     return FALSE;
1331
1332   int rect_x = ALIGNED_TEXT_XPOS(rect);
1333   int rect_y = ALIGNED_TEXT_YPOS(rect);
1334
1335   return (x >= rect_x && x < rect_x + rect->width &&
1336           y >= rect_y && y < rect_y + rect->height);
1337 }
1338
1339 static boolean insideTextPosRect(struct TextPosInfo *rect, int x, int y)
1340 {
1341   if (rect == NULL)
1342     return FALSE;
1343
1344   int rect_x = ALIGNED_TEXT_XPOS(rect);
1345   int rect_y = ALIGNED_TEXT_YPOS(rect);
1346
1347 #if 0
1348   printf("::: insideTextPosRect: (%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d\n",
1349          x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1350          (x >= rect_x && x < rect_x + rect->width &&
1351           y >= rect_y && y < rect_y + rect->height));
1352 #endif
1353
1354   return (x >= rect_x && x < rect_x + rect->width &&
1355           y >= rect_y && y < rect_y + rect->height);
1356 }
1357
1358 static boolean insidePreviewRect(struct PreviewInfo *preview, int x, int y)
1359 {
1360   int rect_width  = preview->xsize * preview->tile_size;
1361   int rect_height = preview->ysize * preview->tile_size;
1362   int rect_x = ALIGNED_XPOS(preview->x, rect_width,  preview->align);
1363   int rect_y = ALIGNED_YPOS(preview->y, rect_height, preview->valign);
1364
1365   return (x >= rect_x && x < rect_x + rect_width &&
1366           y >= rect_y && y < rect_y + rect_height);
1367 }
1368
1369 static void AdjustScrollbar(int id, int items_max, int items_visible,
1370                             int item_position)
1371 {
1372   struct GadgetInfo *gi = screen_gadget[id];
1373
1374   if (item_position > items_max - items_visible)
1375     item_position = items_max - items_visible;
1376
1377   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1378                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1379                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1380 }
1381
1382 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1383 {
1384   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
1385                   first_entry);
1386 }
1387
1388 static void clearMenuListArea(void)
1389 {
1390   int scrollbar_xpos = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
1391
1392   /* correct scrollbar position if placed outside menu (playfield) area */
1393   if (scrollbar_xpos > SX + SC_SCROLLBAR_XPOS)
1394     scrollbar_xpos = SX + SC_SCROLLBAR_XPOS;
1395
1396   /* clear menu list area, but not title or scrollbar */
1397   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1398                  scrollbar_xpos - mSX, NUM_MENU_ENTRIES_ON_SCREEN * 32);
1399 }
1400
1401 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1402 {
1403   static int cursor_array[MAX_LEV_FIELDY];
1404   int x = mSX + TILEX * xpos;
1405   int y = mSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1406
1407   if (xpos == 0)
1408   {
1409     if (graphic != -1)
1410       cursor_array[ypos] = graphic;
1411     else
1412       graphic = cursor_array[ypos];
1413   }
1414
1415   if (active)
1416     graphic = BUTTON_ACTIVE(graphic);
1417
1418   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1419   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1420 }
1421
1422 static void initCursor(int ypos, int graphic)
1423 {
1424   drawCursorExt(0, ypos, FALSE, graphic);
1425 }
1426
1427 static void drawCursor(int ypos, boolean active)
1428 {
1429   drawCursorExt(0, ypos, active, -1);
1430 }
1431
1432 static void drawCursorXY(int xpos, int ypos, int graphic)
1433 {
1434   drawCursorExt(xpos, ypos, FALSE, graphic);
1435 }
1436
1437 static void drawChooseTreeCursor(int ypos, boolean active)
1438 {
1439   drawCursorExt(0, ypos, active, -1);
1440 }
1441
1442 static void DrawHeadline(void)
1443 {
1444   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, main_text_title_1);
1445   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, main_text_title_2);
1446 }
1447
1448 static void DrawTitleScreenImage(int nr, boolean initial)
1449 {
1450   int graphic = getTitleScreenGraphic(nr, initial);
1451   Bitmap *bitmap = graphic_info[graphic].bitmap;
1452   int width  = graphic_info[graphic].width;
1453   int height = graphic_info[graphic].height;
1454   int src_x = graphic_info[graphic].src_x;
1455   int src_y = graphic_info[graphic].src_y;
1456   int dst_x, dst_y;
1457
1458   if (bitmap == NULL)
1459     return;
1460
1461   if (width > WIN_XSIZE)
1462   {
1463     /* image width too large for window => center image horizontally */
1464     src_x = (width - WIN_XSIZE) / 2;
1465     width = WIN_XSIZE;
1466   }
1467
1468   if (height > WIN_YSIZE)
1469   {
1470     /* image height too large for window => center image vertically */
1471     src_y = (height - WIN_YSIZE) / 2;
1472     height = WIN_YSIZE;
1473   }
1474
1475   /* always display title screens centered */
1476   dst_x = (WIN_XSIZE - width) / 2;
1477   dst_y = (WIN_YSIZE - height) / 2;
1478
1479   SetDrawBackgroundMask(REDRAW_ALL);
1480   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1481
1482   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1483
1484   if (DrawingOnBackground(dst_x, dst_y))
1485     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1486   else
1487     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1488
1489   redraw_mask = REDRAW_ALL;
1490 }
1491
1492 static void DrawTitleScreenMessage(int nr, boolean initial)
1493 {
1494   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1495   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1496
1497   if (filename == NULL)
1498     return;
1499
1500   /* force TITLE font on title message screen */
1501   SetFontStatus(getTitleMessageGameMode(initial));
1502
1503   /* if chars *and* width set to "-1", automatically determine width */
1504   if (tmi->chars == -1 && tmi->width == -1)
1505     tmi->width = viewport.window[game_status].width;
1506
1507   /* if lines *and* height set to "-1", automatically determine height */
1508   if (tmi->lines == -1 && tmi->height == -1)
1509     tmi->height = viewport.window[game_status].height;
1510
1511   /* if chars set to "-1", automatically determine by text and font width */
1512   if (tmi->chars == -1)
1513     tmi->chars = tmi->width / getFontWidth(tmi->font);
1514   else
1515     tmi->width = tmi->chars * getFontWidth(tmi->font);
1516
1517   /* if lines set to "-1", automatically determine by text and font height */
1518   if (tmi->lines == -1)
1519     tmi->lines = tmi->height / getFontHeight(tmi->font);
1520   else
1521     tmi->height = tmi->lines * getFontHeight(tmi->font);
1522
1523   /* if x set to "-1", automatically determine by width and alignment */
1524   if (tmi->x == -1)
1525     tmi->x = -1 * ALIGNED_XPOS(0, tmi->width, tmi->align);
1526
1527   /* if y set to "-1", automatically determine by height and alignment */
1528   if (tmi->y == -1)
1529     tmi->y = -1 * ALIGNED_YPOS(0, tmi->height, tmi->valign);
1530
1531   SetDrawBackgroundMask(REDRAW_ALL);
1532   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1533
1534   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1535
1536   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1537                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1538                tmi->autowrap, tmi->centered, tmi->parse_comments);
1539
1540   ResetFontStatus();
1541 }
1542
1543 static void DrawTitleScreen(void)
1544 {
1545   KeyboardAutoRepeatOff();
1546
1547   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1548 }
1549
1550 static boolean CheckTitleScreen(boolean levelset_has_changed)
1551 {
1552   static boolean show_title_initial = TRUE;
1553   boolean show_titlescreen = FALSE;
1554
1555   /* needed to be able to skip title screen, if no image or message defined */
1556   InitializeTitleControls(show_title_initial);
1557
1558   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1559     show_titlescreen = TRUE;
1560
1561   /* show initial title images and messages only once at program start */
1562   show_title_initial = FALSE;
1563
1564   return (show_titlescreen && num_title_screens > 0);
1565 }
1566
1567 void DrawMainMenu(void)
1568 {
1569   static LevelDirTree *leveldir_last_valid = NULL;
1570   boolean levelset_has_changed = FALSE;
1571   int fade_mask = REDRAW_FIELD;
1572
1573   LimitScreenUpdates(FALSE);
1574
1575   FadeSetLeaveScreen();
1576
1577   /* do not fade out here -- function may continue and fade on editor screen */
1578
1579   UnmapAllGadgets();
1580   FadeMenuSoundsAndMusic();
1581
1582   ExpireSoundLoops(FALSE);
1583
1584   KeyboardAutoRepeatOn();
1585
1586   audio.sound_deactivated = FALSE;
1587
1588   GetPlayerConfig();
1589
1590   /* needed if last screen was the playing screen, invoked from level editor */
1591   if (level_editor_test_game)
1592   {
1593     CloseDoor(DOOR_CLOSE_ALL);
1594
1595     SetGameStatus(GAME_MODE_EDITOR);
1596
1597     DrawLevelEd();
1598
1599     return;
1600   }
1601
1602   /* needed if last screen was the setup screen and fullscreen state changed */
1603   // (moved to "execSetupGraphics()" to change fullscreen state directly)
1604   // ToggleFullscreenOrChangeWindowScalingIfNeeded();
1605
1606   /* leveldir_current may be invalid (level group, parent link) */
1607   if (!validLevelSeries(leveldir_current))
1608     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1609
1610   if (leveldir_current != leveldir_last_valid)
1611     levelset_has_changed = TRUE;
1612
1613   /* store valid level series information */
1614   leveldir_last_valid = leveldir_current;
1615
1616   init_last = init;                     /* switch to new busy animation */
1617
1618   /* needed if last screen (level choice) changed graphics, sounds or music */
1619   ReloadCustomArtwork(0);
1620
1621   if (CheckTitleScreen(levelset_has_changed))
1622   {
1623     SetGameStatus(GAME_MODE_TITLE);
1624
1625     DrawTitleScreen();
1626
1627     return;
1628   }
1629
1630   if (redraw_mask & REDRAW_ALL)
1631     fade_mask = REDRAW_ALL;
1632
1633   if (CheckIfGlobalBorderOrPlayfieldViewportHasChanged())
1634     fade_mask = REDRAW_ALL;
1635
1636   FadeOut(fade_mask);
1637
1638   /* needed if different viewport properties defined for menues */
1639   ChangeViewportPropertiesIfNeeded();
1640
1641   SetDrawtoField(DRAW_TO_BACKBUFFER);
1642
1643   /* level_nr may have been set to value over handicap with level editor */
1644   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1645     level_nr = leveldir_current->handicap_level;
1646
1647   LoadLevel(level_nr);
1648   LoadScore(level_nr);
1649
1650   SaveLevelSetup_SeriesInfo();
1651
1652   // set this after "ChangeViewportPropertiesIfNeeded()" (which may reset it)
1653   SetDrawDeactivationMask(REDRAW_NONE);
1654   SetDrawBackgroundMask(REDRAW_FIELD);
1655
1656   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1657
1658 #if 0
1659   if (fade_mask == REDRAW_ALL)
1660     RedrawGlobalBorder();
1661 #endif
1662
1663   ClearField();
1664
1665   InitializeMainControls();
1666
1667   DrawCursorAndText_Main(-1, FALSE, FALSE);
1668   DrawPreviewLevelInitial();
1669   DrawNetworkPlayers();
1670
1671   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1672
1673   TapeStop();
1674   if (TAPE_IS_EMPTY(tape))
1675     LoadTape(level_nr);
1676   DrawCompleteVideoDisplay();
1677
1678   PlayMenuSoundsAndMusic();
1679
1680   /* create gadgets for main menu screen */
1681   FreeScreenGadgets();
1682   CreateScreenGadgets();
1683
1684   /* may be required if audio buttons shown on tape and changed in setup menu */
1685   FreeGameButtons();
1686   CreateGameButtons();
1687
1688   /* map gadgets for main menu screen */
1689   MapTapeButtons();
1690   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1691   UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1692
1693   /* copy actual game door content to door double buffer for OpenDoor() */
1694   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
1695   BlitBitmap(drawto, bitmap_db_door_2, VX, VY, VXSIZE, VYSIZE, 0, 0);
1696
1697   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1698
1699   DrawMaskedBorder(fade_mask);
1700
1701   FadeIn(fade_mask);
1702   FadeSetEnterMenu();
1703
1704   /* update screen area with special editor door */
1705   redraw_mask |= REDRAW_ALL;
1706   BackToFront();
1707
1708   SetMouseCursor(CURSOR_DEFAULT);
1709
1710   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1711 }
1712
1713 static void gotoTopLevelDir(void)
1714 {
1715   /* move upwards until inside (but not above) top level directory */
1716   while (leveldir_current->node_parent &&
1717          !strEqual(leveldir_current->node_parent->subdir, STRING_TOP_DIRECTORY))
1718   {
1719     /* write a "path" into level tree for easy navigation to last level */
1720     if (leveldir_current->node_parent->node_group->cl_first == -1)
1721     {
1722       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1723       int leveldir_pos = posTreeInfo(leveldir_current);
1724       int num_page_entries;
1725       int cl_first, cl_cursor;
1726
1727       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
1728         num_page_entries = num_leveldirs;
1729       else
1730         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1731
1732       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1733       cl_cursor = leveldir_pos - cl_first;
1734
1735       leveldir_current->node_parent->node_group->cl_first = cl_first;
1736       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1737     }
1738
1739     leveldir_current = leveldir_current->node_parent;
1740   }
1741 }
1742
1743 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1744 {
1745   static unsigned int title_delay = 0;
1746   static int title_screen_nr = 0;
1747   static int last_sound = -1, last_music = -1;
1748   boolean return_to_main_menu = FALSE;
1749   struct TitleControlInfo *tci;
1750   int sound, music;
1751
1752   if (button == MB_MENU_INITIALIZE)
1753   {
1754     title_delay = 0;
1755     title_screen_nr = 0;
1756     tci = &title_controls[title_screen_nr];
1757
1758     SetAnimStatus(getTitleAnimMode(tci));
1759
1760     last_sound = SND_UNDEFINED;
1761     last_music = MUS_UNDEFINED;
1762
1763     if (num_title_screens != 0)
1764     {
1765       FadeSetEnterScreen();
1766
1767       /* use individual title fading instead of global "enter screen" fading */
1768       fading = getTitleFading(tci);
1769     }
1770
1771     if (game_status_last_screen == GAME_MODE_INFO)
1772     {
1773       if (num_title_screens == 0)
1774       {
1775         /* switch game mode from title screen mode back to info screen mode */
1776         SetGameStatus(GAME_MODE_INFO);
1777
1778         /* store that last screen was info screen, not main menu screen */
1779         game_status_last_screen = GAME_MODE_INFO;
1780
1781         DrawInfoScreen_NotAvailable("Title screen information:",
1782                                     "No title screen for this level set.");
1783         return;
1784       }
1785
1786       FadeMenuSoundsAndMusic();
1787     }
1788
1789     FadeOut(REDRAW_ALL);
1790
1791     /* title screens may have different window size */
1792     ChangeViewportPropertiesIfNeeded();
1793
1794     /* only required to update logic for redrawing global border */
1795     ClearField();
1796
1797     if (tci->is_image)
1798       DrawTitleScreenImage(tci->local_nr, tci->initial);
1799     else
1800       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1801
1802     sound = getTitleSound(tci);
1803     music = getTitleMusic(tci);
1804
1805     if (sound != last_sound)
1806       PlayMenuSoundExt(sound);
1807     if (music != last_music)
1808       PlayMenuMusicExt(music);
1809
1810     last_sound = sound;
1811     last_music = music;
1812
1813     SetMouseCursor(CURSOR_NONE);
1814
1815     FadeIn(REDRAW_ALL);
1816
1817     DelayReached(&title_delay, 0);      /* reset delay counter */
1818
1819     return;
1820   }
1821
1822   if (fading.auto_delay > 0 && DelayReached(&title_delay, fading.auto_delay))
1823     button = MB_MENU_CHOICE;
1824
1825   if (button == MB_MENU_LEAVE)
1826   {
1827     return_to_main_menu = TRUE;
1828   }
1829   else if (button == MB_MENU_CHOICE)
1830   {
1831     if (game_status_last_screen == GAME_MODE_INFO && num_title_screens == 0)
1832     {
1833       SetGameStatus(GAME_MODE_INFO);
1834
1835       info_mode = INFO_MODE_MAIN;
1836
1837       DrawInfoScreen();
1838
1839       return;
1840     }
1841
1842     title_screen_nr++;
1843
1844     if (title_screen_nr < num_title_screens)
1845     {
1846       tci = &title_controls[title_screen_nr];
1847
1848       SetAnimStatus(getTitleAnimMode(tci));
1849
1850       sound = getTitleSound(tci);
1851       music = getTitleMusic(tci);
1852
1853       if (last_sound != SND_UNDEFINED && sound != last_sound)
1854         FadeSound(last_sound);
1855       if (last_music != MUS_UNDEFINED && music != last_music)
1856         FadeMusic();
1857
1858       fading = getTitleFading(tci);
1859
1860       FadeOut(REDRAW_ALL);
1861
1862       if (tci->is_image)
1863         DrawTitleScreenImage(tci->local_nr, tci->initial);
1864       else
1865         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1866
1867       sound = getTitleSound(tci);
1868       music = getTitleMusic(tci);
1869
1870       if (sound != last_sound)
1871         PlayMenuSoundExt(sound);
1872       if (music != last_music)
1873         PlayMenuMusicExt(music);
1874
1875       last_sound = sound;
1876       last_music = music;
1877
1878       FadeIn(REDRAW_ALL);
1879
1880       DelayReached(&title_delay, 0);    /* reset delay counter */
1881     }
1882     else
1883     {
1884       FadeMenuSoundsAndMusic();
1885
1886       return_to_main_menu = TRUE;
1887     }
1888   }
1889
1890   if (return_to_main_menu)
1891   {
1892     SetMouseCursor(CURSOR_DEFAULT);
1893
1894     /* force full menu screen redraw after displaying title screens */
1895     redraw_mask = REDRAW_ALL;
1896
1897     if (game_status_last_screen == GAME_MODE_INFO)
1898     {
1899       SetGameStatus(GAME_MODE_INFO);
1900
1901       info_mode = INFO_MODE_MAIN;
1902
1903       DrawInfoScreen();
1904     }
1905     else        /* default: return to main menu */
1906     {
1907       SetGameStatus(GAME_MODE_MAIN);
1908
1909       DrawMainMenu();
1910     }
1911   }
1912 }
1913
1914 static void HandleMainMenu_SelectLevel(int step, int direction,
1915                                        int selected_level_nr)
1916 {
1917   int old_level_nr = level_nr;
1918   int new_level_nr;
1919
1920   if (selected_level_nr != NO_DIRECT_LEVEL_SELECT)
1921     new_level_nr = selected_level_nr;
1922   else
1923     new_level_nr = old_level_nr + step * direction;
1924
1925   if (new_level_nr < leveldir_current->first_level)
1926     new_level_nr = leveldir_current->first_level;
1927   if (new_level_nr > leveldir_current->last_level)
1928     new_level_nr = leveldir_current->last_level;
1929
1930   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1931   {
1932     /* skipping levels is only allowed when trying to skip single level */
1933     if (setup.skip_levels && new_level_nr == old_level_nr + 1 &&
1934         Request("Level still unsolved! Skip despite handicap?", REQ_ASK))
1935     {
1936       leveldir_current->handicap_level++;
1937       SaveLevelSetup_SeriesInfo();
1938     }
1939
1940     new_level_nr = leveldir_current->handicap_level;
1941   }
1942
1943   if (new_level_nr != old_level_nr)
1944   {
1945     struct MainControlInfo *mci= getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1946
1947     PlaySound(SND_MENU_ITEM_SELECTING);
1948
1949     level_nr = new_level_nr;
1950
1951     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1952              int2str(level_nr, menu.main.text.level_number.size),
1953              mci->pos_text->font);
1954
1955     LoadLevel(level_nr);
1956     DrawPreviewLevelInitial();
1957
1958     TapeErase();
1959     LoadTape(level_nr);
1960     DrawCompleteVideoDisplay();
1961
1962     SaveLevelSetup_SeriesInfo();
1963
1964     UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1965
1966     /* needed because DrawPreviewLevelInitial() takes some time */
1967     BackToFront();
1968     /* SyncDisplay(); */
1969   }
1970 }
1971
1972 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1973 {
1974   static int choice = MAIN_CONTROL_GAME;
1975   static boolean button_pressed_last = FALSE;
1976   boolean button_pressed = FALSE;
1977   int pos = choice;
1978   int i;
1979
1980   if (button == MB_MENU_INITIALIZE)
1981   {
1982     DrawCursorAndText_Main(choice, TRUE, FALSE);
1983
1984     return;
1985   }
1986
1987   if (mx || my)         /* mouse input */
1988   {
1989     pos = -1;
1990
1991     for (i = 0; main_controls[i].nr != -1; i++)
1992     {
1993       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1994           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1995           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1996       {
1997         pos = main_controls[i].nr;
1998
1999         break;
2000       }
2001     }
2002
2003     /* check if level preview was clicked */
2004     if (insidePreviewRect(&preview, mx - SX, my - SY))
2005       pos = MAIN_CONTROL_GAME;
2006
2007     // handle pressed/unpressed state for active/inactive menu buttons
2008     // (if pos != -1, "i" contains index position corresponding to "pos")
2009     if (button &&
2010         pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT &&
2011         insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY))
2012       button_pressed = TRUE;
2013
2014     if (button_pressed != button_pressed_last)
2015     {
2016       DrawCursorAndText_Main(choice, TRUE, button_pressed);
2017
2018       if (button_pressed)
2019         PlaySound(SND_MENU_BUTTON_PRESSING);
2020       else
2021         PlaySound(SND_MENU_BUTTON_RELEASING);
2022     }
2023   }
2024   else if (dx || dy)    /* keyboard input */
2025   {
2026     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
2027                    choice == MAIN_CONTROL_SETUP))
2028       button = MB_MENU_CHOICE;
2029     else if (dy)
2030       pos = choice + dy;
2031   }
2032
2033   if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
2034   {
2035     HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
2036   }
2037   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
2038   {
2039     HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
2040   }
2041   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
2042   {
2043     CloseDoor(DOOR_CLOSE_2);
2044
2045     SetGameStatus(GAME_MODE_LEVELNR);
2046
2047     DrawChooseLevelNr();
2048   }
2049   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
2050   {
2051     if (button)
2052     {
2053       if (pos != choice)
2054       {
2055         PlaySound(SND_MENU_ITEM_ACTIVATING);
2056
2057         DrawCursorAndText_Main(choice, FALSE, FALSE);
2058         DrawCursorAndText_Main(pos, TRUE, button_pressed);
2059
2060         choice = pos;
2061       }
2062       else if (dx != 0)
2063       {
2064         if (choice != MAIN_CONTROL_INFO &&
2065             choice != MAIN_CONTROL_SETUP)
2066           HandleMainMenu_SelectLevel(1, dx, NO_DIRECT_LEVEL_SELECT);
2067       }
2068     }
2069     else
2070     {
2071       PlaySound(SND_MENU_ITEM_SELECTING);
2072
2073       if (pos == MAIN_CONTROL_NAME)
2074       {
2075         SetGameStatus(GAME_MODE_PSEUDO_TYPENAME);
2076
2077         HandleTypeName(strlen(setup.player_name), 0);
2078       }
2079       else if (pos == MAIN_CONTROL_LEVELS)
2080       {
2081         if (leveldir_first)
2082         {
2083           CloseDoor(DOOR_CLOSE_2);
2084
2085           SetGameStatus(GAME_MODE_LEVELS);
2086
2087           SaveLevelSetup_LastSeries();
2088           SaveLevelSetup_SeriesInfo();
2089
2090           if (setup.internal.choose_from_top_leveldir)
2091             gotoTopLevelDir();
2092
2093           DrawChooseLevelSet();
2094         }
2095       }
2096       else if (pos == MAIN_CONTROL_SCORES)
2097       {
2098         CloseDoor(DOOR_CLOSE_2);
2099
2100         SetGameStatus(GAME_MODE_SCORES);
2101
2102         DrawHallOfFame(level_nr, -1);
2103       }
2104       else if (pos == MAIN_CONTROL_EDITOR)
2105       {
2106         if (leveldir_current->readonly &&
2107             !strEqual(setup.player_name, "Artsoft"))
2108           Request("This level is read only!", REQ_CONFIRM);
2109
2110         CloseDoor(DOOR_CLOSE_2);
2111
2112         SetGameStatus(GAME_MODE_EDITOR);
2113
2114         FadeSetEnterScreen();
2115
2116         DrawLevelEd();
2117       }
2118       else if (pos == MAIN_CONTROL_INFO)
2119       {
2120         CloseDoor(DOOR_CLOSE_2);
2121
2122         SetGameStatus(GAME_MODE_INFO);
2123
2124         info_mode = INFO_MODE_MAIN;
2125
2126         DrawInfoScreen();
2127       }
2128       else if (pos == MAIN_CONTROL_GAME)
2129       {
2130         StartGameActions(network.enabled, setup.autorecord, level.random_seed);
2131       }
2132       else if (pos == MAIN_CONTROL_SETUP)
2133       {
2134         CloseDoor(DOOR_CLOSE_2);
2135
2136         SetGameStatus(GAME_MODE_SETUP);
2137
2138         setup_mode = SETUP_MODE_MAIN;
2139
2140         DrawSetupScreen();
2141       }
2142       else if (pos == MAIN_CONTROL_QUIT)
2143       {
2144         SaveLevelSetup_LastSeries();
2145         SaveLevelSetup_SeriesInfo();
2146
2147         if (Request("Do you really want to quit?", REQ_ASK | REQ_STAY_CLOSED))
2148           SetGameStatus(GAME_MODE_QUIT);
2149       }
2150     }
2151   }
2152
2153   button_pressed_last = button_pressed;
2154 }
2155
2156
2157 /* ========================================================================= */
2158 /* info screen functions                                                     */
2159 /* ========================================================================= */
2160
2161 static struct TokenInfo *info_info;
2162 static int num_info_info;       /* number of info entries shown on screen */
2163 static int max_info_info;       /* total number of info entries in list */
2164
2165 static void execInfoTitleScreen(void)
2166 {
2167   info_mode = INFO_MODE_TITLE;
2168
2169   DrawInfoScreen();
2170 }
2171
2172 static void execInfoElements(void)
2173 {
2174   info_mode = INFO_MODE_ELEMENTS;
2175
2176   DrawInfoScreen();
2177 }
2178
2179 static void execInfoMusic(void)
2180 {
2181   info_mode = INFO_MODE_MUSIC;
2182
2183   DrawInfoScreen();
2184 }
2185
2186 static void execInfoCredits(void)
2187 {
2188   info_mode = INFO_MODE_CREDITS;
2189
2190   DrawInfoScreen();
2191 }
2192
2193 static void execInfoProgram(void)
2194 {
2195   info_mode = INFO_MODE_PROGRAM;
2196
2197   DrawInfoScreen();
2198 }
2199
2200 static void execInfoVersion(void)
2201 {
2202   info_mode = INFO_MODE_VERSION;
2203
2204   DrawInfoScreen();
2205 }
2206
2207 static void execInfoLevelSet(void)
2208 {
2209   info_mode = INFO_MODE_LEVELSET;
2210
2211   DrawInfoScreen();
2212 }
2213
2214 static void execExitInfo(void)
2215 {
2216   SetGameStatus(GAME_MODE_MAIN);
2217
2218   DrawMainMenu();
2219 }
2220
2221 static struct TokenInfo info_info_main[] =
2222 {
2223   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
2224   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
2225   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
2226   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
2227   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
2228   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
2229   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
2230   { TYPE_EMPTY,         NULL,                   ""                      },
2231   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
2232
2233   { 0,                  NULL,                   NULL                    }
2234 };
2235
2236 static int getMenuTextFont(int type)
2237 {
2238   if (type & (TYPE_SWITCH       |
2239               TYPE_YES_NO       |
2240               TYPE_YES_NO_AUTO  |
2241               TYPE_STRING       |
2242               TYPE_PLAYER       |
2243               TYPE_ECS_AGA      |
2244               TYPE_KEYTEXT      |
2245               TYPE_ENTER_LIST   |
2246               TYPE_TEXT_INPUT))
2247     return FONT_MENU_2;
2248   else
2249     return FONT_MENU_1;
2250 }
2251
2252 static struct TokenInfo *setup_info;
2253 static struct TokenInfo setup_info_input[];
2254
2255 static struct TokenInfo *menu_info;
2256
2257 static void DrawCursorAndText_Menu_Ext(struct TokenInfo *token_info,
2258                                        int screen_pos, int menu_info_pos_raw,
2259                                        boolean active)
2260 {
2261   int pos = (menu_info_pos_raw < 0 ? screen_pos : menu_info_pos_raw);
2262   struct TokenInfo *ti = &token_info[pos];
2263   int xpos = MENU_SCREEN_START_XPOS;
2264   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
2265   int font_nr = getMenuTextFont(ti->type);
2266
2267   if (setup_mode == SETUP_MODE_INPUT)
2268     font_nr = FONT_MENU_1;
2269
2270   if (active)
2271     font_nr = FONT_ACTIVE(font_nr);
2272
2273   DrawText(mSX + xpos * 32, mSY + ypos * 32, ti->text, font_nr);
2274
2275   if (ti->type & ~TYPE_SKIP_ENTRY)
2276     drawCursor(screen_pos, active);
2277 }
2278
2279 static void DrawCursorAndText_Menu(int screen_pos, int menu_info_pos_raw,
2280                                    boolean active)
2281 {
2282   DrawCursorAndText_Menu_Ext(menu_info, screen_pos, menu_info_pos_raw, active);
2283 }
2284
2285 static void DrawCursorAndText_Setup(int screen_pos, int menu_info_pos_raw,
2286                                     boolean active)
2287 {
2288   DrawCursorAndText_Menu_Ext(setup_info, screen_pos, menu_info_pos_raw, active);
2289 }
2290
2291 static char *window_size_text;
2292 static char *scaling_type_text;
2293 static char *network_server_text;
2294
2295 static void drawSetupValue(int, int);
2296
2297 static void drawMenuInfoList(int first_entry, int num_page_entries,
2298                              int max_page_entries)
2299 {
2300   int i;
2301
2302   if (first_entry + num_page_entries > max_page_entries)
2303     first_entry = 0;
2304
2305   clearMenuListArea();
2306
2307   for (i = 0; i < num_page_entries; i++)
2308   {
2309     int menu_info_pos = first_entry + i;
2310     struct TokenInfo *si = &menu_info[menu_info_pos];
2311     void *value_ptr = si->value;
2312
2313     /* set some entries to "unchangeable" according to other variables */
2314     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2315         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2316         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2317         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
2318         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
2319         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
2320       si->type |= TYPE_GHOSTED;
2321
2322     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2323       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2324     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2325       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2326     else if (si->type & ~TYPE_SKIP_ENTRY)
2327       initCursor(i, IMG_MENU_BUTTON);
2328
2329     DrawCursorAndText_Menu(i, menu_info_pos, FALSE);
2330
2331     if (si->type & TYPE_STRING)
2332     {
2333       int gadget_id = -1;
2334
2335       if (value_ptr == &network_server_text)
2336         gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
2337
2338       if (gadget_id != -1)
2339       {
2340         struct GadgetInfo *gi = screen_gadget[gadget_id];
2341         int xpos = MENU_SCREEN_START_XPOS;
2342         int ypos = MENU_SCREEN_START_YPOS + i;
2343         int x = mSX + xpos * 32;
2344         int y = mSY + ypos * 32;
2345
2346         ModifyGadget(gi, GDI_X, x, GDI_Y, y, GDI_END);
2347       }
2348     }
2349
2350     if (si->type & TYPE_VALUE &&
2351         menu_info == setup_info)
2352       drawSetupValue(i, menu_info_pos);
2353   }
2354 }
2355
2356 static void DrawInfoScreen_Main(void)
2357 {
2358   int fade_mask = REDRAW_FIELD;
2359   int i;
2360
2361   if (redraw_mask & REDRAW_ALL)
2362     fade_mask = REDRAW_ALL;
2363
2364   if (CheckIfGlobalBorderOrPlayfieldViewportHasChanged())
2365     fade_mask = REDRAW_ALL;
2366
2367   UnmapAllGadgets();
2368   FadeMenuSoundsAndMusic();
2369
2370   FreeScreenGadgets();
2371   CreateScreenGadgets();
2372
2373   /* (needed after displaying title screens which disable auto repeat) */
2374   KeyboardAutoRepeatOn();
2375
2376   FadeSetLeaveScreen();
2377
2378   FadeOut(fade_mask);
2379
2380   /* needed if different viewport properties defined for info screen */
2381   ChangeViewportPropertiesIfNeeded();
2382
2383   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
2384
2385   ClearField();
2386
2387   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2388
2389   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2390
2391   info_info = info_info_main;
2392
2393   // determine maximal number of info entries that can be displayed on screen
2394   num_info_info = 0;
2395   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2396     num_info_info++;
2397
2398   // determine maximal number of info entries available for menu of info screen
2399   max_info_info = 0;
2400   for (i = 0; info_info[i].type != 0; i++)
2401     max_info_info++;
2402
2403   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2404
2405   MapScreenGadgets(max_info_info);
2406
2407   PlayMenuSoundsAndMusic();
2408
2409   DrawMaskedBorder(fade_mask);
2410
2411   FadeIn(fade_mask);
2412 }
2413
2414 static void changeSetupValue(int, int, int);
2415
2416 static void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
2417                              int mode, int num_page_entries,
2418                              int max_page_entries)
2419 {
2420   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2421   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2422   static int first_entry_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2423   int *num_page_entries_last = num_page_entries_all_last[game_status];
2424   int *choice_store = choice_stores[game_status];
2425   int *first_entry_store = first_entry_stores[game_status];
2426   int choice = choice_store[mode];              /* starts with 0 */
2427   int first_entry = first_entry_store[mode];    /* starts with 0 */
2428   int x = 0;
2429   int y = choice - first_entry;
2430   int y_old = y;
2431   boolean position_set_by_scrollbar = (dx == 999);
2432   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
2433   int i;
2434
2435   if (button == MB_MENU_INITIALIZE)
2436   {
2437     // check if number of menu page entries has changed (may happen by change
2438     // of custom artwork definition value for 'list_size' for this menu screen)
2439     // (in this case, the last menu position most probably has to be corrected)
2440     if (num_page_entries != num_page_entries_last[mode])
2441     {
2442       choice_store[mode] = first_entry_store[mode] = 0;
2443
2444       choice = first_entry = 0;
2445       y = y_old = 0;
2446
2447       num_page_entries_last[mode] = num_page_entries;
2448     }
2449
2450     /* advance to first valid menu entry */
2451     while (choice < num_page_entries &&
2452            menu_info[choice].type & TYPE_SKIP_ENTRY)
2453       choice++;
2454
2455     if (position_set_by_scrollbar)
2456       first_entry = first_entry_store[mode] = dy;
2457     else
2458       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2459                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2460
2461     drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2462
2463     if (choice < first_entry)
2464     {
2465       choice = first_entry;
2466
2467       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2468         choice++;
2469     }
2470     else if (choice > first_entry + num_page_entries - 1)
2471     {
2472       choice = first_entry + num_page_entries - 1;
2473
2474       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2475         choice--;
2476     }
2477
2478     choice_store[mode] = choice;
2479
2480     DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2481
2482     return;
2483   }
2484   else if (button == MB_MENU_LEAVE)
2485   {
2486     PlaySound(SND_MENU_ITEM_SELECTING);
2487
2488     for (i = 0; i < max_page_entries; i++)
2489     {
2490       if (menu_info[i].type & TYPE_LEAVE_MENU)
2491       {
2492         void (*menu_callback_function)(void) = menu_info[i].value;
2493
2494         FadeSetLeaveMenu();
2495
2496         menu_callback_function();
2497
2498         break;  /* absolutely needed because function changes 'menu_info'! */
2499       }
2500     }
2501
2502     return;
2503   }
2504
2505   if (mx || my)         /* mouse input */
2506   {
2507     x = (mx - mSX) / 32;
2508     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2509   }
2510   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
2511   {
2512     /* move cursor instead of scrolling when already at start/end of list */
2513     if (dy == -1 * SCROLL_LINE && first_entry == 0)
2514       dy = -1;
2515     else if (dy == +1 * SCROLL_LINE &&
2516              first_entry + num_page_entries == max_page_entries)
2517       dy = 1;
2518
2519     /* handle scrolling screen one line or page */
2520     if (y + dy < 0 ||
2521         y + dy > num_page_entries - 1)
2522     {
2523       boolean redraw = FALSE;
2524
2525       if (ABS(dy) == SCROLL_PAGE)
2526         step = num_page_entries - 1;
2527
2528       if (dy < 0 && first_entry > 0)
2529       {
2530         /* scroll page/line up */
2531
2532         first_entry -= step;
2533         if (first_entry < 0)
2534           first_entry = 0;
2535
2536         redraw = TRUE;
2537       }
2538       else if (dy > 0 && first_entry + num_page_entries < max_page_entries)
2539       {
2540         /* scroll page/line down */
2541
2542         first_entry += step;
2543         if (first_entry + num_page_entries > max_page_entries)
2544           first_entry = MAX(0, max_page_entries - num_page_entries);
2545
2546         redraw = TRUE;
2547       }
2548
2549       if (redraw)
2550       {
2551         choice += first_entry - first_entry_store[mode];
2552
2553         if (choice < first_entry)
2554         {
2555           choice = first_entry;
2556
2557           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2558             choice++;
2559         }
2560         else if (choice > first_entry + num_page_entries - 1)
2561         {
2562           choice = first_entry + num_page_entries - 1;
2563
2564           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2565             choice--;
2566         }
2567         else if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2568         {
2569           choice += SIGN(dy);
2570
2571           if (choice < first_entry ||
2572               choice > first_entry + num_page_entries - 1)
2573           first_entry += SIGN(dy);
2574         }
2575
2576         first_entry_store[mode] = first_entry;
2577         choice_store[mode] = choice;
2578
2579         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2580
2581         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2582
2583         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2584                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2585       }
2586
2587       return;
2588     }
2589
2590     if (dx)
2591     {
2592       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2593
2594       if (menu_info[choice].type & menu_navigation_type ||
2595           menu_info[choice].type & TYPE_BOOLEAN_STYLE ||
2596           menu_info[choice].type & TYPE_YES_NO_AUTO ||
2597           menu_info[choice].type & TYPE_PLAYER)
2598         button = MB_MENU_CHOICE;
2599     }
2600     else if (dy)
2601       y += dy;
2602
2603     /* jump to next non-empty menu entry (up or down) */
2604     while (first_entry + y > 0 &&
2605            first_entry + y < max_page_entries - 1 &&
2606            menu_info[first_entry + y].type & TYPE_SKIP_ENTRY)
2607       y += dy;
2608
2609     if (!IN_VIS_MENU(x, y))
2610     {
2611       choice += y - y_old;
2612
2613       if (choice < first_entry)
2614         first_entry = choice;
2615       else if (choice > first_entry + num_page_entries - 1)
2616         first_entry = choice - num_page_entries + 1;
2617
2618       if (first_entry >= 0 &&
2619           first_entry + num_page_entries <= max_page_entries)
2620       {
2621         first_entry_store[mode] = first_entry;
2622
2623         if (choice < first_entry)
2624           choice = first_entry;
2625         else if (choice > first_entry + num_page_entries - 1)
2626           choice = first_entry + num_page_entries - 1;
2627
2628         choice_store[mode] = choice;
2629
2630         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2631
2632         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2633
2634         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2635                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2636       }
2637
2638       return;
2639     }
2640   }
2641
2642   if (!anyScrollbarGadgetActive() &&
2643       IN_VIS_MENU(x, y) &&
2644       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
2645       y >= 0 && y < num_page_entries)
2646   {
2647     if (button)
2648     {
2649       if (first_entry + y != choice &&
2650           menu_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
2651       {
2652         PlaySound(SND_MENU_ITEM_ACTIVATING);
2653
2654         DrawCursorAndText_Menu(choice - first_entry, choice, FALSE);
2655         DrawCursorAndText_Menu(y, first_entry + y, TRUE);
2656
2657         choice = choice_store[mode] = first_entry + y;
2658       }
2659       else if (dx < 0)
2660       {
2661         PlaySound(SND_MENU_ITEM_SELECTING);
2662
2663         for (i = 0; menu_info[i].type != 0; i++)
2664         {
2665           if (menu_info[i].type & TYPE_LEAVE_MENU)
2666           {
2667             void (*menu_callback_function)(void) = menu_info[i].value;
2668
2669             FadeSetLeaveMenu();
2670
2671             menu_callback_function();
2672
2673             /* absolutely needed because function changes 'menu_info'! */
2674             break;
2675           }
2676         }
2677
2678         return;
2679       }
2680     }
2681     else if (!(menu_info[first_entry + y].type & TYPE_GHOSTED))
2682     {
2683       PlaySound(SND_MENU_ITEM_SELECTING);
2684
2685       /* when selecting key headline, execute function for key value change */
2686       if (menu_info[first_entry + y].type & TYPE_KEYTEXT &&
2687           menu_info[first_entry + y + 1].type & TYPE_KEY)
2688         y++;
2689
2690       /* when selecting string value, execute function for list selection */
2691       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2692           menu_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
2693         y--;
2694
2695       /* when selecting string value, execute function for text input gadget */
2696       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2697           menu_info[first_entry + y - 1].type & TYPE_TEXT_INPUT)
2698         y--;
2699
2700       if (menu_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
2701       {
2702         void (*menu_callback_function)(void) =
2703           menu_info[first_entry + y].value;
2704
2705         FadeSetFromType(menu_info[first_entry + y].type);
2706
2707         menu_callback_function();
2708       }
2709       else if (menu_info[first_entry + y].type & TYPE_TEXT_INPUT)
2710       {
2711         void (*gadget_callback_function)(void) =
2712           menu_info[first_entry + y].value;
2713
2714         gadget_callback_function();
2715       }
2716       else if (menu_info[first_entry + y].type & TYPE_VALUE &&
2717                menu_info == setup_info)
2718       {
2719         changeSetupValue(y, first_entry + y, dx);
2720       }
2721     }
2722   }
2723 }
2724
2725 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2726 {
2727   menu_info = info_info;
2728
2729   HandleMenuScreen(mx, my, dx, dy, button,
2730                    info_mode, num_info_info, max_info_info);
2731 }
2732
2733 static int getMenuFontSpacing(int spacing_height, int font_nr)
2734 {
2735   int font_spacing = getFontHeight(font_nr) + EXTRA_SPACING(game_status);
2736
2737   return (spacing_height < 0 ? ABS(spacing_height) * font_spacing :
2738           spacing_height);
2739 }
2740
2741 static int getMenuTextSpacing(int spacing_height, int font_nr)
2742 {
2743   return (getMenuFontSpacing(spacing_height, font_nr) +
2744           EXTRA_SPACING(game_status));
2745 }
2746
2747 static int getMenuTextStep(int spacing_height, int font_nr)
2748 {
2749   return getFontHeight(font_nr) + getMenuTextSpacing(spacing_height, font_nr);
2750 }
2751
2752 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2753 {
2754   int font_title = MENU_INFO_FONT_TITLE;
2755   int font_error = FONT_TEXT_2;
2756   int font_foot  = MENU_INFO_FONT_FOOT;
2757   int spacing_title = menu.headline1_spacing_info[info_mode];
2758   int ystep_title = getMenuTextStep(spacing_title, font_title);
2759   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2760   int ystart2 = ystart1 + ystep_title;
2761   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2762
2763   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO);
2764
2765   FadeOut(REDRAW_FIELD);
2766
2767   ClearField();
2768   DrawHeadline();
2769
2770   DrawTextSCentered(ystart1, font_title, text_title);
2771   DrawTextSCentered(ystart2, font_error, text_error);
2772
2773   DrawTextSCentered(ybottom, font_foot,
2774                     "Press any key or button for info menu");
2775
2776   FadeIn(REDRAW_FIELD);
2777 }
2778
2779 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2780 {
2781   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2782   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2783   int font_title = MENU_INFO_FONT_TITLE;
2784   int font_foot  = MENU_INFO_FONT_FOOT;
2785   int xstart  = mSX + MENU_SCREEN_INFO_SPACE_LEFT;
2786   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2787   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2788   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2789   int ystep = MENU_SCREEN_INFO_YSTEP;
2790   int element, action, direction;
2791   int graphic;
2792   int delay;
2793   int sync_frame;
2794   int i, j;
2795
2796   if (init)
2797   {
2798     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2799       infoscreen_step[i] = infoscreen_frame[i] = 0;
2800
2801     ClearField();
2802     DrawHeadline();
2803
2804     DrawTextSCentered(ystart1, font_title, "The Game Elements:");
2805
2806     DrawTextSCentered(ybottom, font_foot,
2807                       "Press any key or button for next page");
2808
2809     FrameCounter = 0;
2810   }
2811
2812   i = j = 0;
2813   while (helpanim_info[j].element != HELPANIM_LIST_END)
2814   {
2815     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2816         i >= max_anims)
2817       break;
2818     else if (i < start)
2819     {
2820       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2821         j++;
2822
2823       j++;
2824       i++;
2825
2826       continue;
2827     }
2828
2829     j += infoscreen_step[i - start];
2830
2831     element = helpanim_info[j].element;
2832     action = helpanim_info[j].action;
2833     direction = helpanim_info[j].direction;
2834
2835     if (element < 0)
2836       element = EL_UNKNOWN;
2837
2838     if (action != -1 && direction != -1)
2839       graphic = el_act_dir2img(element, action, direction);
2840     else if (action != -1)
2841       graphic = el_act2img(element, action);
2842     else if (direction != -1)
2843       graphic = el_dir2img(element, direction);
2844     else
2845       graphic = el2img(element);
2846
2847     delay = helpanim_info[j++].delay;
2848
2849     if (delay == -1)
2850       delay = 1000000;
2851
2852     if (infoscreen_frame[i - start] == 0)
2853     {
2854       sync_frame = 0;
2855       infoscreen_frame[i - start] = delay - 1;
2856     }
2857     else
2858     {
2859       sync_frame = delay - infoscreen_frame[i - start];
2860       infoscreen_frame[i - start]--;
2861     }
2862
2863     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2864     {
2865       if (!infoscreen_frame[i - start])
2866         infoscreen_step[i - start] = 0;
2867     }
2868     else
2869     {
2870       if (!infoscreen_frame[i - start])
2871         infoscreen_step[i - start]++;
2872       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2873         j++;
2874     }
2875
2876     j++;
2877
2878     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2879                                TILEX, TILEY);
2880     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2881                                  graphic, sync_frame, USE_MASKING);
2882
2883     if (init)
2884       DrawInfoScreen_HelpText(element, action, direction, i - start);
2885
2886     i++;
2887   }
2888
2889   redraw_mask |= REDRAW_FIELD;
2890
2891   FrameCounter++;
2892 }
2893
2894 static char *getHelpText(int element, int action, int direction)
2895 {
2896   char token[MAX_LINE_LEN];
2897
2898   strcpy(token, element_info[element].token_name);
2899
2900   if (action != -1)
2901     strcat(token, element_action_info[action].suffix);
2902
2903   if (direction != -1)
2904     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2905
2906   return getHashEntry(helptext_info, token);
2907 }
2908
2909 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2910 {
2911   int font_nr = FONT_INFO_ELEMENTS;
2912   int font_width = getFontWidth(font_nr);
2913   int font_height = getFontHeight(font_nr);
2914   int yoffset = (TILEX - 2 * font_height) / 2;
2915   int xstart = mSX + MENU_SCREEN_INFO_SPACE_LEFT + TILEX + MINI_TILEX;
2916   int ystart = mSY + MENU_SCREEN_INFO_YSTART2 + yoffset;
2917   int ystep = TILEY + 4;
2918   int pad_left = xstart - SX;
2919   int pad_right = MENU_SCREEN_INFO_SPACE_RIGHT;
2920   int max_chars_per_line = (SXSIZE - pad_left - pad_right) / font_width;
2921   int max_lines_per_text = 2;    
2922   char *text = NULL;
2923
2924   if (action != -1 && direction != -1)          /* element.action.direction */
2925     text = getHelpText(element, action, direction);
2926
2927   if (text == NULL && action != -1)             /* element.action */
2928     text = getHelpText(element, action, -1);
2929
2930   if (text == NULL && direction != -1)          /* element.direction */
2931     text = getHelpText(element, -1, direction);
2932
2933   if (text == NULL)                             /* base element */
2934     text = getHelpText(element, -1, -1);
2935
2936   if (text == NULL)                             /* not found */
2937     text = "No description available";
2938
2939   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2940     ystart += getFontHeight(font_nr) / 2;
2941
2942   DrawTextBuffer(xstart, ystart + ypos * ystep, text, font_nr,
2943                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2944                  TRUE, FALSE, FALSE);
2945 }
2946
2947 static void DrawInfoScreen_TitleScreen(void)
2948 {
2949   SetGameStatus(GAME_MODE_TITLE);
2950
2951   DrawTitleScreen();
2952 }
2953
2954 void HandleInfoScreen_TitleScreen(int button)
2955 {
2956   HandleTitleScreen(0, 0, 0, 0, button);
2957 }
2958
2959 static void DrawInfoScreen_Elements(void)
2960 {
2961   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2962
2963   FadeOut(REDRAW_FIELD);
2964
2965   LoadHelpAnimInfo();
2966   LoadHelpTextInfo();
2967
2968   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2969
2970   FadeIn(REDRAW_FIELD);
2971 }
2972
2973 void HandleInfoScreen_Elements(int button)
2974 {
2975   static unsigned int info_delay = 0;
2976   static int num_anims;
2977   static int num_pages;
2978   static int page;
2979   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
2980   int i;
2981
2982   if (button == MB_MENU_INITIALIZE)
2983   {
2984     boolean new_element = TRUE;
2985
2986     num_anims = 0;
2987
2988     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2989     {
2990       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2991         new_element = TRUE;
2992       else if (new_element)
2993       {
2994         num_anims++;
2995         new_element = FALSE;
2996       }
2997     }
2998
2999     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
3000     page = 0;
3001   }
3002
3003   if (button == MB_MENU_LEAVE)
3004   {
3005     PlaySound(SND_MENU_ITEM_SELECTING);
3006
3007     info_mode = INFO_MODE_MAIN;
3008     DrawInfoScreen();
3009
3010     return;
3011   }
3012   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3013   {
3014     if (button != MB_MENU_INITIALIZE)
3015     {
3016       PlaySound(SND_MENU_ITEM_SELECTING);
3017
3018       page++;
3019     }
3020
3021     if (page >= num_pages)
3022     {
3023       FadeMenuSoundsAndMusic();
3024
3025       info_mode = INFO_MODE_MAIN;
3026       DrawInfoScreen();
3027
3028       return;
3029     }
3030
3031     if (page > 0)
3032       FadeSetNextScreen();
3033
3034     if (button != MB_MENU_INITIALIZE)
3035       FadeOut(REDRAW_FIELD);
3036
3037     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
3038
3039     if (button != MB_MENU_INITIALIZE)
3040       FadeIn(REDRAW_FIELD);
3041   }
3042   else
3043   {
3044     if (DelayReached(&info_delay, GameFrameDelay))
3045       if (page < num_pages)
3046         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
3047
3048     PlayMenuSoundIfLoop();
3049   }
3050 }
3051
3052 static void DrawInfoScreen_Music(void)
3053 {
3054   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
3055
3056   FadeOut(REDRAW_FIELD);
3057
3058   ClearField();
3059   DrawHeadline();
3060
3061   LoadMusicInfo();
3062
3063   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
3064
3065   FadeIn(REDRAW_FIELD);
3066 }
3067
3068 void HandleInfoScreen_Music(int button)
3069 {
3070   static struct MusicFileInfo *list = NULL;
3071   int font_title = MENU_INFO_FONT_TITLE;
3072   int font_head  = MENU_INFO_FONT_HEAD;
3073   int font_text  = MENU_INFO_FONT_TEXT;
3074   int font_foot  = MENU_INFO_FONT_FOOT;
3075   int spacing_title = menu.headline1_spacing_info[info_mode];
3076   int spacing_head  = menu.headline2_spacing_info[info_mode];
3077   int ystep_title = getMenuTextStep(spacing_title, font_title);
3078   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3079   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3080   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3081
3082   if (button == MB_MENU_INITIALIZE)
3083   {
3084     list = music_file_info;
3085
3086     if (list == NULL)
3087     {
3088       FadeMenuSoundsAndMusic();
3089
3090       ClearField();
3091       DrawHeadline();
3092
3093       DrawTextSCentered(ystart, font_title,
3094                         "No music info for this level set.");
3095
3096       DrawTextSCentered(ybottom, font_foot,
3097                         "Press any key or button for info menu");
3098
3099       return;
3100     }
3101   }
3102
3103   if (button == MB_MENU_LEAVE)
3104   {
3105     PlaySound(SND_MENU_ITEM_SELECTING);
3106
3107     FadeMenuSoundsAndMusic();
3108
3109     info_mode = INFO_MODE_MAIN;
3110     DrawInfoScreen();
3111
3112     return;
3113   }
3114   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3115   {
3116     if (button != MB_MENU_INITIALIZE)
3117     {
3118       PlaySound(SND_MENU_ITEM_SELECTING);
3119
3120       if (list != NULL)
3121         list = list->next;
3122     }
3123
3124     if (list == NULL)
3125     {
3126       FadeMenuSoundsAndMusic();
3127
3128       info_mode = INFO_MODE_MAIN;
3129       DrawInfoScreen();
3130
3131       return;
3132     }
3133
3134     FadeMenuSoundsAndMusic();
3135
3136     if (list != music_file_info)
3137       FadeSetNextScreen();
3138
3139     if (button != MB_MENU_INITIALIZE)
3140       FadeOut(REDRAW_FIELD);
3141
3142     ClearField();
3143     DrawHeadline();
3144
3145     if (list->is_sound)
3146     {
3147       int sound = list->music;
3148
3149       if (sound_info[sound].loop)
3150         PlaySoundLoop(sound);
3151       else
3152         PlaySound(sound);
3153
3154       DrawTextSCentered(ystart, font_title, "The Game Background Sounds:");
3155     }
3156     else
3157     {
3158       int music = list->music;
3159
3160       if (music_info[music].loop)
3161         PlayMusicLoop(music);
3162       else
3163         PlayMusic(music);
3164
3165       DrawTextSCentered(ystart, font_title, "The Game Background Music:");
3166     }
3167
3168     ystart += ystep_title;
3169
3170     if (!strEqual(list->title, UNKNOWN_NAME))
3171     {
3172       if (!strEqual(list->title_header, UNKNOWN_NAME))
3173       {
3174         DrawTextSCentered(ystart, font_head, list->title_header);
3175         ystart += ystep_head;
3176       }
3177
3178       DrawTextFCentered(ystart, font_text, "\"%s\"", list->title);
3179       ystart += ystep_head;
3180     }
3181
3182     if (!strEqual(list->artist, UNKNOWN_NAME))
3183     {
3184       if (!strEqual(list->artist_header, UNKNOWN_NAME))
3185         DrawTextSCentered(ystart, font_head, list->artist_header);
3186       else
3187         DrawTextSCentered(ystart, font_head, "by");
3188
3189       ystart += ystep_head;
3190
3191       DrawTextFCentered(ystart, font_text, "%s", list->artist);
3192       ystart += ystep_head;
3193     }
3194
3195     if (!strEqual(list->album, UNKNOWN_NAME))
3196     {
3197       if (!strEqual(list->album_header, UNKNOWN_NAME))
3198         DrawTextSCentered(ystart, font_head, list->album_header);
3199       else
3200         DrawTextSCentered(ystart, font_head, "from the album");
3201
3202       ystart += ystep_head;
3203
3204       DrawTextFCentered(ystart, font_text, "\"%s\"", list->album);
3205       ystart += ystep_head;
3206     }
3207
3208     if (!strEqual(list->year, UNKNOWN_NAME))
3209     {
3210       if (!strEqual(list->year_header, UNKNOWN_NAME))
3211         DrawTextSCentered(ystart, font_head, list->year_header);
3212       else
3213         DrawTextSCentered(ystart, font_head, "from the year");
3214
3215       ystart += ystep_head;
3216
3217       DrawTextFCentered(ystart, font_text, "%s", list->year);
3218       ystart += ystep_head;
3219     }
3220
3221     DrawTextSCentered(ybottom, FONT_TEXT_4,
3222                       "Press any key or button for next page");
3223
3224     if (button != MB_MENU_INITIALIZE)
3225       FadeIn(REDRAW_FIELD);
3226   }
3227
3228   if (list != NULL && list->is_sound && sound_info[list->music].loop)
3229     PlaySoundLoop(list->music);
3230 }
3231
3232 static void DrawInfoScreen_CreditsScreen(int screen_nr)
3233 {
3234   int font_title = MENU_INFO_FONT_TITLE;
3235   int font_head  = MENU_INFO_FONT_HEAD;
3236   int font_text  = MENU_INFO_FONT_TEXT;
3237   int font_foot  = MENU_INFO_FONT_FOOT;
3238   int spacing_title = menu.headline1_spacing_info[info_mode];
3239   int spacing_head  = menu.headline2_spacing_info[info_mode];
3240   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3241   int spacing_line  = menu.line_spacing_info[info_mode];
3242   int ystep_title = getMenuTextStep(spacing_title, font_title);
3243   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3244   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3245   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3246   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3247   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3248
3249   ClearField();
3250   DrawHeadline();
3251
3252   DrawTextSCentered(ystart, font_title, "Credits:");
3253   ystart += ystep_title;
3254
3255   if (screen_nr == 0)
3256   {
3257     DrawTextSCentered(ystart, font_head,
3258                       "Special thanks to");
3259     ystart += ystep_head;
3260     DrawTextSCentered(ystart, font_text,
3261                       "Peter Liepa");
3262     ystart += ystep_head;
3263     DrawTextSCentered(ystart, font_head,
3264                       "for creating");
3265     ystart += ystep_head;
3266     DrawTextSCentered(ystart, font_text,
3267                       "\"Boulder Dash\"");
3268     ystart += ystep_head;
3269     DrawTextSCentered(ystart, font_head,
3270                       "in the year");
3271     ystart += ystep_head;
3272     DrawTextSCentered(ystart, font_text,
3273                       "1984");
3274     ystart += ystep_head;
3275     DrawTextSCentered(ystart, font_head,
3276                       "published by");
3277     ystart += ystep_head;
3278     DrawTextSCentered(ystart, font_text,
3279                       "First Star Software");
3280   }
3281   else if (screen_nr == 1)
3282   {
3283     DrawTextSCentered(ystart, font_head,
3284                       "Special thanks to");
3285     ystart += ystep_head;
3286     DrawTextSCentered(ystart, font_text,
3287                       "Klaus Heinz & Volker Wertich");
3288     ystart += ystep_head;
3289     DrawTextSCentered(ystart, font_head,
3290                       "for creating");
3291     ystart += ystep_head;
3292     DrawTextSCentered(ystart, font_text,
3293                       "\"Emerald Mine\"");
3294     ystart += ystep_head;
3295     DrawTextSCentered(ystart, font_head,
3296                       "in the year");
3297     ystart += ystep_head;
3298     DrawTextSCentered(ystart, font_text,
3299                       "1987");
3300     ystart += ystep_head;
3301     DrawTextSCentered(ystart, font_head,
3302                       "published by");
3303     ystart += ystep_head;
3304     DrawTextSCentered(ystart, font_text,
3305                       "Kingsoft");
3306   }
3307   else if (screen_nr == 2)
3308   {
3309     DrawTextSCentered(ystart, font_head,
3310                       "Special thanks to");
3311     ystart += ystep_head;
3312     DrawTextSCentered(ystart, font_text,
3313                       "Michael Stopp & Philip Jespersen");
3314     ystart += ystep_head;
3315     DrawTextSCentered(ystart, font_head,
3316                       "for creating");
3317     ystart += ystep_head;
3318     DrawTextSCentered(ystart, font_text,
3319                       "\"Supaplex\"");
3320     ystart += ystep_head;
3321     DrawTextSCentered(ystart, font_head,
3322                       "in the year");
3323     ystart += ystep_head;
3324     DrawTextSCentered(ystart, font_text,
3325                       "1991");
3326     ystart += ystep_head;
3327     DrawTextSCentered(ystart, font_head,
3328                       "published by");
3329     ystart += ystep_head;
3330     DrawTextSCentered(ystart, font_text,
3331                       "Digital Integration");
3332   }
3333   else if (screen_nr == 3)
3334   {
3335     DrawTextSCentered(ystart, font_head,
3336                       "Special thanks to");
3337     ystart += ystep_head;
3338     DrawTextSCentered(ystart, font_text,
3339                       "Hiroyuki Imabayashi");
3340     ystart += ystep_head;
3341     DrawTextSCentered(ystart, font_head,
3342                       "for creating");
3343     ystart += ystep_head;
3344     DrawTextSCentered(ystart, font_text,
3345                       "\"Sokoban\"");
3346     ystart += ystep_head;
3347     DrawTextSCentered(ystart, font_head,
3348                       "in the year");
3349     ystart += ystep_head;
3350     DrawTextSCentered(ystart, font_text,
3351                       "1982");
3352     ystart += ystep_head;
3353     DrawTextSCentered(ystart, font_head,
3354                       "published by");
3355     ystart += ystep_head;
3356     DrawTextSCentered(ystart, font_text,
3357                       "Thinking Rabbit");
3358   }
3359   else if (screen_nr == 4)
3360   {
3361     DrawTextSCentered(ystart, font_head,
3362                       "Special thanks to");
3363     ystart += ystep_head;
3364     DrawTextSCentered(ystart, font_text,
3365                       "Alan Bond");
3366     ystart += ystep_head;
3367     DrawTextSCentered(ystart, font_head,
3368                       "and");
3369     ystart += ystep_head;
3370     DrawTextSCentered(ystart, font_text,
3371                       "J\xfcrgen Bonhagen");
3372     ystart += ystep_head;
3373     DrawTextSCentered(ystart, font_head,
3374                       "for the continuous creation");
3375     ystart += ystep_line;
3376     DrawTextSCentered(ystart, font_head,
3377                       "of outstanding level sets");
3378   }
3379   else if (screen_nr == 5)
3380   {
3381     DrawTextSCentered(ystart, font_head,
3382                       "Thanks to");
3383     ystart += ystep_head;
3384     DrawTextSCentered(ystart, font_text,
3385                       "Peter Elzner");
3386     ystart += ystep_head;
3387     DrawTextSCentered(ystart, font_head,
3388                       "for ideas and inspiration by");
3389     ystart += ystep_head;
3390     DrawTextSCentered(ystart, font_text,
3391                       "Diamond Caves");
3392     ystart += ystep_para;
3393
3394     DrawTextSCentered(ystart, font_head,
3395                       "Thanks to");
3396     ystart += ystep_head;
3397     DrawTextSCentered(ystart, font_text,
3398                       "Steffest");
3399     ystart += ystep_head;
3400     DrawTextSCentered(ystart, font_head,
3401                       "for ideas and inspiration by");
3402     ystart += ystep_head;
3403     DrawTextSCentered(ystart, font_text,
3404                       "DX-Boulderdash");
3405   }
3406   else if (screen_nr == 6)
3407   {
3408     DrawTextSCentered(ystart, font_head,
3409                       "Thanks to");
3410     ystart += ystep_head;
3411     DrawTextSCentered(ystart, font_text,
3412                       "David Tritscher");
3413     ystart += ystep_head;
3414     DrawTextSCentered(ystart, font_head,
3415                       "for the code base used for the");
3416     ystart += ystep_line;
3417     DrawTextSCentered(ystart, font_head,
3418                       "native Emerald Mine engine");
3419   }
3420   else if (screen_nr == 7)
3421   {
3422     DrawTextSCentered(ystart, font_head,
3423                       "Thanks to");
3424     ystart += ystep_head;
3425     DrawTextSCentered(ystart, font_text,
3426                       "Guido Schulz");
3427     ystart += ystep_head;
3428     DrawTextSCentered(ystart, font_head,
3429                       "for the initial DOS port");
3430     ystart += ystep_para;
3431
3432     DrawTextSCentered(ystart, font_head,
3433                       "Thanks to");
3434     ystart += ystep_head;
3435     DrawTextSCentered(ystart, font_text,
3436                       "Karl H\xf6rnell");
3437     ystart += ystep_head;
3438     DrawTextSCentered(ystart, font_head,
3439                       "for some additional toons");
3440   }
3441   else if (screen_nr == 8)
3442   {
3443     DrawTextSCentered(ystart, font_head,
3444                       "And not to forget:");
3445     ystart += ystep_head;
3446     DrawTextSCentered(ystart, font_head,
3447                       "Many thanks to");
3448     ystart += ystep_head;
3449     DrawTextSCentered(ystart, font_text,
3450                       "All those who contributed");
3451     ystart += ystep_line;
3452     DrawTextSCentered(ystart, font_text,
3453                       "levels to this game");
3454     ystart += ystep_line;
3455     DrawTextSCentered(ystart, font_text,
3456                       "since 1995");
3457   }
3458
3459   DrawTextSCentered(ybottom, font_foot,
3460                     "Press any key or button for next page");
3461 }
3462
3463 static void DrawInfoScreen_Credits(void)
3464 {
3465   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3466
3467   FadeMenuSoundsAndMusic();
3468
3469   FadeOut(REDRAW_FIELD);
3470
3471   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3472
3473   FadeIn(REDRAW_FIELD);
3474 }
3475
3476 void HandleInfoScreen_Credits(int button)
3477 {
3478   static int screen_nr = 0;
3479   int num_screens = 9;
3480
3481   if (button == MB_MENU_INITIALIZE)
3482   {
3483     screen_nr = 0;
3484
3485     // DrawInfoScreen_CreditsScreen(screen_nr);
3486   }
3487
3488   if (button == MB_MENU_LEAVE)
3489   {
3490     PlaySound(SND_MENU_ITEM_SELECTING);
3491
3492     info_mode = INFO_MODE_MAIN;
3493     DrawInfoScreen();
3494
3495     return;
3496   }
3497   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3498   {
3499     if (button != MB_MENU_INITIALIZE)
3500     {
3501       PlaySound(SND_MENU_ITEM_SELECTING);
3502
3503       screen_nr++;
3504     }
3505
3506     if (screen_nr >= num_screens)
3507     {
3508       FadeMenuSoundsAndMusic();
3509
3510       info_mode = INFO_MODE_MAIN;
3511       DrawInfoScreen();
3512
3513       return;
3514     }
3515
3516     if (screen_nr > 0)
3517       FadeSetNextScreen();
3518
3519     if (button != MB_MENU_INITIALIZE)
3520       FadeOut(REDRAW_FIELD);
3521
3522     DrawInfoScreen_CreditsScreen(screen_nr);
3523
3524     if (button != MB_MENU_INITIALIZE)
3525       FadeIn(REDRAW_FIELD);
3526   }
3527   else
3528   {
3529     PlayMenuSoundIfLoop();
3530   }
3531 }
3532
3533 static void DrawInfoScreen_Program(void)
3534 {
3535   int font_title = MENU_INFO_FONT_TITLE;
3536   int font_head  = MENU_INFO_FONT_HEAD;
3537   int font_text  = MENU_INFO_FONT_TEXT;
3538   int font_foot  = MENU_INFO_FONT_FOOT;
3539   int spacing_title = menu.headline1_spacing_info[info_mode];
3540   int spacing_head  = menu.headline2_spacing_info[info_mode];
3541   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3542   int spacing_line  = menu.line_spacing_info[info_mode];
3543   int ystep_title = getMenuTextStep(spacing_title, font_title);
3544   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3545   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3546   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3547   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3548   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3549
3550   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3551
3552   FadeOut(REDRAW_FIELD);
3553
3554   ClearField();
3555   DrawHeadline();
3556
3557   DrawTextSCentered(ystart, font_title, "Program Information:");
3558   ystart += ystep_title;
3559
3560   DrawTextSCentered(ystart, font_head,
3561                     "This game is Freeware!");
3562   ystart += ystep_head;
3563   DrawTextSCentered(ystart, font_head,
3564                     "If you like it, send e-mail to:");
3565   ystart += ystep_head;
3566   DrawTextSCentered(ystart, font_text,
3567                     setup.internal.program_email);
3568   ystart += ystep_para;
3569
3570   DrawTextSCentered(ystart, font_head,
3571                     "More information and levels:");
3572   ystart += ystep_head;
3573   DrawTextSCentered(ystart, font_text,
3574                     setup.internal.program_website);
3575   ystart += ystep_para;
3576
3577   DrawTextSCentered(ystart, font_head,
3578                     "If you have created new levels,");
3579   ystart += ystep_line;
3580   DrawTextSCentered(ystart, font_head,
3581                     "send them to me to include them!");
3582   ystart += ystep_head;
3583   DrawTextSCentered(ystart, font_head,
3584                     ":-)");
3585
3586   DrawTextSCentered(ybottom, font_foot,
3587                     "Press any key or button for info menu");
3588
3589   FadeIn(REDRAW_FIELD);
3590 }
3591
3592 void HandleInfoScreen_Program(int button)
3593 {
3594   if (button == MB_MENU_LEAVE)
3595   {
3596     PlaySound(SND_MENU_ITEM_SELECTING);
3597
3598     info_mode = INFO_MODE_MAIN;
3599     DrawInfoScreen();
3600
3601     return;
3602   }
3603   else if (button == MB_MENU_CHOICE)
3604   {
3605     PlaySound(SND_MENU_ITEM_SELECTING);
3606
3607     FadeMenuSoundsAndMusic();
3608
3609     info_mode = INFO_MODE_MAIN;
3610     DrawInfoScreen();
3611   }
3612   else
3613   {
3614     PlayMenuSoundIfLoop();
3615   }
3616 }
3617
3618 static void DrawInfoScreen_Version(void)
3619 {
3620   int font_title = MENU_INFO_FONT_TITLE;
3621   int font_head  = MENU_INFO_FONT_HEAD;
3622   int font_text  = MENU_INFO_FONT_TEXT;
3623   int font_foot  = MENU_INFO_FONT_FOOT;
3624   int spacing_title = menu.headline1_spacing_info[info_mode];
3625   int spacing_head  = menu.headline2_spacing_info[info_mode];
3626   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3627   int spacing_line  = menu.line_spacing_info[info_mode];
3628   int xstep = getFontWidth(font_text);
3629   int ystep_title = getMenuTextStep(spacing_title, font_title);
3630   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3631   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3632   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3633   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3634   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3635   int xstart1 = mSX - SX + 2 * xstep;
3636   int xstart2 = mSX - SX + 18 * xstep;
3637   int xstart3 = mSX - SX + 28 * xstep;
3638   SDL_version sdl_version_compiled;
3639   const SDL_version *sdl_version_linked;
3640   int driver_name_len = 10;
3641 #if defined(TARGET_SDL2)
3642   SDL_version sdl_version_linked_ext;
3643   const char *driver_name = NULL;
3644 #else
3645   char driver_name[driver_name_len];
3646 #endif
3647
3648   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3649
3650   FadeOut(REDRAW_FIELD);
3651
3652   ClearField();
3653   DrawHeadline();
3654
3655   DrawTextSCentered(ystart, font_title, "Version Information:");
3656   ystart += ystep_title;
3657
3658   DrawTextF(xstart1, ystart, font_head, "Name");
3659   DrawTextF(xstart2, ystart, font_text, getProgramTitleString());
3660   ystart += ystep_line;
3661
3662   if (!strEqual(getProgramVersionString(), getProgramRealVersionString()))
3663   {
3664     DrawTextF(xstart1, ystart, font_head, "Version (fake)");
3665     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3666     ystart += ystep_line;
3667
3668     DrawTextF(xstart1, ystart, font_head, "Version (real)");
3669     DrawTextF(xstart2, ystart, font_text, getProgramRealVersionString());
3670     ystart += ystep_line;
3671   }
3672   else
3673   {
3674     DrawTextF(xstart1, ystart, font_head, "Version");
3675     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3676     ystart += ystep_line;
3677   }
3678
3679   DrawTextF(xstart1, ystart, font_head, "Platform");
3680   DrawTextF(xstart2, ystart, font_text, PLATFORM_STRING);
3681   ystart += ystep_line;
3682
3683   DrawTextF(xstart1, ystart, font_head, "Target");
3684   DrawTextF(xstart2, ystart, font_text, TARGET_STRING);
3685   ystart += ystep_line;
3686
3687   DrawTextF(xstart1, ystart, font_head, "Source date");
3688   DrawTextF(xstart2, ystart, font_text, getSourceDateString());
3689   ystart += ystep_para;
3690
3691   DrawTextF(xstart1, ystart, font_head, "Library");
3692   DrawTextF(xstart2, ystart, font_head, "compiled");
3693   DrawTextF(xstart3, ystart, font_head, "linked");
3694   ystart += ystep_head;
3695
3696   SDL_VERSION(&sdl_version_compiled);
3697 #if defined(TARGET_SDL2)
3698   SDL_GetVersion(&sdl_version_linked_ext);
3699   sdl_version_linked = &sdl_version_linked_ext;
3700 #else
3701   sdl_version_linked = SDL_Linked_Version();
3702 #endif
3703
3704   DrawTextF(xstart1, ystart, font_text, "SDL");
3705   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3706             sdl_version_compiled.major,
3707             sdl_version_compiled.minor,
3708             sdl_version_compiled.patch);
3709   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3710             sdl_version_linked->major,
3711             sdl_version_linked->minor,
3712             sdl_version_linked->patch);
3713   ystart += ystep_line;
3714
3715   SDL_IMAGE_VERSION(&sdl_version_compiled);
3716   sdl_version_linked = IMG_Linked_Version();
3717
3718   DrawTextF(xstart1, ystart, font_text, "SDL_image");
3719   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3720             sdl_version_compiled.major,
3721             sdl_version_compiled.minor,
3722             sdl_version_compiled.patch);
3723   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3724             sdl_version_linked->major,
3725             sdl_version_linked->minor,
3726             sdl_version_linked->patch);
3727   ystart += ystep_line;
3728
3729   SDL_MIXER_VERSION(&sdl_version_compiled);
3730   sdl_version_linked = Mix_Linked_Version();
3731
3732   DrawTextF(xstart1, ystart, font_text, "SDL_mixer");
3733   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3734             sdl_version_compiled.major,
3735             sdl_version_compiled.minor,
3736             sdl_version_compiled.patch);
3737   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3738             sdl_version_linked->major,
3739             sdl_version_linked->minor,
3740             sdl_version_linked->patch);
3741   ystart += ystep_line;
3742
3743   SDL_NET_VERSION(&sdl_version_compiled);
3744   sdl_version_linked = SDLNet_Linked_Version();
3745
3746   DrawTextF(xstart1, ystart, font_text, "SDL_net");
3747   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3748             sdl_version_compiled.major,
3749             sdl_version_compiled.minor,
3750             sdl_version_compiled.patch);
3751   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3752             sdl_version_linked->major,
3753             sdl_version_linked->minor,
3754             sdl_version_linked->patch);
3755   ystart += ystep_para;
3756