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