Changeset 1ce9afe in opengl-game for game-gui-glfw.cpp


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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.