Ejemplo n.º 1
0
/** Handle a key press event. */
void HandleKeyPressEvent(XKeyEvent *event) {
   if(event->keycode == keyRight || event->keycode == keyL) {
      moveRight = 1;
   } else if(event->keycode == keyLeft || event->keycode == keyJ) {
      moveLeft = 1;
   } else if(event->keycode == keyESC) {
      shouldExit = 1;
   } else if(event->keycode == keySpace) {
      if(isPaused) {
         isPaused = 0;
      } else {
         isPaused = 1;
      }
      SetPaused(isPaused);
   } else if(event->keycode == keyBackspace) {
      EraseBall();
      UpdateLives(-1);
      SetBallForRestart();
      didDie = 1;
   }
}
Ejemplo n.º 2
0
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
   switch(Message)
   {
      case WM_CREATE:
         hbmBall = LoadBitmap(g_hInst, "BALLBMP");
         hbmMask = LoadBitmap(g_hInst, "MASKBMP");
         if(!hbmBall || !hbmMask){
            MessageBox(hwnd, "Load of resources failed.", "Error",
               MB_OK | MB_ICONEXCLAMATION);
            return -1;
         }

         GetObject(hbmBall, sizeof(bm), &bm);
         SetTimer(hwnd, idTimer1, nTimerDelay, NULL);

         ballX = 0;
         ballY = 0;
         deltaX = deltaValue;
         deltaY = deltaValue;

      break;
      case WM_TIMER:
         if(hbmBall && hbmMask)
         {
            HDC hdcWindow;
            hdcWindow = GetDC(hwnd);

            EraseBall(hdcWindow);
            UpdateBall(hwnd);
            DrawBall(hdcWindow);

            ReleaseDC(hwnd, hdcWindow);
         }
      break;
      case WM_PAINT:
         if(hbmBall && hbmMask)
         {
            PAINTSTRUCT ps;
            HDC hdcWindow;
            hdcWindow = BeginPaint(hwnd, &ps);

            DrawBall(hdcWindow);
            
            EndPaint(hwnd, &ps);
         }
      break;
      case WM_CLOSE:
         DestroyWindow(hwnd);
      break;
      case WM_DESTROY:
         KillTimer(hwnd, idTimer1);
         
         DeleteObject(hbmBall);
         DeleteObject(hbmMask);
         PostQuitMessage(0);
      break;
      default:
         return DefWindowProc(hwnd, Message, wParam, lParam);
   }
   return 0;
}
Ejemplo n.º 3
0
/** 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(&current, 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);
         }

      }

   }
}