source: opengl-game/game-gui-glfw.cpp@ 7fc5e27

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

Change all game-gui function names to lower camel case

  • 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 windowWidth = -1;
22 windowHeight = -1;
23
24 return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
25}
26
27void GameGui_GLFW::shutdown() {
28 glfwTerminate();
29}
30
31void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
32 GLFWwindow* window = nullptr;
33 GLFWmonitor* mon = nullptr;
34
35#if defined(GAMEGUI_INCLUDE_VULKAN)
36 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // This is for Vulkan, OpenGL needs different flags
37#elif defined(MAC)
38 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
39 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
40 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
41 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
42#else
43 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
44 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
45#endif
46
47 glfwWindowHint(GLFW_SAMPLES, 16);
48 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
49
50 if (fullscreen) {
51 mon = glfwGetPrimaryMonitor();
52 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
53
54 windowWidth = vmode->width;
55 windowHeight = vmode->height;
56 } else {
57 windowWidth = width;
58 windowHeight = height;
59 }
60
61 window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
62 //glfwMakeContextCurrent(window);
63
64 //glfwSetMouseButtonCallback(window, mouse_button_callback);
65 glfwSetKeyCallback(window, glfw_key_callback);
66
67 fill(GameGui_GLFW::s_keyState, GameGui_GLFW::s_keyState + NUM_KEYS, KEY_STATE_UNCHANGED);
68
69 return window;
70}
71
72void GameGui_GLFW::destroyWindow() {
73 // TODO: This function can throw some errors. They should be handled
74 glfwDestroyWindow(window);
75}
76
77#ifdef GAMEGUI_INCLUDE_VULKAN
78
79bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
80 return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
81 RTWO_SUCCESS : RTWO_ERROR;
82}
83
84#endif
85
86vector<const char*> GameGui_GLFW::getRequiredExtensions() {
87 uint32_t glfwExtensionCount = 0;
88 const char** glfwExtensions;
89
90 glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
91
92 vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
93
94 return extensions;
95}
96
97void GameGui_GLFW::getWindowSize(int* width, int* height) {
98 // This function segfaults on OSX, check other platforms
99 //glfwGetFramebufferSize(window, width, height);
100
101 *width = windowWidth;
102 *height = windowHeight;
103}
104
105void glfw_error_callback(int error, const char* description) {
106 GameGui_GLFW::s_errorMessage = description;
107}
108
109void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
110 GameGui_GLFW::s_keyState[key] = action;
111
112 // should be true for GLFW_PRESS and GLFW_REPEAT
113 GameGui_GLFW::s_keyDown[key] = (action != GLFW_RELEASE);
114}
Note: See TracBrowser for help on using the repository browser.