X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fevents.c;h=2d8eca3e861657b6442169036ac88d5395f08934;hp=f1cd50dafaa7dca21d6b7c4583d94300c0540e78;hb=50c422d96b6da2c0412b6bd3763e9d1a029a6014;hpb=b2a0ff1ddd4430110b331129469dabb8ea7b6ba7 diff --git a/src/events.c b/src/events.c index f1cd50da..2d8eca3e 100644 --- a/src/events.c +++ b/src/events.c @@ -20,6 +20,12 @@ #include "misc.h" #include "tape.h" #include "joystick.h" +#include "network.h" + +/* values for key_status */ +#define KEY_NOT_PRESSED FALSE +#define KEY_RELEASED FALSE +#define KEY_PRESSED TRUE void EventLoop(void) { @@ -33,46 +39,35 @@ void EventLoop(void) switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; - case UnmapNotify: - SleepWhileUnmapped(); - break; case ButtonPress: case ButtonRelease: HandleButtonEvent((XButtonEvent *) &event); break; + case MotionNotify: HandleMotionEvent((XMotionEvent *) &event); break; + case KeyPress: case KeyRelease: HandleKeyEvent((XKeyEvent *) &event); break; - case FocusIn: - case FocusOut: - HandleFocusEvent((XFocusChangeEvent *) &event); - break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); - break; + default: + HandleOtherEvents(&event); break; } } - else /* got no event, but don't be lazy... */ - { - HandleNoXEvent(); - /* don't use all CPU time when idle; the main loop while playing - has its own synchronization and is CPU friendly, too */ + HandleNoXEvent(); - if (game_status != PLAYING) - { - XSync(display, FALSE); - Delay(10); - } + /* 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) @@ -80,6 +75,32 @@ void EventLoop(void) } } +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)) @@ -90,26 +111,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_joystick_mapping = 0; break; - case FocusIn: - case FocusOut: - HandleFocusEvent((XFocusChangeEvent *) &event); - break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); - break; + default: + HandleOtherEvents(&event); break; } } @@ -117,7 +128,7 @@ void ClearEventQueue() void SleepWhileUnmapped() { - BOOL window_unmapped = TRUE; + boolean window_unmapped = TRUE; XAutoRepeatOn(display); @@ -129,22 +140,27 @@ void SleepWhileUnmapped() switch(event.type) { - case Expose: - HandleExposeEvent((XExposeEvent *) &event); - break; case ButtonRelease: button_status = MB_RELEASED; break; + case KeyRelease: key_joystick_mapping = 0; break; + case MapNotify: window_unmapped = FALSE; break; - case ClientMessage: - HandleClientMessageEvent((XClientMessageEvent *) &event); + + 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; } } @@ -158,7 +174,7 @@ void HandleExposeEvent(XExposeEvent *event) int x = event->x, y = event->y; int width = event->width, height = event->height; - 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; @@ -175,7 +191,7 @@ void HandleExposeEvent(XExposeEvent *event) SetDrawtoField(DRAW_DIRECT); } - if (soft_scrolling_on && game_status == PLAYING) + if (setup.soft_scrolling && game_status == PLAYING) { int fx = FX, fy = FY; @@ -242,9 +258,11 @@ void HandleFocusEvent(XFocusChangeEvent *event) void HandleClientMessageEvent(XClientMessageEvent *event) { +#ifndef MSDOS if ((event->window == window) && (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE))) - CloseAll(); + CloseAllAndExit(0); +#endif } void HandleButton(int mx, int my, int button) @@ -261,60 +279,70 @@ void HandleButton(int mx, int my, int button) old_mx = mx; old_my = my; - HandleVideoButtons(mx,my,button); - HandleSoundButtons(mx,my,button); - HandleGameButtons(mx,my,button); - } - -#ifdef DEBUG - if (game_status == PLAYING && !button) - { - int sx = (mx - SX) / TILEX; - int sy = (my - SY) / TILEY; - - if (IN_VIS_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"); - } + HandleVideoButtons(mx,my, button); + HandleSoundButtons(mx,my, button); + HandleGameButtons(mx,my, button); } -#endif 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); + 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: - HandleGameActions(0); +#ifdef DEBUG + if (button == MB_RELEASED) + { + int sx = (mx - SX) / TILEX; + int sy = (my - SY) / TILEY; + + if (IN_VIS_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 break; + default: break; } @@ -323,125 +351,53 @@ void HandleButton(int mx, int my, int button) void HandleKey(KeySym key, int key_status) { 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; - case XK_x: - { - 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= 4 && nread >= 4 + readbuf[3]) + if (button_status && game_status != PLAYING) { - len = readbuf[3]; - if (readbuf[0] || readbuf[1] || readbuf[2]) - fatal("Wrong server line length"); - - memcpy(buf, &readbuf[4], len); - nread -= 4 + len; - copydown(readbuf, readbuf + 4 + len, nread); - - switch(buf[1]) - { - case OP_YOUARE: - printf("OP_YOUARE: %d\n", buf[0]); - break; - - case OP_NEW: - printf("OP_NEW: %d\n", buf[0]); - sprintf(msgbuf, "new client %d connected", buf[0]); - sysmsg(msgbuf); - break; - - case OP_GONE: - printf("OP_GONE: %d\n", buf[0]); - sprintf(msgbuf, "client %d disconnected", buf[0]); - sysmsg(msgbuf); - break; - - case OP_BADVERS: - { - static char tmpbuf[128]; - - sprintf(tmpbuf, "Protocol version mismatch: server expects %d.%d.x instead of %d.%d.%d\n", buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); - fatal(tmpbuf); - } - break; - - case OP_PLAY: - printf("OP_PLAY: %d\n", buf[0]); - sprintf(msgbuf, "client %d starts game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_PAUSE: - printf("OP_PAUSE: %d\n", buf[0]); - sprintf(msgbuf, "client %d pauses game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_CONT: - printf("OP_CONT: %d\n", buf[0]); - sprintf(msgbuf, "client %d continues game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_WON: - printf("OP_WON: %d\n", buf[0]); - sprintf(msgbuf, "client %d wins the game", buf[0]); - sysmsg(msgbuf); - break; - - case OP_ZERO: - printf("OP_ZERO: %d\n", buf[0]); - sprintf(msgbuf, "client %d resets game counters", buf[0]); - sysmsg(msgbuf); - break; - - case OP_NICK: - printf("OP_NICK: %d\n", buf[0]); - sprintf(msgbuf, "client %d calls itself %s", buf[0], &buf[2]); - sysmsg(msgbuf); - break; - - case OP_MSG: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d sends message", buf[0]); - break; - - case OP_LOST: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d has lost", buf[0]); - break; - - case OP_LEVEL: - printf("OP_MSG: %d\n", buf[0]); - sprintf(msgbuf, "client %d sets level to %d", buf[0], buf[2]); - break; - } + HandleButton(-1,-1, button_status); + return; } - fflush(stdout); -} +#ifndef MSDOS + if (options.network) + HandleNetworking(); +#endif + HandleJoystick(); + if (game_status == PLAYING) + HandleGameActions(); +} -static void HandleNetworking() +static int HandleJoystickForAllPlayers() { - static struct timeval tv = { 0, 0 }; - fd_set rfds; - int r = 0; - - if (standalone) - return; - - flushbuf(); - - FD_ZERO(&rfds); - FD_SET(sfd, &rfds); - - r = select(sfd + 1, &rfds, NULL, NULL, &tv); + int i; + int result = 0; - if (r < 0 && errno != EINTR) + for (i=0; i