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