Changeset f6521fb in opengl-game


Ignore:
Timestamp:
Sep 13, 2019, 1:30:24 AM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
c61323a
Parents:
203ab1b
Message:

Add processEvents() and pollEvent() to GameGui, implement them for GameGui_SDL, and add a union for different event types, similar to the SDL_Event union

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • game-gui-glfw.hpp

    r203ab1b rf6521fb  
    4747
    4848      int windowWidth, windowHeight;
     49      static queue<UIEvent> s_events;
    4950};
    5051
  • game-gui-sdl.cpp

    r203ab1b rf6521fb  
    55
    66#include "consts.hpp"
    7 
    8 map<unsigned int, unsigned char> s_keyState;
    9 map<unsigned int, bool> s_keyDown;
    107
    118using namespace std;
     
    8178}
    8279
    83 /*
    8480void GameGui_SDL::processEvents() {
     81}
     82
     83int GameGui_SDL::pollEvent(UIEvent* event) {
    8584   SDL_Event e;
    8685
    87    s_keyState.clear();
    88    while (SDL_PollEvent(&e)) {
    89       if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
    90          if (e.type == SDL_KEYDOWN && !e.key.repeat) {
    91             s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_PRESSED;
    92          } else if (e.type == SDL_KEYUP) {
    93             s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_RELEASED;
    94          } else {
    95             s_keyState.erase(e.key.keysym.sym);
    96          }
     86   /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
     87    * as mouse events since this game isn't targeting mobile devices
     88    */
    9789
    98          s_keyDown[e.key.keysym.sym] = e.type == SDL_KEYDOWN;
    99       } else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
    100          MouseEvent mouseEvent { 0, 0, e.button.x, e.button.y };
     90   if (SDL_PollEvent(&e)) {
     91      switch(e.type) {
     92         case SDL_QUIT:
     93            event->type = UI_EVENT_QUIT;
     94            break;
     95         case SDL_WINDOWEVENT:
     96            event->type = UI_EVENT_WINDOW;
     97            break;
     98         case SDL_KEYUP:
     99         case SDL_KEYDOWN:
     100            event->type = UI_EVENT_KEY;
     101            event->key.keycode = e.key.keysym.scancode;
     102            break;
     103         case SDL_MOUSEBUTTONDOWN:
     104         case SDL_FINGERDOWN:
     105            event->type = UI_EVENT_MOUSEBUTTONDOWN;
     106            break;
     107         case SDL_MOUSEBUTTONUP:
     108         case SDL_FINGERUP:
     109            event->type = UI_EVENT_MOUSEBUTTONUP;
     110            break;
     111         case SDL_MOUSEMOTION:
     112         case SDL_FINGERMOTION:
     113            event->type = UI_EVENT_MOUSEMOTION;
     114            break;
     115         // Ignore the following events
     116         case SDL_AUDIODEVICEADDED:
     117         case SDL_AUDIODEVICEREMOVED:
     118            event = nullptr;
     119            return 0;
     120            break;
     121         default:
     122            cout << "Unknown event type: 0x" << hex << e.type << dec << endl;
     123            event = nullptr;
     124            return 0;
     125      }
    101126
    102          mouseEvents.push(mouseEvent);
    103       } else {
    104          events.push(e);
    105       }
    106    }
    107 }
    108 
    109 int GameGui_SDL::pollMouseEvent(MouseEvent* event) {
    110    if (mouseEvents.empty()) {
    111       return 0;
     127      return 1;
    112128   }
    113129
    114    *event = mouseEvents.front();
    115    mouseEvents.pop();
    116 
    117    return 1;
     130   event = nullptr;
     131   return 0;
    118132}
    119 
    120 unsigned char GameGui_SDL::getKeyEvent(unsigned int key) {
    121    if (s_keyDown.count(key)) {
    122       return s_keyState[key];
    123    } else {
    124       return RTWO_KEY_EVENT_NONE;
    125    }
    126 }
    127 
    128 bool GameGui_SDL::isKeyPressed(unsigned int key) {
    129    if (s_keyDown.count(key)) {
    130       return s_keyDown[key];
    131    } else {
    132       return false;
    133    }
    134 }
    135 
    136 int GameGui_SDL::pollEvent(SDL_Event* event) {
    137    if (events.empty()) {
    138       return 0;
    139    }
    140 
    141    *event = events.front();
    142    events.pop();
    143 
    144    return 1;
    145 }
    146 */
    147133
    148134#ifdef GAMEGUI_INCLUDE_VULKAN
  • game-gui-sdl.hpp

    r203ab1b rf6521fb  
    1818      void* createWindow(const string& title, int width, int height, bool fullscreen);
    1919      void destroyWindow();
     20
     21      void processEvents();
     22      int pollEvent(UIEvent* event);
    2023
    2124      // temporary
  • game-gui.hpp

    r203ab1b rf6521fb  
    55#include <vector>
    66
     7// TODO: Remove the line below once the couts in the game-gui-* files are moved
     8#include <iostream>
     9
    710#ifdef GAMEGUI_INCLUDE_VULKAN
    811   #include <vulkan/vulkan.h>
    912#endif
    1013
    11 // TODO: Remove the line below once the couts in the game-gui-* files are moved
    12 #include <iostream>
     14using namespace std;
    1315
    14 using namespace std;
     16enum EventType {
     17   UI_EVENT_QUIT,
     18   UI_EVENT_WINDOW,
     19   UI_EVENT_KEY,
     20   UI_EVENT_MOUSEBUTTONDOWN,
     21   UI_EVENT_MOUSEBUTTONUP,
     22   UI_EVENT_MOUSEMOTION
     23};
     24
     25struct WindowEvent {
     26   EventType type;
     27};
     28
     29struct KeyEvent {
     30   EventType type;
     31   unsigned int keycode;
     32};
     33
     34struct MouseEvent {
     35   EventType type;
     36};
     37
     38union UIEvent {
     39   EventType type;
     40   WindowEvent window;
     41   KeyEvent key;
     42   MouseEvent mouse;
     43};
    1544
    1645/*
     
    3564      virtual void destroyWindow() = 0;
    3665
     66      virtual void processEvents() = 0;
     67      virtual int pollEvent(UIEvent* event) = 0;
     68
    3769      /*
    3870      virtual void processEvents() = 0;
  • vulkan-game.cpp

    r203ab1b rf6521fb  
    6161
    6262void VulkanGame::mainLoop() {
    63    SDL_Event e;
    64    //MouseEvent mouseEvent;
     63   UIEvent e;
    6564   bool quit = false;
    6665
    6766   while (!quit) {
    68       /*
    6967      gui->processEvents();
    7068
    71       if (gui->getKeyEvent(SDLK_ESCAPE) == RTWO_KEY_EVENT_PRESSED) {
    72          quit = true;
    73       }
    74 
    75       while (gui->pollMouseEvent(&mouseEvent)) {
    76          cout << "Mouse click detected at (" << mouseEvent.x << ", " << mouseEvent.y << ")" << endl;
    77       }
    78       */
    79 
    80       while (SDL_PollEvent(&e)) {
    81          if (e.type == SDL_QUIT) {
    82             quit = true;
    83          }
    84          if (e.type == SDL_KEYDOWN) {
    85             quit = true;
    86          }
    87          if (e.type == SDL_MOUSEBUTTONDOWN) {
    88             quit = true;
     69      while (gui->pollEvent(&e)) {
     70         switch(e.type) {
     71            case UI_EVENT_QUIT:
     72               cout << "Quit event detected" << endl;
     73               quit = true;
     74               break;
     75            case UI_EVENT_WINDOW:
     76               cout << "Window event detected" << endl;
     77               // Currently unused
     78               break;
     79            case UI_EVENT_KEY:
     80               if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
     81                  quit = true;
     82               } else {
     83                  cout << "Key event detected" << endl;
     84               }
     85               break;
     86            case UI_EVENT_MOUSEBUTTONDOWN:
     87               cout << "Mouse button down event detected" << endl;
     88               break;
     89            case UI_EVENT_MOUSEBUTTONUP:
     90               cout << "Mouse button up event detected" << endl;
     91               break;
     92            case UI_EVENT_MOUSEMOTION:
     93               break;
    8994         }
    9095      }
Note: See TracChangeset for help on using the changeset viewer.