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)
261 if ((event->window == window) &&
262 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
266 void HandleButton(int mx, int my, int button)
268 static int old_mx = 0, old_my = 0;
280 HandleVideoButtons(mx,my, button);
281 HandleSoundButtons(mx,my, button);
282 HandleGameButtons(mx,my, button);
288 HandleMainMenu(mx,my, 0,0, button);
292 HandleTypeName(0, XK_Return);
296 HandleChooseLevel(mx,my, 0,0, button);
300 HandleHallOfFame(button);
304 LevelEd(mx,my, button);
308 HandleHelpScreen(button);
312 HandleSetupScreen(mx,my, 0,0, button);
316 HandleSetupInputScreen(mx,my, 0,0, button);
321 if (button == MB_RELEASED)
323 int sx = (mx - SX) / TILEX;
324 int sy = (my - SY) / TILEY;
326 if (IN_VIS_FIELD(sx,sy))
331 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
332 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
333 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
334 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
335 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
336 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
337 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
349 void HandleKey(KeySym key, int key_status)
352 static struct SetupKeyboardInfo custom_key;
355 KeySym *keysym_custom;
356 KeySym keysym_default;
360 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
361 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
362 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
363 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
364 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
365 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
368 if (game_status == PLAYING)
372 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
377 if (setup.input[pnr].use_joystick)
380 custom_key = setup.input[pnr].key;
383 if (key == *key_info[i].keysym_custom)
384 key_action |= key_info[i].action;
386 if (key_status == KEY_PRESSED)
387 stored_player[pnr].action |= key_action;
389 stored_player[pnr].action &= ~key_action;
397 if (key == key_info[i].keysym_default)
398 joy |= key_info[i].action;
403 if (key_status == KEY_PRESSED)
404 key_joystick_mapping |= joy;
406 key_joystick_mapping &= ~joy;
411 if (game_status != PLAYING)
412 key_joystick_mapping = 0;
414 if (key_status == KEY_RELEASED)
417 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
419 CloseDoor(DOOR_CLOSE_1);
420 game_status = MAINMENU;
425 /* allow quick escape to the main menu with the Escape key */
426 if (key == XK_Escape && game_status != MAINMENU)
428 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
429 game_status = MAINMENU;
438 if (game_status == PLAYING && (tape.playing || tape.pausing))
448 HandleTypeName(0, key);
458 if (game_status == MAINMENU)
459 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
460 else if (game_status == CHOOSELEVEL)
461 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
462 else if (game_status == SETUP)
463 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
464 else if (game_status == SETUPINPUT)
465 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
474 HandleHelpScreen(MB_RELEASED);
481 game_status = MAINMENU;
492 LevelNameTyping(key);
513 if (GameFrameDelay == 500)
514 GameFrameDelay = GAME_FRAME_DELAY;
516 GameFrameDelay = 500;
519 GameFrameDelay = (key - XK_0) * 10;
520 printf("Game speed == %d%% (%d ms delay between two frames)\n",
521 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
527 if (ScrollStepSize == TILEX/8)
528 ScrollStepSize = TILEX/4;
530 ScrollStepSize = TILEX/8;
531 printf("ScrollStepSize == %d\n", ScrollStepSize);
536 ScrollStepSize = TILEX/8;
537 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
541 ScrollStepSize = TILEX/4;
542 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
546 ScrollStepSize = TILEX/2;
547 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
551 ScrollStepSize = TILEX;
552 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
559 local_player->dynamite = 1000;
570 for(i=0; i<MAX_PLAYERS; i++)
572 printf("Player %d:\n", i);
573 printf(" jx == %d, jy == %d\n",
574 stored_player[i].jx, stored_player[i].jy);
575 printf(" last_jx == %d, last_jy == %d\n",
576 stored_player[i].last_jx, stored_player[i].last_jy);
595 void HandleNoXEvent()
597 if (button_status && game_status != PLAYING)
599 HandleButton(-1,-1, button_status);
608 if (game_status == PLAYING)
612 static int HandleJoystickForAllPlayers()
617 for (i=0; i<MAX_PLAYERS; i++)
621 if (!setup.input[i].use_joystick)
624 joy_action = Joystick(i);
625 result |= joy_action;
627 stored_player[i].action = joy_action;
633 void HandleJoystick()
635 int joystick = HandleJoystickForAllPlayers();
636 int keyboard = key_joystick_mapping;
637 int joy = (joystick | keyboard);
638 int left = joy & JOY_LEFT;
639 int right = joy & JOY_RIGHT;
640 int up = joy & JOY_UP;
641 int down = joy & JOY_DOWN;
642 int button = joy & JOY_BUTTON;
643 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
644 int dx = (left ? -1 : right ? 1 : 0);
645 int dy = (up ? -1 : down ? 1 : 0);
654 static long joystickmove_delay = 0;
656 if (joystick && !button && !DelayReached(&joystickmove_delay, 150))
657 newbutton = dx = dy = 0;
659 if (game_status==MAINMENU)
660 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
661 else if (game_status==CHOOSELEVEL)
662 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
663 else if (game_status==SETUP)
664 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
665 else if (game_status==SETUPINPUT)
666 HandleSetupInputScreen(0,0,dx,dy,
667 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
672 HandleHallOfFame(!newbutton);
676 HandleHelpScreen(!newbutton);
680 if (tape.playing || keyboard)
681 newbutton = ((joy & JOY_BUTTON) != 0);
683 if (AllPlayersGone && newbutton)
685 CloseDoor(DOOR_CLOSE_1);
686 game_status = MAINMENU;