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;
283 HandleGadgets(mx,my, button);
284 HandleVideoButtons(mx,my, button);
285 HandleSoundButtons(mx,my, button);
286 HandleGameButtons(mx,my, button);
292 HandleMainMenu(mx,my, 0,0, button);
296 HandleTypeName(0, XK_Return);
300 HandleChooseLevel(mx,my, 0,0, button);
304 HandleHallOfFame(button);
308 LevelEd(mx,my, button);
312 HandleHelpScreen(button);
316 HandleSetupScreen(mx,my, 0,0, button);
320 HandleSetupInputScreen(mx,my, 0,0, button);
325 if (button == MB_RELEASED)
327 int sx = (mx - SX) / TILEX;
328 int sy = (my - SY) / TILEY;
330 if (IN_VIS_FIELD(sx,sy))
335 printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
336 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
337 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
338 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
339 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
340 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
341 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
353 void HandleKey(KeySym key, int key_status)
356 static struct SetupKeyboardInfo custom_key;
359 KeySym *keysym_custom;
360 KeySym keysym_default;
364 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
365 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
366 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
367 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
368 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
369 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
372 if (game_status == PLAYING)
376 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
381 if (setup.input[pnr].use_joystick)
384 custom_key = setup.input[pnr].key;
387 if (key == *key_info[i].keysym_custom)
388 key_action |= key_info[i].action;
390 if (key_status == KEY_PRESSED)
391 stored_player[pnr].action |= key_action;
393 stored_player[pnr].action &= ~key_action;
401 if (key == key_info[i].keysym_default)
402 joy |= key_info[i].action;
407 if (key_status == KEY_PRESSED)
408 key_joystick_mapping |= joy;
410 key_joystick_mapping &= ~joy;
415 if (game_status != PLAYING)
416 key_joystick_mapping = 0;
418 if (key_status == KEY_RELEASED)
421 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
423 CloseDoor(DOOR_CLOSE_1);
424 game_status = MAINMENU;
429 /* allow quick escape to the main menu with the Escape key */
430 if (key == XK_Escape && game_status != MAINMENU)
432 if (game_status == LEVELED)
434 /* draw smaller door */
435 XCopyArea(display, pix[PIX_DOOR], drawto, gc,
439 redraw_mask |= REDRAW_ALL;
442 CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
443 game_status = MAINMENU;
452 if (game_status == PLAYING && (tape.playing || tape.pausing))
462 HandleTypeName(0, key);
472 if (game_status == MAINMENU)
473 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
474 else if (game_status == CHOOSELEVEL)
475 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
476 else if (game_status == SETUP)
477 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
478 else if (game_status == SETUPINPUT)
479 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
488 HandleHelpScreen(MB_RELEASED);
495 game_status = MAINMENU;
506 LevelNameTyping(key);
527 if (GameFrameDelay == 500)
528 GameFrameDelay = GAME_FRAME_DELAY;
530 GameFrameDelay = 500;
533 GameFrameDelay = (key - XK_0) * 10;
534 printf("Game speed == %d%% (%d ms delay between two frames)\n",
535 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
541 if (ScrollStepSize == TILEX/8)
542 ScrollStepSize = TILEX/4;
544 ScrollStepSize = TILEX/8;
545 printf("ScrollStepSize == %d\n", ScrollStepSize);
550 ScrollStepSize = TILEX/8;
551 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
555 ScrollStepSize = TILEX/4;
556 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
560 ScrollStepSize = TILEX/2;
561 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
565 ScrollStepSize = TILEX;
566 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
573 local_player->dynamite = 1000;
584 for(i=0; i<MAX_PLAYERS; i++)
586 printf("Player %d:\n", i);
587 printf(" jx == %d, jy == %d\n",
588 stored_player[i].jx, stored_player[i].jy);
589 printf(" last_jx == %d, last_jy == %d\n",
590 stored_player[i].last_jx, stored_player[i].last_jy);
609 void HandleNoXEvent()
611 if (button_status && game_status != PLAYING)
613 HandleButton(-1,-1, button_status);
624 if (game_status == PLAYING)
628 static int HandleJoystickForAllPlayers()
633 for (i=0; i<MAX_PLAYERS; i++)
638 if (!setup.input[i].use_joystick)
642 joy_action = Joystick(i);
643 result |= joy_action;
646 if (!setup.input[i].use_joystick)
650 stored_player[i].action = joy_action;
656 void HandleJoystick()
658 int joystick = HandleJoystickForAllPlayers();
659 int keyboard = key_joystick_mapping;
660 int joy = (joystick | keyboard);
661 int left = joy & JOY_LEFT;
662 int right = joy & JOY_RIGHT;
663 int up = joy & JOY_UP;
664 int down = joy & JOY_DOWN;
665 int button = joy & JOY_BUTTON;
666 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
667 int dx = (left ? -1 : right ? 1 : 0);
668 int dy = (up ? -1 : down ? 1 : 0);
677 static unsigned long joystickmove_delay = 0;
679 if (joystick && !button &&
680 !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
681 newbutton = dx = dy = 0;
683 if (game_status==MAINMENU)
684 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
685 else if (game_status==CHOOSELEVEL)
686 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
687 else if (game_status==SETUP)
688 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
689 else if (game_status==SETUPINPUT)
690 HandleSetupInputScreen(0,0,dx,dy,
691 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
696 HandleHallOfFame(!newbutton);
700 HandleHelpScreen(!newbutton);
704 if (tape.playing || keyboard)
705 newbutton = ((joy & JOY_BUTTON) != 0);
707 if (AllPlayersGone && newbutton)
709 CloseDoor(DOOR_CLOSE_1);
710 game_status = MAINMENU;