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