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