SHARE
TWEET

EventHandler

a guest Dec 27th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <SDL2/SDL.h>
  4. #include "event_handler.h"
  5.  
  6. int mouse_x;
  7. int mouse_y;
  8. bool keyState[MAX_KEYS];
  9. unsigned int keyTimeStamp[MAX_KEYS];
  10. SDL_Thread *eventHandlerThread = NULL;
  11.  
  12. int getMouseX()
  13. {
  14.     return mouse_x;
  15. }
  16.  
  17. int getMouseY()
  18. {
  19.     return mouse_y;
  20. }
  21.  
  22. bool isAnyKeyBeingPressed()
  23. {
  24.     return keyState[W] || keyState[A] || keyState[S] || keyState[D] || keyState[UP] || keyState[LEFT] || keyState[RIGHT] || keyState[DOWN] || keyState[ESC] || keyState[RETURN1] || keyState[RETURN2];
  25. }
  26.  
  27. void eventHandler()
  28. {
  29.     const Uint8 *keys = SDL_GetKeyboardState(NULL);
  30.  
  31.  
  32.     unsigned int timeStamp;
  33.     for (int i = 0; i < MAX_KEYS; i++)
  34.     {
  35.         keyState[i] = RELEASED;
  36.         keyTimeStamp[i] = 0;
  37.     }
  38.  
  39.  
  40.     while (true)
  41.     {
  42.  
  43.         timeStamp = SDL_GetTicks();
  44.         SDL_PumpEvents();
  45.         SDL_Event event;
  46.  
  47.         while(SDL_PollEvent(&event))
  48.         {
  49.             if (event.type == SDL_QUIT)
  50.             {
  51.                 SDL_Quit();
  52.                 return;
  53.             }
  54.             if (event.type == SDL_MOUSEBUTTONDOWN)
  55.             {
  56.                 if (event.button.button = SDL_BUTTON_LEFT)
  57.                 {
  58.                     printf("Left Mouse Pressed\n");
  59.                 }
  60.             }
  61.             if (event.type == SDL_MOUSEBUTTONUP)
  62.             {
  63.                 if (event.button.button = SDL_BUTTON_LEFT)
  64.                 {
  65.                     printf("Left Mouse Released\n");
  66.                 }
  67.             }
  68.         }
  69.  
  70.         // Get current mouse position
  71.         SDL_GetMouseState(&mouse_x, &mouse_y);
  72.  
  73.         if (keys[W] == PRESSED) {
  74.             keyTimeStamp[W] =  timeStamp;
  75.             if (keyState[W] == RELEASED) {
  76.                 keyState[W] = PRESSED;
  77.                 printf("W was pressed\n");
  78.             }
  79.         }
  80.         if (keys[A]  == PRESSED) {
  81.             keyTimeStamp[A] =  timeStamp;
  82.             if (keyState[A] == RELEASED) {
  83.                 keyState[A] = PRESSED;
  84.                 printf("A was pressed\n");
  85.             }
  86.         }
  87.         if (keys[S] == PRESSED) {
  88.             keyTimeStamp[S] =  timeStamp;
  89.             if (keyState[S] == RELEASED) {
  90.                 keyState[S] = PRESSED;
  91.                 printf("S was pressed\n");
  92.             }
  93.         }
  94.         if (keys[D] == PRESSED) {
  95.             keyTimeStamp[D] =  timeStamp;
  96.             if (keyState[D] == RELEASED) {
  97.                 keyState[D] = PRESSED;
  98.                 printf("D was pressed\n");
  99.             }
  100.         }
  101.         if (keys[UP] == PRESSED)
  102.         {
  103.             keyTimeStamp[UP] =  timeStamp;
  104.             if (keyState[UP] == RELEASED) {
  105.                 keyState[UP] = PRESSED;
  106.                 printf("UP was pressed\n");
  107.             }
  108.         }
  109.         if (keys[LEFT] == PRESSED)
  110.         {
  111.             keyTimeStamp[LEFT] =  timeStamp;
  112.             if (keyState[LEFT] == RELEASED) {
  113.                 keyState[LEFT] = PRESSED;
  114.                 printf("LEFT was pressed\n");
  115.             }
  116.         }
  117.         if (keys[DOWN] == PRESSED)
  118.         {
  119.             keyTimeStamp[DOWN] =  timeStamp;
  120.             if (keyState[DOWN] == RELEASED) {
  121.                 keyState[DOWN] = PRESSED;
  122.                 printf("DOWN was pressed\n");
  123.             }
  124.         }
  125.         if (keys[RIGHT] == PRESSED)
  126.         {
  127.             keyTimeStamp[RIGHT] =  timeStamp;
  128.             if (keyState[RIGHT] == RELEASED) {
  129.                 keyState[RIGHT] = PRESSED;
  130.                 printf("RIGHT was pressed\n");
  131.             }
  132.         }
  133.         if (keys[ESC] == PRESSED)
  134.         {
  135.             keyTimeStamp[ESC] =  timeStamp;
  136.             if (keyState[ESC] == RELEASED) {
  137.                 keyState[ESC] = PRESSED;
  138.                 printf("ESC was pressed\n");
  139.             }
  140.         }
  141.         if (keys[RETURN1] == PRESSED)
  142.         {
  143.             keyTimeStamp[RETURN1] =  timeStamp;
  144.             if (keyState[RETURN1] == RELEASED) {
  145.                 keyState[RETURN1] = PRESSED;
  146.                 printf("RETURN1 was pressed\n");
  147.             }
  148.         }
  149.         if (keys[RETURN2] == PRESSED)
  150.         {
  151.             keyTimeStamp[RETURN2] =  timeStamp;
  152.             if (keyState[RETURN2] == RELEASED) {
  153.                 keyState[RETURN2] = PRESSED;
  154.                 printf("RETURN2 was pressed\n");
  155.             }
  156.         }
  157.         if (eventHandlerThread == NULL &&  isAnyKeyBeingPressed()) {
  158.             eventHandlerThread = SDL_CreateThread(checkForRelease, "checkForRelease", (void *)NULL);
  159.         }
  160.         SDL_Delay(10);
  161.     }
  162. }
  163.  
  164. void checkForRelease()
  165. {
  166.     unsigned int timeStamp;
  167.  
  168.     // Continues to run until all relevant keys have been released.
  169.  
  170.     while (isAnyKeyBeingPressed())
  171.     {
  172.         timeStamp = SDL_GetTicks() - 100;
  173.         if (timeStamp > keyTimeStamp[W] && keyState[W])
  174.         {
  175.             printf("W was released\n");
  176.             keyState[W] = RELEASED;
  177.         }
  178.         if (timeStamp > keyTimeStamp[A] && keyState[A])
  179.         {
  180.             printf("A was released\n");
  181.             keyState[A] = RELEASED;
  182.         }
  183.         if (timeStamp > keyTimeStamp[S] && keyState[S])
  184.         {
  185.             printf("S was released\n");
  186.             keyState[S] = RELEASED;
  187.         }
  188.         if (timeStamp > keyTimeStamp[D] && keyState[D])
  189.         {
  190.             printf("D was released\n");
  191.             keyState[D] = RELEASED;
  192.         }
  193.         if (timeStamp > keyTimeStamp[ESC] && keyState[ESC])
  194.         {
  195.             printf("ESC was released\n");
  196.             keyState[ESC] = RELEASED;
  197.         }
  198.         if (timeStamp > keyTimeStamp[UP] && keyState[UP])
  199.         {
  200.             printf("UP was released\n");
  201.             keyState[UP] = RELEASED;
  202.         }
  203.         if (timeStamp > keyTimeStamp[LEFT] && keyState[LEFT])
  204.         {
  205.             printf("LEFT was released\n");
  206.             keyState[LEFT] = RELEASED;
  207.         }
  208.         if (timeStamp > keyTimeStamp[DOWN] && keyState[DOWN])
  209.         {
  210.             printf("DOWN was released\n");
  211.             keyState[DOWN] = RELEASED;
  212.         }
  213.         if (timeStamp > keyTimeStamp[RIGHT] && keyState[RIGHT])
  214.         {
  215.             printf("RIGHT was released\n");
  216.             keyState[RIGHT] = RELEASED;
  217.         }
  218.         if (timeStamp > keyTimeStamp[RETURN1] && keyState[RETURN1])
  219.         {
  220.             printf("RETURN1 was released\n");
  221.             keyState[RETURN1] = RELEASED;
  222.         }
  223.         if (timeStamp > keyTimeStamp[RETURN2] && keyState[RETURN2])
  224.         {
  225.             printf("RETURN2 was released\n");
  226.             keyState[RETURN2] = RELEASED;
  227.         }
  228.         SDL_Delay(5);
  229.     }
  230.     eventHandlerThread = NULL;
  231. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top