1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * ©1995 Artsoft Development *
6 * 33659 Bielefeld-Senne *
7 * Telefon: (0521) 493245 *
8 * eMail: aeglos@valinor.owl.de *
9 * aeglos@uni-paderborn.de *
10 * q99492@pbhrzx.uni-paderborn.de *
11 *----------------------------------------------------------*
13 ***********************************************************/
29 if (XPending(display)) /* got an event */
33 XNextEvent(display, &event);
38 HandleExposeEvent((XExposeEvent *) &event);
45 HandleButtonEvent((XButtonEvent *) &event);
48 HandleMotionEvent((XMotionEvent *) &event);
52 HandleKeyEvent((XKeyEvent *) &event);
56 HandleFocusEvent((XFocusChangeEvent *) &event);
59 HandleClientMessageEvent((XClientMessageEvent *) &event);
65 else /* got no event, but don't be lazy... */
69 /* don't use all CPU time when idle; the main loop while playing
70 has its own synchronization and is CPU friendly, too */
72 if (game_status != PLAYING)
74 XSync(display, FALSE);
79 if (game_status == EXITGAME)
84 void ClearEventQueue()
86 while(XPending(display))
90 XNextEvent(display, &event);
95 HandleExposeEvent((XExposeEvent *) &event);
101 button_status = MB_RELEASED;
104 key_joystick_mapping = 0;
108 HandleFocusEvent((XFocusChangeEvent *) &event);
111 HandleClientMessageEvent((XClientMessageEvent *) &event);
119 void SleepWhileUnmapped()
121 BOOL window_unmapped = TRUE;
123 XAutoRepeatOn(display);
125 while(window_unmapped)
129 XNextEvent(display, &event);
134 HandleExposeEvent((XExposeEvent *) &event);
137 button_status = MB_RELEASED;
140 key_joystick_mapping = 0;
143 window_unmapped = FALSE;
146 HandleClientMessageEvent((XClientMessageEvent *) &event);
153 if (game_status==PLAYING)
154 XAutoRepeatOff(display);
157 void HandleExposeEvent(XExposeEvent *event)
159 int x = event->x, y = event->y;
160 int width = event->width, height = event->height;
162 if (direct_draw_on && game_status==PLAYING)
165 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
166 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
168 SetDrawtoField(DRAW_BACKBUFFER);
170 for(xx=0;xx<SCR_FIELDX;xx++)
171 for(yy=0;yy<SCR_FIELDY;yy++)
172 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
173 DrawScreenField(xx,yy);
176 SetDrawtoField(DRAW_DIRECT);
179 if (soft_scrolling_on && game_status == PLAYING)
181 int fx = FX, fy = FY;
183 fx += (PlayerMovDir & (MV_LEFT|MV_RIGHT) ? ScreenMovPos : 0);
184 fy += (PlayerMovDir & (MV_UP|MV_DOWN) ? ScreenMovPos : 0);
186 XCopyArea(display,fieldbuffer,backbuffer,gc,
187 fx,fy, SXSIZE,SYSIZE,
191 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
196 void HandleButtonEvent(XButtonEvent *event)
198 motion_status = FALSE;
200 if (event->type==ButtonPress)
201 button_status = event->button;
203 button_status = MB_RELEASED;
205 HandleButton(event->x, event->y, button_status);
208 void HandleMotionEvent(XMotionEvent *event)
210 motion_status = TRUE;
212 HandleButton(event->x, event->y, button_status);
215 void HandleKeyEvent(XKeyEvent *event)
217 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
218 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
219 KeySym key = XLookupKeysym(event, event_state);
221 HandleKey(key, key_status);
224 void HandleFocusEvent(XFocusChangeEvent *event)
226 static int old_joystick_status = -1;
228 if (event->type == FocusOut)
230 XAutoRepeatOn(display);
231 old_joystick_status = joystick_status;
232 joystick_status = JOYSTICK_OFF;
233 key_joystick_mapping = 0;
235 else if (event->type == FocusIn)
237 if (game_status == PLAYING)
238 XAutoRepeatOff(display);
239 if (old_joystick_status != -1)
240 joystick_status = old_joystick_status;
244 void HandleClientMessageEvent(XClientMessageEvent *event)
246 if ((event->window == window) &&
247 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
251 void HandleButton(int mx, int my, int button)
253 static int old_mx = 0, old_my = 0;
265 HandleVideoButtons(mx,my,button);
266 HandleSoundButtons(mx,my,button);
267 HandleGameButtons(mx,my,button);
273 HandleMainMenu(mx,my,0,0,button);
276 HandleTypeName(0,XK_Return);
279 HandleChooseLevel(mx,my,0,0,button);
282 HandleHallOfFame(button);
285 LevelEd(mx,my,button);
288 HandleHelpScreen(button);
291 HandleSetupScreen(mx,my,0,0,button);
301 void HandleKey(KeySym key, int key_status)
305 /* Map cursor keys to joystick directions */
309 case XK_Left: /* normale Richtungen */
354 case XK_KP_Home: /* Diagonalrichtungen */
357 joy |= JOY_UP | JOY_LEFT;
363 joy = JOY_UP | JOY_RIGHT;
369 joy |= JOY_DOWN | JOY_LEFT;
371 #ifdef XK_KP_Page_Down
372 case XK_KP_Page_Down:
375 joy |= JOY_DOWN | JOY_RIGHT;
378 case XK_S: /* Feld entfernen */
381 joy |= JOY_BUTTON_1 | JOY_LEFT;
387 joy |= JOY_BUTTON_1 | JOY_RIGHT;
393 joy |= JOY_BUTTON_1 | JOY_UP;
399 joy |= JOY_BUTTON_1 | JOY_DOWN;
401 case XK_Shift_L: /* Linker Feuerknopf */
409 case XK_Shift_R: /* Rechter Feuerknopf */
416 case XK_B: /* (Bombe legen) */
427 if (key_status == KEY_PRESSED)
428 key_joystick_mapping |= joy;
430 key_joystick_mapping &= ~joy;
435 if (game_status != PLAYING)
436 key_joystick_mapping = 0;
438 if (key_status == KEY_RELEASED)
441 if (key==XK_Return && game_status==PLAYING && GameOver)
443 CloseDoor(DOOR_CLOSE_1);
444 game_status = MAINMENU;
449 if (key==XK_Escape && game_status!=MAINMENU) /* quick quit to MAINMENU */
451 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
452 game_status = MAINMENU;
457 if (game_status==PLAYING && (tape.playing || tape.pausing))
463 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);
485 HandleHelpScreen(MB_RELEASED);
491 game_status = MAINMENU;
500 LevelNameTyping(key);
519 GameFrameDelay = 500;
521 GameFrameDelay = (key - XK_0) * 10;
522 printf("Game speed == %d%% (%d ms delay between two frames)\n",
523 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
527 if (ScrollStepSize == TILEX/8)
528 ScrollStepSize = TILEX/4;
530 ScrollStepSize = TILEX/8;
531 printf("ScrollStepSize == %d\n", ScrollStepSize);
535 ScrollStepSize = TILEX/8;
536 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
539 ScrollStepSize = TILEX/4;
540 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
543 ScrollStepSize = TILEX/2;
544 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
547 ScrollStepSize = TILEX;
548 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
561 int i,j,k, num_steps = 8, step_size = TILEX / num_steps;
562 static long scroll_delay=0;
563 long scroll_delay_value = 4*4 / num_steps;
565 printf("Scroll test\n");
569 for(j=0;j<SCR_FIELDX;j++)
571 for(k=0;k<num_steps;k++)
573 int xxx = j*TILEX+k*step_size;
578 if (DelayReached(&scroll_delay, scroll_delay_value))
580 XCopyArea(display,fieldbuffer,window,gc,
584 XCopyArea(display,fieldbuffer,window,gc,
590 XSync(display,FALSE);
601 Delay(160 / num_steps);
604 Delay(120 / num_steps);
615 printf("FX = %d, FY = %d\n", FX,FY);
617 XCopyArea(display,fieldbuffer,window,gc,
619 MIN(WIN_XSIZE,FXSIZE),MIN(WIN_YSIZE,FYSIZE),
622 XSync(display,FALSE);
639 void HandleNoXEvent()
641 if (button_status && game_status != PLAYING)
643 HandleButton(-1,-1,button_status);
665 void HandleJoystick()
667 int joystick = Joystick();
668 int keyboard = key_joystick_mapping;
669 int joy = (tape.playing ? TapePlayAction() : (joystick | keyboard));
670 int left = joy & JOY_LEFT;
671 int right = joy & JOY_RIGHT;
672 int up = joy & JOY_UP;
673 int down = joy & JOY_DOWN;
674 int button = joy & JOY_BUTTON;
675 int button1 = joy & JOY_BUTTON_1;
676 int button2 = joy & JOY_BUTTON_2;
677 int newbutton = (JoystickButton() == JOY_BUTTON_NEW_PRESSED);
678 int dx = (left ? -1 : right ? 1 : 0);
679 int dy = (up ? -1 : down ? 1 : 0);
681 if (game_status==PLAYING && (tape.playing || keyboard))
682 newbutton = ((joy & JOY_BUTTON) != 0);
690 static long joystickmove_delay = 0;
692 if (joystick && !button && !DelayReached(&joystickmove_delay,15))
693 newbutton = dx = dy = 0;
695 if (game_status==MAINMENU)
696 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
697 else if (game_status==CHOOSELEVEL)
698 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
699 else if (game_status==SETUP)
700 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
704 HandleHallOfFame(!newbutton);
707 HandleHelpScreen(!newbutton);
711 static int player_frame_reset_delay = 0;
712 BOOL moved = FALSE, snapped = FALSE, bombed = FALSE;
714 if (GameOver && newbutton)
716 CloseDoor(DOOR_CLOSE_1);
717 game_status = MAINMENU;
722 if (tape.pausing || PlayerGone)
727 player_frame_reset_delay = 0;
730 snapped = SnapField(dx,dy);
734 bombed = PlaceBomb();
735 moved = MoveFigure(dx,dy);
738 if (tape.recording && (moved || snapped || bombed))
740 if (bombed && !moved)
742 TapeRecordAction(joy);
744 else if (tape.playing && snapped)
745 SnapField(0,0); /* stop snapping */
749 DigField(0,0,0,0,DF_NO_PUSH);
751 if (++player_frame_reset_delay > MoveSpeed)
755 if (tape.playing && !tape.pausing && !joy && tape.counter<tape.length)
758 tape.pos[tape.counter].joystickdata & (JOY_LEFT|JOY_RIGHT);
760 if (next_joy == JOY_LEFT || next_joy == JOY_RIGHT)
762 int dx = (next_joy == JOY_LEFT ? -1 : +1);
764 if (IN_LEV_FIELD(JX+dx,JY) && IS_PUSHABLE(Feld[JX+dx][JY]))
766 int el = Feld[JX+dx][JY];
767 int push_delay = (IS_SB_ELEMENT(el) || el==EL_SONDE ? 2 : 10);
769 if (tape.delay_played + push_delay >= tape.pos[tape.counter].delay)
771 PlayerMovDir = next_joy;
772 PlayerFrame = FrameCounter % 4;
773 PlayerPushing = TRUE;