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