1 | #include "game-gui-glfw.hpp"
|
---|
2 |
|
---|
3 | #include <queue>
|
---|
4 |
|
---|
5 | #include "compiler.hpp"
|
---|
6 | #include "consts.hpp"
|
---|
7 |
|
---|
8 | int GameGui_GLFW::s_keyState[GLFW_KEY_LAST];
|
---|
9 | bool GameGui_GLFW::s_keyDown[GLFW_KEY_LAST];
|
---|
10 |
|
---|
11 | // queue<MouseEvent> mouseEvents;
|
---|
12 |
|
---|
13 | string GameGui_GLFW::s_errorMessage;
|
---|
14 |
|
---|
15 | string& GameGui_GLFW::getError() {
|
---|
16 | return GameGui_GLFW::s_errorMessage;
|
---|
17 | }
|
---|
18 |
|
---|
19 | bool GameGui_GLFW::init() {
|
---|
20 | GameGui_GLFW::s_errorMessage = "No error";
|
---|
21 | glfwSetErrorCallback(glfw_error_callback);
|
---|
22 |
|
---|
23 | windowWidth = -1;
|
---|
24 | windowHeight = -1;
|
---|
25 |
|
---|
26 | return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
27 | }
|
---|
28 |
|
---|
29 | void GameGui_GLFW::shutdown() {
|
---|
30 | glfwTerminate();
|
---|
31 | }
|
---|
32 |
|
---|
33 | void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
34 | GLFWwindow* window = nullptr;
|
---|
35 | GLFWmonitor* mon = nullptr;
|
---|
36 |
|
---|
37 | #if defined(GAMEGUI_INCLUDE_VULKAN)
|
---|
38 | glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // This is for Vulkan, OpenGL needs different flags
|
---|
39 | #elif defined(MAC)
|
---|
40 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
41 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
42 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
43 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
44 | #else
|
---|
45 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
---|
46 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | glfwWindowHint(GLFW_SAMPLES, 16);
|
---|
50 | glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
|
---|
51 |
|
---|
52 | if (fullscreen) {
|
---|
53 | mon = glfwGetPrimaryMonitor();
|
---|
54 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
55 |
|
---|
56 | windowWidth = vmode->width;
|
---|
57 | windowHeight = vmode->height;
|
---|
58 | } else {
|
---|
59 | windowWidth = width;
|
---|
60 | windowHeight = height;
|
---|
61 | }
|
---|
62 |
|
---|
63 | window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
|
---|
64 | //glfwMakeContextCurrent(window);
|
---|
65 |
|
---|
66 | glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
|
---|
67 | 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);
|
---|
71 |
|
---|
72 | return window;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void GameGui_GLFW::destroyWindow() {
|
---|
76 | // TODO: This function can throw some errors. They should be handled
|
---|
77 | glfwDestroyWindow(window);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | void GameGui_GLFW::processEvents() {
|
---|
82 | fill(s_keyState, s_keyState + GLFW_KEY_LAST, RTWO_KEY_EVENT_NONE);
|
---|
83 |
|
---|
84 | glfwPollEvents();
|
---|
85 | }
|
---|
86 |
|
---|
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()) {
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | *event = mouseEvents.front();
|
---|
101 | mouseEvents.pop();
|
---|
102 |
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 | */
|
---|
106 |
|
---|
107 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
108 |
|
---|
109 | bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
110 | return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
|
---|
111 | RTWO_SUCCESS : RTWO_ERROR;
|
---|
112 | }
|
---|
113 |
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | vector<const char*> GameGui_GLFW::getRequiredExtensions() {
|
---|
117 | uint32_t glfwExtensionCount = 0;
|
---|
118 | const char** glfwExtensions;
|
---|
119 |
|
---|
120 | glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
---|
121 |
|
---|
122 | vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
|
---|
123 |
|
---|
124 | return extensions;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void GameGui_GLFW::getWindowSize(int* width, int* height) {
|
---|
128 | // This function segfaults on OSX, check other platforms
|
---|
129 | //glfwGetFramebufferSize(window, width, height);
|
---|
130 |
|
---|
131 | *width = windowWidth;
|
---|
132 | *height = windowHeight;
|
---|
133 | }
|
---|
134 |
|
---|
135 | void glfw_error_callback(int error, const char* description) {
|
---|
136 | GameGui_GLFW::s_errorMessage = description;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
140 | double x, y;
|
---|
141 | glfwGetCursorPos(window, &x, &y);
|
---|
142 |
|
---|
143 | /*
|
---|
144 | MouseEvent e { button, action, (int)x, (int)y };
|
---|
145 |
|
---|
146 | mouseEvents.push(e);
|
---|
147 | */
|
---|
148 | }
|
---|
149 |
|
---|
150 | void 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 | }
|
---|
164 |
|
---|
165 | // should be true for GLFW_PRESS and GLFW_REPEAT
|
---|
166 | GameGui_GLFW::s_keyDown[key] = (action != GLFW_RELEASE);
|
---|
167 | */
|
---|
168 | }
|
---|