X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fevents.c;h=a336f013d3a5ae35140ad8095734aa216e643a58;hp=1e8fcd1c37fc53a57a5c5553c3aea5d4ba118e8c;hb=3ae3dff29ac1a6022b1e5af414ba2c99391148c8;hpb=d0893e6987c21c25ec137438a18cfe1288362139 diff --git a/src/events.c b/src/events.c index 1e8fcd1c..a336f013 100644 --- a/src/events.c +++ b/src/events.c @@ -1,30 +1,38 @@ /*********************************************************** * Rocks'n'Diamonds -- McDuffin Strikes Back! * *----------------------------------------------------------* -* ©1995 Artsoft Development * -* Holger Schemel * -* 33659 Bielefeld-Senne * -* Telefon: (0521) 493245 * -* eMail: aeglos@valinor.owl.de * -* aeglos@uni-paderborn.de * -* q99492@pbhrzx.uni-paderborn.de * +* (c) 1995-98 Artsoft Entertainment * +* Holger Schemel * +* Oststrasse 11a * +* 33604 Bielefeld * +* phone: ++49 +521 290471 * +* email: aeglos@valinor.owl.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" +#include "buttons.h" +#include "network.h" + +/* values for key_status */ +#define KEY_NOT_PRESSED FALSE +#define KEY_RELEASED FALSE +#define KEY_PRESSED TRUE void EventLoop(void) { while(1) { - if (XPending(display)) /* got an event */ + if (XPending(display)) /* got event from X server */ { XEvent event; @@ -32,50 +40,71 @@ void EventLoop(void) switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; - case UnmapNotify: - SleepWhileUnmapped(); - break; case ButtonPress: - HandleButtonEvent((XButtonEvent *) &event); - break; case ButtonRelease: HandleButtonEvent((XButtonEvent *) &event); break; + case MotionNotify: 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); - break; + default: + HandleOtherEvents(&event); break; } } - else /* got no event, but don't be lazy... */ - { - HandleNoXEvent(); - if (game_status!=PLAYING) - Delay(10000); /* don't use all CPU time when idle */ + HandleNoXEvent(); + + /* 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) + /* refresh window contents from drawing buffer, if needed */ + BackToFront(); + + if (game_status == EXITGAME) return; } } +void HandleOtherEvents(XEvent *event) +{ + switch(event->type) + { + case Expose: + HandleExposeEvent((XExposeEvent *) event); + break; + + case UnmapNotify: + SleepWhileUnmapped(); + break; + + case FocusIn: + case FocusOut: + HandleFocusEvent((XFocusChangeEvent *) event); + break; + + case ClientMessage: + HandleClientMessageEvent((XClientMessageEvent *) event); + break; + + default: + break; + } +} + void ClearEventQueue() { while(XPending(display)) @@ -86,25 +115,16 @@ void ClearEventQueue() switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; - case UnmapNotify: - SleepWhileUnmapped(); - break; case ButtonRelease: button_status = MB_RELEASED; break; + case KeyRelease: - key_status = KEY_RELEASED; - break; - case FocusIn: - HandleFocusEvent(FOCUS_IN); - break; - case FocusOut: - HandleFocusEvent(FOCUS_OUT); + key_joystick_mapping = 0; break; + default: + HandleOtherEvents(&event); break; } } @@ -112,7 +132,7 @@ void ClearEventQueue() void SleepWhileUnmapped() { - BOOL window_unmapped = TRUE; + boolean window_unmapped = TRUE; XAutoRepeatOn(display); @@ -124,24 +144,32 @@ void SleepWhileUnmapped() switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; case ButtonRelease: button_status = MB_RELEASED; break; + case KeyRelease: - key_status = KEY_RELEASED; + key_joystick_mapping = 0; break; + case MapNotify: window_unmapped = FALSE; break; + + case UnmapNotify: + /* this is only to surely prevent the 'should not happen' case + * of recursively looping between 'SleepWhileUnmapped()' and + * 'HandleOtherEvents()' which usually calls this funtion. + */ + break; + default: + HandleOtherEvents(&event); break; } } - if (game_status==PLAYING) + if (game_status == PLAYING) XAutoRepeatOff(display); } @@ -150,21 +178,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) + if (setup.direct_draw && 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 (setup.soft_scrolling && game_status == PLAYING) + { + int fx = FX, fy = FY; + + fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0); + fy += (ScreenMovDir & (MV_UP|MV_DOWN) ? ScreenGfxPos : 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); } @@ -172,7 +216,7 @@ void HandleButtonEvent(XButtonEvent *event) { motion_status = FALSE; - if (event->type==ButtonPress) + if (event->type == ButtonPress) button_status = event->button; else button_status = MB_RELEASED; @@ -182,208 +226,332 @@ void HandleButtonEvent(XButtonEvent *event) void HandleMotionEvent(XMotionEvent *event) { + Window root, child; + int root_x, root_y; + int win_x, win_y; + unsigned int mask; + + if (!XQueryPointer(display, window, &root, &child, &root_x, &root_y, + &win_x, &win_y, &mask)) + return; + + if (!button_status && game_status != LEVELED) + return; + motion_status = TRUE; - HandleButton(event->x, event->y, button_status); + HandleButton(win_x, win_y, button_status); } 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); + KeySym key; - if (game_status==PLAYING && - (old_keycode!=new_keycode || key_status!=new_key_status)) + if (game_status == PLAYING) { - DigField(0,0,DF_NO_PUSH); - SnapField(0,0); + /* use '0' instead of 'event->state' to get the key without modifiers */ + key = XLookupKeysym(event, 0); } - - if (event->type==KeyPress) + else { - key_status = KEY_PRESSED; - HandleKey(new_key); - old_keycode = new_keycode; + /* get the key with all modifiers */ + char buffer[10]; + int buffer_size = 10; + XComposeStatus compose; + int char_count; + + char_count = XLookupString(event, buffer, buffer_size, &key, &compose); + buffer[char_count] = '\0'; } - else if (key_status==KEY_PRESSED && old_keycode==new_keycode) - key_status = KEY_RELEASED; + + HandleKey(key, key_status); } -void HandleFocusEvent(int focus_status) +void HandleFocusEvent(XFocusChangeEvent *event) { - if (focus_status==FOCUS_OUT) + static int old_joystick_status = -1; + + if (event->type == FocusOut) + { + int i; + XAutoRepeatOn(display); - else if (game_status==PLAYING) - XAutoRepeatOff(display); + old_joystick_status = joystick_status; + joystick_status = JOYSTICK_OFF; + + /* simulate key release events for still pressed keys */ + key_joystick_mapping = 0; + for (i=0; itype == FocusIn) + { + /* When there are two Rocks'n'Diamonds windows which overlap and + the player moves the pointer from one game window to the other, + a 'FocusOut' event is generated for the window the pointer is + leaving and a 'FocusIn' event is generated for the window the + pointer is entering. In some cases, it can happen that the + 'FocusIn' event is handled by the one game process before the + 'FocusOut' event by the other game process. In this case the + X11 environment would end up with activated keyboard auto repeat, + because unfortunately this is a global setting and not (which + would be far better) set for each X11 window individually. + The effect would be keyboard auto repeat while playing the game + (game_status == PLAYING), which is not desired. + To avoid this special case, we just wait 1/50 second before + processing the 'FocusIn' event. + */ + + Delay(20); + if (game_status == PLAYING) + XAutoRepeatOff(display); + if (old_joystick_status != -1) + joystick_status = old_joystick_status; + } +} + +void HandleClientMessageEvent(XClientMessageEvent *event) +{ +#ifndef MSDOS + if ((event->window == window) && + (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE))) + CloseAllAndExit(0); +#endif } void HandleButton(int mx, int my, int button) { static int old_mx = 0, old_my = 0; - if (mx<0 || my<0) + if (button < 0) { mx = old_mx; my = old_my; + button = -button; } else { old_mx = mx; old_my = my; - HandleVideoButtons(mx,my,button); - HandleSoundButtons(mx,my,button); - HandleGameButtons(mx,my,button); + /* + HandleVideoButtons(mx,my, button); + HandleSoundButtons(mx,my, button); + HandleGameButtons(mx,my, button); + */ } + HandleGadgets(mx, my, button); + switch(game_status) { case MAINMENU: - HandleMainMenu(mx,my,0,0,button); + HandleMainMenu(mx,my, 0,0, button); break; + case TYPENAME: - HandleTypeName(0,XK_Return); + HandleTypeName(0, XK_Return); break; + case CHOOSELEVEL: - HandleChooseLevel(mx,my,0,0,button); + HandleChooseLevel(mx,my, 0,0, button); break; + case HALLOFFAME: HandleHallOfFame(button); break; + case LEVELED: - LevelEd(mx,my,button); break; + case HELPSCREEN: HandleHelpScreen(button); break; + case SETUP: - HandleSetupScreen(mx,my,0,0,button); + HandleSetupScreen(mx,my, 0,0, button); break; + + case SETUPINPUT: + HandleSetupInputScreen(mx,my, 0,0, button); + break; + case PLAYING: - if (!LevelSolved) +#ifdef DEBUG + if (button == MB_RELEASED) { - switch(GameActions(mx,my,button)) + int sx = (mx - SX) / TILEX; + int sy = (my - SY) / TILEY; + + if (IN_VIS_FIELD(sx,sy)) { - 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: + int x = LEVELX(sx); + int y = LEVELY(sy); + + printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y); + + if (!IN_LEV_FIELD(x, y)) break; + + printf(" 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"); } } - BackToFront(); - Delay(10000); +#endif break; + default: break; } } -void HandleKey(KeySym key) +void HandleKey(KeySym key, int key_status) { - static KeySym old_key = 0; + int joy = 0; + static struct SetupKeyboardInfo custom_key; + static struct + { + KeySym *keysym_custom; + KeySym keysym_default; + byte action; + } key_info[] = + { + { &custom_key.left, DEFAULT_KEY_LEFT, JOY_LEFT }, + { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT }, + { &custom_key.up, DEFAULT_KEY_UP, JOY_UP }, + { &custom_key.down, DEFAULT_KEY_DOWN, JOY_DOWN }, + { &custom_key.snap, DEFAULT_KEY_SNAP, JOY_BUTTON_1 }, + { &custom_key.bomb, DEFAULT_KEY_BOMB, JOY_BUTTON_2 } + }; + + if (game_status == PLAYING) + { + int pnr; + + for (pnr=0; pnrdynamite = 1000; 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); +#if 0 + + case XK_z: + { + int i; + + for(i=0; i