Changeset 1ce9afe in opengl-game


Ignore:
Timestamp:
Sep 2, 2019, 7:40:49 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
7fc5e27
Parents:
301d0d4
Message:

Add a fullscreen flag to GameGui::CreateWindow and implement fullscreen functionality and the ability to detect key presses in openglgame

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • game-gui-glfw.cpp

    r301d0d4 r1ce9afe  
    11#include "game-gui-glfw.hpp"
    22
     3#include "compiler.hpp"
    34#include "consts.hpp"
     5
     6const int KEY_STATE_UNCHANGED = -1;
    47
    58string GameGui_GLFW::s_errorMessage;
    69
    7 void glfw_error_callback(int error, const char* description) {
    8    GameGui_GLFW::s_errorMessage = description;
    9 }
     10int GameGui_GLFW::s_keyState[NUM_KEYS];
     11bool GameGui_GLFW::s_keyDown[NUM_KEYS];
    1012
    1113string& GameGui_GLFW::GetError() {
     
    2426}
    2527
    26 void* GameGui_GLFW::CreateWindow(const string& title, unsigned int width, unsigned int height) {
    27    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
     28void* GameGui_GLFW::CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen) {
     29   GLFWwindow* window = nullptr;
     30   GLFWmonitor* mon = nullptr;
    2831
    29    window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
     32#if defined(GAMEGUI_INCLUDE_VULKAN)
     33   glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // This is for Vulkan, OpenGL needs different flags
     34#elif defined(MAC)
     35   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     36   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     37   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
     38   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     39#else
     40   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
     41   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     42#endif
     43
     44   glfwWindowHint(GLFW_SAMPLES, 16);
     45   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
     46
     47   if (fullscreen) {
     48      mon = glfwGetPrimaryMonitor();
     49      const GLFWvidmode* vmode = glfwGetVideoMode(mon);
     50
     51      width = vmode->width;
     52      height = vmode->height;
     53
     54      // TODO: Should probably enable some way to retrieve this from outside this class
     55      // and print it out there
     56      cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
     57   }
     58
     59   window = glfwCreateWindow(width, height, title.c_str(), mon, nullptr);
     60   //glfwMakeContextCurrent(window);
     61
     62   //glfwSetMouseButtonCallback(window, mouse_button_callback);
     63   glfwSetKeyCallback(window, glfw_key_callback);
     64
     65   fill(GameGui_GLFW::s_keyState, GameGui_GLFW::s_keyState + NUM_KEYS, KEY_STATE_UNCHANGED);
    3066
    3167   return window;
     
    6096   glfwGetFramebufferSize(window, width, height);
    6197}
     98
     99void glfw_error_callback(int error, const char* description) {
     100   GameGui_GLFW::s_errorMessage = description;
     101}
     102
     103void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
     104   GameGui_GLFW::s_keyState[key] = action;
     105
     106   // should be true for GLFW_PRESS and GLFW_REPEAT
     107   GameGui_GLFW::s_keyDown[key] = (action != GLFW_RELEASE);
     108}
  • game-gui-glfw.hpp

    r301d0d4 r1ce9afe  
    1010#include <GLFW/glfw3.h>
    1111
     12#define NUM_KEYS (512)
     13
    1214class GameGui_GLFW : public GameGui {
    1315   public:
     16      static string s_errorMessage; // Has to be public so that glfw_error_callback can access it
     17
     18      // Both have to be public so that glfw_key_callback can access them
     19      // TODO: Implement a more generic public api over this to get the key state
     20      static int s_keyState[NUM_KEYS];
     21      static bool s_keyDown[NUM_KEYS];
     22
    1423      string& GetError();
    15 
    16       static string s_errorMessage; // Has to be public so that glfw_error_callback can access it
    1724
    1825      bool Init();
    1926      void Shutdown();
    2027
    21       void* CreateWindow(const string& title, unsigned int width, unsigned int height);
     28      void* CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen);
    2229      void DestroyWindow();
    2330
     
    3441
    3542void glfw_error_callback(int error, const char* description);
     43void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
    3644
    3745#endif // _GAME_GUI_GLFW_H
  • game-gui-sdl.cpp

    r301d0d4 r1ce9afe  
    3737}
    3838
    39 void* GameGui_SDL::CreateWindow(const string& title, unsigned int width, unsigned int height) {
     39void* GameGui_SDL::CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen) {
     40   cout << "About to go fullscreen in SDL..." << endl;
     41
     42   // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
     43
    4044   // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
    4145   // otherwise you will not receive a High DPI OpenGL canvas.
  • game-gui-sdl.hpp

    r301d0d4 r1ce9afe  
    1616      void Shutdown();
    1717
    18       void* CreateWindow(const string& title, unsigned int width, unsigned int height);
     18      void* CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen);
    1919      void DestroyWindow();
    2020
  • game-gui.hpp

    r301d0d4 r1ce9afe  
    88   #include <vulkan/vulkan.h>
    99#endif
     10
     11// TODO: Remove the line below once the couts in the game-gui-* files are moved
     12#include <iostream>
    1013
    1114using namespace std;
     
    2023      virtual void Shutdown() = 0;
    2124
    22       virtual void* CreateWindow(const string& title, unsigned int width, unsigned int height) = 0;
     25      virtual void* CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen) = 0;
    2326      virtual void DestroyWindow() = 0;
    2427
  • opengl-game.cpp

    r301d0d4 r1ce9afe  
    3636   cout << "GUI init succeeded" << endl;
    3737
    38    window = (GLFWwindow*) gui->CreateWindow("OpenGL Game", width, height);
     38   window = (GLFWwindow*) gui->CreateWindow("OpenGL Game", width, height, guiFlags | GUI_FLAGS_WINDOW_FULLSCREEN);
    3939   if (window == nullptr) {
    4040      cout << "Window could not be created!" << endl;
     
    5252      glfwPollEvents();
    5353
     54      if (GameGui_GLFW::s_keyState[GLFW_KEY_ESCAPE] == GLFW_PRESS) {
     55         glfwSetWindowShouldClose(window, 1);
     56      }
     57
    5458      glfwSwapBuffers(window);
    5559   }
  • vulkan-game.cpp

    r301d0d4 r1ce9afe  
    3737   cout << "GUI init succeeded" << endl;
    3838
    39    window = (SDL_Window*) gui->CreateWindow("Vulkan Game", width, height);
     39   window = (SDL_Window*) gui->CreateWindow("Vulkan Game", width, height, guiFlags | GUI_FLAGS_WINDOW_FULLSCREEN);
    4040   if (window == nullptr) {
    4141      cout << "Window could not be created!" << endl;
Note: See TracChangeset for help on using the changeset viewer.