ad93781bee436baf95e965061c84db04b49550f9
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * screens.c                                                *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "screens.h"
17 #include "events.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "editor.h"
21 #include "files.h"
22 #include "tape.h"
23 #include "cartoons.h"
24 #include "network.h"
25 #include "init.h"
26
27 /* screens in the setup menu */
28 #define SETUP_MODE_MAIN                 0
29 #define SETUP_MODE_GAME                 1
30 #define SETUP_MODE_INPUT                2
31 #define SETUP_MODE_SHORTCUT             3
32 #define SETUP_MODE_GRAPHICS             4
33 #define SETUP_MODE_SOUND                5
34 #define SETUP_MODE_ARTWORK              6
35 #define SETUP_MODE_CHOOSE_GRAPHICS      7
36 #define SETUP_MODE_CHOOSE_SOUNDS        8
37 #define SETUP_MODE_CHOOSE_MUSIC         9
38
39 #define MAX_SETUP_MODES                 10
40
41 /* for input setup functions */
42 #define SETUPINPUT_SCREEN_POS_START     0
43 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
44 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
45 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
46
47 /* for various menu stuff  */
48 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - 2)
49 #define MENU_SCREEN_START_YPOS          2
50 #define MENU_SCREEN_VALUE_XPOS          14
51
52 /* buttons and scrollbars identifiers */
53 #define SCREEN_CTRL_ID_SCROLL_UP        0
54 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
55 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
56
57 #define NUM_SCREEN_SCROLLBUTTONS        2
58 #define NUM_SCREEN_SCROLLBARS           1
59 #define NUM_SCREEN_GADGETS              3
60
61 /* forward declarations of internal functions */
62 static void HandleScreenGadgets(struct GadgetInfo *);
63 static void HandleSetupScreen_Generic(int, int, int, int, int);
64 static void HandleSetupScreen_Input(int, int, int, int, int);
65 static void CustomizeKeyboard(int);
66 static void CalibrateJoystick(int);
67 static void execSetupArtwork(void);
68 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
69
70 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
71 static int setup_mode = SETUP_MODE_MAIN;
72
73 static void drawCursorExt(int pos, int color, int graphic)
74 {
75   static int cursor_array[SCR_FIELDY];
76
77   if (graphic)
78     cursor_array[pos] = graphic;
79
80   graphic = cursor_array[pos];
81
82   if (color == FC_RED)
83     graphic = (graphic == IMG_ARROW_BLUE_LEFT  ? IMG_ARROW_RED_LEFT  :
84                graphic == IMG_ARROW_BLUE_RIGHT ? IMG_ARROW_RED_RIGHT :
85                IMG_BALL_RED);
86
87   DrawGraphic(0, MENU_SCREEN_START_YPOS + pos, graphic, 0);
88 }
89
90 static void initCursor(int pos, int graphic)
91 {
92   drawCursorExt(pos, FC_BLUE, graphic);
93 }
94
95 static void drawCursor(int pos, int color)
96 {
97   drawCursorExt(pos, color, 0);
98 }
99
100 void DrawHeadline()
101 {
102   int x = SX + (SXSIZE - strlen(PROGRAM_TITLE_STRING) * FONT1_XSIZE) / 2;
103
104   DrawText(x, SY + 8, PROGRAM_TITLE_STRING, FS_BIG, FC_YELLOW);
105   DrawTextFCentered(46, FC_RED, WINDOW_SUBTITLE_STRING);
106 }
107
108 static void ToggleFullscreenIfNeeded()
109 {
110   if (setup.fullscreen != video.fullscreen_enabled)
111   {
112     /* save old door content */
113     BlitBitmap(backbuffer, bitmap_db_door,
114                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
115
116     /* toggle fullscreen */
117     ChangeVideoModeIfNeeded(setup.fullscreen);
118     setup.fullscreen = video.fullscreen_enabled;
119
120     /* redraw background to newly created backbuffer */
121     BlitBitmap(new_graphic_info[IMG_MENU_BACK].bitmap, backbuffer,
122                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
123
124     /* restore old door content */
125     BlitBitmap(bitmap_db_door, backbuffer,
126                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
127
128     redraw_mask = REDRAW_ALL;
129   }
130 }
131
132 void DrawMainMenu()
133 {
134   static LevelDirTree *leveldir_last_valid = NULL;
135   int i;
136   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
137
138   UnmapAllGadgets();
139   FadeSounds();
140   KeyboardAutoRepeatOn();
141   ActivateJoystick();
142   SetDrawDeactivationMask(REDRAW_NONE);
143   audio.sound_deactivated = FALSE;
144
145   /* needed if last screen was the playing screen, invoked from level editor */
146   if (level_editor_test_game)
147   {
148     game_status = LEVELED;
149     DrawLevelEd();
150     return;
151   }
152
153   /* needed if last screen was the editor screen */
154   UndrawSpecialEditorDoor();
155
156   /* needed if last screen was the setup screen and fullscreen state changed */
157   ToggleFullscreenIfNeeded();
158
159   /* needed if last screen (level choice) changed graphics, sounds or music */
160   ReloadCustomArtwork();
161
162 #ifdef TARGET_SDL
163   SetDrawtoField(DRAW_BACKBUFFER);
164 #endif
165
166   /* map gadgets for main menu screen */
167   MapTapeButtons();
168
169   /* leveldir_current may be invalid (level group, parent link) */
170   if (!validLevelSeries(leveldir_current))
171     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
172
173   /* store valid level series information */
174   leveldir_last_valid = leveldir_current;
175
176   /* level_nr may have been set to value over handicap with level editor */
177   if (setup.handicap && level_nr > leveldir_current->handicap_level)
178     level_nr = leveldir_current->handicap_level;
179
180   GetPlayerConfig();
181   LoadLevel(level_nr);
182
183   ClearWindow();
184   DrawHeadline();
185   DrawText(SX + 32,    SY + 2*32, name_text, FS_BIG, FC_GREEN);
186   DrawText(SX + 6*32,  SY + 2*32, setup.player_name, FS_BIG, FC_RED);
187   DrawText(SX + 32,    SY + 3*32, "Level:", FS_BIG, FC_GREEN);
188   DrawText(SX + 11*32, SY + 3*32, int2str(level_nr,3), FS_BIG,
189            (leveldir_current->readonly ? FC_RED : FC_YELLOW));
190   DrawText(SX + 32,    SY + 4*32, "Hall Of Fame", FS_BIG, FC_GREEN);
191   DrawText(SX + 32,    SY + 5*32, "Level Creator", FS_BIG, FC_GREEN);
192   DrawText(SY + 32,    SY + 6*32, "Info Screen", FS_BIG, FC_GREEN);
193   DrawText(SX + 32,    SY + 7*32, "Start Game", FS_BIG, FC_GREEN);
194   DrawText(SX + 32,    SY + 8*32, "Setup", FS_BIG, FC_GREEN);
195   DrawText(SX + 32,    SY + 9*32, "Quit", FS_BIG, FC_GREEN);
196
197   DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
198
199   DrawTextF(7*32 + 6, 3*32 + 9, FC_RED, "%d-%d",
200             leveldir_current->first_level,
201             leveldir_current->last_level);
202
203   if (leveldir_current->readonly)
204   {
205     DrawTextF(15*32 + 6, 3*32 + 9 - 7, FC_RED, "READ");
206     DrawTextF(15*32 + 6, 3*32 + 9 + 7, FC_RED, "ONLY");
207   }
208
209   for(i=0; i<8; i++)
210     initCursor(i, (i == 1 || i == 6 ? IMG_ARROW_BLUE_RIGHT : IMG_BALL_BLUE));
211
212   DrawGraphic(10, 3, IMG_ARROW_BLUE_LEFT, 0);
213   DrawGraphic(14, 3, IMG_ARROW_BLUE_RIGHT, 0);
214
215   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment",
216            FS_SMALL, FC_RED);
217
218   if (leveldir_current->name)
219   {
220     int len = strlen(leveldir_current->name);
221     int lxpos = SX + (SXSIZE - len * FONT4_XSIZE) / 2;
222     int lypos = SY + 352;
223
224     DrawText(lxpos, lypos, leveldir_current->name, FS_SMALL, FC_SPECIAL2);
225   }
226
227   FadeToFront();
228   InitAnimation();
229   HandleMainMenu(0,0, 0,0, MB_MENU_INITIALIZE);
230
231   TapeStop();
232   if (TAPE_IS_EMPTY(tape))
233     LoadTape(level_nr);
234   DrawCompleteVideoDisplay();
235
236   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
237
238 #if 0
239   ClearEventQueue();
240 #endif
241 }
242
243 static void gotoTopLevelDir()
244 {
245   /* move upwards to top level directory */
246   while (leveldir_current->node_parent)
247   {
248     /* write a "path" into level tree for easy navigation to last level */
249     if (leveldir_current->node_parent->node_group->cl_first == -1)
250     {
251       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
252       int leveldir_pos = posTreeInfo(leveldir_current);
253       int num_page_entries;
254       int cl_first, cl_cursor;
255
256       if (num_leveldirs <= MAX_MENU_ENTRIES_ON_SCREEN)
257         num_page_entries = num_leveldirs;
258       else
259         num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
260
261       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
262       cl_cursor = leveldir_pos - cl_first;
263
264       leveldir_current->node_parent->node_group->cl_first = cl_first;
265       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
266     }
267
268     leveldir_current = leveldir_current->node_parent;
269   }
270 }
271
272 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
273 {
274   static int choice = 0;
275   int x = 0;
276   int y = choice;
277
278   if (button == MB_MENU_INITIALIZE)
279   {
280     drawCursor(choice, FC_RED);
281     return;
282   }
283
284   if (mx || my)         /* mouse input */
285   {
286     x = (mx - SX) / 32;
287     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
288   }
289   else if (dx || dy)    /* keyboard input */
290   {
291     if (dx && choice == 1)
292       x = (dx < 0 ? 10 : 14);
293     else if (dy)
294       y = choice + dy;
295   }
296
297   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
298                  (x == 14 && level_nr < leveldir_current->last_level)) &&
299       button)
300   {
301     static unsigned long level_delay = 0;
302     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
303     int new_level_nr, old_level_nr = level_nr;
304     int font_color = (leveldir_current->readonly ? FC_RED : FC_YELLOW);
305
306     new_level_nr = level_nr + (x == 10 ? -step : +step);
307     if (new_level_nr < leveldir_current->first_level)
308       new_level_nr = leveldir_current->first_level;
309     if (new_level_nr > leveldir_current->last_level)
310       new_level_nr = leveldir_current->last_level;
311
312     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
313       new_level_nr = leveldir_current->handicap_level;
314
315     if (old_level_nr == new_level_nr ||
316         !DelayReached(&level_delay, GADGET_FRAME_DELAY))
317       goto out;
318
319     level_nr = new_level_nr;
320
321     DrawTextExt(drawto, SX + 11 * 32, SY + 3 * 32,
322                 int2str(level_nr, 3), FS_BIG, font_color);
323     DrawTextExt(window, SX + 11 * 32, SY + 3 * 32,
324                 int2str(level_nr, 3), FS_BIG, font_color);
325
326     LoadLevel(level_nr);
327     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
328
329     TapeErase();
330     LoadTape(level_nr);
331     DrawCompleteVideoDisplay();
332
333     /* needed because DrawMicroLevel() takes some time */
334     BackToFront();
335     SyncDisplay();
336     DelayReached(&level_delay, 0);      /* reset delay counter */
337   }
338   else if (x == 0 && y >= 0 && y <= 7)
339   {
340     if (button)
341     {
342       if (y != choice)
343       {
344         drawCursor(y, FC_RED);
345         drawCursor(choice, FC_BLUE);
346         choice = y;
347       }
348     }
349     else
350     {
351       if (y == 0)
352       {
353         game_status = TYPENAME;
354         HandleTypeName(strlen(setup.player_name), 0);
355       }
356       else if (y == 1)
357       {
358         if (leveldir_first)
359         {
360           game_status = CHOOSELEVEL;
361           SaveLevelSetup_LastSeries();
362           SaveLevelSetup_SeriesInfo();
363
364           gotoTopLevelDir();
365
366           DrawChooseLevel();
367         }
368       }
369       else if (y == 2)
370       {
371         game_status = HALLOFFAME;
372         DrawHallOfFame(-1);
373       }
374       else if (y == 3)
375       {
376         if (leveldir_current->readonly &&
377             strcmp(setup.player_name, "Artsoft") != 0)
378           Request("This level is read only !", REQ_CONFIRM);
379         game_status = LEVELED;
380         DrawLevelEd();
381       }
382       else if (y == 4)
383       {
384         game_status = HELPSCREEN;
385         DrawHelpScreen();
386       }
387       else if (y == 5)
388       {
389         if (setup.autorecord)
390           TapeStartRecording();
391
392 #if defined(PLATFORM_UNIX)
393         if (options.network)
394           SendToServer_StartPlaying();
395         else
396 #endif
397         {
398           game_status = PLAYING;
399           StopAnimation();
400           InitGame();
401         }
402       }
403       else if (y == 6)
404       {
405         game_status = SETUP;
406         setup_mode = SETUP_MODE_MAIN;
407         DrawSetupScreen();
408       }
409       else if (y == 7)
410       {
411         SaveLevelSetup_LastSeries();
412         SaveLevelSetup_SeriesInfo();
413         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
414           game_status = EXITGAME;
415       }
416     }
417   }
418
419   BackToFront();
420
421   out:
422
423   if (game_status == MAINMENU)
424   {
425     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, FALSE);
426     DoAnimation();
427   }
428 }
429
430
431 #define MAX_HELPSCREEN_ELS      10
432 #define HA_NEXT                 -999
433 #define HA_END                  -1000
434
435 static long helpscreen_state;
436 static int helpscreen_step[MAX_HELPSCREEN_ELS];
437 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
438 #if 0
439 static int OLD_helpscreen_action[] =
440 {
441   GFX_SPIELER1_DOWN,4,2,
442   GFX_SPIELER1_UP,4,2,
443   GFX_SPIELER1_LEFT,4,2,
444   GFX_SPIELER1_RIGHT,4,2,
445   GFX_SPIELER1_PUSH_LEFT,4,2,
446   GFX_SPIELER1_PUSH_RIGHT,4,2,                                  HA_NEXT,
447   GFX_ERDREICH,1,100,                                           HA_NEXT,
448   GFX_LEERRAUM,1,100,                                           HA_NEXT,
449   GFX_MORAST_LEER,1,100,                                        HA_NEXT,
450   GFX_BETON,1,100,                                              HA_NEXT,
451   GFX_MAUERWERK,1,100,                                          HA_NEXT,
452   GFX_MAUER_L1,  3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,
453   GFX_MAUER_R1,  3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,
454   GFX_MAUER_UP,  3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,
455   GFX_MAUER_DOWN,3,4, GFX_MAUERWERK,1,20, GFX_LEERRAUM,1,10,    HA_NEXT,
456   GFX_UNSICHTBAR,1,100,                                         HA_NEXT,
457   GFX_FELSBODEN,1,100,                                          HA_NEXT,
458   GFX_CHAR_A,30,4, GFX_CHAR_AUSRUF,32,4,                        HA_NEXT,
459   GFX_EDELSTEIN,2,5,                                            HA_NEXT,
460   GFX_DIAMANT,2,5,                                              HA_NEXT,
461   GFX_EDELSTEIN_BD,2,5,                                         HA_NEXT,
462   GFX_EDELSTEIN_GELB,2,5, GFX_EDELSTEIN_ROT,2,5,
463   GFX_EDELSTEIN_LILA,2,5,                                       HA_NEXT,
464   GFX_FELSBROCKEN,4,5,                                          HA_NEXT,
465   GFX_BOMBE,1,50, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,         HA_NEXT,
466   GFX_KOKOSNUSS,1,50, GFX_CRACKINGNUT,3,1, GFX_EDELSTEIN,1,10,  HA_NEXT,
467   GFX_ERZ_EDEL,1,50, GFX_EXPLOSION,8,1, GFX_EDELSTEIN,1,10,     HA_NEXT,
468   GFX_ERZ_DIAM,1,50, GFX_EXPLOSION,8,1, GFX_DIAMANT,1,10,       HA_NEXT,
469   GFX_ERZ_EDEL_BD,1,50, GFX_EXPLOSION,8,1,GFX_EDELSTEIN_BD,1,10,HA_NEXT,
470   GFX_ERZ_EDEL_GELB,1,50, GFX_EXPLOSION,8,1,
471   GFX_EDELSTEIN_GELB,1,10, GFX_ERZ_EDEL_ROT,1,50,
472   GFX_EXPLOSION,8,1, GFX_EDELSTEIN_ROT,1,10,
473   GFX_ERZ_EDEL_LILA,1,50, GFX_EXPLOSION,8,1,
474   GFX_EDELSTEIN_LILA,1,10,                                      HA_NEXT,
475   GFX_GEBLUBBER,4,4,                                            HA_NEXT,
476   GFX_SCHLUESSEL1,4,25,                                         HA_NEXT,
477   GFX_PFORTE1,4,25,                                             HA_NEXT,
478   GFX_PFORTE1X,4,25,                                            HA_NEXT,
479   GFX_DYNAMIT_AUS,1,100,                                        HA_NEXT,
480   GFX_DYNAMIT,7,6, GFX_EXPLOSION,8,1, GFX_LEERRAUM,1,10,        HA_NEXT,
481   GFX_DYNABOMB+0,4,3, GFX_DYNABOMB+3,1,3, GFX_DYNABOMB+2,1,3,
482   GFX_DYNABOMB+1,1,3, GFX_DYNABOMB+0,1,3, GFX_EXPLOSION,8,1,
483   GFX_LEERRAUM,1,10,                                            HA_NEXT,
484   GFX_DYNABOMB_NR,1,100,                                        HA_NEXT,
485   GFX_DYNABOMB_SZ,1,100,                                        HA_NEXT,
486   GFX_FLIEGER+4,1,3, GFX_FLIEGER+0,1,3, GFX_FLIEGER+4,1,3,
487   GFX_FLIEGER+5,1,3, GFX_FLIEGER+1,1,3, GFX_FLIEGER+5,1,3,
488   GFX_FLIEGER+6,1,3, GFX_FLIEGER+2,1,3, GFX_FLIEGER+6,1,3,
489   GFX_FLIEGER+7,1,3, GFX_FLIEGER+3,1,3, GFX_FLIEGER+7,1,3,      HA_NEXT,
490   GFX_KAEFER+4,1,1, GFX_KAEFER+0,1,1, GFX_KAEFER+4,1,1,
491   GFX_KAEFER+5,1,1, GFX_KAEFER+1,1,1, GFX_KAEFER+5,1,1,
492   GFX_KAEFER+6,1,1, GFX_KAEFER+2,1,1, GFX_KAEFER+6,1,1,
493   GFX_KAEFER+7,1,1, GFX_KAEFER+3,1,1, GFX_KAEFER+7,1,1,         HA_NEXT,
494   GFX_BUTTERFLY,2,2,                                            HA_NEXT,
495   GFX_FIREFLY,2,2,                                              HA_NEXT,
496   GFX_PACMAN+0,1,3, GFX_PACMAN+4,1,2, GFX_PACMAN+0,1,3,
497   GFX_PACMAN+1,1,3, GFX_PACMAN+5,1,2, GFX_PACMAN+1,1,3,
498   GFX_PACMAN+2,1,3, GFX_PACMAN+6,1,2, GFX_PACMAN+2,1,3,
499   GFX_PACMAN+3,1,3, GFX_PACMAN+7,1,2, GFX_PACMAN+3,1,3,         HA_NEXT,
500   GFX_MAMPFER+0,4,1, GFX_MAMPFER+3,1,1, GFX_MAMPFER+2,1,1,
501   GFX_MAMPFER+1,1,1, GFX_MAMPFER+0,1,1,                         HA_NEXT,
502   GFX_MAMPFER2+0,4,1, GFX_MAMPFER2+3,1,1, GFX_MAMPFER2+2,1,1,
503   GFX_MAMPFER2+1,1,1, GFX_MAMPFER2+0,1,1,                       HA_NEXT,
504   GFX_ROBOT+0,4,1, GFX_ROBOT+3,1,1, GFX_ROBOT+2,1,1,
505   GFX_ROBOT+1,1,1, GFX_ROBOT+0,1,1,                             HA_NEXT,
506   GFX_MOLE_DOWN,4,2,
507   GFX_MOLE_UP,4,2,
508   GFX_MOLE_LEFT,4,2,
509   GFX_MOLE_RIGHT,4,2,                                           HA_NEXT,
510   GFX_PINGUIN_DOWN,4,2,
511   GFX_PINGUIN_UP,4,2,
512   GFX_PINGUIN_LEFT,4,2,
513   GFX_PINGUIN_RIGHT,4,2,                                        HA_NEXT,
514   GFX_SCHWEIN_DOWN,4,2,
515   GFX_SCHWEIN_UP,4,2,
516   GFX_SCHWEIN_LEFT,4,2,
517   GFX_SCHWEIN_RIGHT,4,2,                                        HA_NEXT,
518   GFX_DRACHE_DOWN,4,2,
519   GFX_DRACHE_UP,4,2,
520   GFX_DRACHE_LEFT,4,2,
521   GFX_DRACHE_RIGHT,4,2,                                         HA_NEXT,
522   GFX_SONDE_START,8,1,                                          HA_NEXT,
523   GFX_ABLENK,4,1,                                               HA_NEXT,
524   GFX_BIRNE_AUS,1,25, GFX_BIRNE_EIN,1,25,                       HA_NEXT,
525   GFX_ZEIT_VOLL,1,25, GFX_ZEIT_LEER,1,25,                       HA_NEXT,
526   GFX_TROPFEN,1,25, GFX_AMOEBING,4,1, GFX_AMOEBE_LEBT,1,10,     HA_NEXT,
527   GFX_AMOEBE_TOT+2,2,50, GFX_AMOEBE_TOT,2,50,                   HA_NEXT,
528   GFX_AMOEBE_LEBT,4,40,                                         HA_NEXT,
529   GFX_AMOEBE_LEBT,1,10, GFX_AMOEBING,4,2,                       HA_NEXT,
530   GFX_AMOEBE_LEBT,1,25, GFX_AMOEBE_TOT,1,25, GFX_EXPLOSION,8,1,
531   GFX_DIAMANT,1,10,                                             HA_NEXT,
532   GFX_LIFE,1,100,                                               HA_NEXT,
533   GFX_LIFE_ASYNC,1,100,                                         HA_NEXT,
534   GFX_MAGIC_WALL_OFF,4,2,                                       HA_NEXT,
535   GFX_MAGIC_WALL_BD_OFF,4,2,                                    HA_NEXT,
536   GFX_AUSGANG_ZU,1,100, GFX_AUSGANG_ACT,4,2,
537   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
538   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
539   GFX_AUSGANG_AUF+0,4,2, GFX_AUSGANG_AUF+3,1,2,
540   GFX_AUSGANG_AUF+2,1,2, GFX_AUSGANG_AUF+1,1,2,                 HA_NEXT,
541   GFX_SOKOBAN_OBJEKT,1,100,                                     HA_NEXT,
542   GFX_SOKOBAN_FELD_LEER,1,100,                                  HA_NEXT,
543   GFX_SOKOBAN_FELD_VOLL,1,100,                                  HA_NEXT,
544   GFX_SPEED_PILL,1,100,                                         HA_NEXT,
545   HA_END
546 };
547 #endif
548
549 static int helpscreen_action[] =
550 {
551   IMG_PLAYER1_DOWN_MOVING,              16,
552   IMG_PLAYER1_UP_MOVING,                16,
553   IMG_PLAYER1_LEFT_MOVING,              16,
554   IMG_PLAYER1_RIGHT_MOVING,             16,
555   IMG_PLAYER1_LEFT_PUSHING,             16,
556   IMG_PLAYER1_RIGHT_PUSHING,            16,                     HA_NEXT,
557
558   IMG_SAND,                             -1,                     HA_NEXT,
559
560   IMG_EMPTY_SPACE,                      -1,                     HA_NEXT,
561
562   IMG_QUICKSAND_EMPTY,                  -1,                     HA_NEXT,
563
564   IMG_STEELWALL,                        -1,                     HA_NEXT,
565
566   IMG_WALL,                             -1,                     HA_NEXT,
567
568   IMG_WALL_GROWING_ACTIVE_LEFT,         20,
569   IMG_WALL,                             50,
570   IMG_EMPTY_SPACE,                      20,
571   IMG_WALL_GROWING_ACTIVE_RIGHT,        20,
572   IMG_WALL,                             50,
573   IMG_EMPTY_SPACE,                      20,
574   IMG_WALL_GROWING_ACTIVE_UP,           20,
575   IMG_WALL,                             50,
576   IMG_EMPTY_SPACE,                      20,
577   IMG_WALL_GROWING_ACTIVE_DOWN,         20,
578   IMG_WALL,                             50,
579   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
580
581   IMG_INVISIBLE_WALL,                   -1,                     HA_NEXT,
582
583   IMG_WALL_CRUMBLED,                    -1,                     HA_NEXT,
584
585   IMG_INFO_FONT_EM_1,                   160,
586   IMG_INFO_FONT_EM_2,                   160,
587   IMG_INFO_FONT_EM_3,                   160,
588   IMG_INFO_FONT_EM_4,                   160,
589   IMG_INFO_FONT_EM_5,                   40,                     HA_NEXT,
590
591   IMG_EMERALD,                          -1,                     HA_NEXT,
592
593   IMG_DIAMOND,                          -1,                     HA_NEXT,
594
595   IMG_BD_DIAMOND,                       -1,                     HA_NEXT,
596
597   IMG_EMERALD_YELLOW,                   50,
598   IMG_EMERALD_RED,                      50,
599   IMG_EMERALD_PURPLE,                   50,                     HA_NEXT,
600
601   IMG_BD_ROCK,                          -1,                     HA_NEXT,
602
603   IMG_BOMB,                             100,
604   IMG_EXPLOSION,                        16,
605   IMG_EMPTY_SPACE,                      10,                     HA_NEXT,
606
607   IMG_NUT,                              100,
608   IMG_NUT_CRACKING,                     6,
609   IMG_EMERALD,                          20,                     HA_NEXT,
610
611   IMG_WALL_EMERALD,                     100,
612   IMG_EXPLOSION,                        16,
613   IMG_EMERALD,                          20,                     HA_NEXT,
614
615   IMG_WALL_DIAMOND,                     100,
616   IMG_EXPLOSION,                        16,
617   IMG_DIAMOND,                          20,                     HA_NEXT,
618
619   IMG_WALL_BD_DIAMOND,                  100,
620   IMG_EXPLOSION,                        16,
621   IMG_BD_DIAMOND,                       20,                     HA_NEXT,
622
623   IMG_WALL_EMERALD_YELLOW,              100,
624   IMG_EXPLOSION,                        16,
625   IMG_EMERALD_YELLOW,                   20,
626   IMG_WALL_EMERALD_RED,                 100,
627   IMG_EXPLOSION,                        16,
628   IMG_EMERALD_RED,                      20,
629   IMG_WALL_EMERALD_PURPLE,              100,
630   IMG_EXPLOSION,                        16,
631   IMG_EMERALD_PURPLE,                   20,                     HA_NEXT,
632
633   IMG_ACID,                             -1,                     HA_NEXT,
634
635   IMG_KEY1,                             50,
636   IMG_KEY2,                             50,
637   IMG_KEY3,                             50,
638   IMG_KEY4,                             50,                     HA_NEXT,
639
640   IMG_GATE1,                            50,
641   IMG_GATE2,                            50,
642   IMG_GATE3,                            50,
643   IMG_GATE4,                            50,                     HA_NEXT,
644
645   IMG_GATE1_GRAY,                       50,
646   IMG_GATE2_GRAY,                       50,
647   IMG_GATE3_GRAY,                       50,
648   IMG_GATE4_GRAY,                       50,                     HA_NEXT,
649
650   IMG_DYNAMITE,                         -1,                     HA_NEXT,
651
652   IMG_DYNAMITE_ACTIVE,                  96,
653   IMG_EXPLOSION,                        16,
654   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
655
656   IMG_DYNABOMB_ACTIVE,                  100,
657   IMG_EXPLOSION,                        16,
658   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
659
660   IMG_DYNABOMB_NR,                      -1,                     HA_NEXT,
661
662   IMG_DYNABOMB_SZ,                      -1,                     HA_NEXT,
663
664   IMG_SPACESHIP_RIGHT,                  16,
665   IMG_SPACESHIP_UP,                     16,
666   IMG_SPACESHIP_LEFT,                   16,
667   IMG_SPACESHIP_DOWN,                   16,                     HA_NEXT,
668
669   IMG_BUG_RIGHT,                        16,
670   IMG_BUG_UP,                           16,
671   IMG_BUG_LEFT,                         16,
672   IMG_BUG_DOWN,                         16,                     HA_NEXT,
673
674   IMG_BD_BUTTERFLY,                     -1,                     HA_NEXT,
675
676   IMG_BD_FIREFLY,                       -1,                     HA_NEXT,
677
678   IMG_PACMAN_RIGHT,                     16,
679   IMG_PACMAN_UP,                        16,
680   IMG_PACMAN_LEFT,                      16,
681   IMG_PACMAN_DOWN,                      16,                     HA_NEXT,
682
683   IMG_YAMYAM,                           -1,                     HA_NEXT,
684
685   IMG_DARK_YAMYAM,                      -1,                     HA_NEXT,
686
687   IMG_ROBOT,                            -1,                     HA_NEXT,
688
689   IMG_MOLE_RIGHT_MOVING,                16,
690   IMG_MOLE_UP_MOVING,                   16,
691   IMG_MOLE_LEFT_MOVING,                 16,
692   IMG_MOLE_DOWN_MOVING,                 16,                     HA_NEXT,
693
694   IMG_PENGUIN_RIGHT_MOVING,             16,
695   IMG_PENGUIN_UP_MOVING,                16,
696   IMG_PENGUIN_LEFT_MOVING,              16,
697   IMG_PENGUIN_DOWN_MOVING,              16,                     HA_NEXT,
698
699   IMG_PIG_RIGHT_MOVING,                 16,
700   IMG_PIG_UP_MOVING,                    16,
701   IMG_PIG_LEFT_MOVING,                  16,
702   IMG_PIG_DOWN_MOVING,                  16,                     HA_NEXT,
703
704   IMG_DRAGON_RIGHT_MOVING,              16,
705   IMG_DRAGON_UP_MOVING,                 16,
706   IMG_DRAGON_LEFT_MOVING,               16,
707   IMG_DRAGON_DOWN_MOVING,               16,                     HA_NEXT,
708
709   IMG_SATELLITE,                        -1,                     HA_NEXT,
710
711   IMG_ROBOT_WHEEL,                      50,
712   IMG_ROBOT_WHEEL_ACTIVE,               100,                    HA_NEXT,
713
714   IMG_LAMP,                             50,
715   IMG_LAMP_ACTIVE,                      50,                     HA_NEXT,
716
717   IMG_TIME_ORB_FULL,                    50,
718   IMG_TIME_ORB_EMPTY,                   50,                     HA_NEXT,
719
720   IMG_AMOEBA_DROP,                      50,
721   IMG_AMOEBA_CREATING,                  6,
722   IMG_AMOEBA_WET,                       20,                     HA_NEXT,
723
724   IMG_AMOEBA_DEAD,                      -1,                     HA_NEXT,
725
726   IMG_AMOEBA_WET,                       -1,                     HA_NEXT,
727
728   IMG_AMOEBA_WET,                       100,
729   IMG_AMOEBA_CREATING,                  6,                      HA_NEXT,
730
731   IMG_AMOEBA_FULL,                      50,
732   IMG_AMOEBA_DEAD,                      50,
733   IMG_EXPLOSION,                        16,
734   IMG_DIAMOND,                          20,                     HA_NEXT,
735
736   IMG_GAMEOFLIFE,                       -1,                     HA_NEXT,
737
738   IMG_BIOMAZE,                          -1,                     HA_NEXT,
739
740   IMG_MAGIC_WALL_ACTIVE,                -1,                     HA_NEXT,
741
742   IMG_BD_MAGIC_WALL_ACTIVE,             -1,                     HA_NEXT,
743
744   IMG_EXIT_CLOSED,                      200,
745   IMG_EXIT_OPENING,                     16,
746   IMG_EXIT_OPEN,                        100,                    HA_NEXT,
747
748   IMG_EXIT_OPEN,                        -1,                     HA_NEXT,
749
750   IMG_SOKOBAN_OBJECT,                   -1,                     HA_NEXT,
751
752   IMG_SOKOBAN_FIELD_EMPTY,              -1,                     HA_NEXT,
753
754   IMG_SOKOBAN_FIELD_FULL,               -1,                     HA_NEXT,
755
756   IMG_SPEED_PILL,                       -1,                     HA_NEXT,
757
758   HA_END
759 };
760 static char *helpscreen_eltext[][2] =
761 {
762  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
763  {"Normal sand:",                       "You can dig through it"},
764  {"Empty field:",                       "You can walk through it"},
765  {"Quicksand: You cannot pass it,",     "but rocks can fall through it"},
766  {"Massive Wall:",                      "Nothing can go through it"},
767  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
768  {"Growing Wall: Grows in several di-", "rections if there is an empty field"},
769  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
770  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
771  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
772  {"Emerald: You must collect enough of","them to finish a level"},
773  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
774  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
775  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
776  {"Rock: Smashes several things;",      "Can be moved by the player"},
777  {"Bomb: You can move it, but be",      "careful when dropping it"},
778  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
779  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
780  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
781  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
782  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
783  {"Acid: Things that fall in are gone", "forever (including our hero)"},
784  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
785  {"Door: Can be opened by the key",     "with the same color"},
786  {"Door: You have to find out the",     "right color of the key for it"},
787  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
788  {"Dynamite: This one explodes after",  "a few seconds"},
789  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
790  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
791  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
792  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
793  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
794  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
795  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
796  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
797  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
798  {"Cruncher (BD style):",               "Eats almost everything"},
799  {"Robot: Tries to kill the player",    ""},
800  {"The mole: Eats the amoeba and turns","empty space into normal sand"},
801  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
802  {"The Pig: Harmless, but eats all",    "gems it can get"},
803  {"The Dragon: Breathes fire,",         "especially to some monsters"},
804  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
805  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
806  {"Light Bulb: All of them must be",    "switched on to finish a level"},
807  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
808  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
809  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
810  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
811  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
812  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
813  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
814  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
815  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
816  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
817  {"Exit door: Opens if you have enough","emeralds to finish the level"},
818  {"Open exit door: Enter here to leave","the level and exit the actual game"},
819  {"Sokoban element: Object which must", "be pushed to an empty field"},
820  {"Sokoban element: Empty field where", "a Sokoban object can be placed on"},
821  {"Sokoban element: Field with object", "which can be pushed away"},
822  {"Speed pill: Lets the player run",    "twice as fast as normally"},
823 };
824 static int num_helpscreen_els = sizeof(helpscreen_eltext) / (2*sizeof(char *));
825
826 static char *helpscreen_music[][3] =
827 {
828   { "Alchemy",                  "Ian Boddy",            "Drive" },
829   { "The Chase",                "Propaganda",           "A Secret Wish" },
830   { "Network 23",               "Tangerine Dream",      "Exit" },
831   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
832   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
833   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
834   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
835 };
836 static int num_helpscreen_music = 7;
837 static int helpscreen_musicpos;
838
839 #if 0
840 void OLD_DrawHelpScreenElAction(int start)
841 {
842   int i = 0, j = 0;
843   int frame, graphic;
844   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
845
846   while(helpscreen_action[j] != HA_END)
847   {
848     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
849       break;
850     else if (i<start || helpscreen_delay[i-start])
851     {
852       if (i>=start && helpscreen_delay[i-start])
853         helpscreen_delay[i-start]--;
854
855       while(helpscreen_action[j] != HA_NEXT)
856         j++;
857       j++;
858       i++;
859       continue;
860     }
861
862     j += 3*helpscreen_step[i-start];
863     graphic = helpscreen_action[j++];
864
865     if (helpscreen_frame[i-start])
866     {
867       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
868       helpscreen_frame[i-start]--;
869     }
870     else
871     {
872       frame = 0;
873       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
874     }
875
876     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
877
878     if (helpscreen_action[j] == HA_NEXT)
879     {
880       if (!helpscreen_frame[i-start])
881         helpscreen_step[i-start] = 0;
882     }
883     else
884     {
885       if (!helpscreen_frame[i-start])
886         helpscreen_step[i-start]++;
887       while(helpscreen_action[j] != HA_NEXT)
888         j++;
889     }
890     j++;
891
892     DrawOldGraphicExt(drawto, xstart, ystart+(i-start)*ystep, graphic+frame);
893     i++;
894   }
895
896   for(i=2;i<16;i++)
897   {
898     MarkTileDirty(0,i);
899     MarkTileDirty(1,i);
900   }
901 }
902 #endif
903
904 void DrawHelpScreenElAction(int start)
905 {
906   int i = 0, j = 0;
907   int xstart = SX + 16;
908   int ystart = SY + 64 + 2 * 32;
909   int ystep = TILEY + 4;
910   int graphic;
911   int frame_count;
912   int sync_frame;
913   int frame;
914
915   while (helpscreen_action[j] != HA_END)
916   {
917     if (i >= start + MAX_HELPSCREEN_ELS || i >= num_helpscreen_els)
918       break;
919     else if (i < start)
920     {
921       while (helpscreen_action[j] != HA_NEXT)
922         j++;
923
924       j++;
925       i++;
926
927       continue;
928     }
929
930     j += 2 * helpscreen_step[i-start];
931     graphic = helpscreen_action[j++];
932     frame_count = helpscreen_action[j++];
933     if (frame_count == -1)
934       frame_count = 1000000;
935
936     if (helpscreen_frame[i-start] == 0)
937     {
938       sync_frame = 0;
939       helpscreen_frame[i-start] = frame_count - 1;
940     }
941     else
942     {
943       sync_frame = frame_count - helpscreen_frame[i-start];
944       helpscreen_frame[i-start]--;
945     }
946
947     if (helpscreen_action[j] == HA_NEXT)
948     {
949       if (!helpscreen_frame[i-start])
950         helpscreen_step[i-start] = 0;
951     }
952     else
953     {
954       if (!helpscreen_frame[i-start])
955         helpscreen_step[i-start]++;
956       while(helpscreen_action[j] != HA_NEXT)
957         j++;
958     }
959     j++;
960
961     frame = getGraphicAnimationFrame(graphic, sync_frame);
962
963     DrawGraphicExt(drawto, xstart, ystart + (i-start) * ystep,
964                    graphic, frame);
965
966     i++;
967   }
968
969   for(i=2; i<16; i++)
970   {
971     MarkTileDirty(0, i);
972     MarkTileDirty(1, i);
973   }
974
975   FrameCounter++;
976 }
977
978 void DrawHelpScreenElText(int start)
979 {
980   int i;
981   int xstart = SX + 56, ystart = SY + 65 + 2 * 32, ystep = TILEY + 4;
982   int ybottom = SYSIZE - 20;
983
984   ClearWindow();
985   DrawHeadline();
986
987   DrawTextFCentered(100, FC_GREEN, "The game elements:");
988
989   for(i=start; i < start + MAX_HELPSCREEN_ELS && i < num_helpscreen_els; i++)
990   {
991     DrawText(xstart,
992              ystart + (i - start) * ystep + (*helpscreen_eltext[i][1] ? 0 : 8),
993              helpscreen_eltext[i][0], FS_SMALL, FC_YELLOW);
994     DrawText(xstart, ystart + (i - start) * ystep + 16,
995              helpscreen_eltext[i][1], FS_SMALL, FC_YELLOW);
996   }
997
998   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
999 }
1000
1001 void DrawHelpScreenMusicText(int num)
1002 {
1003   int ystart = 150, ystep = 30;
1004   int ybottom = SYSIZE - 20;
1005
1006   FadeSounds();
1007   ClearWindow();
1008   DrawHeadline();
1009
1010   DrawTextFCentered(100, FC_GREEN, "The game background music loops:");
1011
1012   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
1013                     "Excerpt from");
1014   DrawTextFCentered(ystart + 1 * ystep, FC_RED, "\"%s\"",
1015                     helpscreen_music[num][0]);
1016   DrawTextFCentered(ystart + 2 * ystep, FC_YELLOW,
1017                     "by");
1018   DrawTextFCentered(ystart + 3 * ystep, FC_RED,
1019                     "%s", helpscreen_music[num][1]);
1020   DrawTextFCentered(ystart + 4 * ystep, FC_YELLOW,
1021                     "from the album");
1022   DrawTextFCentered(ystart + 5 * ystep, FC_RED, "\"%s\"",
1023                     helpscreen_music[num][2]);
1024
1025   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
1026
1027 #if 0
1028   PlaySoundLoop(background_loop[num]);
1029 #endif
1030 }
1031
1032 void DrawHelpScreenCreditsText()
1033 {
1034   int ystart = 150, ystep = 30;
1035   int ybottom = SYSIZE - 20;
1036
1037   FadeSounds();
1038   ClearWindow();
1039   DrawHeadline();
1040
1041   DrawTextFCentered(100, FC_GREEN,
1042                     "Credits:");
1043   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
1044                     "DOS port of the game:");
1045   DrawTextFCentered(ystart + 1 * ystep, FC_RED,
1046                     "Guido Schulz");
1047   DrawTextFCentered(ystart + 2 * ystep, FC_YELLOW,
1048                     "Additional toons:");
1049   DrawTextFCentered(ystart + 3 * ystep, FC_RED,
1050                     "Karl Hörnell");
1051   DrawTextFCentered(ystart + 5 * ystep, FC_YELLOW,
1052                     "...and many thanks to all contributors");
1053   DrawTextFCentered(ystart + 6 * ystep, FC_YELLOW,
1054                     "of new levels!");
1055
1056   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for next page");
1057 }
1058
1059 void DrawHelpScreenContactText()
1060 {
1061   int ystart = 150, ystep = 30;
1062   int ybottom = SYSIZE - 20;
1063
1064   ClearWindow();
1065   DrawHeadline();
1066
1067   DrawTextFCentered(100, FC_GREEN, "Program information:");
1068
1069   DrawTextFCentered(ystart + 0 * ystep, FC_YELLOW,
1070                     "This game is Freeware!");
1071   DrawTextFCentered(ystart + 1 * ystep, FC_YELLOW,
1072                     "If you like it, send e-mail to:");
1073   DrawTextFCentered(ystart + 2 * ystep, FC_RED,
1074                     "info@artsoft.org");
1075   DrawTextFCentered(ystart + 3 * ystep, FC_YELLOW,
1076                     "or SnailMail to:");
1077   DrawTextFCentered(ystart + 4 * ystep + 0, FC_RED,
1078                     "Holger Schemel");
1079   DrawTextFCentered(ystart + 4 * ystep + 20, FC_RED,
1080                     "Detmolder Strasse 189");
1081   DrawTextFCentered(ystart + 4 * ystep + 40, FC_RED,
1082                     "33604 Bielefeld");
1083   DrawTextFCentered(ystart + 4 * ystep + 60, FC_RED,
1084                     "Germany");
1085
1086   DrawTextFCentered(ystart + 7 * ystep, FC_YELLOW,
1087                     "If you have created new levels,");
1088   DrawTextFCentered(ystart + 8 * ystep, FC_YELLOW,
1089                     "send them to me to include them!");
1090   DrawTextFCentered(ystart + 9 * ystep, FC_YELLOW,
1091                     ":-)");
1092
1093   DrawTextFCentered(ybottom, FC_BLUE, "Press any key or button for main menu");
1094 }
1095
1096 void DrawHelpScreen()
1097 {
1098   int i;
1099
1100   UnmapAllGadgets();
1101   CloseDoor(DOOR_CLOSE_2);
1102
1103   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1104     helpscreen_step[i] = helpscreen_frame[i] = 0;
1105   helpscreen_musicpos = 0;
1106   helpscreen_state = 0;
1107   DrawHelpScreenElText(0);
1108   DrawHelpScreenElAction(0);
1109
1110   FadeToFront();
1111   InitAnimation();
1112   PlaySoundLoop(SND_MENU_INFO_SCREEN);
1113 }
1114
1115 void HandleHelpScreen(int button)
1116 {
1117   static unsigned long hs_delay = 0;
1118   int num_helpscreen_els_pages =
1119     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
1120   int button_released = !button;
1121   int i;
1122
1123   if (button_released)
1124   {
1125     if (helpscreen_state < num_helpscreen_els_pages - 1)
1126     {
1127       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1128         helpscreen_step[i] = helpscreen_frame[i] = 0;
1129       helpscreen_state++;
1130
1131       FrameCounter = 0;
1132       DrawHelpScreenElText(helpscreen_state * MAX_HELPSCREEN_ELS);
1133       DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1134     }
1135     else if (helpscreen_state <
1136              num_helpscreen_els_pages + num_helpscreen_music - 1)
1137     {
1138       helpscreen_state++;
1139       DrawHelpScreenMusicText(helpscreen_state - num_helpscreen_els_pages);
1140     }
1141     else if (helpscreen_state ==
1142              num_helpscreen_els_pages + num_helpscreen_music - 1)
1143     {
1144       helpscreen_state++;
1145       DrawHelpScreenCreditsText();
1146     }
1147     else if (helpscreen_state ==
1148              num_helpscreen_els_pages + num_helpscreen_music)
1149     {
1150       helpscreen_state++;
1151       DrawHelpScreenContactText();
1152     }
1153     else
1154     {
1155       FadeSounds();
1156       DrawMainMenu();
1157       game_status = MAINMENU;
1158     }
1159   }
1160   else
1161   {
1162     if (DelayReached(&hs_delay, GAME_FRAME_DELAY))
1163     {
1164       if (helpscreen_state < num_helpscreen_els_pages)
1165         DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1166     }
1167
1168     /* !!! workaround for playing "music" that is really a sound loop (and
1169        must therefore periodically be reactivated with the current sound
1170        engine !!! */
1171     PlaySoundLoop(SND_MENU_INFO_SCREEN);
1172
1173     DoAnimation();
1174   }
1175
1176   BackToFront();
1177 }
1178
1179 void HandleTypeName(int newxpos, Key key)
1180 {
1181   static int xpos = 0, ypos = 2;
1182
1183   if (newxpos)
1184   {
1185     xpos = newxpos;
1186     DrawText(SX + 6*32, SY + ypos*32, setup.player_name, FS_BIG, FC_YELLOW);
1187     DrawGraphic(xpos + 6, ypos, IMG_BALL_RED, 0);
1188     return;
1189   }
1190
1191   if (((key >= KSYM_A && key <= KSYM_Z) ||
1192        (key >= KSYM_a && key <= KSYM_z)) && 
1193       xpos < MAX_PLAYER_NAME_LEN)
1194   {
1195     char ascii;
1196
1197     if (key >= KSYM_A && key <= KSYM_Z)
1198       ascii = 'A' + (char)(key - KSYM_A);
1199     else
1200       ascii = 'a' + (char)(key - KSYM_a);
1201
1202     setup.player_name[xpos] = ascii;
1203     setup.player_name[xpos + 1] = 0;
1204     xpos++;
1205     DrawTextExt(drawto, SX + 6*32, SY + ypos*32,
1206                 setup.player_name, FS_BIG, FC_YELLOW);
1207     DrawTextExt(window, SX + 6*32, SY + ypos*32,
1208                 setup.player_name, FS_BIG, FC_YELLOW);
1209     DrawGraphic(xpos + 6, ypos, IMG_BALL_RED, 0);
1210   }
1211   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1212   {
1213     xpos--;
1214     setup.player_name[xpos] = 0;
1215     DrawGraphic(xpos + 6, ypos, IMG_BALL_RED, 0);
1216     DrawGraphic(xpos + 7, ypos, IMG_EMPTY, 0);
1217   }
1218   else if (key == KSYM_Return && xpos > 0)
1219   {
1220     DrawText(SX + 6*32, SY + ypos*32, setup.player_name, FS_BIG, FC_RED);
1221     DrawGraphic(xpos + 6, ypos, IMG_EMPTY, 0);
1222
1223     SaveSetup();
1224     game_status = MAINMENU;
1225   }
1226
1227   BackToFront();
1228 }
1229
1230 static void DrawChooseTree(TreeInfo **ti_ptr)
1231 {
1232   UnmapAllGadgets();
1233   CloseDoor(DOOR_CLOSE_2);
1234
1235   ClearWindow();
1236   HandleChooseTree(0,0, 0,0, MB_MENU_INITIALIZE, ti_ptr);
1237   MapChooseTreeGadgets(*ti_ptr);
1238
1239   FadeToFront();
1240   InitAnimation();
1241 }
1242
1243 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1244 {
1245   struct GadgetInfo *gi = screen_gadget[id];
1246   int items_max, items_visible, item_position;
1247
1248   items_max = numTreeInfoInGroup(ti);
1249   items_visible = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1250   item_position = first_entry;
1251
1252   if (item_position > items_max - items_visible)
1253     item_position = items_max - items_visible;
1254
1255   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1256                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1257 }
1258
1259 static void drawChooseTreeList(int first_entry, int num_page_entries,
1260                                TreeInfo *ti)
1261 {
1262   int i;
1263   char buffer[SCR_FIELDX * 2];
1264   int max_buffer_len = (SCR_FIELDX - 2) * 2;
1265   int num_entries = numTreeInfoInGroup(ti);
1266   char *title_string = NULL;
1267   int offset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : 16);
1268
1269   ClearRectangle(backbuffer, SX, SY, SXSIZE - 32, SYSIZE);
1270   redraw_mask |= REDRAW_FIELD;
1271
1272   title_string =
1273     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Directories" :
1274      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1275      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1276      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1277
1278   DrawText(SX + offset, SY + offset, title_string, FS_BIG,
1279            (ti->type == TREE_TYPE_LEVEL_DIR ? FC_GREEN : FC_YELLOW));
1280
1281   for(i=0; i<num_page_entries; i++)
1282   {
1283     TreeInfo *node, *node_first;
1284     int entry_pos = first_entry + i;
1285     int ypos = MENU_SCREEN_START_YPOS + i;
1286
1287     node_first = getTreeInfoFirstGroupEntry(ti);
1288     node = getTreeInfoFromPos(node_first, entry_pos);
1289
1290     strncpy(buffer, node->name , max_buffer_len);
1291     buffer[max_buffer_len] = '\0';
1292
1293     DrawText(SX + 32, SY + ypos * 32, buffer, FS_MEDIUM, node->color);
1294
1295     if (node->parent_link)
1296       initCursor(i, IMG_ARROW_BLUE_LEFT);
1297     else if (node->level_group)
1298       initCursor(i, IMG_ARROW_BLUE_RIGHT);
1299     else
1300       initCursor(i, IMG_BALL_BLUE);
1301   }
1302
1303   if (first_entry > 0)
1304     DrawGraphic(0, 1, IMG_ARROW_BLUE_UP, 0);
1305
1306   if (first_entry + num_page_entries < num_entries)
1307     DrawGraphic(0, MAX_MENU_ENTRIES_ON_SCREEN + 1, IMG_ARROW_BLUE_DOWN, 0);
1308 }
1309
1310 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1311 {
1312   TreeInfo *node, *node_first;
1313   int x, last_redraw_mask = redraw_mask;
1314
1315   if (ti->type != TREE_TYPE_LEVEL_DIR)
1316     return;
1317
1318   node_first = getTreeInfoFirstGroupEntry(ti);
1319   node = getTreeInfoFromPos(node_first, entry_pos);
1320
1321   ClearRectangle(drawto, SX + 32, SY + 32, SXSIZE - 64, 32);
1322
1323   if (node->parent_link)
1324     DrawTextFCentered(40, FC_RED, "leave group \"%s\"", node->class_desc);
1325   else if (node->level_group)
1326     DrawTextFCentered(40, FC_RED, "enter group \"%s\"", node->class_desc);
1327   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1328     DrawTextFCentered(40, FC_RED, "%3d levels (%s)",
1329                       node->levels, node->class_desc);
1330
1331   /* let BackToFront() redraw only what is needed */
1332   redraw_mask = last_redraw_mask | REDRAW_TILES;
1333   for (x=0; x<SCR_FIELDX; x++)
1334     MarkTileDirty(x, 1);
1335 }
1336
1337 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1338                              TreeInfo **ti_ptr)
1339 {
1340   static unsigned long choose_delay = 0;
1341   TreeInfo *ti = *ti_ptr;
1342   int x = 0;
1343   int y = ti->cl_cursor;
1344   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1345   int num_entries = numTreeInfoInGroup(ti);
1346   int num_page_entries;
1347
1348   if (num_entries <= MAX_MENU_ENTRIES_ON_SCREEN)
1349     num_page_entries = num_entries;
1350   else
1351     num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1352
1353   if (button == MB_MENU_INITIALIZE)
1354   {
1355     int entry_pos = posTreeInfo(ti);
1356
1357     if (ti->cl_first == -1)
1358     {
1359       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1360       ti->cl_cursor =
1361         entry_pos - ti->cl_first;
1362     }
1363
1364     if (dx == 999)      /* first entry is set by scrollbar position */
1365       ti->cl_first = dy;
1366     else
1367       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1368                                 ti->cl_first, ti);
1369
1370     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1371     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1372     drawCursor(ti->cl_cursor, FC_RED);
1373     return;
1374   }
1375   else if (button == MB_MENU_LEAVE)
1376   {
1377     if (ti->node_parent)
1378     {
1379       *ti_ptr = ti->node_parent;
1380       DrawChooseTree(ti_ptr);
1381     }
1382     else if (game_status == SETUP)
1383     {
1384       execSetupArtwork();
1385     }
1386     else
1387     {
1388       game_status = MAINMENU;
1389       DrawMainMenu();
1390     }
1391
1392     return;
1393   }
1394
1395   if (mx || my)         /* mouse input */
1396   {
1397     x = (mx - SX) / 32;
1398     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
1399   }
1400   else if (dx || dy)    /* keyboard input */
1401   {
1402     if (dy)
1403       y = ti->cl_cursor + dy;
1404
1405     if (ABS(dy) == SCR_FIELDY)  /* handle KSYM_Page_Up, KSYM_Page_Down */
1406     {
1407       dy = SIGN(dy);
1408       step = num_page_entries - 1;
1409       y = (dy < 0 ? -1 : num_page_entries);
1410     }
1411   }
1412
1413   if (x == 0 && y == -1)
1414   {
1415     if (ti->cl_first > 0 &&
1416         (dy || DelayReached(&choose_delay, GADGET_FRAME_DELAY)))
1417     {
1418       ti->cl_first -= step;
1419       if (ti->cl_first < 0)
1420         ti->cl_first = 0;
1421
1422       drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1423       drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1424       drawCursor(ti->cl_cursor, FC_RED);
1425       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1426                                 ti->cl_first, ti);
1427       return;
1428     }
1429   }
1430   else if (x == 0 && y > num_page_entries - 1)
1431   {
1432     if (ti->cl_first + num_page_entries < num_entries &&
1433         (dy || DelayReached(&choose_delay, GADGET_FRAME_DELAY)))
1434     {
1435       ti->cl_first += step;
1436       if (ti->cl_first + num_page_entries > num_entries)
1437         ti->cl_first = MAX(0, num_entries - num_page_entries);
1438
1439       drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1440       drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1441       drawCursor(ti->cl_cursor, FC_RED);
1442       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1443                                 ti->cl_first, ti);
1444       return;
1445     }
1446   }
1447
1448   if (dx == 1)
1449   {
1450     TreeInfo *node_first, *node_cursor;
1451     int entry_pos = ti->cl_first + y;
1452
1453     node_first = getTreeInfoFirstGroupEntry(ti);
1454     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1455
1456     if (node_cursor->node_group)
1457     {
1458       node_cursor->cl_first = ti->cl_first;
1459       node_cursor->cl_cursor = ti->cl_cursor;
1460       *ti_ptr = node_cursor->node_group;
1461       DrawChooseTree(ti_ptr);
1462       return;
1463     }
1464   }
1465   else if (dx == -1 && ti->node_parent)
1466   {
1467     *ti_ptr = ti->node_parent;
1468     DrawChooseTree(ti_ptr);
1469     return;
1470   }
1471
1472   if (x == 0 && y >= 0 && y < num_page_entries)
1473   {
1474     if (button)
1475     {
1476       if (y != ti->cl_cursor)
1477       {
1478         drawCursor(y, FC_RED);
1479         drawCursor(ti->cl_cursor, FC_BLUE);
1480         drawChooseTreeInfo(ti->cl_first + y, ti);
1481         ti->cl_cursor = y;
1482       }
1483     }
1484     else
1485     {
1486       TreeInfo *node_first, *node_cursor;
1487       int entry_pos = ti->cl_first + y;
1488
1489       node_first = getTreeInfoFirstGroupEntry(ti);
1490       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1491
1492       if (node_cursor->node_group)
1493       {
1494         node_cursor->cl_first = ti->cl_first;
1495         node_cursor->cl_cursor = ti->cl_cursor;
1496         *ti_ptr = node_cursor->node_group;
1497         DrawChooseTree(ti_ptr);
1498       }
1499       else if (node_cursor->parent_link)
1500       {
1501         *ti_ptr = node_cursor->node_parent;
1502         DrawChooseTree(ti_ptr);
1503       }
1504       else
1505       {
1506         node_cursor->cl_first = ti->cl_first;
1507         node_cursor->cl_cursor = ti->cl_cursor;
1508         *ti_ptr = node_cursor;
1509
1510         if (ti->type == TREE_TYPE_LEVEL_DIR)
1511         {
1512           LoadLevelSetup_SeriesInfo();
1513
1514           SaveLevelSetup_LastSeries();
1515           SaveLevelSetup_SeriesInfo();
1516           TapeErase();
1517         }
1518
1519         if (game_status == SETUP)
1520         {
1521           execSetupArtwork();
1522         }
1523         else
1524         {
1525           game_status = MAINMENU;
1526           DrawMainMenu();
1527         }
1528       }
1529     }
1530   }
1531
1532   BackToFront();
1533
1534   if (game_status == CHOOSELEVEL || game_status == SETUP)
1535     DoAnimation();
1536 }
1537
1538 void DrawChooseLevel()
1539 {
1540   DrawChooseTree(&leveldir_current);
1541 }
1542
1543 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1544 {
1545   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1546 }
1547
1548 void DrawHallOfFame(int highlight_position)
1549 {
1550   UnmapAllGadgets();
1551   FadeSounds();
1552   CloseDoor(DOOR_CLOSE_2);
1553
1554   if (highlight_position < 0) 
1555     LoadScore(level_nr);
1556
1557   FadeToFront();
1558   InitAnimation();
1559   HandleHallOfFame(highlight_position,0, 0,0, MB_MENU_INITIALIZE);
1560   PlaySound(SND_MENU_HALL_OF_FAME);
1561 }
1562
1563 static void drawHallOfFameList(int first_entry, int highlight_position)
1564 {
1565   int i;
1566
1567   ClearWindow();
1568   DrawText(SX + 80, SY + 8, "Hall Of Fame", FS_BIG, FC_YELLOW);
1569   DrawTextFCentered(46, FC_RED, "HighScores of Level %d", level_nr);
1570
1571   for(i=0; i<MAX_MENU_ENTRIES_ON_SCREEN; i++)
1572   {
1573     int entry = first_entry + i;
1574     int color = (entry == highlight_position ? FC_RED : FC_GREEN);
1575
1576 #if 0
1577     DrawText(SX, SY + 64 + i * 32, ".................", FS_BIG, color);
1578     DrawText(SX, SY + 64 + i * 32, highscore[i].Name, FS_BIG, color);
1579     DrawText(SX + 12 * 32, SY + 64 + i * 32,
1580              int2str(highscore[i].Score, 5), FS_BIG, color);
1581 #else
1582     DrawText(SX, SY + 64 + i * 32, "..................................",
1583              FS_MEDIUM, FC_YELLOW);
1584     DrawText(SX, SY + 64 + i * 32, int2str(entry + 1, 3),
1585              FS_MEDIUM, FC_YELLOW);
1586     DrawText(SX + 64, SY + 64 + i * 32, highscore[entry].Name, FS_BIG, color);
1587     DrawText(SX + 14 * 32 + 16, SY + 64 + i * 32,
1588              int2str(highscore[entry].Score, 5), FS_MEDIUM, color);
1589 #endif
1590   }
1591 }
1592
1593 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1594 {
1595   static int first_entry = 0;
1596   static int highlight_position = 0;
1597   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1598   int button_released = !button;
1599
1600   if (button == MB_MENU_INITIALIZE)
1601   {
1602     first_entry = 0;
1603     highlight_position = mx;
1604     drawHallOfFameList(first_entry, highlight_position);
1605     return;
1606   }
1607
1608   if (ABS(dy) == SCR_FIELDY)    /* handle KSYM_Page_Up, KSYM_Page_Down */
1609     step = MAX_MENU_ENTRIES_ON_SCREEN - 1;
1610
1611   if (dy < 0)
1612   {
1613     if (first_entry > 0)
1614     {
1615       first_entry -= step;
1616       if (first_entry < 0)
1617         first_entry = 0;
1618
1619       drawHallOfFameList(first_entry, highlight_position);
1620       return;
1621     }
1622   }
1623   else if (dy > 0)
1624   {
1625     if (first_entry + MAX_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1626     {
1627       first_entry += step;
1628       if (first_entry + MAX_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1629         first_entry = MAX(0, MAX_SCORE_ENTRIES - MAX_MENU_ENTRIES_ON_SCREEN);
1630
1631       drawHallOfFameList(first_entry, highlight_position);
1632       return;
1633     }
1634   }
1635
1636   if (button_released)
1637   {
1638     FadeSound(SND_MENU_HALL_OF_FAME);
1639     game_status = MAINMENU;
1640     DrawMainMenu();
1641   }
1642
1643   BackToFront();
1644
1645   if (game_status == HALLOFFAME)
1646     DoAnimation();
1647 }
1648
1649
1650 /* ========================================================================= */
1651 /* setup screen functions                                                    */
1652 /* ========================================================================= */
1653
1654 static struct TokenInfo *setup_info;
1655 static int num_setup_info;
1656
1657 static char *graphics_set_name;
1658 static char *sounds_set_name;
1659 static char *music_set_name;
1660
1661 static void execSetupMain()
1662 {
1663   setup_mode = SETUP_MODE_MAIN;
1664   DrawSetupScreen();
1665 }
1666
1667 static void execSetupGame()
1668 {
1669   setup_mode = SETUP_MODE_GAME;
1670   DrawSetupScreen();
1671 }
1672
1673 static void execSetupGraphics()
1674 {
1675   setup_mode = SETUP_MODE_GRAPHICS;
1676   DrawSetupScreen();
1677 }
1678
1679 static void execSetupSound()
1680 {
1681   setup_mode = SETUP_MODE_SOUND;
1682   DrawSetupScreen();
1683 }
1684
1685 static void execSetupArtwork()
1686 {
1687   /* needed if last screen (setup choice) changed graphics, sounds or music */
1688   ReloadCustomArtwork();
1689
1690   setup.graphics_set = artwork.gfx_current->identifier;
1691   setup.sounds_set = artwork.snd_current->identifier;
1692   setup.music_set = artwork.mus_current->identifier;
1693
1694   /* needed for displaying artwork name instead of artwork identifier */
1695   graphics_set_name = artwork.gfx_current->name;
1696   sounds_set_name = artwork.snd_current->name;
1697   music_set_name = artwork.mus_current->name;
1698
1699   setup_mode = SETUP_MODE_ARTWORK;
1700   DrawSetupScreen();
1701 }
1702
1703 static void execSetupChooseGraphics()
1704 {
1705   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
1706   DrawSetupScreen();
1707 }
1708
1709 static void execSetupChooseSounds()
1710 {
1711   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
1712   DrawSetupScreen();
1713 }
1714
1715 static void execSetupChooseMusic()
1716 {
1717   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
1718   DrawSetupScreen();
1719 }
1720
1721 static void execSetupInput()
1722 {
1723   setup_mode = SETUP_MODE_INPUT;
1724   DrawSetupScreen();
1725 }
1726
1727 static void execSetupShortcut()
1728 {
1729   setup_mode = SETUP_MODE_SHORTCUT;
1730   DrawSetupScreen();
1731 }
1732
1733 static void execExitSetup()
1734 {
1735   game_status = MAINMENU;
1736   DrawMainMenu();
1737 }
1738
1739 static void execSaveAndExitSetup()
1740 {
1741   SaveSetup();
1742   execExitSetup();
1743 }
1744
1745 static struct TokenInfo setup_info_main[] =
1746 {
1747   { TYPE_ENTER_MENU,    execSetupGame,          "Game Settings"         },
1748   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
1749   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
1750   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
1751   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
1752   { TYPE_ENTER_MENU,    execSetupShortcut,      "Key Shortcuts"         },
1753   { TYPE_EMPTY,         NULL,                   ""                      },
1754   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
1755   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and exit"         },
1756   { 0,                  NULL,                   NULL                    }
1757 };
1758
1759 static struct TokenInfo setup_info_game[] =
1760 {
1761   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode:"            },
1762   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
1763   { TYPE_SWITCH,        &setup.time_limit,      "Timelimit:"            },
1764   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record:"          },
1765   { TYPE_EMPTY,         NULL,                   ""                      },
1766   { TYPE_LEAVE_MENU,    execSetupMain,          "Exit"                  },
1767   { 0,                  NULL,                   NULL                    }
1768 };
1769
1770 static struct TokenInfo setup_info_graphics[] =
1771 {
1772   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
1773   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
1774   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scroll.:"         },
1775 #if 0
1776   { TYPE_SWITCH,        &setup.double_buffering,"Buffered gfx:"         },
1777   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
1778 #endif
1779   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Doors:"          },
1780   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
1781   { TYPE_EMPTY,         NULL,                   ""                      },
1782   { TYPE_LEAVE_MENU,    execSetupMain,          "Exit"                  },
1783   { 0,                  NULL,                   NULL                    }
1784 };
1785
1786 static struct TokenInfo setup_info_sound[] =
1787 {
1788   { TYPE_SWITCH,        &setup.sound,           "Sound:",               },
1789   { TYPE_EMPTY,         NULL,                   ""                      },
1790   { TYPE_SWITCH,        &setup.sound_simple,    "Simple Sound:"         },
1791   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Loops:"          },
1792   { TYPE_SWITCH,        &setup.sound_music,     "Game Music:"           },
1793   { TYPE_EMPTY,         NULL,                   ""                      },
1794   { TYPE_LEAVE_MENU,    execSetupMain,          "Exit"                  },
1795   { 0,                  NULL,                   NULL                    }
1796 };
1797
1798 static struct TokenInfo setup_info_artwork[] =
1799 {
1800   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
1801   { TYPE_STRING,        &graphics_set_name,     ""                      },
1802   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
1803   { TYPE_STRING,        &sounds_set_name,       ""                      },
1804   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
1805   { TYPE_STRING,        &music_set_name,        ""                      },
1806   { TYPE_EMPTY,         NULL,                   ""                      },
1807   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
1808   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
1809   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
1810   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
1811   { TYPE_EMPTY,         NULL,                   ""                      },
1812   { TYPE_LEAVE_MENU,    execSetupMain,          "Exit"                  },
1813   { 0,                  NULL,                   NULL                    }
1814 };
1815
1816 static struct TokenInfo setup_info_shortcut[] =
1817 {
1818   { TYPE_KEYTEXT,       NULL,                   "Quick Save Game:",     },
1819   { TYPE_KEY,           &setup.shortcut.save_game,      ""              },
1820   { TYPE_KEYTEXT,       NULL,                   "Quick Load Game:",     },
1821   { TYPE_KEY,           &setup.shortcut.load_game,      ""              },
1822   { TYPE_KEYTEXT,       NULL,                   "Toggle Pause:",        },
1823   { TYPE_KEY,           &setup.shortcut.toggle_pause,   ""              },
1824   { TYPE_EMPTY,         NULL,                   ""                      },
1825   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on Esc:"           },
1826   { TYPE_EMPTY,         NULL,                   ""                      },
1827   { TYPE_LEAVE_MENU,    execSetupMain,          "Exit"                  },
1828   { 0,                  NULL,                   NULL                    }
1829 };
1830
1831 static Key getSetupKey()
1832 {
1833   Key key = KSYM_UNDEFINED;
1834   boolean got_key_event = FALSE;
1835
1836   while (!got_key_event)
1837   {
1838     if (PendingEvent())         /* got event */
1839     {
1840       Event event;
1841
1842       NextEvent(&event);
1843
1844       switch(event.type)
1845       {
1846         case EVENT_KEYPRESS:
1847           {
1848             key = GetEventKey((KeyEvent *)&event, TRUE);
1849
1850             /* press 'Escape' or 'Enter' to keep the existing key binding */
1851             if (key == KSYM_Escape || key == KSYM_Return)
1852               key = KSYM_UNDEFINED;     /* keep old value */
1853
1854             got_key_event = TRUE;
1855           }
1856           break;
1857
1858         case EVENT_KEYRELEASE:
1859           key_joystick_mapping = 0;
1860           break;
1861
1862         default:
1863           HandleOtherEvents(&event);
1864           break;
1865       }
1866     }
1867
1868     BackToFront();
1869     DoAnimation();
1870
1871     /* don't eat all CPU time */
1872     Delay(10);
1873   }
1874
1875   return key;
1876 }
1877
1878 static void drawSetupValue(int pos)
1879 {
1880   int xpos = MENU_SCREEN_VALUE_XPOS;
1881   int ypos = MENU_SCREEN_START_YPOS + pos;
1882   int font_size = FS_BIG;
1883   int font_color = FC_YELLOW;
1884   char *value_string = getSetupValue(setup_info[pos].type & ~TYPE_GHOSTED,
1885                                      setup_info[pos].value);
1886
1887   if (value_string == NULL)
1888     return;
1889
1890   if (setup_info[pos].type & TYPE_KEY)
1891   {
1892     xpos = 3;
1893
1894     if (setup_info[pos].type & TYPE_QUERY)
1895     {
1896       value_string = "<press key>";
1897       font_color = FC_RED;
1898     }
1899   }
1900   else if (setup_info[pos].type & TYPE_STRING)
1901   {
1902     int max_value_len = (SCR_FIELDX - 2) * 2;
1903
1904     xpos = 1;
1905     font_size = FS_MEDIUM;
1906
1907     if (strlen(value_string) > max_value_len)
1908       value_string[max_value_len] = '\0';
1909   }
1910   else if (setup_info[pos].type & TYPE_BOOLEAN_STYLE &&
1911            !*(boolean *)(setup_info[pos].value))
1912     font_color = FC_BLUE;
1913
1914   DrawText(SX + xpos * 32, SY + ypos * 32,
1915            (xpos == 3 ? "              " : "   "), FS_BIG, FC_YELLOW);
1916   DrawText(SX + xpos * 32, SY + ypos * 32, value_string, font_size,font_color);
1917 }
1918
1919 static void changeSetupValue(int pos)
1920 {
1921   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
1922   {
1923     *(boolean *)setup_info[pos].value ^= TRUE;
1924   }
1925   else if (setup_info[pos].type & TYPE_KEY)
1926   {
1927     Key key;
1928
1929     setup_info[pos].type |= TYPE_QUERY;
1930     drawSetupValue(pos);
1931     setup_info[pos].type &= ~TYPE_QUERY;
1932
1933     key = getSetupKey();
1934     if (key != KSYM_UNDEFINED)
1935       *(Key *)setup_info[pos].value = key;
1936   }
1937
1938   drawSetupValue(pos);
1939 }
1940
1941 static void DrawSetupScreen_Generic()
1942 {
1943   char *title_string = NULL;
1944   int i;
1945
1946   UnmapAllGadgets();
1947   CloseDoor(DOOR_CLOSE_2);
1948   ClearWindow();
1949
1950   if (setup_mode == SETUP_MODE_MAIN)
1951   {
1952     setup_info = setup_info_main;
1953     title_string = "Setup";
1954   }
1955   else if (setup_mode == SETUP_MODE_GAME)
1956   {
1957     setup_info = setup_info_game;
1958     title_string = "Setup Game";
1959   }
1960   else if (setup_mode == SETUP_MODE_GRAPHICS)
1961   {
1962     setup_info = setup_info_graphics;
1963     title_string = "Setup Graphics";
1964   }
1965   else if (setup_mode == SETUP_MODE_SOUND)
1966   {
1967     setup_info = setup_info_sound;
1968     title_string = "Setup Sound";
1969   }
1970   else if (setup_mode == SETUP_MODE_ARTWORK)
1971   {
1972     setup_info = setup_info_artwork;
1973     title_string = "Custom Artwork";
1974   }
1975   else if (setup_mode == SETUP_MODE_SHORTCUT)
1976   {
1977     setup_info = setup_info_shortcut;
1978     title_string = "Setup Shortcuts";
1979   }
1980
1981   DrawText(SX + 16, SY + 16, title_string, FS_BIG, FC_YELLOW);
1982
1983   num_setup_info = 0;
1984   for(i=0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
1985   {
1986     void *value_ptr = setup_info[i].value;
1987     int ypos = MENU_SCREEN_START_YPOS + i;
1988     int font_size = FS_BIG;
1989
1990     /* set some entries to "unchangeable" according to other variables */
1991     if ((value_ptr == &setup.sound       && !audio.sound_available) ||
1992         (value_ptr == &setup.sound_loops && !audio.loops_available) ||
1993         (value_ptr == &setup.sound_music && !audio.music_available) ||
1994         (value_ptr == &setup.fullscreen  && !video.fullscreen_available))
1995       setup_info[i].type |= TYPE_GHOSTED;
1996
1997     if (setup_info[i].type & TYPE_STRING)
1998       font_size = FS_MEDIUM;
1999
2000     DrawText(SX + 32, SY + ypos * 32, setup_info[i].text, font_size, FC_GREEN);
2001
2002     if (setup_info[i].type & TYPE_ENTER_MENU)
2003       initCursor(i, IMG_ARROW_BLUE_RIGHT);
2004     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2005       initCursor(i, IMG_ARROW_BLUE_LEFT);
2006     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2007       initCursor(i, IMG_BALL_BLUE);
2008
2009     if (setup_info[i].type & TYPE_VALUE)
2010       drawSetupValue(i);
2011
2012     num_setup_info++;
2013   }
2014
2015   FadeToFront();
2016   InitAnimation();
2017   HandleSetupScreen_Generic(0,0,0,0,MB_MENU_INITIALIZE);
2018 }
2019
2020 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2021 {
2022   static int choice_store[MAX_SETUP_MODES];
2023   int choice = choice_store[setup_mode];
2024   int x = 0;
2025   int y = choice;
2026
2027   if (button == MB_MENU_INITIALIZE)
2028   {
2029     drawCursor(choice, FC_RED);
2030     return;
2031   }
2032   else if (button == MB_MENU_LEAVE)
2033   {
2034     for (y=0; y<num_setup_info; y++)
2035     {
2036       if (setup_info[y].type & TYPE_LEAVE_MENU)
2037       {
2038         void (*menu_callback_function)(void) = setup_info[y].value;
2039
2040         menu_callback_function();
2041         break;  /* absolutely needed because function changes 'setup_info'! */
2042       }
2043     }
2044
2045     return;
2046   }
2047
2048   if (mx || my)         /* mouse input */
2049   {
2050     x = (mx - SX) / 32;
2051     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
2052   }
2053   else if (dx || dy)    /* keyboard input */
2054   {
2055     if (dx)
2056     {
2057       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2058
2059       if ((setup_info[choice].type & menu_navigation_type) ||
2060           (setup_info[choice].type & TYPE_BOOLEAN_STYLE))
2061         button = MB_MENU_CHOICE;
2062     }
2063     else if (dy)
2064       y = choice + dy;
2065
2066     /* jump to next non-empty menu entry (up or down) */
2067     while (y > 0 && y < num_setup_info - 1 &&
2068            (setup_info[y].type & TYPE_SKIP_ENTRY))
2069       y += dy;
2070   }
2071
2072   if (x == 0 && y >= 0 && y < num_setup_info &&
2073       (setup_info[y].type & ~TYPE_SKIP_ENTRY))
2074   {
2075     if (button)
2076     {
2077       if (y != choice)
2078       {
2079         drawCursor(y, FC_RED);
2080         drawCursor(choice, FC_BLUE);
2081         choice = choice_store[setup_mode] = y;
2082       }
2083     }
2084     else if (!(setup_info[y].type & TYPE_GHOSTED))
2085     {
2086       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2087       {
2088         void (*menu_callback_function)(void) = setup_info[choice].value;
2089
2090         menu_callback_function();
2091       }
2092       else
2093       {
2094         if ((setup_info[y].type & TYPE_KEYTEXT) &&
2095             (setup_info[y + 1].type & TYPE_KEY))
2096           y++;
2097
2098         if (setup_info[y].type & TYPE_VALUE)
2099           changeSetupValue(y);
2100       }
2101     }
2102   }
2103
2104   BackToFront();
2105
2106   if (game_status == SETUP)
2107     DoAnimation();
2108 }
2109
2110 void DrawSetupScreen_Input()
2111 {
2112   ClearWindow();
2113   DrawText(SX+16, SY+16, "Setup Input", FS_BIG, FC_YELLOW);
2114
2115   initCursor(0, IMG_BALL_BLUE);
2116   initCursor(1, IMG_BALL_BLUE);
2117   initCursor(2, IMG_ARROW_BLUE_RIGHT);
2118   initCursor(13, IMG_ARROW_BLUE_LEFT);
2119
2120   DrawGraphic(10, MENU_SCREEN_START_YPOS, IMG_ARROW_BLUE_LEFT, 0);
2121   DrawGraphic(12, MENU_SCREEN_START_YPOS, IMG_ARROW_BLUE_RIGHT, 0);
2122
2123   DrawText(SX+32, SY+2*32, "Player:", FS_BIG, FC_GREEN);
2124   DrawText(SX+32, SY+3*32, "Device:", FS_BIG, FC_GREEN);
2125   DrawText(SX+32, SY+15*32, "Exit", FS_BIG, FC_GREEN);
2126
2127 #if 0
2128   DeactivateJoystickForCalibration();
2129   DrawTextFCentered(SYSIZE - 20, FC_BLUE,
2130                     "Joysticks deactivated on this screen");
2131 #endif
2132
2133   HandleSetupScreen_Input(0,0, 0,0, MB_MENU_INITIALIZE);
2134   FadeToFront();
2135   InitAnimation();
2136 }
2137
2138 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2139 {
2140   if (device_name == NULL)
2141     return;
2142
2143   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2144     device_nr = 0;
2145
2146   if (strlen(device_name) > 1)
2147   {
2148     char c1 = device_name[strlen(device_name) - 1];
2149     char c2 = device_name[strlen(device_name) - 2];
2150
2151     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2152       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2153   }
2154   else
2155     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2156             strlen(device_name));
2157 }
2158
2159 static void drawPlayerSetupInputInfo(int player_nr)
2160 {
2161   int i;
2162   static struct SetupKeyboardInfo custom_key;
2163   static struct
2164   {
2165     Key *key;
2166     char *text;
2167   } custom[] =
2168   {
2169     { &custom_key.left,  "Joystick Left"  },
2170     { &custom_key.right, "Joystick Right" },
2171     { &custom_key.up,    "Joystick Up"    },
2172     { &custom_key.down,  "Joystick Down"  },
2173     { &custom_key.snap,  "Button 1"       },
2174     { &custom_key.bomb,  "Button 2"       }
2175   };
2176   static char *joystick_name[MAX_PLAYERS] =
2177   {
2178     "Joystick1",
2179     "Joystick2",
2180     "Joystick3",
2181     "Joystick4"
2182   };
2183
2184   custom_key = setup.input[player_nr].key;
2185
2186   DrawText(SX+11*32, SY+2*32, int2str(player_nr + 1, 1), FS_BIG, FC_RED);
2187   DrawGraphic(8, 2, PLAYER_NR_GFX(IMG_PLAYER1, player_nr), 0);
2188
2189   if (setup.input[player_nr].use_joystick)
2190   {
2191     char *device_name = setup.input[player_nr].joy.device_name;
2192
2193     DrawText(SX+8*32, SY+3*32,
2194              joystick_name[getJoystickNrFromDeviceName(device_name)],
2195              FS_BIG, FC_YELLOW);
2196     DrawText(SX+32, SY+4*32, "Calibrate", FS_BIG, FC_GREEN);
2197   }
2198   else
2199   {
2200     DrawText(SX+8*32, SY+3*32, "Keyboard ", FS_BIG, FC_YELLOW);
2201     DrawText(SX+32, SY+4*32, "Customize", FS_BIG, FC_GREEN);
2202   }
2203
2204   DrawText(SX+32, SY+5*32, "Actual Settings:", FS_BIG, FC_GREEN);
2205   DrawGraphic(1, 6, IMG_ARROW_BLUE_LEFT, 0);
2206   DrawGraphic(1, 7, IMG_ARROW_BLUE_RIGHT, 0);
2207   DrawGraphic(1, 8, IMG_ARROW_BLUE_UP, 0);
2208   DrawGraphic(1, 9, IMG_ARROW_BLUE_DOWN, 0);
2209   DrawText(SX+2*32, SY+6*32, ":", FS_BIG, FC_BLUE);
2210   DrawText(SX+2*32, SY+7*32, ":", FS_BIG, FC_BLUE);
2211   DrawText(SX+2*32, SY+8*32, ":", FS_BIG, FC_BLUE);
2212   DrawText(SX+2*32, SY+9*32, ":", FS_BIG, FC_BLUE);
2213   DrawText(SX+32, SY+10*32, "Snap Field:", FS_BIG, FC_BLUE);
2214   DrawText(SX+32, SY+12*32, "Place Bomb:", FS_BIG, FC_BLUE);
2215
2216   for (i=0; i<6; i++)
2217   {
2218     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2219
2220     DrawText(SX + 3*32, SY + ypos*32,
2221              "              ", FS_BIG, FC_YELLOW);
2222     DrawText(SX + 3*32, SY + ypos*32,
2223              (setup.input[player_nr].use_joystick ?
2224               custom[i].text :
2225               getKeyNameFromKey(*custom[i].key)),
2226              FS_BIG, FC_YELLOW);
2227   }
2228 }
2229
2230 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2231 {
2232   static int choice = 0;
2233   static int player_nr = 0;
2234   int x = 0;
2235   int y = choice;
2236   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2237   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2238   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2239   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2240
2241   if (button == MB_MENU_INITIALIZE)
2242   {
2243     drawPlayerSetupInputInfo(player_nr);
2244     drawCursor(choice, FC_RED);
2245     return;
2246   }
2247   else if (button == MB_MENU_LEAVE)
2248   {
2249     setup_mode = SETUP_MODE_MAIN;
2250     DrawSetupScreen();
2251     InitJoysticks();
2252   }
2253
2254   if (mx || my)         /* mouse input */
2255   {
2256     x = (mx - SX) / 32;
2257     y = (my - SY) / 32 - MENU_SCREEN_START_YPOS;
2258   }
2259   else if (dx || dy)    /* keyboard input */
2260   {
2261     if (dx && choice == 0)
2262       x = (dx < 0 ? 10 : 12);
2263     else if ((dx && choice == 1) ||
2264              (dx == +1 && choice == 2) ||
2265              (dx == -1 && choice == pos_end))
2266       button = MB_MENU_CHOICE;
2267     else if (dy)
2268       y = choice + dy;
2269
2270     if (y >= pos_empty1 && y <= pos_empty2)
2271       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2272   }
2273
2274   if (y == 0 && ((x == 0 && !button) || ((x == 10 || x == 12) && button)))
2275   {
2276     static unsigned long delay = 0;
2277
2278     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2279       goto out;
2280
2281     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2282
2283     drawPlayerSetupInputInfo(player_nr);
2284   }
2285   else if (x == 0 && y >= pos_start && y <= pos_end &&
2286            !(y >= pos_empty1 && y <= pos_empty2))
2287   {
2288     if (button)
2289     {
2290       if (y != choice)
2291       {
2292         drawCursor(y, FC_RED);
2293         drawCursor(choice, FC_BLUE);
2294         choice = y;
2295       }
2296     }
2297     else
2298     {
2299       if (y == 1)
2300       {
2301         char *device_name = setup.input[player_nr].joy.device_name;
2302
2303         if (!setup.input[player_nr].use_joystick)
2304         {
2305           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2306
2307           setJoystickDeviceToNr(device_name, new_device_nr);
2308           setup.input[player_nr].use_joystick = TRUE;
2309         }
2310         else
2311         {
2312           int device_nr = getJoystickNrFromDeviceName(device_name);
2313           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2314
2315           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2316             setup.input[player_nr].use_joystick = FALSE;
2317           else
2318             setJoystickDeviceToNr(device_name, new_device_nr);
2319         }
2320
2321         drawPlayerSetupInputInfo(player_nr);
2322       }
2323       else if (y == 2)
2324       {
2325         if (setup.input[player_nr].use_joystick)
2326         {
2327           InitJoysticks();
2328           CalibrateJoystick(player_nr);
2329         }
2330         else
2331           CustomizeKeyboard(player_nr);
2332       }
2333       else if (y == pos_end)
2334       {
2335         InitJoysticks();
2336
2337         setup_mode = SETUP_MODE_MAIN;
2338         DrawSetupScreen();
2339       }
2340     }
2341   }
2342
2343   BackToFront();
2344
2345   out:
2346
2347   if (game_status == SETUP)
2348     DoAnimation();
2349 }
2350
2351 void CustomizeKeyboard(int player_nr)
2352 {
2353   int i;
2354   int step_nr;
2355   boolean finished = FALSE;
2356   static struct SetupKeyboardInfo custom_key;
2357   static struct
2358   {
2359     Key *key;
2360     char *text;
2361   } customize_step[] =
2362   {
2363     { &custom_key.left,  "Move Left"  },
2364     { &custom_key.right, "Move Right" },
2365     { &custom_key.up,    "Move Up"    },
2366     { &custom_key.down,  "Move Down"  },
2367     { &custom_key.snap,  "Snap Field" },
2368     { &custom_key.bomb,  "Place Bomb" }
2369   };
2370
2371   /* read existing key bindings from player setup */
2372   custom_key = setup.input[player_nr].key;
2373
2374   ClearWindow();
2375   DrawText(SX + 16, SY + 16, "Keyboard Input", FS_BIG, FC_YELLOW);
2376
2377   BackToFront();
2378   InitAnimation();
2379
2380   step_nr = 0;
2381   DrawText(SX, SY + (2+2*step_nr)*32,
2382            customize_step[step_nr].text, FS_BIG, FC_RED);
2383   DrawText(SX, SY + (2+2*step_nr+1)*32,
2384            "Key:", FS_BIG, FC_RED);
2385   DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
2386            getKeyNameFromKey(*customize_step[step_nr].key),
2387            FS_BIG, FC_BLUE);
2388
2389   while(!finished)
2390   {
2391     if (PendingEvent())         /* got event */
2392     {
2393       Event event;
2394
2395       NextEvent(&event);
2396
2397       switch(event.type)
2398       {
2399         case EVENT_KEYPRESS:
2400           {
2401             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2402
2403             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2404             {
2405               finished = TRUE;
2406               break;
2407             }
2408
2409             /* all keys configured -- wait for "Escape" or "Return" key */
2410             if (step_nr == 6)
2411               break;
2412
2413             /* press 'Enter' to keep the existing key binding */
2414             if (key == KSYM_Return)
2415               key = *customize_step[step_nr].key;
2416
2417             /* check if key already used */
2418             for (i=0; i<step_nr; i++)
2419               if (*customize_step[i].key == key)
2420                 break;
2421             if (i < step_nr)
2422               break;
2423
2424             /* got new key binding */
2425             *customize_step[step_nr].key = key;
2426             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
2427                      "             ", FS_BIG, FC_YELLOW);
2428             DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32,
2429                      getKeyNameFromKey(key), FS_BIG, FC_YELLOW);
2430             step_nr++;
2431
2432             /* un-highlight last query */
2433             DrawText(SX, SY+(2+2*(step_nr-1))*32,
2434                      customize_step[step_nr-1].text, FS_BIG, FC_GREEN);
2435             DrawText(SX, SY+(2+2*(step_nr-1)+1)*32,
2436                      "Key:", FS_BIG, FC_GREEN);
2437
2438             /* press 'Enter' to leave */
2439             if (step_nr == 6)
2440             {
2441               DrawText(SX + 16, SY + 15*32+16,
2442                        "Press Enter", FS_BIG, FC_YELLOW);
2443               break;
2444             }
2445
2446             /* query next key binding */
2447             DrawText(SX, SY+(2+2*step_nr)*32,
2448                      customize_step[step_nr].text, FS_BIG, FC_RED);
2449             DrawText(SX, SY+(2+2*step_nr+1)*32,
2450                      "Key:", FS_BIG, FC_RED);
2451             DrawText(SX + 4*32, SY+(2+2*step_nr+1)*32,
2452                      getKeyNameFromKey(*customize_step[step_nr].key),
2453                      FS_BIG, FC_BLUE);
2454           }
2455           break;
2456
2457         case EVENT_KEYRELEASE:
2458           key_joystick_mapping = 0;
2459           break;
2460
2461         default:
2462           HandleOtherEvents(&event);
2463           break;
2464       }
2465     }
2466
2467     BackToFront();
2468     DoAnimation();
2469
2470     /* don't eat all CPU time */
2471     Delay(10);
2472   }
2473
2474   /* write new key bindings back to player setup */
2475   setup.input[player_nr].key = custom_key;
2476
2477   StopAnimation();
2478   DrawSetupScreen_Input();
2479 }
2480
2481 static boolean CalibrateJoystickMain(int player_nr)
2482 {
2483   int new_joystick_xleft = JOYSTICK_XMIDDLE;
2484   int new_joystick_xright = JOYSTICK_XMIDDLE;
2485   int new_joystick_yupper = JOYSTICK_YMIDDLE;
2486   int new_joystick_ylower = JOYSTICK_YMIDDLE;
2487   int new_joystick_xmiddle, new_joystick_ymiddle;
2488
2489   int joystick_fd = joystick.fd[player_nr];
2490   int x, y, last_x, last_y, xpos = 8, ypos = 3;
2491   boolean check[3][3];
2492   int check_remaining = 3 * 3;
2493   int joy_x, joy_y;
2494   int joy_value;
2495   int result = -1;
2496
2497   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
2498     return FALSE;
2499
2500   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
2501     return FALSE;
2502
2503   ClearWindow();
2504
2505   for(y=0; y<3; y++)
2506   {
2507     for(x=0; x<3; x++)
2508     {
2509       check[x][y] = FALSE;
2510       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_BALL_BLUE, 0);
2511     }
2512   }
2513
2514   DrawText(SX,      SY +  6 * 32, " ROTATE JOYSTICK ", FS_BIG, FC_YELLOW);
2515   DrawText(SX,      SY +  7 * 32, "IN ALL DIRECTIONS", FS_BIG, FC_YELLOW);
2516   DrawText(SX + 16, SY +  9 * 32, "  IF ALL BALLS  ",  FS_BIG, FC_YELLOW);
2517   DrawText(SX,      SY + 10 * 32, "   ARE YELLOW,   ", FS_BIG, FC_YELLOW);
2518   DrawText(SX,      SY + 11 * 32, " CENTER JOYSTICK ", FS_BIG, FC_YELLOW);
2519   DrawText(SX,      SY + 12 * 32, "       AND       ", FS_BIG, FC_YELLOW);
2520   DrawText(SX,      SY + 13 * 32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW);
2521
2522   joy_value = Joystick(player_nr);
2523   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2524   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2525
2526   /* eventually uncalibrated center position (joystick could be uncentered) */
2527   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2528     return FALSE;
2529
2530   new_joystick_xmiddle = joy_x;
2531   new_joystick_ymiddle = joy_y;
2532
2533   DrawGraphic(xpos + last_x, ypos + last_y, IMG_BALL_RED, 0);
2534   BackToFront();
2535
2536   while(Joystick(player_nr) & JOY_BUTTON);      /* wait for released button */
2537   InitAnimation();
2538
2539   while(result < 0)
2540   {
2541     if (PendingEvent())         /* got event */
2542     {
2543       Event event;
2544
2545       NextEvent(&event);
2546
2547       switch(event.type)
2548       {
2549         case EVENT_KEYPRESS:
2550           switch(GetEventKey((KeyEvent *)&event, TRUE))
2551           {
2552             case KSYM_Return:
2553               if (check_remaining == 0)
2554                 result = 1;
2555               break;
2556
2557             case KSYM_Escape:
2558               result = 0;
2559               break;
2560
2561             default:
2562               break;
2563           }
2564           break;
2565
2566         case EVENT_KEYRELEASE:
2567           key_joystick_mapping = 0;
2568           break;
2569
2570         default:
2571           HandleOtherEvents(&event);
2572           break;
2573       }
2574     }
2575
2576     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2577       return FALSE;
2578
2579     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
2580     new_joystick_xright = MAX(new_joystick_xright, joy_x);
2581     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
2582     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
2583
2584     setup.input[player_nr].joy.xleft = new_joystick_xleft;
2585     setup.input[player_nr].joy.yupper = new_joystick_yupper;
2586     setup.input[player_nr].joy.xright = new_joystick_xright;
2587     setup.input[player_nr].joy.ylower = new_joystick_ylower;
2588     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2589     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2590
2591     CheckJoystickData();
2592
2593     joy_value = Joystick(player_nr);
2594
2595     if (joy_value & JOY_BUTTON && check_remaining == 0)
2596       result = 1;
2597
2598     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2599     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2600
2601     if (x != last_x || y != last_y)
2602     {
2603       DrawGraphic(xpos + last_x, ypos + last_y, IMG_BALL_YELLOW, 0);
2604       DrawGraphic(xpos + x,      ypos + y,      IMG_BALL_RED, 0);
2605
2606       last_x = x;
2607       last_y = y;
2608
2609       if (check_remaining > 0 && !check[x+1][y+1])
2610       {
2611         check[x+1][y+1] = TRUE;
2612         check_remaining--;
2613       }
2614
2615 #if 0
2616 #ifdef DEBUG
2617       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
2618              setup.input[player_nr].joy.xleft,
2619              setup.input[player_nr].joy.xmiddle,
2620              setup.input[player_nr].joy.xright);
2621       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
2622              setup.input[player_nr].joy.yupper,
2623              setup.input[player_nr].joy.ymiddle,
2624              setup.input[player_nr].joy.ylower);
2625 #endif
2626 #endif
2627
2628     }
2629
2630     BackToFront();
2631     DoAnimation();
2632
2633     /* don't eat all CPU time */
2634     Delay(10);
2635   }
2636
2637   /* calibrated center position (joystick should now be centered) */
2638   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2639     return FALSE;
2640
2641   new_joystick_xmiddle = joy_x;
2642   new_joystick_ymiddle = joy_y;
2643
2644   StopAnimation();
2645
2646   DrawSetupScreen_Input();
2647
2648   /* wait until the last pressed button was released */
2649   while (Joystick(player_nr) & JOY_BUTTON)
2650   {
2651     if (PendingEvent())         /* got event */
2652     {
2653       Event event;
2654
2655       NextEvent(&event);
2656       HandleOtherEvents(&event);
2657
2658       Delay(10);
2659     }
2660   }
2661
2662   return TRUE;
2663 }
2664
2665 void CalibrateJoystick(int player_nr)
2666 {
2667   if (!CalibrateJoystickMain(player_nr))
2668   {
2669     ClearWindow();
2670
2671     DrawText(SX + 16, SY + 6*32, "  JOYSTICK NOT  ",  FS_BIG, FC_YELLOW);
2672     DrawText(SX,      SY + 7*32, "    AVAILABLE    ", FS_BIG, FC_YELLOW);
2673     BackToFront();
2674     Delay(2000);        /* show error message for two seconds */
2675   }
2676 }
2677
2678 void DrawSetupScreen()
2679 {
2680   DeactivateJoystick();
2681
2682   if (setup_mode == SETUP_MODE_INPUT)
2683     DrawSetupScreen_Input();
2684   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2685     DrawChooseTree(&artwork.gfx_current);
2686   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2687     DrawChooseTree(&artwork.snd_current);
2688   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2689     DrawChooseTree(&artwork.mus_current);
2690   else
2691     DrawSetupScreen_Generic();
2692 }
2693
2694 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
2695 {
2696   if (setup_mode == SETUP_MODE_INPUT)
2697     HandleSetupScreen_Input(mx, my, dx, dy, button);
2698   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2699     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
2700   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2701     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
2702   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2703     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
2704   else
2705     HandleSetupScreen_Generic(mx, my, dx, dy, button);
2706 }
2707
2708 void HandleGameActions()
2709 {
2710   if (game_status != PLAYING)
2711     return;
2712
2713   if (local_player->LevelSolved)
2714     GameWon();
2715
2716   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2717     TapeStop();
2718
2719   GameActions();
2720
2721   BackToFront();
2722 }
2723
2724 /* ---------- new screen button stuff -------------------------------------- */
2725
2726 /* graphic position and size values for buttons and scrollbars */
2727 #define SC_SCROLLBUTTON_XSIZE           32
2728 #define SC_SCROLLBUTTON_YSIZE           32
2729
2730 #define SC_SCROLL_UP_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
2731 #define SC_SCROLL_UP_YPOS               SC_SCROLLBUTTON_YSIZE
2732 #define SC_SCROLL_DOWN_XPOS             SC_SCROLL_UP_XPOS
2733 #define SC_SCROLL_DOWN_YPOS             (SYSIZE - SC_SCROLLBUTTON_YSIZE)
2734 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLL_UP_XPOS
2735 #define SC_SCROLL_VERTICAL_YPOS   (SC_SCROLL_UP_YPOS + SC_SCROLLBUTTON_YSIZE)
2736 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
2737 #define SC_SCROLL_VERTICAL_YSIZE        (SYSIZE - 3 * SC_SCROLLBUTTON_YSIZE)
2738
2739 #define SC_BORDER_SIZE                  14
2740
2741 static struct
2742 {
2743   int gfx_unpressed, gfx_pressed;
2744   int x, y;
2745   int gadget_id;
2746   char *infotext;
2747 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
2748 {
2749   {
2750     IMG_ARROW_BLUE_UP, IMG_ARROW_RED_UP,
2751     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
2752     SCREEN_CTRL_ID_SCROLL_UP,
2753     "scroll up"
2754   },
2755   {
2756     IMG_ARROW_BLUE_DOWN, IMG_ARROW_RED_DOWN,
2757     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
2758     SCREEN_CTRL_ID_SCROLL_DOWN,
2759     "scroll down"
2760   }
2761 };
2762
2763 static struct
2764 {
2765   int gfx_unpressed, gfx_pressed;
2766   int x, y;
2767   int width, height;
2768   int type;
2769   int gadget_id;
2770   char *infotext;
2771 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
2772 {
2773   {
2774     IMG_SCROLLBAR_BLUE, IMG_SCROLLBAR_RED,
2775     SX + SC_SCROLL_VERTICAL_XPOS, SY + SC_SCROLL_VERTICAL_YPOS,
2776     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
2777     GD_TYPE_SCROLLBAR_VERTICAL,
2778     SCREEN_CTRL_ID_SCROLL_VERTICAL,
2779     "scroll level series vertically"
2780   }
2781 };
2782
2783 static void CreateScreenScrollbuttons()
2784 {
2785   struct GadgetInfo *gi;
2786   unsigned long event_mask;
2787   int i;
2788
2789   for (i=0; i<NUM_SCREEN_SCROLLBUTTONS; i++)
2790   {
2791     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2792     int gfx_unpressed, gfx_pressed;
2793     int x, y, width, height;
2794     int gd_x1, gd_x2, gd_y1, gd_y2;
2795     int id = scrollbutton_info[i].gadget_id;
2796
2797     x = scrollbutton_info[i].x;
2798     y = scrollbutton_info[i].y;
2799
2800     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
2801
2802     x += SX;
2803     y += SY;
2804     width = SC_SCROLLBUTTON_XSIZE;
2805     height = SC_SCROLLBUTTON_YSIZE;
2806
2807     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
2808     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
2809     gd_bitmap_unpressed = new_graphic_info[gfx_unpressed].bitmap;
2810     gd_bitmap_pressed   = new_graphic_info[gfx_pressed].bitmap;
2811     gd_x1 = new_graphic_info[gfx_unpressed].src_x;
2812     gd_y1 = new_graphic_info[gfx_unpressed].src_y;
2813     gd_x2 = new_graphic_info[gfx_pressed].src_x;
2814     gd_y2 = new_graphic_info[gfx_pressed].src_y;
2815
2816     gi = CreateGadget(GDI_CUSTOM_ID, id,
2817                       GDI_CUSTOM_TYPE_ID, i,
2818                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
2819                       GDI_X, x,
2820                       GDI_Y, y,
2821                       GDI_WIDTH, width,
2822                       GDI_HEIGHT, height,
2823                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2824                       GDI_STATE, GD_BUTTON_UNPRESSED,
2825                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2826                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2827                       GDI_EVENT_MASK, event_mask,
2828                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2829                       GDI_END);
2830
2831     if (gi == NULL)
2832       Error(ERR_EXIT, "cannot create gadget");
2833
2834     screen_gadget[id] = gi;
2835   }
2836 }
2837
2838 static void CreateScreenScrollbars()
2839 {
2840   int i;
2841
2842   for (i=0; i<NUM_SCREEN_SCROLLBARS; i++)
2843   {
2844     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2845     int gfx_unpressed, gfx_pressed;
2846     int gd_x1, gd_x2, gd_y1, gd_y2;
2847     struct GadgetInfo *gi;
2848     int items_max, items_visible, item_position;
2849     unsigned long event_mask;
2850     int num_page_entries = MAX_MENU_ENTRIES_ON_SCREEN - 1;
2851     int id = scrollbar_info[i].gadget_id;
2852
2853     items_max = num_page_entries;
2854     items_visible = num_page_entries;
2855     item_position = 0;
2856
2857     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
2858
2859     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
2860     gfx_pressed   = scrollbar_info[i].gfx_pressed;
2861     gd_bitmap_unpressed = new_graphic_info[gfx_unpressed].bitmap;
2862     gd_bitmap_pressed   = new_graphic_info[gfx_pressed].bitmap;
2863     gd_x1 = new_graphic_info[gfx_unpressed].src_x;
2864     gd_y1 = new_graphic_info[gfx_unpressed].src_y;
2865     gd_x2 = new_graphic_info[gfx_pressed].src_x;
2866     gd_y2 = new_graphic_info[gfx_pressed].src_y;
2867
2868     gi = CreateGadget(GDI_CUSTOM_ID, id,
2869                       GDI_CUSTOM_TYPE_ID, i,
2870                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
2871                       GDI_X, scrollbar_info[i].x,
2872                       GDI_Y, scrollbar_info[i].y,
2873                       GDI_WIDTH, scrollbar_info[i].width,
2874                       GDI_HEIGHT, scrollbar_info[i].height,
2875                       GDI_TYPE, scrollbar_info[i].type,
2876                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
2877                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
2878                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
2879                       GDI_STATE, GD_BUTTON_UNPRESSED,
2880                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2881                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2882                       GDI_BORDER_SIZE, SC_BORDER_SIZE,
2883                       GDI_EVENT_MASK, event_mask,
2884                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2885                       GDI_END);
2886
2887     if (gi == NULL)
2888       Error(ERR_EXIT, "cannot create gadget");
2889
2890     screen_gadget[id] = gi;
2891   }
2892 }
2893
2894 void CreateScreenGadgets()
2895 {
2896   CreateScreenScrollbuttons();
2897   CreateScreenScrollbars();
2898 }
2899
2900 void FreeScreenGadgets()
2901 {
2902   int i;
2903
2904   for (i=0; i<NUM_SCREEN_GADGETS; i++)
2905     FreeGadget(screen_gadget[i]);
2906 }
2907
2908 void MapChooseTreeGadgets(TreeInfo *ti)
2909 {
2910   int num_entries = numTreeInfoInGroup(ti);
2911   int i;
2912
2913   if (num_entries <= MAX_MENU_ENTRIES_ON_SCREEN)
2914     return;
2915
2916   for (i=0; i<NUM_SCREEN_GADGETS; i++)
2917     MapGadget(screen_gadget[i]);
2918 }
2919
2920 void UnmapChooseTreeGadgets()
2921 {
2922   int i;
2923
2924   for (i=0; i<NUM_SCREEN_GADGETS; i++)
2925     UnmapGadget(screen_gadget[i]);
2926 }
2927
2928 static void HandleScreenGadgets(struct GadgetInfo *gi)
2929 {
2930   int id = gi->custom_id;
2931
2932   if (game_status != CHOOSELEVEL && game_status != SETUP)
2933     return;
2934
2935   switch (id)
2936   {
2937     case SCREEN_CTRL_ID_SCROLL_UP:
2938       if (game_status == CHOOSELEVEL)
2939         HandleChooseLevel(SX,SY + 32, 0,0, MB_MENU_MARK);
2940       else if (game_status == SETUP)
2941         HandleSetupScreen(SX,SY + 32, 0,0, MB_MENU_MARK);
2942       break;
2943
2944     case SCREEN_CTRL_ID_SCROLL_DOWN:
2945       if (game_status == CHOOSELEVEL)
2946         HandleChooseLevel(SX,SY + SYSIZE - 32, 0,0, MB_MENU_MARK);
2947       else if (game_status == SETUP)
2948         HandleSetupScreen(SX,SY + SYSIZE - 32, 0,0, MB_MENU_MARK);
2949       break;
2950
2951     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
2952       if (game_status == CHOOSELEVEL)
2953         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
2954       else if (game_status == SETUP)
2955         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
2956       break;
2957
2958     default:
2959       break;
2960   }
2961 }