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;
429 if (game_status == PLAYING && (tape.playing || tape.pausing))
435 HandleTypeName(0, key);
445 if (game_status == MAINMENU)
446 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
447 else if (game_status == CHOOSELEVEL)
448 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
449 else if (game_status == SETUP)
450 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
451 else if (game_status == SETUPINPUT)
452 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
461 HandleHelpScreen(MB_RELEASED);
468 game_status = MAINMENU;
479 LevelNameTyping(key);
499 GameFrameDelay = 500;
501 GameFrameDelay = (key - XK_0) * 10;
502 printf("Game speed == %d%% (%d ms delay between two frames)\n",
503 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
509 if (ScrollStepSize == TILEX/8)
510 ScrollStepSize = TILEX/4;
512 ScrollStepSize = TILEX/8;
513 printf("ScrollStepSize == %d\n", ScrollStepSize);
518 ScrollStepSize = TILEX/8;
519 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
523 ScrollStepSize = TILEX/4;
524 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
528 ScrollStepSize = TILEX/2;
529 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
533 ScrollStepSize = TILEX;
534 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
541 local_player->dynamite = 1000;
552 for(i=0; i<MAX_PLAYERS; i++)
554 printf("Player %d:\n", i);
555 printf(" jx == %d, jy == %d\n",
556 stored_player[i].jx, stored_player[i].jy);
557 printf(" last_jx == %d, last_jy == %d\n",
558 stored_player[i].last_jx, stored_player[i].last_jy);
577 void HandleNoXEvent()
579 if (button_status && game_status != PLAYING)
581 HandleButton(-1,-1, button_status);
590 if (game_status == PLAYING)
594 static int HandleJoystickForAllPlayers()
599 for (i=0; i<MAX_PLAYERS; i++)
603 if (!setup.input[i].use_joystick)
606 joy_action = Joystick(i);
607 result |= joy_action;
609 stored_player[i].action = joy_action;
615 void HandleJoystick()
617 int joystick = HandleJoystickForAllPlayers();
618 int keyboard = key_joystick_mapping;
619 int joy = (joystick | keyboard);
620 int left = joy & JOY_LEFT;
621 int right = joy & JOY_RIGHT;
622 int up = joy & JOY_UP;
623 int down = joy & JOY_DOWN;
624 int button = joy & JOY_BUTTON;
625 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
626 int dx = (left ? -1 : right ? 1 : 0);
627 int dy = (up ? -1 : down ? 1 : 0);
636 static long joystickmove_delay = 0;
638 if (joystick && !button && !DelayReached(&joystickmove_delay,150))
639 newbutton = dx = dy = 0;
641 if (game_status==MAINMENU)
642 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
643 else if (game_status==CHOOSELEVEL)
644 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
645 else if (game_status==SETUP)
646 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
647 else if (game_status==SETUPINPUT)
648 HandleSetupInputScreen(0,0,dx,dy,
649 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
654 HandleHallOfFame(!newbutton);
658 HandleHelpScreen(!newbutton);
662 if (tape.playing || keyboard)
663 newbutton = ((joy & JOY_BUTTON) != 0);
665 if (AllPlayersGone && newbutton)
667 CloseDoor(DOOR_CLOSE_1);
668 game_status = MAINMENU;