1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #include "libgame/libgame.h"
26 /* values for key_status */
27 #define KEY_NOT_PRESSED FALSE
28 #define KEY_RELEASED FALSE
29 #define KEY_PRESSED TRUE
32 static boolean cursor_inside_playfield = FALSE;
33 static boolean playfield_cursor_set = FALSE;
34 static unsigned long playfield_cursor_delay = 0;
37 /* event filter especially needed for SDL event filtering due to
38 delay problems with lots of mouse motion events when mouse button
39 not pressed (X11 can handle this with 'PointerMotionHintMask') */
41 int FilterMouseMotionEvents(const Event *event)
45 /* non-motion events are directly passed to event handler functions */
46 if (event->type != EVENT_MOTIONNOTIFY)
49 motion = (MotionEvent *)event;
50 cursor_inside_playfield = (motion->x >= SX && motion->x < SX + SXSIZE &&
51 motion->y >= SY && motion->y < SY + SYSIZE);
53 if (game_status == PLAYING && playfield_cursor_set)
55 SetMouseCursor(CURSOR_DEFAULT);
56 playfield_cursor_set = FALSE;
57 DelayReached(&playfield_cursor_delay, 0);
60 /* skip mouse motion events without pressed button outside level editor */
61 if (button_status == MB_RELEASED && game_status != LEVELED)
67 /* this is only really needed for non-SDL targets to filter unwanted events;
68 when using SDL with properly installed event filter, this function can be
69 replaced with a simple "NextEvent()" call, but it doesn't hurt either */
71 static boolean NextValidEvent(Event *event)
73 while (PendingEvent())
77 if (FilterMouseMotionEvents(event))
88 if (PendingEvent()) /* got event */
92 if (NextValidEvent(&event))
96 case EVENT_BUTTONPRESS:
97 case EVENT_BUTTONRELEASE:
98 HandleButtonEvent((ButtonEvent *) &event);
101 case EVENT_MOTIONNOTIFY:
102 HandleMotionEvent((MotionEvent *) &event);
106 case EVENT_KEYRELEASE:
107 HandleKeyEvent((KeyEvent *) &event);
111 HandleOtherEvents(&event);
118 /* when playing, display a special mouse pointer inside the playfield */
119 if (game_status == PLAYING)
121 if (!playfield_cursor_set && cursor_inside_playfield &&
122 DelayReached(&playfield_cursor_delay, 1000))
124 SetMouseCursor(CURSOR_PLAYFIELD);
125 playfield_cursor_set = TRUE;
128 else if (playfield_cursor_set)
130 SetMouseCursor(CURSOR_DEFAULT);
131 playfield_cursor_set = FALSE;
137 /* don't use all CPU time when idle; the main loop while playing
138 has its own synchronization and is CPU friendly, too */
140 if (game_status == PLAYING)
145 if (!PendingEvent()) /* delay only if no pending events */
149 /* refresh window contents from drawing buffer, if needed */
152 if (game_status == EXITGAME)
157 void HandleOtherEvents(Event *event)
162 HandleExposeEvent((ExposeEvent *) event);
165 case EVENT_UNMAPNOTIFY:
167 /* This causes the game to stop not only when iconified, but also
168 when on another virtual desktop, which might be not desired. */
169 SleepWhileUnmapped();
175 HandleFocusEvent((FocusChangeEvent *) event);
178 case EVENT_CLIENTMESSAGE:
179 HandleClientMessageEvent((ClientMessageEvent *) event);
182 #if defined(TARGET_SDL)
183 case SDL_JOYAXISMOTION:
184 case SDL_JOYBUTTONDOWN:
185 case SDL_JOYBUTTONUP:
186 HandleJoystickEvent(event);
195 void ClearEventQueue()
197 while (PendingEvent())
205 case EVENT_BUTTONRELEASE:
206 button_status = MB_RELEASED;
209 case EVENT_KEYRELEASE:
210 key_joystick_mapping = 0;
214 HandleOtherEvents(&event);
220 void ClearPlayerAction()
224 /* simulate key release events for still pressed keys */
225 key_joystick_mapping = 0;
226 for (i=0; i<MAX_PLAYERS; i++)
227 stored_player[i].action = 0;
230 void SleepWhileUnmapped()
232 boolean window_unmapped = TRUE;
234 KeyboardAutoRepeatOn();
236 while(window_unmapped)
244 case EVENT_BUTTONRELEASE:
245 button_status = MB_RELEASED;
248 case EVENT_KEYRELEASE:
249 key_joystick_mapping = 0;
252 case EVENT_MAPNOTIFY:
253 window_unmapped = FALSE;
256 case EVENT_UNMAPNOTIFY:
257 /* this is only to surely prevent the 'should not happen' case
258 * of recursively looping between 'SleepWhileUnmapped()' and
259 * 'HandleOtherEvents()' which usually calls this funtion.
264 HandleOtherEvents(&event);
269 if (game_status == PLAYING)
270 KeyboardAutoRepeatOff();
273 void HandleExposeEvent(ExposeEvent *event)
276 RedrawPlayfield(FALSE, event->x, event->y, event->width, event->height);
281 void HandleButtonEvent(ButtonEvent *event)
283 motion_status = FALSE;
285 if (event->type == EVENT_BUTTONPRESS)
286 button_status = event->button;
288 button_status = MB_RELEASED;
290 HandleButton(event->x, event->y, button_status);
293 void HandleMotionEvent(MotionEvent *event)
295 if (!PointerInWindow(window))
296 return; /* window and pointer are on different screens */
299 if (button_status == MB_RELEASED && game_status != LEVELED)
303 motion_status = TRUE;
305 HandleButton(event->x, event->y, button_status);
308 void HandleKeyEvent(KeyEvent *event)
310 int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
311 boolean with_modifiers = (game_status == PLAYING ? FALSE : TRUE);
312 Key key = GetEventKey(event, with_modifiers);
314 HandleKey(key, key_status);
317 void HandleFocusEvent(FocusChangeEvent *event)
319 static int old_joystick_status = -1;
321 if (event->type == EVENT_FOCUSOUT)
323 KeyboardAutoRepeatOn();
324 old_joystick_status = joystick.status;
325 joystick.status = JOYSTICK_NOT_AVAILABLE;
329 else if (event->type == EVENT_FOCUSIN)
331 /* When there are two Rocks'n'Diamonds windows which overlap and
332 the player moves the pointer from one game window to the other,
333 a 'FocusOut' event is generated for the window the pointer is
334 leaving and a 'FocusIn' event is generated for the window the
335 pointer is entering. In some cases, it can happen that the
336 'FocusIn' event is handled by the one game process before the
337 'FocusOut' event by the other game process. In this case the
338 X11 environment would end up with activated keyboard auto repeat,
339 because unfortunately this is a global setting and not (which
340 would be far better) set for each X11 window individually.
341 The effect would be keyboard auto repeat while playing the game
342 (game_status == PLAYING), which is not desired.
343 To avoid this special case, we just wait 1/10 second before
344 processing the 'FocusIn' event.
347 if (game_status == PLAYING)
350 KeyboardAutoRepeatOff();
352 if (old_joystick_status != -1)
353 joystick.status = old_joystick_status;
357 void HandleClientMessageEvent(ClientMessageEvent *event)
359 if (CheckCloseWindowEvent(event))
363 void HandleButton(int mx, int my, int button)
365 static int old_mx = 0, old_my = 0;
379 HandleGadgets(mx, my, button);
384 HandleMainMenu(mx,my, 0,0, button);
388 HandleTypeName(0, KSYM_Return);
392 HandleChooseLevel(mx,my, 0,0, button);
396 HandleHallOfFame(0,0, 0,0, button);
403 HandleHelpScreen(button);
407 HandleSetupScreen(mx,my, 0,0, button);
412 if (button == MB_RELEASED)
414 int sx = (mx - SX) / TILEX;
415 int sy = (my - SY) / TILEY;
417 if (IN_VIS_FIELD(sx,sy))
422 printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
424 if (!IN_LEV_FIELD(x, y))
427 printf(" Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
428 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
429 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
430 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
431 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
432 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
433 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
445 void HandleKey(Key key, int key_status)
448 boolean anyTextGadgetActiveOrJustFinished = anyTextGadgetActive();
449 static struct SetupKeyboardInfo custom_key;
457 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
458 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
459 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
460 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
461 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
462 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
465 if (game_status == PLAYING)
467 /* only needed for single-step tape recording mode */
468 static boolean clear_button_2[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
469 static boolean bomb_placed[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
472 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
477 if (setup.input[pnr].use_joystick)
480 custom_key = setup.input[pnr].key;
483 if (key == *key_info[i].key_custom)
484 key_action |= key_info[i].action;
486 if (tape.single_step && clear_button_2[pnr])
488 stored_player[pnr].action &= ~KEY_BUTTON_2;
489 clear_button_2[pnr] = FALSE;
492 if (key_status == KEY_PRESSED)
493 stored_player[pnr].action |= key_action;
495 stored_player[pnr].action &= ~key_action;
497 if (tape.single_step && tape.recording && tape.pausing)
499 if (key_status == KEY_PRESSED &&
500 (key_action & (KEY_MOTION | KEY_BUTTON_1)))
502 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
504 if (key_action & KEY_MOTION)
506 if (stored_player[pnr].action & KEY_BUTTON_2)
507 bomb_placed[pnr] = TRUE;
510 else if (key_status == KEY_RELEASED &&
511 (key_action & KEY_BUTTON_2))
513 if (!bomb_placed[pnr])
515 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
517 stored_player[pnr].action |= KEY_BUTTON_2;
518 clear_button_2[pnr] = TRUE;
521 bomb_placed[pnr] = FALSE;
524 else if (tape.recording && tape.pausing && (key_action & KEY_ACTION))
525 TapeTogglePause(TAPE_TOGGLE_MANUAL);
533 if (key == key_info[i].key_default)
534 joy |= key_info[i].action;
539 if (key_status == KEY_PRESSED)
540 key_joystick_mapping |= joy;
542 key_joystick_mapping &= ~joy;
547 if (game_status != PLAYING)
548 key_joystick_mapping = 0;
550 if (key_status == KEY_RELEASED)
553 if ((key == KSYM_Return || key == setup.shortcut.toggle_pause) &&
554 game_status == PLAYING && AllPlayersGone)
556 CloseDoor(DOOR_CLOSE_1);
557 game_status = MAINMENU;
562 /* allow quick escape to the main menu with the Escape key */
563 if (key == KSYM_Escape &&
564 game_status != MAINMENU &&
565 game_status != PLAYING &&
566 game_status != LEVELED &&
567 game_status != CHOOSELEVEL &&
568 game_status != SETUP)
570 game_status = MAINMENU;
575 /* special key shortcuts */
576 if (game_status == MAINMENU || game_status == PLAYING)
578 if (key == setup.shortcut.save_game)
580 else if (key == setup.shortcut.load_game)
582 else if (key == setup.shortcut.toggle_pause)
583 TapeTogglePause(TAPE_TOGGLE_MANUAL);
589 if (game_status == PLAYING && (tape.playing || tape.pausing))
596 HandleGadgetsKeyInput(key);
601 HandleTypeName(0, key);
610 if (game_status == MAINMENU)
611 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
612 else if (game_status == CHOOSELEVEL)
613 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
614 else if (game_status == SETUP)
615 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
619 if (game_status == CHOOSELEVEL)
620 HandleChooseLevel(0,0, 0,0, MB_MENU_LEAVE);
621 else if (game_status == SETUP)
622 HandleSetupScreen(0,0, 0,0, MB_MENU_LEAVE);
626 if (game_status == CHOOSELEVEL)
627 HandleChooseLevel(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
628 else if (game_status == SETUP)
629 HandleSetupScreen(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
633 if (game_status == CHOOSELEVEL)
634 HandleChooseLevel(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
635 else if (game_status == SETUP)
636 HandleSetupScreen(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
651 HandleHelpScreen(MB_RELEASED);
658 game_status = MAINMENU;
664 HandleHallOfFame(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
668 HandleHallOfFame(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
677 if (!anyTextGadgetActiveOrJustFinished || key == KSYM_Escape)
678 HandleLevelEditorKeyInput(key);
686 RequestQuitGame(setup.ask_on_escape);
702 if (GameFrameDelay == 500)
703 GameFrameDelay = GAME_FRAME_DELAY;
705 GameFrameDelay = 500;
708 GameFrameDelay = (key - KSYM_0) * 10;
709 printf("Game speed == %d%% (%d ms delay between two frames)\n",
710 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
716 options.debug = FALSE;
717 printf("debug mode disabled\n");
721 options.debug = TRUE;
722 printf("debug mode enabled\n");
727 if (!global.fps_slowdown)
729 global.fps_slowdown = TRUE;
730 global.fps_slowdown_factor = 2;
731 printf("fps slowdown enabled -- display only every 2nd frame\n");
733 else if (global.fps_slowdown_factor == 2)
735 global.fps_slowdown_factor = 4;
736 printf("fps slowdown enabled -- display only every 4th frame\n");
740 global.fps_slowdown = FALSE;
741 global.fps_slowdown_factor = 1;
742 printf("fps slowdown disabled\n");
748 if (ScrollStepSize == TILEX/8)
749 ScrollStepSize = TILEX/4;
751 ScrollStepSize = TILEX/8;
752 printf("ScrollStepSize == %d\n", ScrollStepSize);
761 ScrollStepSize = TILEX/4;
766 ScrollStepSize = TILEX/8;
768 printf("MoveSpeed == %d\n", MoveSpeed);
773 ScrollStepSize = TILEX/8;
774 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
778 ScrollStepSize = TILEX/4;
779 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
783 ScrollStepSize = TILEX/2;
784 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
788 ScrollStepSize = TILEX;
789 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
794 local_player->dynamite = 1000;
805 for(i=0; i<MAX_PLAYERS; i++)
807 printf("Player %d:\n", i);
808 printf(" jx == %d, jy == %d\n",
809 stored_player[i].jx, stored_player[i].jy);
810 printf(" last_jx == %d, last_jy == %d\n",
811 stored_player[i].last_jx, stored_player[i].last_jy);
832 if (button_status && game_status != PLAYING)
834 HandleButton(0, 0, -button_status);
838 #if defined(PLATFORM_UNIX)
846 static int HandleJoystickForAllPlayers()
851 for (i=0; i<MAX_PLAYERS; i++)
856 if (!setup.input[i].use_joystick)
860 joy_action = Joystick(i);
861 result |= joy_action;
863 if (!setup.input[i].use_joystick)
866 stored_player[i].action = joy_action;
872 void HandleJoystick()
874 int joystick = HandleJoystickForAllPlayers();
875 int keyboard = key_joystick_mapping;
876 int joy = (joystick | keyboard);
877 int left = joy & JOY_LEFT;
878 int right = joy & JOY_RIGHT;
879 int up = joy & JOY_UP;
880 int down = joy & JOY_DOWN;
881 int button = joy & JOY_BUTTON;
882 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
883 int dx = (left ? -1 : right ? 1 : 0);
884 int dy = (up ? -1 : down ? 1 : 0);
892 static unsigned long joystickmove_delay = 0;
894 if (joystick && !button &&
895 !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
896 newbutton = dx = dy = 0;
898 if (game_status == MAINMENU)
899 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
900 else if (game_status == CHOOSELEVEL)
901 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
902 else if (game_status == SETUP)
903 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
908 HandleHallOfFame(0,0, dx,dy, !newbutton);
912 HandleHelpScreen(!newbutton);
916 HandleLevelEditorIdle();
920 if (tape.playing || keyboard)
921 newbutton = ((joy & JOY_BUTTON) != 0);
923 if (AllPlayersGone && newbutton)
925 CloseDoor(DOOR_CLOSE_1);
926 game_status = MAINMENU;