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