Changeset c61323a in opengl-game


Ignore:
Timestamp:
Sep 13, 2019, 2:51:30 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
1fcca9e
Parents:
f6521fb
Message:

Implement processEvents() and pollEvent() for GameGui_GLFW and re-implement the main loop in opengl-game using those functions

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • consts.hpp

    rf6521fb rc61323a  
    1212constexpr unsigned char GUI_FLAGS_WINDOW_FULLSCREEN { 1 << 0 };
    1313
    14 /*
    15 constexpr unsigned char RTWO_KEY_EVENT_NONE { 0 };
    16 constexpr unsigned char RTWO_KEY_EVENT_PRESSED { 1 };
    17 constexpr unsigned char RTWO_KEY_EVENT_RELEASED { 2 };
    18 */
    19 
    2014#endif // _RTWO_CONSTS_H
  • game-gui-glfw.cpp

    rf6521fb rc61323a  
    11#include "game-gui-glfw.hpp"
    2 
    3 #include <queue>
    42
    53#include "compiler.hpp"
    64#include "consts.hpp"
    75
    8 int GameGui_GLFW::s_keyState[GLFW_KEY_LAST];
    9 bool GameGui_GLFW::s_keyDown[GLFW_KEY_LAST];
    10 
    11 // queue<MouseEvent> mouseEvents;
    12 
     6queue<UIEvent> GameGui_GLFW::s_events;
    137string GameGui_GLFW::s_errorMessage;
    148
     
    3226
    3327void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
    34    GLFWwindow* window = nullptr;
    3528   GLFWmonitor* mon = nullptr;
    3629
     
    6255
    6356   window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
    64    //glfwMakeContextCurrent(window);
    6557
    6658   glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
    6759   glfwSetKeyCallback(window, glfw_key_callback);
    68 
    69    // fill(s_keyState, s_keyState + GLFW_KEY_LAST, RTWO_KEY_EVENT_NONE);
    70    // fill(s_keyDown, s_keyDown + GLFW_KEY_LAST, false);
    7160
    7261   return window;
     
    7867}
    7968
    80 /*
    8169void GameGui_GLFW::processEvents() {
    82    fill(s_keyState, s_keyState + GLFW_KEY_LAST, RTWO_KEY_EVENT_NONE);
     70   glfwPollEvents();
    8371
    84    glfwPollEvents();
     72   if (glfwWindowShouldClose(window)) {
     73      UIEvent e;
     74      e.type = UI_EVENT_QUIT;
     75
     76      s_events.push(e);
     77   }
    8578}
    8679
    87 unsigned char GameGui_GLFW::getKeyEvent(unsigned int key) {
    88    return s_keyState[key];
    89 }
    90 
    91 bool GameGui_GLFW::isKeyPressed(unsigned int key) {
    92    return s_keyDown[key];
    93 }
    94 
    95 int GameGui_GLFW::pollMouseEvent(MouseEvent* event) {
    96    if (mouseEvents.empty()) {
     80int GameGui_GLFW::pollEvent(UIEvent* event) {
     81   if (s_events.empty()) {
    9782      return 0;
    9883   }
    9984
    100    *event = mouseEvents.front();
    101    mouseEvents.pop();
     85   *event = s_events.front();
     86   s_events.pop();
    10287
    10388   return 1;
    10489}
    105 */
    10690
    10791#ifdef GAMEGUI_INCLUDE_VULKAN
     
    149133
    150134void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    151    GameGui_GLFW::s_keyState[key] = action;
    152    /*
    153    switch(action) {
    154       case GLFW_PRESS:
    155          s_keyState[key] = RTWO_KEY_EVENT_PRESSED;
    156          break;
    157       case GLFW_RELEASE:
    158          s_keyState[key] = RTWO_KEY_EVENT_RELEASED;
    159          break;
    160       default:
    161          s_keyState[key] = RTWO_KEY_EVENT_NONE;
    162          break;
    163    }
     135   UIEvent e;
     136   e.type = UI_EVENT_KEY;
     137   e.key.keycode = key;
    164138
    165    // should be true for GLFW_PRESS and GLFW_REPEAT
    166    GameGui_GLFW::s_keyDown[key] = (action != GLFW_RELEASE);
    167    */
     139   GameGui_GLFW::s_events.push(e);
    168140}
  • game-gui-glfw.hpp

    rf6521fb rc61323a  
    11#ifndef _GAME_GUI_GLFW_H
    22#define _GAME_GUI_GLFW_H
     3
     4#include <queue>
    35
    46#include "game-gui.hpp"
     
    1416      static string s_errorMessage; // Has to be public so that glfw_error_callback can access it
    1517
    16       // Both have to be public so that glfw_key_callback can access them
    17       // TODO: Implement a more generic public api over this to get the key state
    18       static int s_keyState[GLFW_KEY_LAST];
    19       static bool s_keyDown[GLFW_KEY_LAST];
     18      // Has to be public so that glfw_key_callback can access it
     19      static queue<UIEvent> s_events;
    2020
    2121      string& getError();
     
    2727      void destroyWindow();
    2828
    29       /*
    3029      void processEvents();
    31 
    32       unsigned char getKeyEvent(unsigned int key);
    33       bool isKeyPressed(unsigned int key);
    34 
    35       int pollMouseEvent(MouseEvent* event);
    36       */
     30      int pollEvent(UIEvent* event);
    3731
    3832#ifdef GAMEGUI_INCLUDE_VULKAN
     
    4741
    4842      int windowWidth, windowHeight;
    49       static queue<UIEvent> s_events;
    5043};
    5144
  • game-gui-sdl.cpp

    rf6521fb rc61323a  
    77
    88using namespace std;
    9 
    10 /*
    11 // Temporary to allow the program using this class to receive events other than keyboard events
    12 // Remove once I add a better game-gui wrapper for doing that
    13 queue<SDL_Event> events;
    14 
    15 queue<MouseEvent> mouseEvents;
    16 */
    179
    1810string GameGui_SDL::s_errorMessage;
     
    116108         case SDL_AUDIODEVICEADDED:
    117109         case SDL_AUDIODEVICEREMOVED:
     110         case SDL_TEXTEDITING: // TODO: Research this one later
    118111            event = nullptr;
    119112            return 0;
  • game-gui-sdl.hpp

    rf6521fb rc61323a  
    2222      int pollEvent(UIEvent* event);
    2323
    24       // temporary
    25       //int pollEvent(SDL_Event* event);
    26 
    27       /*
    28       void processEvents();
    29 
    30       unsigned char getKeyEvent(unsigned int key);
    31       bool isKeyPressed(unsigned int key);
    32 
    33       int pollMouseEvent(MouseEvent* event);
    34       */
    35 
    3624#ifdef GAMEGUI_INCLUDE_VULKAN
    3725      bool createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface);
  • opengl-game.cpp

    rf6521fb rc61323a  
    5757
    5858void OpenGLGame::mainLoop() {
    59    //MouseEvent e;
     59   UIEvent e;
     60   bool quit = false;
    6061
    61    while (!glfwWindowShouldClose(window)) {
    62       /*
     62   while (!quit) {
    6363      gui->processEvents();
    6464
    65       if (gui->getKeyEvent(GLFW_KEY_ESCAPE) == RTWO_KEY_EVENT_PRESSED) {
    66          glfwSetWindowShouldClose(window, 1);
    67       }
    68 
    69       while (gui->pollMouseEvent(&e)) {
    70          cout << "Mouse click detected at (" << e.x << ", " << e.y << ")" << endl;
    71       }
    72       */
    73 
    74       glfwPollEvents();
    75 
    76       if (GameGui_GLFW::s_keyState[GLFW_KEY_ESCAPE] == GLFW_PRESS) {
    77          glfwSetWindowShouldClose(window, 1);
     65      while (gui->pollEvent(&e)) {
     66         switch (e.type) {
     67            case UI_EVENT_QUIT:
     68               cout << "Quit event detected" << endl;
     69               quit = true;
     70               break;
     71            case UI_EVENT_KEY:
     72               if (e.key.keycode == GLFW_KEY_ESCAPE) {
     73                  quit = true;
     74               } else {
     75                  cout << "Key event detected" << endl;
     76               }
     77               break;
     78            default:
     79               cout << "Unhandled UI event: " << e.type << endl;
     80         }
    7881      }
    7982
  • vulkan-game.cpp

    rf6521fb rc61323a  
    9292            case UI_EVENT_MOUSEMOTION:
    9393               break;
     94            default:
     95               cout << "Unhandled UI event: " << e.type << endl;
    9496         }
    9597      }
Note: See TracChangeset for help on using the changeset viewer.