[0e6ecf3] | 1 | #include "game-gui-glfw.hpp"
|
---|
| 2 |
|
---|
[1ce9afe] | 3 | #include "compiler.hpp"
|
---|
[9546928] | 4 | #include "consts.hpp"
|
---|
| 5 |
|
---|
[1ce9afe] | 6 | const int KEY_STATE_UNCHANGED = -1;
|
---|
| 7 |
|
---|
[d8cb15e] | 8 | string GameGui_GLFW::s_errorMessage;
|
---|
| 9 |
|
---|
[1ce9afe] | 10 | int GameGui_GLFW::s_keyState[NUM_KEYS];
|
---|
| 11 | bool GameGui_GLFW::s_keyDown[NUM_KEYS];
|
---|
[d8cb15e] | 12 |
|
---|
[7fc5e27] | 13 | string& GameGui_GLFW::getError() {
|
---|
[d8cb15e] | 14 | return GameGui_GLFW::s_errorMessage;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[7fc5e27] | 17 | bool GameGui_GLFW::init() {
|
---|
[d8cb15e] | 18 | GameGui_GLFW::s_errorMessage = "No error";
|
---|
| 19 | glfwSetErrorCallback(glfw_error_callback);
|
---|
| 20 |
|
---|
[7fc5e27] | 21 | windowWidth = -1;
|
---|
| 22 | windowHeight = -1;
|
---|
| 23 |
|
---|
[d8cb15e] | 24 | return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 25 | }
|
---|
| 26 |
|
---|
[7fc5e27] | 27 | void GameGui_GLFW::shutdown() {
|
---|
[0e6ecf3] | 28 | glfwTerminate();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[7fc5e27] | 31 | void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 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 |
|
---|
[7fc5e27] | 54 | windowWidth = vmode->width;
|
---|
| 55 | windowHeight = vmode->height;
|
---|
| 56 | } else {
|
---|
| 57 | windowWidth = width;
|
---|
| 58 | windowHeight = height;
|
---|
[1ce9afe] | 59 | }
|
---|
[0e6ecf3] | 60 |
|
---|
[7fc5e27] | 61 | window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
|
---|
[1ce9afe] | 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);
|
---|
[0e6ecf3] | 68 |
|
---|
| 69 | return window;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[7fc5e27] | 72 | void GameGui_GLFW::destroyWindow() {
|
---|
[0e6ecf3] | 73 | // TODO: This function can throw some errors. They should be handled
|
---|
| 74 | glfwDestroyWindow(window);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[4eb4d0a] | 77 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 78 |
|
---|
[7fc5e27] | 79 | bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[0e6ecf3] | 80 | return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
|
---|
| 81 | RTWO_SUCCESS : RTWO_ERROR;
|
---|
| 82 | }
|
---|
[8667f76] | 83 |
|
---|
[4eb4d0a] | 84 | #endif
|
---|
| 85 |
|
---|
[7fc5e27] | 86 | vector<const char*> GameGui_GLFW::getRequiredExtensions() {
|
---|
[8667f76] | 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 |
|
---|
[7fc5e27] | 97 | void 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;
|
---|
[8667f76] | 103 | }
|
---|
[1ce9afe] | 104 |
|
---|
| 105 | void glfw_error_callback(int error, const char* description) {
|
---|
| 106 | GameGui_GLFW::s_errorMessage = description;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void 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 | }
|
---|