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)
76 if (game_status==EXITGAME)
81 void ClearEventQueue()
83 while(XPending(display))
87 XNextEvent(display, &event);
92 HandleExposeEvent((XExposeEvent *) &event);
98 button_status = MB_RELEASED;
101 key_joystick_mapping = 0;
105 HandleFocusEvent((XFocusChangeEvent *) &event);
108 HandleClientMessageEvent((XClientMessageEvent *) &event);
116 void SleepWhileUnmapped()
118 BOOL window_unmapped = TRUE;
120 XAutoRepeatOn(display);
122 while(window_unmapped)
126 XNextEvent(display, &event);
131 HandleExposeEvent((XExposeEvent *) &event);
134 button_status = MB_RELEASED;
137 key_joystick_mapping = 0;
140 window_unmapped = FALSE;
143 HandleClientMessageEvent((XClientMessageEvent *) &event);
150 if (game_status==PLAYING)
151 XAutoRepeatOff(display);
154 void HandleExposeEvent(XExposeEvent *event)
156 int x = event->x, y = event->y;
157 int width = event->width, height = event->height;
159 if (direct_draw_on && game_status==PLAYING)
162 int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
163 int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
165 SetDrawtoField(DRAW_BACKBUFFER);
167 for(xx=0;xx<SCR_FIELDX;xx++)
168 for(yy=0;yy<SCR_FIELDY;yy++)
169 if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
170 DrawScreenField(xx,yy);
173 SetDrawtoField(DRAW_DIRECT);
176 if (soft_scrolling_on && game_status==PLAYING)
177 XCopyArea(display,fieldbuffer,backbuffer,gc,
178 FX,FY, SXSIZE,SYSIZE,
181 XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
186 void HandleButtonEvent(XButtonEvent *event)
188 motion_status = FALSE;
190 if (event->type==ButtonPress)
191 button_status = event->button;
193 button_status = MB_RELEASED;
195 HandleButton(event->x, event->y, button_status);
198 void HandleMotionEvent(XMotionEvent *event)
200 motion_status = TRUE;
202 HandleButton(event->x, event->y, button_status);
205 void HandleKeyEvent(XKeyEvent *event)
207 int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
208 unsigned int event_state = (game_status != PLAYING ? event->state : 0);
209 KeySym key = XLookupKeysym(event, event_state);
211 HandleKey(key, key_status);
214 void HandleFocusEvent(XFocusChangeEvent *event)
216 static int old_joystick_status = -1;
218 if (event->type == FocusOut)
220 XAutoRepeatOn(display);
221 old_joystick_status = joystick_status;
222 joystick_status = JOYSTICK_OFF;
223 key_joystick_mapping = 0;
225 else if (event->type == FocusIn)
227 if (game_status == PLAYING)
228 XAutoRepeatOff(display);
229 if (old_joystick_status != -1)
230 joystick_status = old_joystick_status;
234 void HandleClientMessageEvent(XClientMessageEvent *event)
236 if ((event->window == window) &&
237 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
241 void HandleButton(int mx, int my, int button)
243 static int old_mx = 0, old_my = 0;
255 HandleVideoButtons(mx,my,button);
256 HandleSoundButtons(mx,my,button);
257 HandleGameButtons(mx,my,button);
263 HandleMainMenu(mx,my,0,0,button);
266 HandleTypeName(0,XK_Return);
269 HandleChooseLevel(mx,my,0,0,button);
272 HandleHallOfFame(button);
275 LevelEd(mx,my,button);
278 HandleHelpScreen(button);
281 HandleSetupScreen(mx,my,0,0,button);
294 void HandleKey(KeySym key, int key_status)
298 /* Map cursor keys to joystick directions */
302 case XK_Left: /* normale Richtungen */
347 case XK_KP_Home: /* Diagonalrichtungen */
350 joy |= JOY_UP | JOY_LEFT;
356 joy = JOY_UP | JOY_RIGHT;
362 joy |= JOY_DOWN | JOY_LEFT;
364 #ifdef XK_KP_Page_Down
365 case XK_KP_Page_Down:
368 joy |= JOY_DOWN | JOY_RIGHT;
371 case XK_S: /* Feld entfernen */
374 joy |= JOY_BUTTON_1 | JOY_LEFT;
380 joy |= JOY_BUTTON_1 | JOY_RIGHT;
386 joy |= JOY_BUTTON_1 | JOY_UP;
392 joy |= JOY_BUTTON_1 | JOY_DOWN;
394 case XK_Shift_L: /* Linker Feuerknopf */
402 case XK_Shift_R: /* Rechter Feuerknopf */
409 case XK_B: /* (Bombe legen) */
420 if (key_status == KEY_PRESSED)
421 key_joystick_mapping |= joy;
423 key_joystick_mapping &= ~joy;
428 if (game_status != PLAYING)
429 key_joystick_mapping = 0;
431 if (key_status == KEY_RELEASED)
434 if (key==XK_Return && game_status==PLAYING && GameOver)
436 CloseDoor(DOOR_CLOSE_1);
437 game_status = MAINMENU;
442 if (key==XK_Escape && game_status!=MAINMENU) /* quick quit to MAINMENU */
444 CloseDoor(DOOR_CLOSE_1 | DOOR_NO_DELAY);
445 game_status = MAINMENU;
450 if (game_status==PLAYING && (tape.playing || tape.pausing))
456 HandleTypeName(0,key);
465 if (game_status==MAINMENU)
466 HandleMainMenu(0,0,0,0,MB_MENU_CHOICE);
467 else if (game_status==CHOOSELEVEL)
468 HandleChooseLevel(0,0,0,0,MB_MENU_CHOICE);
469 else if (game_status==SETUP)
470 HandleSetupScreen(0,0,0,0,MB_MENU_CHOICE);
478 HandleHelpScreen(MB_RELEASED);
484 game_status = MAINMENU;
493 LevelNameTyping(key);
514 GameSpeed = key - XK_0;
515 printf("GameSpeed == %d\n", GameSpeed);
519 if (ScrollStepSize == TILEX/8)
520 ScrollStepSize = TILEX/4;
522 ScrollStepSize = TILEX/8;
523 printf("ScrollStepSize == %d\n", ScrollStepSize);
527 ScrollStepSize = TILEX/8;
528 printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
531 ScrollStepSize = TILEX/4;
532 printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
535 ScrollStepSize = TILEX/2;
536 printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
539 ScrollStepSize = TILEX;
540 printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
553 int i,j,k, num_steps = 8, step_size = TILEX / num_steps;
554 static long scroll_delay=0;
555 long scroll_delay_value = 4*4 / num_steps;
557 printf("Scroll test\n");
561 for(j=0;j<SCR_FIELDX;j++)
563 for(k=0;k<num_steps;k++)
565 int xxx = j*TILEX+k*step_size;
570 if (DelayReached(&scroll_delay, scroll_delay_value))
572 XCopyArea(display,fieldbuffer,window,gc,
576 XCopyArea(display,fieldbuffer,window,gc,
582 XSync(display,FALSE);
593 Delay(160000 / num_steps);
596 Delay(120000 / num_steps);
607 printf("FX = %d, FY = %d\n", FX,FY);
609 XCopyArea(display,fieldbuffer,window,gc,
611 MIN(WIN_XSIZE,FXSIZE),MIN(WIN_YSIZE,FYSIZE),
614 XSync(display,FALSE);
631 void HandleNoXEvent()
633 if (button_status && game_status != PLAYING)
635 HandleButton(-1,-1,button_status);
657 void HandleJoystick()
659 int joystick = Joystick();
660 int keyboard = key_joystick_mapping;
661 int joy = (tape.playing ? TapePlayAction() : (joystick | keyboard));
662 int left = joy & JOY_LEFT;
663 int right = joy & JOY_RIGHT;
664 int up = joy & JOY_UP;
665 int down = joy & JOY_DOWN;
666 int button = joy & JOY_BUTTON;
667 int button1 = joy & JOY_BUTTON_1;
668 int button2 = joy & JOY_BUTTON_2;
669 int newbutton = (JoystickButton() == JOY_BUTTON_NEW_PRESSED);
670 int dx = (left ? -1 : right ? 1 : 0);
671 int dy = (up ? -1 : down ? 1 : 0);
673 if (game_status==PLAYING && (tape.playing || keyboard))
674 newbutton = ((joy & JOY_BUTTON) != 0);
682 static long joystickmove_delay = 0;
684 if (joystick && !button && !DelayReached(&joystickmove_delay,15))
685 newbutton = dx = dy = 0;
687 if (game_status==MAINMENU)
688 HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
689 else if (game_status==CHOOSELEVEL)
690 HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
691 else if (game_status==SETUP)
692 HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
696 HandleHallOfFame(!newbutton);
699 HandleHelpScreen(!newbutton);
703 BOOL moved = FALSE, snapped = FALSE, bombed = FALSE;
705 if (GameOver && newbutton)
707 CloseDoor(DOOR_CLOSE_1);
708 game_status = MAINMENU;
713 if (tape.pausing || PlayerGone)
719 snapped = SnapField(dx,dy);
723 bombed = PlaceBomb();
724 moved = MoveFigure(dx,dy);
727 if (tape.recording && (moved || snapped || bombed))
729 if (bombed && !moved)
731 TapeRecordAction(joy);
733 else if (tape.playing && snapped)
734 SnapField(0,0); /* stop snapping */
738 DigField(0,0,0,0,DF_NO_PUSH);
743 if (tape.playing && !tape.pausing && !joy && tape.counter<tape.length)
746 tape.pos[tape.counter].joystickdata & (JOY_LEFT|JOY_RIGHT);
748 if (next_joy == JOY_LEFT || next_joy == JOY_RIGHT)
750 int dx = (next_joy == JOY_LEFT ? -1 : +1);
752 if (IN_LEV_FIELD(JX+dx,JY) && IS_PUSHABLE(Feld[JX+dx][JY]))
754 int el = Feld[JX+dx][JY];
755 int push_delay = (IS_SB_ELEMENT(el) || el==EL_SONDE ? 2 : 10);
757 if (tape.delay_played + push_delay >= tape.pos[tape.counter].delay)
759 PlayerMovDir = next_joy;
760 PlayerFrame = FrameCounter % 4;
761 PlayerPushing = TRUE;