1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
26 /* values for key_status */
27 #define KEY_NOT_PRESSED FALSE
28 #define KEY_RELEASED FALSE
29 #define KEY_PRESSED TRUE
35 if (XPending(display)) /* got event from X server */
39 XNextEvent(display, &event);
45 HandleButtonEvent((XButtonEvent *) &event);
49 HandleMotionEvent((XMotionEvent *) &event);
54 HandleKeyEvent((XKeyEvent *) &event);
58 HandleOtherEvents(&event);
65 /* don't use all CPU time when idle; the main loop while playing
66 has its own synchronization and is CPU friendly, too */
68 if (game_status != PLAYING)
70 XSync(display, FALSE);
74 if (game_status == EXITGAME)
79 void HandleOtherEvents(XEvent *event)
84 HandleExposeEvent((XExposeEvent *) event);
93 HandleFocusEvent((XFocusChangeEvent *) event);
97 HandleClientMessageEvent((XClientMessageEvent *) event);
105 void ClearEventQueue()
107 while(XPending(display))
111 XNextEvent(display, &event);
116 button_status = MB_RELEASED;
120 key_joystick_mapping = 0;
124 HandleOtherEvents(&event);
130 void SleepWhileUnmapped()
132 boolean window_unmapped = TRUE;
134 XAutoRepeatOn(display);
136 while(window_unmapped)
140 XNextEvent(display, &event);
145 button_status = MB_RELEASED;
149 key_joystick_mapping = 0;
153 window_unmapped = FALSE;
157 /* this is only to surely prevent the 'should not happen' case
158 * of recursively looping between 'SleepWhileUnmapped()' and
159 * 'HandleOtherEvents()' which usually calls this funtion.
164 HandleOtherEvents(&event);
169 if (game_status==PLAYING)
170 XAutoRepeatOff(display);
173 void HandleExposeEvent(XExposeEvent *event)
175 int x = event->x, y = event->y;
176 int width = event->width, height = event->height;
178 if (setup.direct_draw && game_status==PLAYING)
181 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
182 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
184 SetDrawtoField(DRAW_BACKBUFFER);
186 for(xx=0; xx<SCR_FIELDX; xx++)
187 for(yy=0; yy<SCR_FIELDY; yy++)
188 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
189 DrawScreenField(xx,yy);
192 SetDrawtoField(DRAW_DIRECT);
195 if (setup.soft_scrolling && game_status == PLAYING)
197 int fx = FX, fy = FY;
199 fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
200 fy += (ScreenMovDir & (MV_UP|MV_DOWN) ? ScreenGfxPos : 0);
202 XCopyArea(display,fieldbuffer,backbuffer,gc,
203 fx,fy, SXSIZE,SYSIZE,
207 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
212 void HandleButtonEvent(XButtonEvent *event)
214 motion_status = FALSE;
216 if (event->type==ButtonPress)
217 button_status = event->button;
219 button_status = MB_RELEASED;
221 HandleButton(event->x, event->y, button_status);
224 void HandleMotionEvent(XMotionEvent *event)
226 motion_status = TRUE;
228 HandleButton(event->x, event->y, button_status);
231 void HandleKeyEvent(XKeyEvent *event)
233 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
234 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
235 KeySym key = XLookupKeysym(event, event_state);
237 HandleKey(key, key_status);
240 void HandleFocusEvent(XFocusChangeEvent *event)
242 static int old_joystick_status = -1;
244 if (event->type == FocusOut)
246 XAutoRepeatOn(display);
247 old_joystick_status = joystick_status;
248 joystick_status = JOYSTICK_OFF;
249 key_joystick_mapping = 0;
251 else if (event->type == FocusIn)
253 if (game_status == PLAYING)
254 XAutoRepeatOff(display);
255 if (old_joystick_status != -1)
256 joystick_status = old_joystick_status;
260 void HandleClientMessageEvent(XClientMessageEvent *event)
263 if ((event->window == window) &&
264 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
269 void HandleButton(int mx, int my, int button)
271 static int old_mx = 0, old_my = 0;
283 HandleGadgets(mx,my, button);
284 HandleVideoButtons(mx,my, button);
285 HandleSoundButtons(mx,my, button);
286 HandleGameButtons(mx,my, button);
292 HandleMainMenu(mx,my, 0,0, button);
296 HandleTypeName(0, XK_Return);
300 HandleChooseLevel(mx,my, 0,0, button);
304 HandleHallOfFame(button);
308 LevelEd(mx,my, button);
312 HandleHelpScreen(button);
316 HandleSetupScreen(mx,my, 0,0, button);
320 HandleSetupInputScreen(mx,my, 0,0, button);
325 if (button == MB_RELEASED)
327 int sx = (mx - SX) / TILEX;
328 int sy = (my - SY) / TILEY;
330 if (IN_VIS_FIELD(sx,sy))
335 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
336 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
337 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
338 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
339 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
340 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
341 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
353 void HandleKey(KeySym key, int key_status)
356 static struct SetupKeyboardInfo custom_key;
359 KeySym *keysym_custom;
360 KeySym keysym_default;
364 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
365 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
366 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
367 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
368 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
369 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
372 if (game_status == PLAYING)
376 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
381 if (setup.input[pnr].use_joystick)
384 custom_key = setup.input[pnr].key;
387 if (key == *key_info[i].keysym_custom)
388 key_action |= key_info[i].action;
390 if (key_status == KEY_PRESSED)
391 stored_player[pnr].action |= key_action;
393 stored_player[pnr].action &= ~key_action;
401 if (key == key_info[i].keysym_default)
402 joy |= key_info[i].action;
407 if (key_status == KEY_PRESSED)
408 key_joystick_mapping |= joy;
410 key_joystick_mapping &= ~joy;
415 if (game_status != PLAYING)
416 key_joystick_mapping = 0;
418 if (key_status == KEY_RELEASED)
421 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
423 CloseDoor(DOOR_CLOSE_1);
424 game_status = MAINMENU;
429 /* allow quick escape to the main menu with the Escape key */
430 if (key == XK_Escape && game_status != MAINMENU)
432 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
433 game_status = MAINMENU;
442 if (game_status == PLAYING && (tape.playing || tape.pausing))
452 HandleTypeName(0, key);
462 if (game_status == MAINMENU)
463 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
464 else if (game_status == CHOOSELEVEL)
465 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
466 else if (game_status == SETUP)
467 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
468 else if (game_status == SETUPINPUT)
469 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
478 HandleHelpScreen(MB_RELEASED);
485 game_status = MAINMENU;
496 LevelNameTyping(key);
517 if (GameFrameDelay == 500)
518 GameFrameDelay = GAME_FRAME_DELAY;
520 GameFrameDelay = 500;
523 GameFrameDelay = (key - XK_0) * 10;
524 printf("Game speed == %d%% (%d ms delay between two frames)\n",
525 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
531 if (ScrollStepSize == TILEX/8)
532 ScrollStepSize = TILEX/4;
534 ScrollStepSize = TILEX/8;
535 printf("ScrollStepSize == %d\n", ScrollStepSize);
540 ScrollStepSize = TILEX/8;
541 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
545 ScrollStepSize = TILEX/4;
546 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
550 ScrollStepSize = TILEX/2;
551 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
555 ScrollStepSize = TILEX;
556 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
563 local_player->dynamite = 1000;
574 for(i=0; i<MAX_PLAYERS; i++)
576 printf("Player %d:\n", i);
577 printf(" jx == %d, jy == %d\n",
578 stored_player[i].jx, stored_player[i].jy);
579 printf(" last_jx == %d, last_jy == %d\n",
580 stored_player[i].last_jx, stored_player[i].last_jy);
599 void HandleNoXEvent()
601 if (button_status && game_status != PLAYING)
603 HandleButton(-1,-1, button_status);
614 if (game_status == PLAYING)
618 static int HandleJoystickForAllPlayers()
623 for (i=0; i<MAX_PLAYERS; i++)
628 if (!setup.input[i].use_joystick)
632 joy_action = Joystick(i);
633 result |= joy_action;
636 if (!setup.input[i].use_joystick)
640 stored_player[i].action = joy_action;
646 void HandleJoystick()
648 int joystick = HandleJoystickForAllPlayers();
649 int keyboard = key_joystick_mapping;
650 int joy = (joystick | keyboard);
651 int left = joy & JOY_LEFT;
652 int right = joy & JOY_RIGHT;
653 int up = joy & JOY_UP;
654 int down = joy & JOY_DOWN;
655 int button = joy & JOY_BUTTON;
656 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
657 int dx = (left ? -1 : right ? 1 : 0);
658 int dy = (up ? -1 : down ? 1 : 0);
667 static unsigned long joystickmove_delay = 0;
669 if (joystick && !button && !DelayReached(&joystickmove_delay, 150))
670 newbutton = dx = dy = 0;
672 if (game_status==MAINMENU)
673 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
674 else if (game_status==CHOOSELEVEL)
675 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
676 else if (game_status==SETUP)
677 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
678 else if (game_status==SETUPINPUT)
679 HandleSetupInputScreen(0,0,dx,dy,
680 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
685 HandleHallOfFame(!newbutton);
689 HandleHelpScreen(!newbutton);
693 if (tape.playing || keyboard)
694 newbutton = ((joy & JOY_BUTTON) != 0);
696 if (AllPlayersGone && newbutton)
698 CloseDoor(DOOR_CLOSE_1);
699 game_status = MAINMENU;