source: opengl-game/game-gui-glfw.cpp@ c61323a

feature/imgui-sdl points-test
Last change on this file since c61323a was c61323a, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Implement processEvents() and pollEvent() for GameGui_GLFW and re-implement the main loop in opengl-game using those functions

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include "game-gui-glfw.hpp"
2
3#include "compiler.hpp"
4#include "consts.hpp"
5
6queue<UIEvent> GameGui_GLFW::s_events;
7string GameGui_GLFW::s_errorMessage;
8
9string& GameGui_GLFW::getError() {
10 return GameGui_GLFW::s_errorMessage;
11}
12
13bool 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
23void GameGui_GLFW::shutdown() {
24 glfwTerminate();
25}
26
27void* 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 glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
59 glfwSetKeyCallback(window, glfw_key_callback);
60
61 return window;
62}
63
64void GameGui_GLFW::destroyWindow() {
65 // TODO: This function can throw some errors. They should be handled
66 glfwDestroyWindow(window);
67}
68
69void GameGui_GLFW::processEvents() {
70 glfwPollEvents();
71
72 if (glfwWindowShouldClose(window)) {
73 UIEvent e;
74 e.type = UI_EVENT_QUIT;
75
76 s_events.push(e);
77 }
78}
79
80int GameGui_GLFW::pollEvent(UIEvent* event) {
81 if (s_events.empty()) {
82 return 0;
83 }
84
85 *event = s_events.front();
86 s_events.pop();
87
88 return 1;
89}
90
91#ifdef GAMEGUI_INCLUDE_VULKAN
92
93bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
94 return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
95 RTWO_SUCCESS : RTWO_ERROR;
96}
97
98#endif
99
100vector<const char*> GameGui_GLFW::getRequiredExtensions() {
101 uint32_t glfwExtensionCount = 0;
102 const char** glfwExtensions;
103
104 glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
105
106 vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
107
108 return extensions;
109}
110
111void GameGui_GLFW::getWindowSize(int* width, int* height) {
112 // This function segfaults on OSX, check other platforms
113 //glfwGetFramebufferSize(window, width, height);
114
115 *width = windowWidth;
116 *height = windowHeight;
117}
118
119void glfw_error_callback(int error, const char* description) {
120 GameGui_GLFW::s_errorMessage = description;
121}
122
123void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
124 double x, y;
125 glfwGetCursorPos(window, &x, &y);
126
127 /*
128 MouseEvent e { button, action, (int)x, (int)y };
129
130 mouseEvents.push(e);
131 */
132}
133
134void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
135 UIEvent e;
136 e.type = UI_EVENT_KEY;
137 e.key.keycode = key;
138
139 GameGui_GLFW::s_events.push(e);
140}
Note: See TracBrowser for help on using the repository browser.