Changeset d8cf709 in opengl-game


Ignore:
Timestamp:
Feb 13, 2021, 11:41:39 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
c6f0793
Parents:
8b823e7
Message:

Change UIEvent to also include the original event from the UI library the game gui is currently using, such as SDL or GLFW.

Files:
20 edited

Legend:

Unmodified
Added
Removed
  • game-gui-glfw.cpp

    r8b823e7 rd8cf709  
    7979   if (glfwWindowShouldClose(window)) {
    8080      UIEvent e;
    81       e.type = UI_EVENT_QUIT;
     81      e.event.type = UI_EVENT_QUIT;
    8282
    8383      s_events.push(e);
     
    147147void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    148148   UIEvent e;
    149    e.type = action == GLFW_RELEASE ? UI_EVENT_KEYUP : UI_EVENT_KEYDOWN;
    150    e.key.keycode = key;
     149   e.event.type = action == GLFW_RELEASE ? UI_EVENT_KEYUP : UI_EVENT_KEYDOWN;
     150   e.event.key.keycode = key;
    151151
    152152   GameGui_GLFW::s_events.push(e);
     
    155155void glfw_window_size_callback(GLFWwindow* window, int width, int height) {
    156156   UIEvent e;
    157    e.type = UI_EVENT_WINDOWRESIZE;
    158    e.windowResize.width = width;
    159    e.windowResize.height = height;
     157   e.event.type = UI_EVENT_WINDOWRESIZE;
     158   e.event.windowResize.width = width;
     159   e.event.windowResize.height = height;
    160160
    161161   GameGui_GLFW::s_events.push(e);
  • game-gui-sdl.cpp

    r8b823e7 rd8cf709  
    7979}
    8080
    81 int GameGui_SDL::pollEvent(UIEvent* event) {
     81int GameGui_SDL::pollEvent(UIEvent* uiEvent) {
    8282   SDL_Event e;
    8383
     
    8787
    8888   if (SDL_PollEvent(&e)) {
     89      uiEvent->rawEvent.sdl = e;
     90
     91      GameEvent* event = &uiEvent->event;
     92
    8993      switch(e.type) {
    9094         case SDL_QUIT:
     
    9397         case SDL_WINDOWEVENT:
    9498            if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
    95                e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
    96                e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
     99                  e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
     100                  e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
    97101               event->type = UI_EVENT_WINDOWRESIZE;
     102            } else if (e.window.event == SDL_WINDOWEVENT_CLOSE &&
     103                  e.window.windowID == SDL_GetWindowID(window)) {
     104               event->type = UI_EVENT_QUIT;
    98105            } else {
    99106               event->type = UI_EVENT_WINDOW;
     
    122129            event->mouse.y = e.motion.y;
    123130         case SDL_FINGERMOTION:
     131            // TODO: Get coordinates for finger events
    124132            event->type = UI_EVENT_MOUSEMOTION;
    125133            break;
    126          // Ignore the following events
     134         // The following events are not currently supported
    127135         case SDL_AUDIODEVICEADDED:
    128136         case SDL_AUDIODEVICEREMOVED:
    129137         case SDL_TEXTINPUT:
    130138         case SDL_TEXTEDITING:
     139         case SDL_MOUSEWHEEL:
    131140            event->type = UI_EVENT_UNKNOWN;
    132             event->unknown.eventType = e.type;
    133141            break;
    134142         default:
    135143            event->type = UI_EVENT_UNKNOWN;
    136             event->unknown.eventType = 0;
    137144      }
    138145
    139146      return 1;
     147   } else {
     148      return 0;
    140149   }
    141 
    142    event = nullptr;
    143    return 0;
    144150}
    145151
  • game-gui-sdl.hpp

    r8b823e7 rd8cf709  
    2323
    2424      void processEvents();
    25       int pollEvent(UIEvent* event);
     25      int pollEvent(UIEvent* uiEvent);
    2626      bool keyPressed(unsigned int key);
    2727
  • game-gui.hpp

    r8b823e7 rd8cf709  
    88   #include <vulkan/vulkan.h>
    99#endif
     10
     11// These are included here so I can implement the RawEvent union, which requires knowledge
     12// of the ui event types of all ui libraries I might use for any of the GameGui clases
     13// GLFW does not have its own event type though, so SDL is currently the only library this is done for
     14#include <SDL2/SDL.h>
    1015
    1116using namespace std;
     
    2530};
    2631
    27 struct WindowEvent {
    28    EventType type;
     32union RawEvent {
     33   SDL_Event sdl;
    2934};
    3035
     
    4550};
    4651
     52struct WindowEvent {
     53   EventType type;
     54};
     55
    4756struct WindowResizeEvent {
    4857   EventType type;
     
    5362struct UnknownEvent {
    5463   EventType type;
    55    unsigned int eventType;
    5664};
    5765
    5866// TODO: Switch from union to std::variant
    5967
    60 union UIEvent {
     68union GameEvent {
    6169   EventType type;
    6270   WindowEvent window;
     
    6573   WindowResizeEvent windowResize;
    6674   UnknownEvent unknown;
     75};
     76
     77struct UIEvent {
     78   RawEvent rawEvent;
     79   GameEvent event;
    6780};
    6881
     
    8093
    8194      virtual void processEvents() = 0;
    82       virtual int pollEvent(UIEvent* event) = 0;
     95      virtual int pollEvent(UIEvent* uiEvent) = 0;
    8396      virtual bool keyPressed(unsigned int key) = 0;
    8497
  • gui/button.cpp

    r8b823e7 rd8cf709  
    8787}
    8888
    89 void Button::handleEvent(UIEvent& e) {
     89void Button::handleEvent(GameEvent& e) {
    9090   switch(e.type) {
    9191      case UI_EVENT_MOUSEMOTION:
  • gui/button.hpp

    r8b823e7 rd8cf709  
    2121   void init() override;
    2222   void render(int x, int y) override;
    23    void handleEvent(UIEvent& e) override;
     23   void handleEvent(GameEvent& e) override;
    2424
    2525private:
  • gui/game-screen.cpp

    r8b823e7 rd8cf709  
    4949}
    5050
    51 void GameScreen::handleEvent(UIEvent& e) {
     51void GameScreen::handleEvent(GameEvent& e) {
    5252   Screen::handleEvent(e);
    5353}
  • gui/game-screen.hpp

    r8b823e7 rd8cf709  
    1313      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) override;
    1414
    15       void handleEvent(UIEvent& e) override;
     15      void handleEvent(GameEvent& e) override;
    1616};
    1717
  • gui/main-screen.cpp

    r8b823e7 rd8cf709  
    2121}
    2222
    23 void MainScreen::handleEvent(UIEvent& e) {
     23void MainScreen::handleEvent(GameEvent& e) {
    2424   Screen::handleEvent(e);
    2525}
  • gui/main-screen.hpp

    r8b823e7 rd8cf709  
    1313      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) override;
    1414
    15       void handleEvent(UIEvent& e) override;
     15      void handleEvent(GameEvent& e) override;
    1616};
    1717
  • gui/panel.cpp

    r8b823e7 rd8cf709  
    7272}
    7373
    74 void Panel::handleEvent(UIEvent& e) {
     74void Panel::handleEvent(GameEvent& e) {
    7575   for (UIElement*& uiElement : this->uiElements) {
    7676      uiElement->handleEvent(e);
  • gui/panel.hpp

    r8b823e7 rd8cf709  
    1919
    2020   void render(int x, int y) override;
    21    void handleEvent(UIEvent& e) override;
     21   void handleEvent(GameEvent& e) override;
    2222
    2323private:
  • gui/screen.cpp

    r8b823e7 rd8cf709  
    2929}
    3030
    31 void Screen::handleEvent(UIEvent& e) {
     31void Screen::handleEvent(GameEvent& e) {
    3232   for (UIElement*& uiElement : this->uiElements) {
    3333      uiElement->handleEvent(e);
  • gui/screen.hpp

    r8b823e7 rd8cf709  
    1717class VulkanGame;
    1818
    19 template<class Type>
    20 struct ValueReference {
    21    
    22 };
    23 
    2419// TODO: Add a function to create an SDL_Color from a uint32_t
    2520
     
    3429   
    3530   virtual void renderUI();
    36    virtual void handleEvent(UIEvent& e);
     31   virtual void handleEvent(GameEvent& e);
    3732   void addUIElement(UIElement* element);
    3833
  • gui/ui-element.cpp

    r8b823e7 rd8cf709  
    2121}
    2222
    23 void UIElement::handleEvent(UIEvent& e) {
     23void UIElement::handleEvent(GameEvent& e) {
    2424}
  • gui/ui-element.hpp

    r8b823e7 rd8cf709  
    1818   virtual void init();
    1919   virtual void render(int x, int y) = 0;
    20    virtual void handleEvent(UIEvent& e);
     20   virtual void handleEvent(GameEvent& e);
    2121
    2222protected:
  • main-vulkan.cpp

    r8b823e7 rd8cf709  
    44#include "crash-logger.hpp"
    55
    6 //#include "vulkan-game.hpp"
    7 #include "sdl-game.hpp"
     6#include "vulkan-game.hpp"
     7//#include "sdl-game.hpp"
    88
    99using namespace std;
     
    2424
    2525   try {
    26       //game.run(800, 600, 0);
     26      game.run(800, 600, 0);
    2727      //game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
    28       game.run(1280, 720, 0);
    2928   } catch (const exception& e) {
    3029      cerr << e.what() << endl;
  • opengl-game.cpp

    r8b823e7 rd8cf709  
    160160
    161161void OpenGLGame::mainLoop() {
    162    UIEvent e;
     162   UIEvent uiEvent;
    163163   bool quit = false;
    164164
     
    166166      gui->processEvents();
    167167
    168       while (gui->pollEvent(&e)) {
     168      while (gui->pollEvent(&uiEvent)) {
     169         GameEvent& e = uiEvent.event;
     170
    169171         switch (e.type) {
    170172            case UI_EVENT_QUIT:
  • opengl-game.hpp

    r8b823e7 rd8cf709  
    44#include <glm/glm.hpp>
    55
     6// TODO: Rewrite this to work with the new version of IMGUI
    67#include "IMGUI/imgui.h"
    78#include "imgui_impl_glfw_gl3.h"
  • vulkan-game.cpp

    r8b823e7 rd8cf709  
    771771
    772772void VulkanGame::mainLoop() {
    773    UIEvent e;
    774773   this->quit = false;
    775774
     
    799798      gui->processEvents();
    800799
    801       while (gui->pollEvent(&e)) {
     800      UIEvent uiEvent;
     801      while (gui->pollEvent(&uiEvent)) {
     802         GameEvent& e = uiEvent.event;
     803
    802804         switch(e.type) {
    803805            case UI_EVENT_QUIT:
Note: See TracChangeset for help on using the changeset viewer.