***********************************************************/
#include "events.h"
+#include "init.h"
#include "screens.h"
#include "tools.h"
#include "game.h"
#include "editor.h"
#include "misc.h"
+#include "tape.h"
+#include "joystick.h"
void EventLoop(void)
{
SleepWhileUnmapped();
break;
case ButtonPress:
- HandleButtonEvent((XButtonEvent *) &event);
- break;
case ButtonRelease:
HandleButtonEvent((XButtonEvent *) &event);
break;
HandleMotionEvent((XMotionEvent *) &event);
break;
case KeyPress:
- HandleKeyEvent((XKeyEvent *) &event);
- break;
case KeyRelease:
HandleKeyEvent((XKeyEvent *) &event);
break;
case FocusIn:
- HandleFocusEvent(FOCUS_IN);
- break;
case FocusOut:
- HandleFocusEvent(FOCUS_OUT);
+ HandleFocusEvent((XFocusChangeEvent *) &event);
+ break;
+ case ClientMessage:
+ HandleClientMessageEvent((XClientMessageEvent *) &event);
break;
default:
break;
{
HandleNoXEvent();
- XSync(display,FALSE);
+ /* don't use all CPU time when idle; the main loop while playing
+ has its own synchronization and is CPU friendly, too */
- if (game_status!=PLAYING)
- Delay(10000); /* don't use all CPU time when idle */
+ if (game_status != PLAYING)
+ {
+ XSync(display, FALSE);
+ Delay(10);
+ }
}
- if (game_status==EXITGAME)
+ if (game_status == EXITGAME)
return;
}
}
button_status = MB_RELEASED;
break;
case KeyRelease:
- key_status = KEY_RELEASED;
+ key_joystick_mapping = 0;
break;
case FocusIn:
- HandleFocusEvent(FOCUS_IN);
- break;
case FocusOut:
- HandleFocusEvent(FOCUS_OUT);
+ HandleFocusEvent((XFocusChangeEvent *) &event);
+ break;
+ case ClientMessage:
+ HandleClientMessageEvent((XClientMessageEvent *) &event);
break;
default:
break;
button_status = MB_RELEASED;
break;
case KeyRelease:
- key_status = KEY_RELEASED;
+ key_joystick_mapping = 0;
break;
case MapNotify:
window_unmapped = FALSE;
break;
+ case ClientMessage:
+ HandleClientMessageEvent((XClientMessageEvent *) &event);
+ break;
default:
break;
}
int x = event->x, y = event->y;
int width = event->width, height = event->height;
- XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
-
if (direct_draw_on && game_status==PLAYING)
{
int xx,yy;
int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
- for(xx=0;xx<SCR_FIELDX;xx++)
- for(yy=0;yy<SCR_FIELDY;yy++)
+ SetDrawtoField(DRAW_BACKBUFFER);
+
+ for(xx=0; xx<SCR_FIELDX; xx++)
+ for(yy=0; yy<SCR_FIELDY; yy++)
if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
DrawScreenField(xx,yy);
- DrawLevelElement(JX,JY,EL_SPIELFIGUR);
+ DrawAllPlayers();
+
+ SetDrawtoField(DRAW_DIRECT);
}
+ if (soft_scrolling_on && game_status == PLAYING)
+ {
+ int fx = FX, fy = FY;
+
+ fx += (local_player->MovDir & (MV_LEFT|MV_RIGHT) ? ScreenMovPos : 0);
+ fy += (local_player->MovDir & (MV_UP|MV_DOWN) ? ScreenMovPos : 0);
+
+ XCopyArea(display,fieldbuffer,backbuffer,gc,
+ fx,fy, SXSIZE,SYSIZE,
+ SX,SY);
+ }
+
+ XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
+
XFlush(display);
}
void HandleKeyEvent(XKeyEvent *event)
{
- static KeySym old_keycode = 0;
- int new_keycode = event->keycode;
- KeySym new_key = XLookupKeysym(event,event->state);
- int new_key_status = (event->type==KeyPress ? KEY_PRESSED : KEY_RELEASED);
+ int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
+ unsigned int event_state = (game_status != PLAYING ? event->state : 0);
+ KeySym key = XLookupKeysym(event, event_state);
+
+ HandleKey(key, key_status);
+}
+
+void HandleFocusEvent(XFocusChangeEvent *event)
+{
+ static int old_joystick_status = -1;
- if (game_status==PLAYING &&
- (old_keycode!=new_keycode || key_status!=new_key_status))
+ if (event->type == FocusOut)
{
- DigField(0,0,DF_NO_PUSH);
- SnapField(0,0);
+ XAutoRepeatOn(display);
+ old_joystick_status = joystick_status;
+ joystick_status = JOYSTICK_OFF;
+ key_joystick_mapping = 0;
}
-
- if (event->type==KeyPress)
+ else if (event->type == FocusIn)
{
- key_status = KEY_PRESSED;
- HandleKey(new_key);
- old_keycode = new_keycode;
+ if (game_status == PLAYING)
+ XAutoRepeatOff(display);
+ if (old_joystick_status != -1)
+ joystick_status = old_joystick_status;
}
- else if (key_status==KEY_PRESSED && old_keycode==new_keycode)
- key_status = KEY_RELEASED;
}
-void HandleFocusEvent(int focus_status)
+void HandleClientMessageEvent(XClientMessageEvent *event)
{
- if (focus_status==FOCUS_OUT)
- XAutoRepeatOn(display);
- else if (game_status==PLAYING)
- XAutoRepeatOff(display);
+ if ((event->window == window) &&
+ (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
+ CloseAll();
}
void HandleButton(int mx, int my, int button)
HandleGameButtons(mx,my,button);
}
+#ifdef DEBUG
+ if (game_status == PLAYING && !button)
+ {
+ int sx = (mx - SX) / TILEX;
+ int sy = (my - SY) / TILEY;
+
+ if (IN_SCR_FIELD(sx,sy))
+ {
+ int x = LEVELX(sx);
+ int y = LEVELY(sy);
+
+ printf("INFO: Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
+ printf(" Store[%d][%d] == %d\n", x,y, Store[x][y]);
+ printf(" Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
+ printf(" StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
+ printf(" MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
+ printf(" MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
+ printf(" MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
+ printf("\n");
+ }
+ }
+#endif
+
switch(game_status)
{
case MAINMENU:
HandleSetupScreen(mx,my,0,0,button);
break;
case PLAYING:
- if (!LevelSolved)
- {
- switch(GameActions(mx,my,button))
- {
- case ACT_GAME_OVER:
- game_status = MAINMENU;
- DrawMainMenu();
- BackToFront();
- break;
- case ACT_NEW_GAME:
- game_status = PLAYING;
- InitGame();
- break;
- case ACT_GO_ON:
- break;
- default:
- break;
- }
- }
- BackToFront();
- Delay(10000);
+ HandleGameActions(0);
break;
default:
break;
}
}
-void HandleKey(KeySym key)
+void HandleKey(KeySym key, int key_status)
{
- static KeySym old_key = 0;
+ int joy = 0;
- if (!key)
- key = old_key;
- else
- old_key = key;
+ /* Map cursor keys to joystick directions */
+
+ switch(key)
+ {
+ case XK_Left: /* normale Richtungen */
+#ifdef XK_KP_Left
+ case XK_KP_Left:
+#endif
+ case XK_KP_4:
+#ifndef MSDOS
+ case XK_J:
+#endif
+ case XK_j:
+ joy |= JOY_LEFT;
+ break;
+ case XK_Right:
+#ifdef XK_KP_Right
+ case XK_KP_Right:
+#endif
+ case XK_KP_6:
+#ifndef MSDOS
+ case XK_K:
+#endif
+ case XK_k:
+ joy |= JOY_RIGHT;
+ break;
+ case XK_Up:
+#ifdef XK_KP_Up
+ case XK_KP_Up:
+#endif
+ case XK_KP_8:
+#ifndef MSDOS
+ case XK_I:
+#endif
+ case XK_i:
+ joy |= JOY_UP;
+ break;
+ case XK_Down:
+#ifdef XK_KP_Down
+ case XK_KP_Down:
+#endif
+ case XK_KP_2:
+#ifndef MSDOS
+ case XK_M:
+#endif
+ case XK_m:
+ joy |= JOY_DOWN;
+ break;
+#ifdef XK_KP_Home
+ case XK_KP_Home: /* Diagonalrichtungen */
+#endif
+ case XK_KP_7:
+ joy |= JOY_UP | JOY_LEFT;
+ break;
+#ifdef XK_KP_Page_Up
+ case XK_KP_Page_Up:
+#endif
+ case XK_KP_9:
+ joy = JOY_UP | JOY_RIGHT;
+ break;
+#ifdef XK_KP_End
+ case XK_KP_End:
+#endif
+ case XK_KP_1:
+ joy |= JOY_DOWN | JOY_LEFT;
+ break;
+#ifdef XK_KP_Page_Down
+ case XK_KP_Page_Down:
+#endif
+ case XK_KP_3:
+ joy |= JOY_DOWN | JOY_RIGHT;
+ break;
+#ifndef MSDOS
+ case XK_S: /* Feld entfernen */
+#endif
+ case XK_s:
+ joy |= JOY_BUTTON_1 | JOY_LEFT;
+ break;
+#ifndef MSDOS
+ case XK_D:
+#endif
+ case XK_d:
+ joy |= JOY_BUTTON_1 | JOY_RIGHT;
+ break;
+#ifndef MSDOS
+ case XK_E:
+#endif
+ case XK_e:
+ joy |= JOY_BUTTON_1 | JOY_UP;
+ break;
+#ifndef MSDOS
+ case XK_X:
+#endif
+ case XK_x:
+ joy |= JOY_BUTTON_1 | JOY_DOWN;
+ break;
+ case XK_Shift_L: /* Linker Feuerknopf */
+#ifndef MSDOS
+ case XK_Control_L:
+ case XK_Alt_L:
+ case XK_Meta_L:
+#endif
+ joy |= JOY_BUTTON_1;
+ break;
+ case XK_Shift_R: /* Rechter Feuerknopf */
+#ifndef MSDOS
+ case XK_Control_R:
+ case XK_Alt_R:
+ case XK_Meta_R:
+ case XK_Mode_switch:
+ case XK_Multi_key:
+ case XK_B: /* (Bombe legen) */
+#endif
+ case XK_b:
+ joy |= JOY_BUTTON_2;
+ break;
+ default:
+ break;
+ }
+
+ if (joy)
+ {
+ if (key_status == KEY_PRESSED)
+ key_joystick_mapping |= joy;
+ else
+ key_joystick_mapping &= ~joy;
+
+ HandleJoystick();
+ }
+
+ if (game_status != PLAYING)
+ key_joystick_mapping = 0;
+
+ if (key_status == KEY_RELEASED)
+ return;
+
+ if (key==XK_Return && game_status==PLAYING && AllPlayersGone)
+ {
+ CloseDoor(DOOR_CLOSE_1);
+ game_status = MAINMENU;
+ DrawMainMenu();
+ return;
+ }
if (key==XK_Escape && game_status!=MAINMENU) /* quick quit to MAINMENU */
{
case CHOOSELEVEL:
case SETUP:
{
- int dx = 0, dy = 0;
-
switch(key)
{
case XK_Return:
else if (game_status==SETUP)
HandleSetupScreen(0,0,0,0,MB_MENU_CHOICE);
break;
- case XK_Left:
-#ifdef XK_KP_Left
- case XK_KP_Left:
-#endif
- case XK_KP_4:
- case XK_J:
- case XK_j:
- dx = -1;
- break;
- case XK_Right:
-#ifdef XK_KP_Right
- case XK_KP_Right:
-#endif
- case XK_KP_6:
- case XK_K:
- case XK_k:
- dx = 1;
- break;
- case XK_Up:
-#ifdef XK_KP_Up
- case XK_KP_Up:
-#endif
- case XK_KP_8:
- case XK_I:
- case XK_i:
- dy = -1;
- break;
- case XK_Down:
-#ifdef XK_KP_Down
- case XK_KP_Down:
-#endif
- case XK_KP_2:
- case XK_M:
- case XK_m:
- dy = 1;
- break;
default:
break;
}
-
- if (dx || dy)
- {
- if (game_status==MAINMENU)
- HandleMainMenu(0,0,dx,dy,MB_MENU_MARK);
- else if (game_status==CHOOSELEVEL)
- HandleChooseLevel(0,0,dx,dy,MB_MENU_MARK);
- else if (game_status==SETUP)
- HandleSetupScreen(0,0,dx,dy,MB_MENU_MARK);
- }
break;
}
case HELPSCREEN:
break;
case PLAYING:
{
- int mvx = 0, mvy = 0;
- int sbx = 0, sby = 0;
- int joy = 0;
- BOOL bomb = FALSE;
- BOOL moved = FALSE, snapped = FALSE, bombed = FALSE;
-
switch(key)
{
- case XK_Left: /* normale Richtungen */
-#ifdef XK_KP_Left
- case XK_KP_Left:
-#endif
- case XK_KP_4:
- case XK_J:
- case XK_j:
- mvx = -1;
- joy = JOY_LEFT;
- break;
- case XK_Right:
-#ifdef XK_KP_Right
- case XK_KP_Right:
-#endif
- case XK_KP_6:
- case XK_K:
- case XK_k:
- mvx = 1;
- joy = JOY_RIGHT;
- break;
- case XK_Up:
-#ifdef XK_KP_Up
- case XK_KP_Up:
-#endif
- case XK_KP_8:
- case XK_I:
- case XK_i:
- mvy = -1;
- joy = JOY_UP;
- break;
- case XK_Down:
-#ifdef XK_KP_Down
- case XK_KP_Down:
-#endif
- case XK_KP_2:
- case XK_M:
- case XK_m:
- mvy = 1;
- joy = JOY_DOWN;
- break;
-#ifdef XK_KP_Home
- case XK_KP_Home: /* Diagonalrichtungen */
-#endif
- case XK_KP_7:
- mvx = -1;
- mvy = -1;
- joy = JOY_UP | JOY_LEFT;
- break;
-#ifdef XK_KP_Page_Up
- case XK_KP_Page_Up:
-#endif
- case XK_KP_9:
- mvx = 1;
- mvy = -1;
- joy = JOY_UP | JOY_RIGHT;
- break;
-#ifdef XK_KP_End
- case XK_KP_End:
-#endif
- case XK_KP_1:
- mvx = -1;
- mvy = 1;
- joy = JOY_DOWN | JOY_LEFT;
- break;
-#ifdef XK_KP_Page_Down
- case XK_KP_Page_Down:
+
+#ifdef DEBUG
+ case XK_0:
+ case XK_1:
+ case XK_2:
+ case XK_3:
+ case XK_4:
+ case XK_5:
+ case XK_6:
+ case XK_7:
+ case XK_8:
+ case XK_9:
+ if (key == XK_0)
+ GameFrameDelay = 500;
+ else
+ GameFrameDelay = (key - XK_0) * 10;
+ printf("Game speed == %d%% (%d ms delay between two frames)\n",
+ GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
+ break;
+
+ case XK_a:
+ if (ScrollStepSize == TILEX/8)
+ ScrollStepSize = TILEX/4;
+ else
+ ScrollStepSize = TILEX/8;
+ printf("ScrollStepSize == %d\n", ScrollStepSize);
+ break;
+
+ case XK_f:
+ ScrollStepSize = TILEX/8;
+ printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
+ break;
+ case XK_g:
+ ScrollStepSize = TILEX/4;
+ printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
+ break;
+ case XK_h:
+ ScrollStepSize = TILEX/2;
+ printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
+ break;
+ case XK_l:
+ ScrollStepSize = TILEX;
+ printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
+ break;
+
+#ifndef MSDOS
+ case XK_Q:
#endif
- case XK_KP_3:
- mvx = 1;
- mvy = 1;
- joy = JOY_DOWN | JOY_RIGHT;
- break;
- case XK_S: /* Feld entfernen */
- case XK_s:
- sbx = -1;
- joy = JOY_BUTTON_1 | JOY_LEFT;
- break;
- case XK_D:
- case XK_d:
- sbx = 1;
- joy = JOY_BUTTON_1 | JOY_RIGHT;
- break;
- case XK_E:
- case XK_e:
- sby = -1;
- joy = JOY_BUTTON_1 | JOY_UP;
+ case XK_q:
+ local_player->dynamite = 1000;
break;
- case XK_X:
+
case XK_x:
- sby = 1;
- joy = JOY_BUTTON_1 | JOY_DOWN;
- break;
- case XK_B: /* Bombe legen */
- case XK_b:
- bomb = TRUE;
- joy = JOY_BUTTON_2;
- break;
- case XK_Q:
- Dynamite = 1000;
+
+ {
+ int i,j,k, num_steps = 8, step_size = TILEX / num_steps;
+ static long scroll_delay=0;
+ long scroll_delay_value = 4*4 / num_steps;
+
+ printf("Scroll test\n");
+
+ for(i=0;i<3;i++)
+ {
+ for(j=0;j<SCR_FIELDX;j++)
+ {
+ for(k=0;k<num_steps;k++)
+ {
+ int xxx = j*TILEX+k*step_size;
+ int done = 0;
+
+ while(!done)
+ {
+ if (DelayReached(&scroll_delay, scroll_delay_value))
+ {
+ XCopyArea(display,fieldbuffer,window,gc,
+ SX+xxx,SY,
+ SXSIZE-xxx,SYSIZE,
+ SX,SY);
+ XCopyArea(display,fieldbuffer,window,gc,
+ SX,SY,
+ xxx,SYSIZE,
+ SX+SXSIZE-xxx,SY);
+
+ XFlush(display);
+ XSync(display,FALSE);
+
+ done = 1;
+ }
+ else
+ {
+ Delay(1);
+ }
+ }
+
+ /*
+ Delay(160 / num_steps);
+ */
+ /*
+ Delay(120 / num_steps);
+ */
+ }
+ }
+ }
+ }
+
+ break;
+
+ case XK_y:
+ /*
+ {
+ printf("FX = %d, FY = %d\n", FX,FY);
+
+ XCopyArea(display,fieldbuffer,window,gc,
+ 0,0,
+ MIN(WIN_XSIZE,FXSIZE),MIN(WIN_YSIZE,FYSIZE),
+ 0,0);
+ XFlush(display);
+ XSync(display,FALSE);
+ Delay(1000);
+ }
+ */
+
+ printf("direct_draw_on == %d\n", direct_draw_on);
+
+ break;
+
+ case XK_z:
+ {
+ int i;
+
+ for(i=0; i<MAX_PLAYERS; i++)
+ {
+ printf("Player %d:\n", i);
+ printf(" jx == %d, jy == %d\n",
+ stored_player[i].jx, stored_player[i].jy);
+ printf(" last_jx == %d, last_jy == %d\n",
+ stored_player[i].last_jx, stored_player[i].last_jy);
+ }
+ printf("\n");
+ }
+
break;
+#endif
+
default:
break;
}
-
- if (mvx || mvy)
- moved = MoveFigure(mvx,mvy);
- else if (sbx || sby)
- snapped = SnapField(sbx,sby);
- else if (bomb)
- bombed = PlaceBomb();
-
- if (tape.recording && (moved || snapped || bombed))
- TapeRecordAction(joy);
-
break;
}
default:
void HandleNoXEvent()
{
- if (button_status)
+ if (button_status && game_status != PLAYING)
{
HandleButton(-1,-1,button_status);
return;
break;
case PLAYING:
HandleJoystick();
- if (key_status)
- HandleKey(0);
- if (game_status!=PLAYING)
- break;
- if (!LevelSolved)
- {
- switch(GameActions(0,0,MB_NOT_PRESSED))
- {
- case ACT_GAME_OVER:
- game_status = MAINMENU;
- DrawMainMenu();
- BackToFront();
- break;
- case ACT_NEW_GAME:
- game_status = PLAYING;
- InitGame();
- break;
- case ACT_GO_ON:
- break;
- default:
- break;
- }
- }
-
- Delay(10000);
+ /*
+ HandleGameActions(0);
+ */
break;
default:
void HandleJoystick()
{
- int joy = Joystick();
+ int joystick = Joystick();
+ int keyboard = key_joystick_mapping;
+ int joy = (tape.playing ? TapePlayAction() : (joystick | keyboard));
int left = joy & JOY_LEFT;
int right = joy & JOY_RIGHT;
int up = joy & JOY_UP;
int down = joy & JOY_DOWN;
int button = joy & JOY_BUTTON;
- int button1 = joy & JOY_BUTTON_1;
- int button2 = joy & JOY_BUTTON_2;
- int newbutton = (JoystickButton()==JOY_BUTTON_NEW_PRESSED);
-
- if (button_status || key_status)
- return;
+ int newbutton = (JoystickButton() == JOY_BUTTON_NEW_PRESSED);
+ int dx = (left ? -1 : right ? 1 : 0);
+ int dy = (up ? -1 : down ? 1 : 0);
switch(game_status)
{
case CHOOSELEVEL:
case SETUP:
{
- int dx = 0, dy = 0;
static long joystickmove_delay = 0;
- if (DelayReached(&joystickmove_delay,15) || button)
- {
- if (left)
- dx = -1;
- else if (right)
- dx = 1;
- if (up)
- dy = -1;
- else if (down)
- dy = 1;
- }
+ if (joystick && !button && !DelayReached(&joystickmove_delay,15))
+ newbutton = dx = dy = 0;
if (game_status==MAINMENU)
HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
break;
}
+
case HALLOFFAME:
HandleHallOfFame(!newbutton);
break;
+
case HELPSCREEN:
HandleHelpScreen(!newbutton);
break;
- case PLAYING:
- {
- int mvx = 0, mvy = 0;
- BOOL moved = FALSE, snapped = FALSE, bombed = FALSE;
- if (tape.playing)
- {
- joy = TapePlayAction();
-
- left = joy & JOY_LEFT;
- right = joy & JOY_RIGHT;
- up = joy & JOY_UP;
- down = joy & JOY_DOWN;
- button = joy & JOY_BUTTON;
- button1 = joy & JOY_BUTTON_1;
- button2 = joy & JOY_BUTTON_2;
- }
- else if (tape.pausing)
- joy = 0;
-
- if (!joy)
- {
- DigField(0,0,DF_NO_PUSH);
- break;
- }
+ case PLAYING:
+ if (tape.playing || keyboard)
+ newbutton = ((joy & JOY_BUTTON) != 0);
- if (GameOver && newbutton)
+ if (AllPlayersGone && newbutton)
{
CloseDoor(DOOR_CLOSE_1);
game_status = MAINMENU;
return;
}
- if (left)
- mvx = -1;
- else if (right)
- mvx = 1;
- if (up)
- mvy = -1;
- else if (down)
- mvy = 1;
-
- if (button1)
- snapped = SnapField(mvx,mvy);
- else
- {
- if (button2)
- bombed = PlaceBomb();
- moved = MoveFigure(mvx,mvy);
- }
-
- if (tape.recording && (moved || snapped || bombed))
- {
- if (bombed && !moved)
- joy &= JOY_BUTTON;
- TapeRecordAction(joy);
- }
- else if (tape.playing && snapped)
- SnapField(0,0);
+ if (tape.pausing || AllPlayersGone)
+ joy = 0;
+ HandleGameActions(joy);
break;
- }
+
default:
break;
}