X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fevents.c;h=9a2cdd41b6f6cce864ff645838e8572564bd3d9d;hb=1478ab1f979ae33fd900e5148c5d00dcba5ad402;hp=1e8fcd1c37fc53a57a5c5553c3aea5d4ba118e8c;hpb=d0893e6987c21c25ec137438a18cfe1288362139;p=rocksndiamonds.git diff --git a/src/events.c b/src/events.c index 1e8fcd1c..9a2cdd41 100644 --- a/src/events.c +++ b/src/events.c @@ -10,15 +10,17 @@ * q99492@pbhrzx.uni-paderborn.de * *----------------------------------------------------------* * events.c * -* * -* Letzte Aenderung: 15.06.1995 * ***********************************************************/ #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) { @@ -39,8 +41,6 @@ void EventLoop(void) SleepWhileUnmapped(); break; case ButtonPress: - HandleButtonEvent((XButtonEvent *) &event); - break; case ButtonRelease: HandleButtonEvent((XButtonEvent *) &event); break; @@ -48,16 +48,15 @@ void EventLoop(void) 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; @@ -67,11 +66,17 @@ void EventLoop(void) { HandleNoXEvent(); - if (game_status!=PLAYING) - Delay(10000); /* don't use all CPU time when idle */ + /* 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) + { + XSync(display, FALSE); + Delay(10); + } } - if (game_status==EXITGAME) + if (game_status == EXITGAME) return; } } @@ -96,13 +101,14 @@ void ClearEventQueue() 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; @@ -131,11 +137,14 @@ void SleepWhileUnmapped() 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; } @@ -150,21 +159,37 @@ void HandleExposeEvent(XExposeEvent *event) 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=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); } @@ -189,34 +214,38 @@ void HandleMotionEvent(XMotionEvent *event) 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); - if (game_status==PLAYING && - (old_keycode!=new_keycode || key_status!=new_key_status)) + HandleKey(key, key_status); +} + +void HandleFocusEvent(XFocusChangeEvent *event) +{ + static int old_joystick_status = -1; + + 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) @@ -238,6 +267,29 @@ 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: @@ -262,41 +314,160 @@ void HandleButton(int mx, int my, int button) 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 */ { @@ -318,8 +489,6 @@ void HandleKey(KeySym key) case CHOOSELEVEL: case SETUP: { - int dx = 0, dy = 0; - switch(key) { case XK_Return: @@ -330,55 +499,9 @@ void HandleKey(KeySym key) 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: @@ -401,128 +524,155 @@ void HandleKey(KeySym key) 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