[0e6ecf3] | 1 | #include "game-gui-glfw.hpp"
|
---|
| 2 |
|
---|
[1ce9afe] | 3 | #include "compiler.hpp"
|
---|
[9546928] | 4 | #include "consts.hpp"
|
---|
| 5 |
|
---|
[c61323a] | 6 | queue<UIEvent> GameGui_GLFW::s_events;
|
---|
[7bf5433] | 7 | string GameGui_GLFW::s_errorMessage;
|
---|
[d8cb15e] | 8 |
|
---|
[7fc5e27] | 9 | string& GameGui_GLFW::getError() {
|
---|
[d8cb15e] | 10 | return GameGui_GLFW::s_errorMessage;
|
---|
| 11 | }
|
---|
| 12 |
|
---|
[7fc5e27] | 13 | bool GameGui_GLFW::init() {
|
---|
[d8cb15e] | 14 | GameGui_GLFW::s_errorMessage = "No error";
|
---|
| 15 | glfwSetErrorCallback(glfw_error_callback);
|
---|
| 16 |
|
---|
[7fc5e27] | 17 | windowWidth = -1;
|
---|
| 18 | windowHeight = -1;
|
---|
| 19 |
|
---|
[d8cb15e] | 20 | return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 21 | }
|
---|
| 22 |
|
---|
[7fc5e27] | 23 | void GameGui_GLFW::shutdown() {
|
---|
[0e6ecf3] | 24 | glfwTerminate();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[7fc5e27] | 27 | void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 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 |
|
---|
[7fc5e27] | 49 | windowWidth = vmode->width;
|
---|
| 50 | windowHeight = vmode->height;
|
---|
| 51 | } else {
|
---|
| 52 | windowWidth = width;
|
---|
| 53 | windowHeight = height;
|
---|
[1ce9afe] | 54 | }
|
---|
[0e6ecf3] | 55 |
|
---|
[7fc5e27] | 56 | window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
|
---|
[1ce9afe] | 57 |
|
---|
[a6f6833] | 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 |
|
---|
[f133da0] | 62 | return window;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void GameGui_GLFW::bindEventHandlers() {
|
---|
[7bf5433] | 66 | glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
|
---|
[1ce9afe] | 67 | glfwSetKeyCallback(window, glfw_key_callback);
|
---|
[a0da009] | 68 | glfwSetWindowSizeCallback(window, glfw_window_size_callback);
|
---|
[0e6ecf3] | 69 | }
|
---|
| 70 |
|
---|
[7fc5e27] | 71 | void GameGui_GLFW::destroyWindow() {
|
---|
[0e6ecf3] | 72 | // TODO: This function can throw some errors. They should be handled
|
---|
| 73 | glfwDestroyWindow(window);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[7bf5433] | 76 | void GameGui_GLFW::processEvents() {
|
---|
| 77 | glfwPollEvents();
|
---|
| 78 |
|
---|
[c61323a] | 79 | if (glfwWindowShouldClose(window)) {
|
---|
| 80 | UIEvent e;
|
---|
| 81 | e.type = UI_EVENT_QUIT;
|
---|
[7bf5433] | 82 |
|
---|
[c61323a] | 83 | s_events.push(e);
|
---|
| 84 | }
|
---|
[7bf5433] | 85 | }
|
---|
| 86 |
|
---|
[c61323a] | 87 | int GameGui_GLFW::pollEvent(UIEvent* event) {
|
---|
| 88 | if (s_events.empty()) {
|
---|
[7bf5433] | 89 | return 0;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[c61323a] | 92 | *event = s_events.front();
|
---|
| 93 | s_events.pop();
|
---|
[7bf5433] | 94 |
|
---|
| 95 | return 1;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[a6f6833] | 98 | void GameGui_GLFW::refreshWindowSize() {
|
---|
| 99 | // glfwGetFramebufferSize(window, width, height) segfaults on OSX, check other platforms
|
---|
| 100 | // I think glfwGetWindowSize(window, width, height) works fine.
|
---|
| 101 | glfwGetWindowSize(window, &windowWidth, &windowHeight);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int GameGui_GLFW::getWindowWidth() {
|
---|
| 105 | return windowWidth;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | int GameGui_GLFW::getWindowHeight() {
|
---|
| 109 | return windowHeight;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[4eb4d0a] | 112 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 113 |
|
---|
[7fc5e27] | 114 | bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[0e6ecf3] | 115 | return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
|
---|
| 116 | RTWO_SUCCESS : RTWO_ERROR;
|
---|
| 117 | }
|
---|
[8667f76] | 118 |
|
---|
[7fc5e27] | 119 | vector<const char*> GameGui_GLFW::getRequiredExtensions() {
|
---|
[8667f76] | 120 | uint32_t glfwExtensionCount = 0;
|
---|
| 121 | const char** glfwExtensions;
|
---|
| 122 |
|
---|
| 123 | glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
---|
| 124 |
|
---|
| 125 | vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
|
---|
| 126 |
|
---|
| 127 | return extensions;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[a6f6833] | 130 | #endif
|
---|
[1ce9afe] | 131 |
|
---|
| 132 | void glfw_error_callback(int error, const char* description) {
|
---|
| 133 | GameGui_GLFW::s_errorMessage = description;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[7bf5433] | 136 | void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 137 | double x, y;
|
---|
| 138 | glfwGetCursorPos(window, &x, &y);
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | MouseEvent e { button, action, (int)x, (int)y };
|
---|
| 142 |
|
---|
| 143 | mouseEvents.push(e);
|
---|
| 144 | */
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[1ce9afe] | 147 | void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
---|
[c61323a] | 148 | UIEvent e;
|
---|
[1802a41] | 149 | e.type = action == GLFW_RELEASE ? UI_EVENT_KEYUP : UI_EVENT_KEYDOWN;
|
---|
[c61323a] | 150 | e.key.keycode = key;
|
---|
[1ce9afe] | 151 |
|
---|
[c61323a] | 152 | GameGui_GLFW::s_events.push(e);
|
---|
[1ce9afe] | 153 | }
|
---|
[a0da009] | 154 |
|
---|
| 155 | void glfw_window_size_callback(GLFWwindow* window, int width, int height) {
|
---|
| 156 | UIEvent e;
|
---|
| 157 | e.type = UI_EVENT_WINDOWRESIZE;
|
---|
[83b5b4b] | 158 | e.windowResize.width = width;
|
---|
| 159 | e.windowResize.height = height;
|
---|
[a0da009] | 160 |
|
---|
| 161 | GameGui_GLFW::s_events.push(e);
|
---|
| 162 | }
|
---|