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 ***********************************************************/
29 if (XPending(display)) /* got event from X server */
33 XNextEvent(display, &event);
39 HandleButtonEvent((XButtonEvent *) &event);
43 HandleMotionEvent((XMotionEvent *) &event);
48 HandleKeyEvent((XKeyEvent *) &event);
52 HandleOtherEvents(&event);
59 /* don't use all CPU time when idle; the main loop while playing
60 has its own synchronization and is CPU friendly, too */
62 if (game_status != PLAYING)
64 XSync(display, FALSE);
68 if (game_status == EXITGAME)
73 void HandleOtherEvents(XEvent *event)
78 HandleExposeEvent((XExposeEvent *) event);
87 HandleFocusEvent((XFocusChangeEvent *) event);
91 HandleClientMessageEvent((XClientMessageEvent *) event);
99 void ClearEventQueue()
101 while(XPending(display))
105 XNextEvent(display, &event);
110 button_status = MB_RELEASED;
114 key_joystick_mapping = 0;
118 HandleOtherEvents(&event);
124 void SleepWhileUnmapped()
126 boolean window_unmapped = TRUE;
128 XAutoRepeatOn(display);
130 while(window_unmapped)
134 XNextEvent(display, &event);
139 button_status = MB_RELEASED;
143 key_joystick_mapping = 0;
147 window_unmapped = FALSE;
151 /* this is only to surely prevent the 'should not happen' case
152 * of recursively looping between 'SleepWhileUnmapped()' and
153 * 'HandleOtherEvents()' which usually calls this funtion.
158 HandleOtherEvents(&event);
163 if (game_status==PLAYING)
164 XAutoRepeatOff(display);
167 void HandleExposeEvent(XExposeEvent *event)
169 int x = event->x, y = event->y;
170 int width = event->width, height = event->height;
172 if (setup.direct_draw && game_status==PLAYING)
175 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
176 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
178 SetDrawtoField(DRAW_BACKBUFFER);
180 for(xx=0; xx<SCR_FIELDX; xx++)
181 for(yy=0; yy<SCR_FIELDY; yy++)
182 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
183 DrawScreenField(xx,yy);
186 SetDrawtoField(DRAW_DIRECT);
189 if (setup.soft_scrolling && game_status == PLAYING)
191 int fx = FX, fy = FY;
193 fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
194 fy += (ScreenMovDir & (MV_UP|MV_DOWN) ? ScreenGfxPos : 0);
196 XCopyArea(display,fieldbuffer,backbuffer,gc,
197 fx,fy, SXSIZE,SYSIZE,
201 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
206 void HandleButtonEvent(XButtonEvent *event)
208 motion_status = FALSE;
210 if (event->type==ButtonPress)
211 button_status = event->button;
213 button_status = MB_RELEASED;
215 HandleButton(event->x, event->y, button_status);
218 void HandleMotionEvent(XMotionEvent *event)
220 motion_status = TRUE;
222 HandleButton(event->x, event->y, button_status);
225 void HandleKeyEvent(XKeyEvent *event)
227 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
228 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
229 KeySym key = XLookupKeysym(event, event_state);
231 HandleKey(key, key_status);
234 void HandleFocusEvent(XFocusChangeEvent *event)
236 static int old_joystick_status = -1;
238 if (event->type == FocusOut)
240 XAutoRepeatOn(display);
241 old_joystick_status = joystick_status;
242 joystick_status = JOYSTICK_OFF;
243 key_joystick_mapping = 0;
245 else if (event->type == FocusIn)
247 if (game_status == PLAYING)
248 XAutoRepeatOff(display);
249 if (old_joystick_status != -1)
250 joystick_status = old_joystick_status;
254 void HandleClientMessageEvent(XClientMessageEvent *event)
256 if ((event->window == window) &&
257 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
261 void HandleButton(int mx, int my, int button)
263 static int old_mx = 0, old_my = 0;
275 HandleVideoButtons(mx,my, button);
276 HandleSoundButtons(mx,my, button);
277 HandleGameButtons(mx,my, button);
283 HandleMainMenu(mx,my, 0,0, button);
287 HandleTypeName(0, XK_Return);
291 HandleChooseLevel(mx,my, 0,0, button);
295 HandleHallOfFame(button);
299 LevelEd(mx,my, button);
303 HandleHelpScreen(button);
307 HandleSetupScreen(mx,my, 0,0, button);
311 HandleSetupInputScreen(mx,my, 0,0, button);
316 if (button == MB_RELEASED)
318 int sx = (mx - SX) / TILEX;
319 int sy = (my - SY) / TILEY;
321 if (IN_VIS_FIELD(sx,sy))
326 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
327 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
328 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
329 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
330 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
331 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
332 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
344 void HandleKey(KeySym key, int key_status)
347 static struct SetupKeyboardInfo custom_key;
350 KeySym *keysym_custom;
351 KeySym keysym_default;
355 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
356 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
357 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
358 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
359 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
360 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
363 if (game_status == PLAYING)
367 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
372 if (setup.input[pnr].use_joystick)
375 custom_key = setup.input[pnr].key;
378 if (key == *key_info[i].keysym_custom)
379 key_action |= key_info[i].action;
381 if (key_status == KEY_PRESSED)
382 stored_player[pnr].action |= key_action;
384 stored_player[pnr].action &= ~key_action;
392 if (key == key_info[i].keysym_default)
393 joy |= key_info[i].action;
398 if (key_status == KEY_PRESSED)
399 key_joystick_mapping |= joy;
401 key_joystick_mapping &= ~joy;
406 if (game_status != PLAYING)
407 key_joystick_mapping = 0;
409 if (key_status == KEY_RELEASED)
412 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
414 CloseDoor(DOOR_CLOSE_1);
415 game_status = MAINMENU;
420 /* allow quick escape to the main menu with the Escape key */
421 if (key == XK_Escape && game_status != MAINMENU)
423 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
424 game_status = MAINMENU;
433 if (game_status == PLAYING && (tape.playing || tape.pausing))
443 HandleTypeName(0, key);
453 if (game_status == MAINMENU)
454 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
455 else if (game_status == CHOOSELEVEL)
456 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
457 else if (game_status == SETUP)
458 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
459 else if (game_status == SETUPINPUT)
460 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
469 HandleHelpScreen(MB_RELEASED);
476 game_status = MAINMENU;
487 LevelNameTyping(key);
508 if (GameFrameDelay == 500)
509 GameFrameDelay = GAME_FRAME_DELAY;
511 GameFrameDelay = 500;
514 GameFrameDelay = (key - XK_0) * 10;
515 printf("Game speed == %d%% (%d ms delay between two frames)\n",
516 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
522 if (ScrollStepSize == TILEX/8)
523 ScrollStepSize = TILEX/4;
525 ScrollStepSize = TILEX/8;
526 printf("ScrollStepSize == %d\n", ScrollStepSize);
531 ScrollStepSize = TILEX/8;
532 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
536 ScrollStepSize = TILEX/4;
537 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
541 ScrollStepSize = TILEX/2;
542 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
546 ScrollStepSize = TILEX;
547 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
554 local_player->dynamite = 1000;
565 for(i=0; i<MAX_PLAYERS; i++)
567 printf("Player %d:\n", i);
568 printf(" jx == %d, jy == %d\n",
569 stored_player[i].jx, stored_player[i].jy);
570 printf(" last_jx == %d, last_jy == %d\n",
571 stored_player[i].last_jx, stored_player[i].last_jy);
590 void HandleNoXEvent()
592 if (button_status && game_status != PLAYING)
594 HandleButton(-1,-1, button_status);
603 if (game_status == PLAYING)
607 static int HandleJoystickForAllPlayers()
612 for (i=0; i<MAX_PLAYERS; i++)
616 if (!setup.input[i].use_joystick)
619 joy_action = Joystick(i);
620 result |= joy_action;
622 stored_player[i].action = joy_action;
628 void HandleJoystick()
630 int joystick = HandleJoystickForAllPlayers();
631 int keyboard = key_joystick_mapping;
632 int joy = (joystick | keyboard);
633 int left = joy & JOY_LEFT;
634 int right = joy & JOY_RIGHT;
635 int up = joy & JOY_UP;
636 int down = joy & JOY_DOWN;
637 int button = joy & JOY_BUTTON;
638 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
639 int dx = (left ? -1 : right ? 1 : 0);
640 int dy = (up ? -1 : down ? 1 : 0);
649 static long joystickmove_delay = 0;
651 if (joystick && !button && !DelayReached(&joystickmove_delay,150))
652 newbutton = dx = dy = 0;
654 if (game_status==MAINMENU)
655 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
656 else if (game_status==CHOOSELEVEL)
657 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
658 else if (game_status==SETUP)
659 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
660 else if (game_status==SETUPINPUT)
661 HandleSetupInputScreen(0,0,dx,dy,
662 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
667 HandleHallOfFame(!newbutton);
671 HandleHelpScreen(!newbutton);
675 if (tape.playing || keyboard)
676 newbutton = ((joy & JOY_BUTTON) != 0);
678 if (AllPlayersGone && newbutton)
680 CloseDoor(DOOR_CLOSE_1);
681 game_status = MAINMENU;