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);
521 GameSpeed = (key - XK_0) * 10;
522 printf("GameSpeed == %d\n", GameSpeed);
526 if (ScrollStepSize == TILEX/8)
527 ScrollStepSize = TILEX/4;
529 ScrollStepSize = TILEX/8;
530 printf("ScrollStepSize == %d\n", ScrollStepSize);
534 ScrollStepSize = TILEX/8;
535 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
538 ScrollStepSize = TILEX/4;
539 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
542 ScrollStepSize = TILEX/2;
543 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
546 ScrollStepSize = TILEX;
547 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
560 int i,j,k, num_steps = 8, step_size = TILEX / num_steps;
561 static long scroll_delay=0;
562 long scroll_delay_value = 4*4 / num_steps;
564 printf("Scroll test\n");
568 for(j=0;j<SCR_FIELDX;j++)
570 for(k=0;k<num_steps;k++)
572 int xxx = j*TILEX+k*step_size;
577 if (DelayReached(&scroll_delay, scroll_delay_value))
579 XCopyArea(display,fieldbuffer,window,gc,
583 XCopyArea(display,fieldbuffer,window,gc,
589 XSync(display,FALSE);
600 Delay(160 / num_steps);
603 Delay(120 / num_steps);
614 printf("FX = %d, FY = %d\n", FX,FY);
616 XCopyArea(display,fieldbuffer,window,gc,
618 MIN(WIN_XSIZE,FXSIZE),MIN(WIN_YSIZE,FYSIZE),
621 XSync(display,FALSE);
638 void HandleNoXEvent()
640 if (button_status && game_status != PLAYING)
642 HandleButton(-1,-1,button_status);
664 void HandleJoystick()
666 int joystick = Joystick();
667 int keyboard = key_joystick_mapping;
668 int joy = (tape.playing ? TapePlayAction() : (joystick | keyboard));
669 int left = joy & JOY_LEFT;
670 int right = joy & JOY_RIGHT;
671 int up = joy & JOY_UP;
672 int down = joy & JOY_DOWN;
673 int button = joy & JOY_BUTTON;
674 int button1 = joy & JOY_BUTTON_1;
675 int button2 = joy & JOY_BUTTON_2;
676 int newbutton = (JoystickButton() == JOY_BUTTON_NEW_PRESSED);
677 int dx = (left ? -1 : right ? 1 : 0);
678 int dy = (up ? -1 : down ? 1 : 0);
680 if (game_status==PLAYING && (tape.playing || keyboard))
681 newbutton = ((joy & JOY_BUTTON) != 0);
689 static long joystickmove_delay = 0;
691 if (joystick && !button && !DelayReached(&joystickmove_delay,15))
692 newbutton = dx = dy = 0;
694 if (game_status==MAINMENU)
695 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
696 else if (game_status==CHOOSELEVEL)
697 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
698 else if (game_status==SETUP)
699 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
703 HandleHallOfFame(!newbutton);
706 HandleHelpScreen(!newbutton);
710 static int player_frame_reset_delay = 0;
711 BOOL moved = FALSE, snapped = FALSE, bombed = FALSE;
713 if (GameOver && newbutton)
715 CloseDoor(DOOR_CLOSE_1);
716 game_status = MAINMENU;
721 if (tape.pausing || PlayerGone)
726 player_frame_reset_delay = 0;
729 snapped = SnapField(dx,dy);
733 bombed = PlaceBomb();
734 moved = MoveFigure(dx,dy);
737 if (tape.recording && (moved || snapped || bombed))
739 if (bombed && !moved)
741 TapeRecordAction(joy);
743 else if (tape.playing && snapped)
744 SnapField(0,0); /* stop snapping */
748 DigField(0,0,0,0,DF_NO_PUSH);
750 if (++player_frame_reset_delay > MoveSpeed)
754 if (tape.playing && !tape.pausing && !joy && tape.counter<tape.length)
757 tape.pos[tape.counter].joystickdata & (JOY_LEFT|JOY_RIGHT);
759 if (next_joy == JOY_LEFT || next_joy == JOY_RIGHT)
761 int dx = (next_joy == JOY_LEFT ? -1 : +1);
763 if (IN_LEV_FIELD(JX+dx,JY) && IS_PUSHABLE(Feld[JX+dx][JY]))
765 int el = Feld[JX+dx][JY];
766 int push_delay = (IS_SB_ELEMENT(el) || el==EL_SONDE ? 2 : 10);
768 if (tape.delay_played + push_delay >= tape.pos[tape.counter].delay)
770 PlayerMovDir = next_joy;
771 PlayerFrame = FrameCounter % 4;
772 PlayerPushing = TRUE;