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 == GAME_MODE_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 != GAME_MODE_EDITOR &&
62 game_status != GAME_MODE_PLAYING)
68 /* this is only really needed for non-SDL targets to filter unwanted events;
69 when using SDL with properly installed event filter, this function can be
70 replaced with a simple "NextEvent()" call, but it doesn't hurt either */
72 static boolean NextValidEvent(Event *event)
74 while (PendingEvent())
78 if (FilterMouseMotionEvents(event))
89 if (PendingEvent()) /* got event */
93 if (NextValidEvent(&event))
97 case EVENT_BUTTONPRESS:
98 case EVENT_BUTTONRELEASE:
99 HandleButtonEvent((ButtonEvent *) &event);
102 case EVENT_MOTIONNOTIFY:
103 HandleMotionEvent((MotionEvent *) &event);
107 case EVENT_KEYRELEASE:
108 HandleKeyEvent((KeyEvent *) &event);
112 HandleOtherEvents(&event);
119 /* when playing, display a special mouse pointer inside the playfield */
120 if (game_status == GAME_MODE_PLAYING)
122 if (!playfield_cursor_set && cursor_inside_playfield &&
123 DelayReached(&playfield_cursor_delay, 1000))
125 SetMouseCursor(CURSOR_PLAYFIELD);
126 playfield_cursor_set = TRUE;
129 else if (playfield_cursor_set)
131 SetMouseCursor(CURSOR_DEFAULT);
132 playfield_cursor_set = FALSE;
138 /* don't use all CPU time when idle; the main loop while playing
139 has its own synchronization and is CPU friendly, too */
141 if (game_status == GAME_MODE_PLAYING)
146 if (!PendingEvent()) /* delay only if no pending events */
150 /* refresh window contents from drawing buffer, if needed */
153 if (game_status == GAME_MODE_QUIT)
158 void HandleOtherEvents(Event *event)
163 HandleExposeEvent((ExposeEvent *) event);
166 case EVENT_UNMAPNOTIFY:
168 /* This causes the game to stop not only when iconified, but also
169 when on another virtual desktop, which might be not desired. */
170 SleepWhileUnmapped();
176 HandleFocusEvent((FocusChangeEvent *) event);
179 case EVENT_CLIENTMESSAGE:
180 HandleClientMessageEvent((ClientMessageEvent *) event);
183 #if defined(TARGET_SDL)
184 case SDL_JOYAXISMOTION:
185 case SDL_JOYBUTTONDOWN:
186 case SDL_JOYBUTTONUP:
187 HandleJoystickEvent(event);
196 void ClearEventQueue()
198 while (PendingEvent())
206 case EVENT_BUTTONRELEASE:
207 button_status = MB_RELEASED;
210 case EVENT_KEYRELEASE:
211 key_joystick_mapping = 0;
215 HandleOtherEvents(&event);
221 void ClearPlayerAction()
225 /* simulate key release events for still pressed keys */
226 key_joystick_mapping = 0;
227 for (i=0; i<MAX_PLAYERS; i++)
228 stored_player[i].action = 0;
231 void SleepWhileUnmapped()
233 boolean window_unmapped = TRUE;
235 KeyboardAutoRepeatOn();
237 while(window_unmapped)
245 case EVENT_BUTTONRELEASE:
246 button_status = MB_RELEASED;
249 case EVENT_KEYRELEASE:
250 key_joystick_mapping = 0;
253 case EVENT_MAPNOTIFY:
254 window_unmapped = FALSE;
257 case EVENT_UNMAPNOTIFY:
258 /* this is only to surely prevent the 'should not happen' case
259 * of recursively looping between 'SleepWhileUnmapped()' and
260 * 'HandleOtherEvents()' which usually calls this funtion.
265 HandleOtherEvents(&event);
270 if (game_status == GAME_MODE_PLAYING)
271 KeyboardAutoRepeatOffUnlessAutoplay();
274 void HandleExposeEvent(ExposeEvent *event)
277 RedrawPlayfield(FALSE, event->x, event->y, event->width, event->height);
282 void HandleButtonEvent(ButtonEvent *event)
284 motion_status = FALSE;
286 if (event->type == EVENT_BUTTONPRESS)
287 button_status = event->button;
289 button_status = MB_RELEASED;
291 HandleButton(event->x, event->y, button_status);
294 void HandleMotionEvent(MotionEvent *event)
296 if (!PointerInWindow(window))
297 return; /* window and pointer are on different screens */
300 if (button_status == MB_RELEASED && game_status != GAME_MODE_EDITOR)
304 motion_status = TRUE;
306 HandleButton(event->x, event->y, button_status);
309 void HandleKeyEvent(KeyEvent *event)
311 int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
312 boolean with_modifiers = (game_status == GAME_MODE_PLAYING ? FALSE : TRUE);
313 Key key = GetEventKey(event, with_modifiers);
315 HandleKey(key, key_status);
318 void HandleFocusEvent(FocusChangeEvent *event)
320 static int old_joystick_status = -1;
322 if (event->type == EVENT_FOCUSOUT)
324 KeyboardAutoRepeatOn();
325 old_joystick_status = joystick.status;
326 joystick.status = JOYSTICK_NOT_AVAILABLE;
330 else if (event->type == EVENT_FOCUSIN)
332 /* When there are two Rocks'n'Diamonds windows which overlap and
333 the player moves the pointer from one game window to the other,
334 a 'FocusOut' event is generated for the window the pointer is
335 leaving and a 'FocusIn' event is generated for the window the
336 pointer is entering. In some cases, it can happen that the
337 'FocusIn' event is handled by the one game process before the
338 'FocusOut' event by the other game process. In this case the
339 X11 environment would end up with activated keyboard auto repeat,
340 because unfortunately this is a global setting and not (which
341 would be far better) set for each X11 window individually.
342 The effect would be keyboard auto repeat while playing the game
343 (game_status == GAME_MODE_PLAYING), which is not desired.
344 To avoid this special case, we just wait 1/10 second before
345 processing the 'FocusIn' event.
348 if (game_status == GAME_MODE_PLAYING)
351 KeyboardAutoRepeatOffUnlessAutoplay();
353 if (old_joystick_status != -1)
354 joystick.status = old_joystick_status;
358 void HandleClientMessageEvent(ClientMessageEvent *event)
360 if (CheckCloseWindowEvent(event))
364 void HandleButton(int mx, int my, int button)
366 static int old_mx = 0, old_my = 0;
380 HandleGadgets(mx, my, button);
385 HandleMainMenu(mx,my, 0,0, button);
388 case GAME_MODE_PSEUDO_TYPENAME:
389 HandleTypeName(0, KSYM_Return);
392 case GAME_MODE_LEVELS:
393 HandleChooseLevel(mx,my, 0,0, button);
396 case GAME_MODE_SCORES:
397 HandleHallOfFame(0,0, 0,0, button);
400 case GAME_MODE_EDITOR:
404 HandleHelpScreen(button);
407 case GAME_MODE_SETUP:
408 HandleSetupScreen(mx,my, 0,0, button);
411 case GAME_MODE_PLAYING:
413 if (button == MB_RELEASED)
415 int sx = (mx - SX) / TILEX;
416 int sy = (my - SY) / TILEY;
418 if (IN_VIS_FIELD(sx,sy))
423 printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
425 if (!IN_LEV_FIELD(x, y))
428 printf(" Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
429 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
430 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
431 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
432 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
433 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
434 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
435 printf(" GfxElement[%d][%d] == %d\n", x,y, GfxElement[x][y]);
447 void HandleKey(Key key, int key_status)
450 boolean anyTextGadgetActiveOrJustFinished = anyTextGadgetActive();
451 static struct SetupKeyboardInfo custom_key;
459 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
460 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
461 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
462 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
463 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
464 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
467 if (game_status == GAME_MODE_PLAYING)
469 /* only needed for single-step tape recording mode */
470 static boolean clear_button_2[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
471 static boolean bomb_placed[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
474 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
479 if (setup.input[pnr].use_joystick)
482 custom_key = setup.input[pnr].key;
485 if (key == *key_info[i].key_custom)
486 key_action |= key_info[i].action;
488 if (tape.single_step && clear_button_2[pnr])
490 stored_player[pnr].action &= ~KEY_BUTTON_2;
491 clear_button_2[pnr] = FALSE;
494 if (key_status == KEY_PRESSED)
495 stored_player[pnr].action |= key_action;
497 stored_player[pnr].action &= ~key_action;
499 if (tape.single_step && tape.recording && tape.pausing)
501 if (key_status == KEY_PRESSED &&
502 (key_action & (KEY_MOTION | KEY_BUTTON_1)))
504 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
506 if (key_action & KEY_MOTION)
508 if (stored_player[pnr].action & KEY_BUTTON_2)
509 bomb_placed[pnr] = TRUE;
512 else if (key_status == KEY_RELEASED &&
513 (key_action & KEY_BUTTON_2))
515 if (!bomb_placed[pnr])
517 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
519 stored_player[pnr].action |= KEY_BUTTON_2;
520 clear_button_2[pnr] = TRUE;
523 bomb_placed[pnr] = FALSE;
526 else if (tape.recording && tape.pausing && (key_action & KEY_ACTION))
527 TapeTogglePause(TAPE_TOGGLE_MANUAL);
535 if (key == key_info[i].key_default)
536 joy |= key_info[i].action;
541 if (key_status == KEY_PRESSED)
542 key_joystick_mapping |= joy;
544 key_joystick_mapping &= ~joy;
549 if (game_status != GAME_MODE_PLAYING)
550 key_joystick_mapping = 0;
552 if (key_status == KEY_RELEASED)
555 if ((key == KSYM_Return || key == setup.shortcut.toggle_pause) &&
556 game_status == GAME_MODE_PLAYING && AllPlayersGone)
558 CloseDoor(DOOR_CLOSE_1);
559 game_status = GAME_MODE_MAIN;
564 /* allow quick escape to the main menu with the Escape key */
565 if (key == KSYM_Escape &&
566 game_status != GAME_MODE_MAIN &&
567 game_status != GAME_MODE_PLAYING &&
568 game_status != GAME_MODE_EDITOR &&
569 game_status != GAME_MODE_LEVELS &&
570 game_status != GAME_MODE_SETUP)
572 game_status = GAME_MODE_MAIN;
577 /* special key shortcuts */
578 if (game_status == GAME_MODE_MAIN || game_status == GAME_MODE_PLAYING)
580 if (key == setup.shortcut.save_game)
582 else if (key == setup.shortcut.load_game)
584 else if (key == setup.shortcut.toggle_pause)
585 TapeTogglePause(TAPE_TOGGLE_MANUAL);
591 if (game_status == GAME_MODE_PLAYING && (tape.playing || tape.pausing))
598 HandleGadgetsKeyInput(key);
602 case GAME_MODE_PSEUDO_TYPENAME:
603 HandleTypeName(0, key);
607 case GAME_MODE_LEVELS:
608 case GAME_MODE_SETUP:
612 if (game_status == GAME_MODE_MAIN)
613 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
614 else if (game_status == GAME_MODE_LEVELS)
615 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
616 else if (game_status == GAME_MODE_SETUP)
617 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
621 if (game_status == GAME_MODE_LEVELS)
622 HandleChooseLevel(0,0, 0,0, MB_MENU_LEAVE);
623 else if (game_status == GAME_MODE_SETUP)
624 HandleSetupScreen(0,0, 0,0, MB_MENU_LEAVE);
628 if (game_status == GAME_MODE_LEVELS)
629 HandleChooseLevel(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
630 else if (game_status == GAME_MODE_SETUP)
631 HandleSetupScreen(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
635 if (game_status == GAME_MODE_LEVELS)
636 HandleChooseLevel(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
637 else if (game_status == GAME_MODE_SETUP)
638 HandleSetupScreen(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
653 HandleHelpScreen(MB_RELEASED);
656 case GAME_MODE_SCORES:
660 game_status = GAME_MODE_MAIN;
666 HandleHallOfFame(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
670 HandleHallOfFame(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
678 case GAME_MODE_EDITOR:
679 if (!anyTextGadgetActiveOrJustFinished || key == KSYM_Escape)
680 HandleLevelEditorKeyInput(key);
683 case GAME_MODE_PLAYING:
688 RequestQuitGame(setup.ask_on_escape);
704 if (GameFrameDelay == 500)
705 GameFrameDelay = GAME_FRAME_DELAY;
707 GameFrameDelay = 500;
710 GameFrameDelay = (key - KSYM_0) * 10;
711 printf("Game speed == %d%% (%d ms delay between two frames)\n",
712 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
718 options.debug = FALSE;
719 printf("debug mode disabled\n");
723 options.debug = TRUE;
724 printf("debug mode enabled\n");
729 if (!global.fps_slowdown)
731 global.fps_slowdown = TRUE;
732 global.fps_slowdown_factor = 2;
733 printf("fps slowdown enabled -- display only every 2nd frame\n");
735 else if (global.fps_slowdown_factor == 2)
737 global.fps_slowdown_factor = 4;
738 printf("fps slowdown enabled -- display only every 4th frame\n");
742 global.fps_slowdown = FALSE;
743 global.fps_slowdown_factor = 1;
744 printf("fps slowdown disabled\n");
750 if (ScrollStepSize == TILEX/8)
751 ScrollStepSize = TILEX/4;
753 ScrollStepSize = TILEX/8;
754 printf("ScrollStepSize == %d\n", ScrollStepSize);
763 ScrollStepSize = TILEX/4;
768 ScrollStepSize = TILEX/8;
770 printf("MoveSpeed == %d\n", MoveSpeed);
775 ScrollStepSize = TILEX/8;
776 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
780 ScrollStepSize = TILEX/4;
781 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
785 ScrollStepSize = TILEX/2;
786 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
790 ScrollStepSize = TILEX;
791 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
796 local_player->dynamite = 1000;
807 for(i=0; i<MAX_PLAYERS; i++)
809 printf("Player %d:\n", i);
810 printf(" jx == %d, jy == %d\n",
811 stored_player[i].jx, stored_player[i].jy);
812 printf(" last_jx == %d, last_jy == %d\n",
813 stored_player[i].last_jx, stored_player[i].last_jy);
834 if (button_status && game_status != GAME_MODE_PLAYING)
836 HandleButton(0, 0, -button_status);
840 #if defined(PLATFORM_UNIX)
848 static int HandleJoystickForAllPlayers()
853 for (i=0; i<MAX_PLAYERS; i++)
858 if (!setup.input[i].use_joystick)
862 joy_action = Joystick(i);
863 result |= joy_action;
865 if (!setup.input[i].use_joystick)
868 stored_player[i].action = joy_action;
874 void HandleJoystick()
876 int joystick = HandleJoystickForAllPlayers();
877 int keyboard = key_joystick_mapping;
878 int joy = (joystick | keyboard);
879 int left = joy & JOY_LEFT;
880 int right = joy & JOY_RIGHT;
881 int up = joy & JOY_UP;
882 int down = joy & JOY_DOWN;
883 int button = joy & JOY_BUTTON;
884 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
885 int dx = (left ? -1 : right ? 1 : 0);
886 int dy = (up ? -1 : down ? 1 : 0);
891 case GAME_MODE_LEVELS:
892 case GAME_MODE_SETUP:
894 static unsigned long joystickmove_delay = 0;
896 if (joystick && !button &&
897 !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
898 newbutton = dx = dy = 0;
900 if (game_status == GAME_MODE_MAIN)
901 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
902 else if (game_status == GAME_MODE_LEVELS)
903 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
904 else if (game_status == GAME_MODE_SETUP)
905 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
909 case GAME_MODE_SCORES:
910 HandleHallOfFame(0,0, dx,dy, !newbutton);
914 HandleHelpScreen(!newbutton);
917 case GAME_MODE_EDITOR:
918 HandleLevelEditorIdle();
921 case GAME_MODE_PLAYING:
922 if (tape.playing || keyboard)
923 newbutton = ((joy & JOY_BUTTON) != 0);
925 if (AllPlayersGone && newbutton)
927 CloseDoor(DOOR_CLOSE_1);
928 game_status = GAME_MODE_MAIN;