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;
284 HandleVideoButtons(mx,my, button);
285 HandleSoundButtons(mx,my, button);
286 HandleGameButtons(mx,my, button);
289 HandleGadgets(mx, my, button);
294 HandleMainMenu(mx,my, 0,0, button);
298 HandleTypeName(0, XK_Return);
302 HandleChooseLevel(mx,my, 0,0, button);
306 HandleHallOfFame(button);
310 LevelEd(mx,my, button);
314 HandleHelpScreen(button);
318 HandleSetupScreen(mx,my, 0,0, button);
322 HandleSetupInputScreen(mx,my, 0,0, button);
327 if (button == MB_RELEASED)
329 int sx = (mx - SX) / TILEX;
330 int sy = (my - SY) / TILEY;
332 if (IN_VIS_FIELD(sx,sy))
337 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
338 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
339 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
340 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
341 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
342 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
343 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
355 void HandleKey(KeySym key, int key_status)
358 static struct SetupKeyboardInfo custom_key;
361 KeySym *keysym_custom;
362 KeySym keysym_default;
366 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
367 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
368 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
369 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
370 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
371 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
374 if (game_status == PLAYING)
378 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
383 if (setup.input[pnr].use_joystick)
386 custom_key = setup.input[pnr].key;
389 if (key == *key_info[i].keysym_custom)
390 key_action |= key_info[i].action;
392 if (key_status == KEY_PRESSED)
393 stored_player[pnr].action |= key_action;
395 stored_player[pnr].action &= ~key_action;
403 if (key == key_info[i].keysym_default)
404 joy |= key_info[i].action;
409 if (key_status == KEY_PRESSED)
410 key_joystick_mapping |= joy;
412 key_joystick_mapping &= ~joy;
417 if (game_status != PLAYING)
418 key_joystick_mapping = 0;
420 if (key_status == KEY_RELEASED)
423 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
425 CloseDoor(DOOR_CLOSE_1);
426 game_status = MAINMENU;
431 /* allow quick escape to the main menu with the Escape key */
432 if (key == XK_Escape && game_status != MAINMENU)
434 if (game_status == LEVELED)
436 /* draw smaller door */
437 XCopyArea(display, pix[PIX_DOOR], drawto, gc,
441 redraw_mask |= REDRAW_ALL;
444 CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
445 game_status = MAINMENU;
454 if (game_status == PLAYING && (tape.playing || tape.pausing))
464 HandleTypeName(0, key);
474 if (game_status == MAINMENU)
475 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
476 else if (game_status == CHOOSELEVEL)
477 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
478 else if (game_status == SETUP)
479 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
480 else if (game_status == SETUPINPUT)
481 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
490 HandleHelpScreen(MB_RELEASED);
497 game_status = MAINMENU;
508 LevelNameTyping(key);
529 if (GameFrameDelay == 500)
530 GameFrameDelay = GAME_FRAME_DELAY;
532 GameFrameDelay = 500;
535 GameFrameDelay = (key - XK_0) * 10;
536 printf("Game speed == %d%% (%d ms delay between two frames)\n",
537 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
543 if (ScrollStepSize == TILEX/8)
544 ScrollStepSize = TILEX/4;
546 ScrollStepSize = TILEX/8;
547 printf("ScrollStepSize == %d\n", ScrollStepSize);
552 ScrollStepSize = TILEX/8;
553 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
557 ScrollStepSize = TILEX/4;
558 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
562 ScrollStepSize = TILEX/2;
563 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
567 ScrollStepSize = TILEX;
568 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
575 local_player->dynamite = 1000;
586 for(i=0; i<MAX_PLAYERS; i++)
588 printf("Player %d:\n", i);
589 printf(" jx == %d, jy == %d\n",
590 stored_player[i].jx, stored_player[i].jy);
591 printf(" last_jx == %d, last_jy == %d\n",
592 stored_player[i].last_jx, stored_player[i].last_jy);
611 void HandleNoXEvent()
613 if (button_status && game_status != PLAYING)
615 HandleButton(0, 0, -button_status);
626 if (game_status == PLAYING)
630 static int HandleJoystickForAllPlayers()
635 for (i=0; i<MAX_PLAYERS; i++)
640 if (!setup.input[i].use_joystick)
644 joy_action = Joystick(i);
645 result |= joy_action;
648 if (!setup.input[i].use_joystick)
652 stored_player[i].action = joy_action;
658 void HandleJoystick()
660 int joystick = HandleJoystickForAllPlayers();
661 int keyboard = key_joystick_mapping;
662 int joy = (joystick | keyboard);
663 int left = joy & JOY_LEFT;
664 int right = joy & JOY_RIGHT;
665 int up = joy & JOY_UP;
666 int down = joy & JOY_DOWN;
667 int button = joy & JOY_BUTTON;
668 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
669 int dx = (left ? -1 : right ? 1 : 0);
670 int dy = (up ? -1 : down ? 1 : 0);
679 static unsigned long joystickmove_delay = 0;
681 if (joystick && !button &&
682 !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
683 newbutton = dx = dy = 0;
685 if (game_status==MAINMENU)
686 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
687 else if (game_status==CHOOSELEVEL)
688 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
689 else if (game_status==SETUP)
690 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
691 else if (game_status==SETUPINPUT)
692 HandleSetupInputScreen(0,0,dx,dy,
693 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
698 HandleHallOfFame(!newbutton);
702 HandleHelpScreen(!newbutton);
706 if (tape.playing || keyboard)
707 newbutton = ((joy & JOY_BUTTON) != 0);
709 if (AllPlayersGone && newbutton)
711 CloseDoor(DOOR_CLOSE_1);
712 game_status = MAINMENU;