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 ***********************************************************/
25 /* values for key_status */
26 #define KEY_NOT_PRESSED FALSE
27 #define KEY_RELEASED FALSE
28 #define KEY_PRESSED TRUE
34 if (XPending(display)) /* got event from X server */
38 XNextEvent(display, &event);
44 HandleButtonEvent((XButtonEvent *) &event);
48 HandleMotionEvent((XMotionEvent *) &event);
53 HandleKeyEvent((XKeyEvent *) &event);
57 HandleOtherEvents(&event);
64 /* don't use all CPU time when idle; the main loop while playing
65 has its own synchronization and is CPU friendly, too */
67 if (game_status != PLAYING)
69 XSync(display, FALSE);
73 if (game_status == EXITGAME)
78 void HandleOtherEvents(XEvent *event)
83 HandleExposeEvent((XExposeEvent *) event);
92 HandleFocusEvent((XFocusChangeEvent *) event);
96 HandleClientMessageEvent((XClientMessageEvent *) event);
104 void ClearEventQueue()
106 while(XPending(display))
110 XNextEvent(display, &event);
115 button_status = MB_RELEASED;
119 key_joystick_mapping = 0;
123 HandleOtherEvents(&event);
129 void SleepWhileUnmapped()
131 boolean window_unmapped = TRUE;
133 XAutoRepeatOn(display);
135 while(window_unmapped)
139 XNextEvent(display, &event);
144 button_status = MB_RELEASED;
148 key_joystick_mapping = 0;
152 window_unmapped = FALSE;
156 /* this is only to surely prevent the 'should not happen' case
157 * of recursively looping between 'SleepWhileUnmapped()' and
158 * 'HandleOtherEvents()' which usually calls this funtion.
163 HandleOtherEvents(&event);
168 if (game_status==PLAYING)
169 XAutoRepeatOff(display);
172 void HandleExposeEvent(XExposeEvent *event)
174 int x = event->x, y = event->y;
175 int width = event->width, height = event->height;
177 if (setup.direct_draw && game_status==PLAYING)
180 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
181 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
183 SetDrawtoField(DRAW_BACKBUFFER);
185 for(xx=0; xx<SCR_FIELDX; xx++)
186 for(yy=0; yy<SCR_FIELDY; yy++)
187 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
188 DrawScreenField(xx,yy);
191 SetDrawtoField(DRAW_DIRECT);
194 if (setup.soft_scrolling && game_status == PLAYING)
196 int fx = FX, fy = FY;
198 fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
199 fy += (ScreenMovDir & (MV_UP|MV_DOWN) ? ScreenGfxPos : 0);
201 XCopyArea(display,fieldbuffer,backbuffer,gc,
202 fx,fy, SXSIZE,SYSIZE,
206 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
211 void HandleButtonEvent(XButtonEvent *event)
213 motion_status = FALSE;
215 if (event->type==ButtonPress)
216 button_status = event->button;
218 button_status = MB_RELEASED;
220 HandleButton(event->x, event->y, button_status);
223 void HandleMotionEvent(XMotionEvent *event)
225 motion_status = TRUE;
227 HandleButton(event->x, event->y, button_status);
230 void HandleKeyEvent(XKeyEvent *event)
232 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
233 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
234 KeySym key = XLookupKeysym(event, event_state);
236 HandleKey(key, key_status);
239 void HandleFocusEvent(XFocusChangeEvent *event)
241 static int old_joystick_status = -1;
243 if (event->type == FocusOut)
245 XAutoRepeatOn(display);
246 old_joystick_status = joystick_status;
247 joystick_status = JOYSTICK_OFF;
248 key_joystick_mapping = 0;
250 else if (event->type == FocusIn)
252 if (game_status == PLAYING)
253 XAutoRepeatOff(display);
254 if (old_joystick_status != -1)
255 joystick_status = old_joystick_status;
259 void HandleClientMessageEvent(XClientMessageEvent *event)
262 if ((event->window == window) &&
263 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
268 void HandleButton(int mx, int my, int button)
270 static int old_mx = 0, old_my = 0;
282 HandleVideoButtons(mx,my, button);
283 HandleSoundButtons(mx,my, button);
284 HandleGameButtons(mx,my, button);
290 HandleMainMenu(mx,my, 0,0, button);
294 HandleTypeName(0, XK_Return);
298 HandleChooseLevel(mx,my, 0,0, button);
302 HandleHallOfFame(button);
306 LevelEd(mx,my, button);
310 HandleHelpScreen(button);
314 HandleSetupScreen(mx,my, 0,0, button);
318 HandleSetupInputScreen(mx,my, 0,0, button);
323 if (button == MB_RELEASED)
325 int sx = (mx - SX) / TILEX;
326 int sy = (my - SY) / TILEY;
328 if (IN_VIS_FIELD(sx,sy))
333 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
334 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
335 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
336 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
337 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
338 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
339 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
351 void HandleKey(KeySym key, int key_status)
354 static struct SetupKeyboardInfo custom_key;
357 KeySym *keysym_custom;
358 KeySym keysym_default;
362 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
363 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
364 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
365 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
366 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
367 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
370 if (game_status == PLAYING)
374 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
379 if (setup.input[pnr].use_joystick)
382 custom_key = setup.input[pnr].key;
385 if (key == *key_info[i].keysym_custom)
386 key_action |= key_info[i].action;
388 if (key_status == KEY_PRESSED)
389 stored_player[pnr].action |= key_action;
391 stored_player[pnr].action &= ~key_action;
399 if (key == key_info[i].keysym_default)
400 joy |= key_info[i].action;
405 if (key_status == KEY_PRESSED)
406 key_joystick_mapping |= joy;
408 key_joystick_mapping &= ~joy;
413 if (game_status != PLAYING)
414 key_joystick_mapping = 0;
416 if (key_status == KEY_RELEASED)
419 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
421 CloseDoor(DOOR_CLOSE_1);
422 game_status = MAINMENU;
427 /* allow quick escape to the main menu with the Escape key */
428 if (key == XK_Escape && game_status != MAINMENU)
430 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
431 game_status = MAINMENU;
440 if (game_status == PLAYING && (tape.playing || tape.pausing))
450 HandleTypeName(0, key);
460 if (game_status == MAINMENU)
461 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
462 else if (game_status == CHOOSELEVEL)
463 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
464 else if (game_status == SETUP)
465 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
466 else if (game_status == SETUPINPUT)
467 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
476 HandleHelpScreen(MB_RELEASED);
483 game_status = MAINMENU;
494 LevelNameTyping(key);
515 if (GameFrameDelay == 500)
516 GameFrameDelay = GAME_FRAME_DELAY;
518 GameFrameDelay = 500;
521 GameFrameDelay = (key - XK_0) * 10;
522 printf("Game speed == %d%% (%d ms delay between two frames)\n",
523 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
529 if (ScrollStepSize == TILEX/8)
530 ScrollStepSize = TILEX/4;
532 ScrollStepSize = TILEX/8;
533 printf("ScrollStepSize == %d\n", ScrollStepSize);
538 ScrollStepSize = TILEX/8;
539 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
543 ScrollStepSize = TILEX/4;
544 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
548 ScrollStepSize = TILEX/2;
549 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
553 ScrollStepSize = TILEX;
554 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
561 local_player->dynamite = 1000;
572 for(i=0; i<MAX_PLAYERS; i++)
574 printf("Player %d:\n", i);
575 printf(" jx == %d, jy == %d\n",
576 stored_player[i].jx, stored_player[i].jy);
577 printf(" last_jx == %d, last_jy == %d\n",
578 stored_player[i].last_jx, stored_player[i].last_jy);
597 void HandleNoXEvent()
599 if (button_status && game_status != PLAYING)
601 HandleButton(-1,-1, button_status);
612 if (game_status == PLAYING)
616 static int HandleJoystickForAllPlayers()
621 for (i=0; i<MAX_PLAYERS; i++)
626 if (!setup.input[i].use_joystick)
630 joy_action = Joystick(i);
631 result |= joy_action;
634 if (!setup.input[i].use_joystick)
638 stored_player[i].action = joy_action;
644 void HandleJoystick()
646 int joystick = HandleJoystickForAllPlayers();
647 int keyboard = key_joystick_mapping;
648 int joy = (joystick | keyboard);
649 int left = joy & JOY_LEFT;
650 int right = joy & JOY_RIGHT;
651 int up = joy & JOY_UP;
652 int down = joy & JOY_DOWN;
653 int button = joy & JOY_BUTTON;
654 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
655 int dx = (left ? -1 : right ? 1 : 0);
656 int dy = (up ? -1 : down ? 1 : 0);
665 static long joystickmove_delay = 0;
667 if (joystick && !button && !DelayReached(&joystickmove_delay, 150))
668 newbutton = dx = dy = 0;
670 if (game_status==MAINMENU)
671 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
672 else if (game_status==CHOOSELEVEL)
673 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
674 else if (game_status==SETUP)
675 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
676 else if (game_status==SETUPINPUT)
677 HandleSetupInputScreen(0,0,dx,dy,
678 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
683 HandleHallOfFame(!newbutton);
687 HandleHelpScreen(!newbutton);
691 if (tape.playing || keyboard)
692 newbutton = ((joy & JOY_BUTTON) != 0);
694 if (AllPlayersGone && newbutton)
696 CloseDoor(DOOR_CLOSE_1);
697 game_status = MAINMENU;