source: opengl-game/game-gui-glfw.cpp@ 1ce9afe

feature/imgui-sdl points-test
Last change on this file since 1ce9afe was 1ce9afe, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

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

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#include "game-gui-glfw.hpp"
2
3#include "compiler.hpp"
4#include "consts.hpp"
5
6const int KEY_STATE_UNCHANGED = -1;
7
8string GameGui_GLFW::s_errorMessage;
9
10int GameGui_GLFW::s_keyState[NUM_KEYS];
11bool GameGui_GLFW::s_keyDown[NUM_KEYS];
12
13string& GameGui_GLFW::GetError() {
14 return GameGui_GLFW::s_errorMessage;
15}
16
17bool GameGui_GLFW::Init() {
18 GameGui_GLFW::s_errorMessage = "No error";
19 glfwSetErrorCallback(glfw_error_callback);
20
21 return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
22}
23
24void GameGui_GLFW::Shutdown() {
25 glfwTerminate();
26}
27
28void* GameGui_GLFW::CreateWindow(const string& title, unsigned int width, unsigned int height, bool fullscreen) {
29 GLFWwindow* window = nullptr;
30 GLFWmonitor* mon = nullptr;
31
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);
66
67 return window;
68}
69
70void GameGui_GLFW::DestroyWindow() {
71 // TODO: This function can throw some errors. They should be handled
72 glfwDestroyWindow(window);
73}
74
75#ifdef GAMEGUI_INCLUDE_VULKAN
76
77bool GameGui_GLFW::CreateVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
78 return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
79 RTWO_SUCCESS : RTWO_ERROR;
80}
81
82#endif
83
84vector<const char*> GameGui_GLFW::GetRequiredExtensions() {
85 uint32_t glfwExtensionCount = 0;
86 const char** glfwExtensions;
87
88 glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
89
90 vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
91
92 return extensions;
93}
94
95void GameGui_GLFW::GetWindowSize(int* width, int* height) {
96 glfwGetFramebufferSize(window, width, height);
97}
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 TracBrowser for help on using the repository browser.