/** Event loop for a win. */ void WinLoop() { struct timeval last, current; unsigned long elapsedTime; XEvent event; int count; if(!GetBonus()) { return; } gettimeofday(&last, NULL); count = 64; while(!shouldExit) { while(XPending(display) > 0) { XNextEvent(display, &event); switch(event.type) { case Expose: HandleExposeEvent(&event.xexpose); break; case KeyPress: if(event.xkey.keycode == keyESC) { shouldExit = 1; } break; default: break; } } gettimeofday(¤t, NULL); elapsedTime = (current.tv_sec - last.tv_sec) * 1000000; elapsedTime += current.tv_usec - last.tv_usec; if(elapsedTime >= COUNTDOWN_SPEED) { last = current; UpdateBonusCountdown(); --count; if(count <= 0) { break; } } } }
void HandleOtherEvents(Event *event) { switch (event->type) { case EVENT_EXPOSE: HandleExposeEvent((ExposeEvent *) event); break; case EVENT_UNMAPNOTIFY: #if 0 /* This causes the game to stop not only when iconified, but also when on another virtual desktop, which might be not desired. */ SleepWhileUnmapped(); #endif break; case EVENT_FOCUSIN: case EVENT_FOCUSOUT: HandleFocusEvent((FocusChangeEvent *) event); break; case EVENT_CLIENTMESSAGE: HandleClientMessageEvent((ClientMessageEvent *) event); break; #if defined(TARGET_SDL) case SDL_JOYAXISMOTION: case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: HandleJoystickEvent(event); break; case SDL_SYSWMEVENT: HandleWindowManagerEvent(event); break; #endif default: break; } }
/** Delay after drawing the game screen. */ void DoStartDelay() { struct timeval last, current; unsigned long elapsedTime; XEvent event; gettimeofday(&last, NULL); while(!shouldExit) { while(XPending(display) > 0) { XNextEvent(display, &event); switch(event.type) { case Expose: HandleExposeEvent(&event.xexpose); break; case KeyPress: if(event.xkey.keycode == keyESC) { shouldExit = 1; } break; default: break; } } gettimeofday(¤t, NULL); elapsedTime = (current.tv_sec - last.tv_sec) * 1000000; elapsedTime += current.tv_usec - last.tv_usec; if(elapsedTime >= START_DELAY) { break; } } moveRight = 0; moveLeft = 0; isPaused = 0; }
Event GUIWindow::GetNextEvent() { // Setup a temporary event and get the next X11 event Event event; XNextEvent(display, &xEvent); // See what type of event we have switch(xEvent.type) { case Expose: HandleExposeEvent(event); break; case KeyPress: HandleKeyPressEvent(event); break; case KeyRelease: HandleKeyReleaseEvent(event); break; case ButtonPress: HandleButtonPressEvent(event); break; case ButtonRelease: HandleButtonReleaseEvent(event); break; case MotionNotify: HandleMotionNotifyEvent(event); break; case ClientMessage: HandleClientMessageEvent(event); break; case ConfigureNotify: { XConfigureEvent xce = xEvent.xconfigure; // Test for resize event if(xce.width != width || xce.height != height) { width = xce.width; height = xce.height; event.eventType = evResize; event.mouseX = width; event.mouseY = height; } } break; default: HandleIgnoreEvent(event); break; } return event; }
/** Game event loop. */ void EventLoop() { struct timeval last, current; unsigned long elapsedTime; int repeat; XEvent event; SetFirstLevel(); XSelectInput(display, mainWindow, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask); shouldExit = 0; DoStartDelay(); gettimeofday(&last, NULL); while(!shouldExit) { if(didWin) { WinLoop(); NextLevel(); moveRight = 0; moveLeft = 0; gettimeofday(&last, NULL); } if(didLose) { Redraw(); DoStartDelay(); break; } while(XPending(display) > 0) { XNextEvent(display, &event); switch(event.type) { case Expose: HandleExposeEvent(&event.xexpose); break; case KeyPress: HandleKeyPressEvent(&event.xkey); break; case KeyRelease: HandleKeyReleaseEvent(&event.xkey); break; case ButtonPress: HandleButtonPressEvent(); break; default: break; } } if(isPaused) { gettimeofday(&last, NULL); continue; } gettimeofday(¤t, NULL); elapsedTime = (current.tv_sec - last.tv_sec) * 1000000; elapsedTime += current.tv_usec - last.tv_usec; if(elapsedTime >= SPEED) { last = current; EraseBall(); repeat = (int)(((float)elapsedTime / SPEED) + 0.5); do { if(moveRight && !moveLeft) { MoveBallRight(); } else if(moveLeft && !moveRight) { MoveBallLeft(); } MoveBall(); --repeat; } while(repeat); DrawBall(); Redraw(); if(didDie) { didDie = 0; DoStartDelay(); gettimeofday(&last, NULL); } } } }