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