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