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