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 /* refresh window contents from drawing buffer, if needed */
77 if (game_status == EXITGAME)
82 void HandleOtherEvents(XEvent *event)
87 HandleExposeEvent((XExposeEvent *) event);
96 HandleFocusEvent((XFocusChangeEvent *) event);
100 HandleClientMessageEvent((XClientMessageEvent *) event);
108 void ClearEventQueue()
110 while(XPending(display))
114 XNextEvent(display, &event);
119 button_status = MB_RELEASED;
123 key_joystick_mapping = 0;
127 HandleOtherEvents(&event);
133 void SleepWhileUnmapped()
135 boolean window_unmapped = TRUE;
137 XAutoRepeatOn(display);
139 while(window_unmapped)
143 XNextEvent(display, &event);
148 button_status = MB_RELEASED;
152 key_joystick_mapping = 0;
156 window_unmapped = FALSE;
160 /* this is only to surely prevent the 'should not happen' case
161 * of recursively looping between 'SleepWhileUnmapped()' and
162 * 'HandleOtherEvents()' which usually calls this funtion.
167 HandleOtherEvents(&event);
172 if (game_status==PLAYING)
173 XAutoRepeatOff(display);
176 void HandleExposeEvent(XExposeEvent *event)
178 int x = event->x, y = event->y;
179 int width = event->width, height = event->height;
181 if (setup.direct_draw && game_status==PLAYING)
184 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
185 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
187 SetDrawtoField(DRAW_BACKBUFFER);
189 for(xx=0; xx<SCR_FIELDX; xx++)
190 for(yy=0; yy<SCR_FIELDY; yy++)
191 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
192 DrawScreenField(xx,yy);
195 SetDrawtoField(DRAW_DIRECT);
198 if (setup.soft_scrolling && game_status == PLAYING)
200 int fx = FX, fy = FY;
202 fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
203 fy += (ScreenMovDir & (MV_UP|MV_DOWN) ? ScreenGfxPos : 0);
205 XCopyArea(display,fieldbuffer,backbuffer,gc,
206 fx,fy, SXSIZE,SYSIZE,
210 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
215 void HandleButtonEvent(XButtonEvent *event)
217 motion_status = FALSE;
219 if (event->type==ButtonPress)
220 button_status = event->button;
222 button_status = MB_RELEASED;
224 HandleButton(event->x, event->y, button_status);
227 void HandleMotionEvent(XMotionEvent *event)
229 motion_status = TRUE;
231 HandleButton(event->x, event->y, button_status);
234 void HandleKeyEvent(XKeyEvent *event)
236 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
237 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
238 KeySym key = XLookupKeysym(event, event_state);
240 HandleKey(key, key_status);
243 void HandleFocusEvent(XFocusChangeEvent *event)
245 static int old_joystick_status = -1;
247 if (event->type == FocusOut)
249 XAutoRepeatOn(display);
250 old_joystick_status = joystick_status;
251 joystick_status = JOYSTICK_OFF;
252 key_joystick_mapping = 0;
254 else if (event->type == FocusIn)
256 if (game_status == PLAYING)
257 XAutoRepeatOff(display);
258 if (old_joystick_status != -1)
259 joystick_status = old_joystick_status;
263 void HandleClientMessageEvent(XClientMessageEvent *event)
266 if ((event->window == window) &&
267 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
272 void HandleButton(int mx, int my, int button)
274 static int old_mx = 0, old_my = 0;
287 HandleVideoButtons(mx,my, button);
288 HandleSoundButtons(mx,my, button);
289 HandleGameButtons(mx,my, button);
292 HandleGadgets(mx, my, button);
297 HandleMainMenu(mx,my, 0,0, button);
301 HandleTypeName(0, XK_Return);
305 HandleChooseLevel(mx,my, 0,0, button);
309 HandleHallOfFame(button);
313 LevelEd(mx,my, button);
317 HandleHelpScreen(button);
321 HandleSetupScreen(mx,my, 0,0, button);
325 HandleSetupInputScreen(mx,my, 0,0, button);
330 if (button == MB_RELEASED)
332 int sx = (mx - SX) / TILEX;
333 int sy = (my - SY) / TILEY;
335 if (IN_VIS_FIELD(sx,sy))
340 printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
342 if (!IN_LEV_FIELD(x, y))
345 printf(" Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
346 printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
347 printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
348 printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
349 printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
350 printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
351 printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
363 void HandleKey(KeySym key, int key_status)
366 static struct SetupKeyboardInfo custom_key;
369 KeySym *keysym_custom;
370 KeySym keysym_default;
374 { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT },
375 { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT },
376 { &custom_key.up, DEFAULT_KEY_UP, JOY_UP },
377 { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN },
378 { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 },
379 { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 }
382 if (game_status == PLAYING)
386 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
391 if (setup.input[pnr].use_joystick)
394 custom_key = setup.input[pnr].key;
397 if (key == *key_info[i].keysym_custom)
398 key_action |= key_info[i].action;
400 if (key_status == KEY_PRESSED)
401 stored_player[pnr].action |= key_action;
403 stored_player[pnr].action &= ~key_action;
411 if (key == key_info[i].keysym_default)
412 joy |= key_info[i].action;
417 if (key_status == KEY_PRESSED)
418 key_joystick_mapping |= joy;
420 key_joystick_mapping &= ~joy;
425 if (game_status != PLAYING)
426 key_joystick_mapping = 0;
428 if (key_status == KEY_RELEASED)
431 if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
433 CloseDoor(DOOR_CLOSE_1);
434 game_status = MAINMENU;
439 /* allow quick escape to the main menu with the Escape key */
440 if (key == XK_Escape && game_status != MAINMENU)
442 if (game_status == LEVELED)
444 /* draw smaller door */
445 XCopyArea(display, pix[PIX_DOOR], drawto, gc,
449 redraw_mask |= REDRAW_ALL;
452 CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
453 game_status = MAINMENU;
462 if (game_status == PLAYING && (tape.playing || tape.pausing))
469 HandleGadgetsKeyInput(key);
474 HandleTypeName(0, key);
484 if (game_status == MAINMENU)
485 HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
486 else if (game_status == CHOOSELEVEL)
487 HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
488 else if (game_status == SETUP)
489 HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
490 else if (game_status == SETUPINPUT)
491 HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
500 HandleHelpScreen(MB_RELEASED);
507 game_status = MAINMENU;
518 HandleLevelEditorKeyInput(key);
519 LevelNameTyping(key);
540 if (GameFrameDelay == 500)
541 GameFrameDelay = GAME_FRAME_DELAY;
543 GameFrameDelay = 500;
546 GameFrameDelay = (key - XK_0) * 10;
547 printf("Game speed == %d%% (%d ms delay between two frames)\n",
548 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
554 if (ScrollStepSize == TILEX/8)
555 ScrollStepSize = TILEX/4;
557 ScrollStepSize = TILEX/8;
558 printf("ScrollStepSize == %d\n", ScrollStepSize);
567 ScrollStepSize = TILEX/4;
572 ScrollStepSize = TILEX/8;
574 printf("MoveSpeed == %d\n", MoveSpeed);
579 ScrollStepSize = TILEX/8;
580 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
584 ScrollStepSize = TILEX/4;
585 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
589 ScrollStepSize = TILEX/2;
590 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
594 ScrollStepSize = TILEX;
595 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
602 local_player->dynamite = 1000;
613 for(i=0; i<MAX_PLAYERS; i++)
615 printf("Player %d:\n", i);
616 printf(" jx == %d, jy == %d\n",
617 stored_player[i].jx, stored_player[i].jy);
618 printf(" last_jx == %d, last_jy == %d\n",
619 stored_player[i].last_jx, stored_player[i].last_jy);
638 void HandleNoXEvent()
640 if (button_status && game_status != PLAYING)
642 HandleButton(0, 0, -button_status);
653 if (game_status == PLAYING)
657 static int HandleJoystickForAllPlayers()
662 for (i=0; i<MAX_PLAYERS; i++)
667 if (!setup.input[i].use_joystick)
671 joy_action = Joystick(i);
672 result |= joy_action;
675 if (!setup.input[i].use_joystick)
679 stored_player[i].action = joy_action;
685 void HandleJoystick()
687 int joystick = HandleJoystickForAllPlayers();
688 int keyboard = key_joystick_mapping;
689 int joy = (joystick | keyboard);
690 int left = joy & JOY_LEFT;
691 int right = joy & JOY_RIGHT;
692 int up = joy & JOY_UP;
693 int down = joy & JOY_DOWN;
694 int button = joy & JOY_BUTTON;
695 int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
696 int dx = (left ? -1 : right ? 1 : 0);
697 int dy = (up ? -1 : down ? 1 : 0);
706 static unsigned long joystickmove_delay = 0;
708 if (joystick && !button &&
709 !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
710 newbutton = dx = dy = 0;
712 if (game_status==MAINMENU)
713 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
714 else if (game_status==CHOOSELEVEL)
715 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
716 else if (game_status==SETUP)
717 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
718 else if (game_status==SETUPINPUT)
719 HandleSetupInputScreen(0,0,dx,dy,
720 newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
725 HandleHallOfFame(!newbutton);
729 HandleHelpScreen(!newbutton);
733 if (tape.playing || keyboard)
734 newbutton = ((joy & JOY_BUTTON) != 0);
736 if (AllPlayersGone && newbutton)
738 CloseDoor(DOOR_CLOSE_1);
739 game_status = MAINMENU;