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

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

Create a new OpenGLGame project for a refactor of the original OpenGL game to make copying its logic over to the Vulkan implementation easier

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[0e6ecf3]1#include "game-gui-glfw.hpp"
2
[7bf5433]3#include <queue>
4
[1ce9afe]5#include "compiler.hpp"
[9546928]6#include "consts.hpp"
7
[7bf5433]8int GameGui_GLFW::s_keyState[GLFW_KEY_LAST];
9bool GameGui_GLFW::s_keyDown[GLFW_KEY_LAST];
[1ce9afe]10
[7bf5433]11// queue<MouseEvent> mouseEvents;
[d8cb15e]12
[7bf5433]13string GameGui_GLFW::s_errorMessage;
[d8cb15e]14
[7fc5e27]15string& GameGui_GLFW::getError() {
[d8cb15e]16 return GameGui_GLFW::s_errorMessage;
17}
18
[7fc5e27]19bool GameGui_GLFW::init() {
[d8cb15e]20 GameGui_GLFW::s_errorMessage = "No error";
21 glfwSetErrorCallback(glfw_error_callback);
22
[7fc5e27]23 windowWidth = -1;
24 windowHeight = -1;
25
[d8cb15e]26 return glfwInit() == GLFW_TRUE ? RTWO_SUCCESS : RTWO_ERROR;
[0e6ecf3]27}
28
[7fc5e27]29void GameGui_GLFW::shutdown() {
[0e6ecf3]30 glfwTerminate();
31}
32
[7fc5e27]33void* GameGui_GLFW::createWindow(const string& title, int width, int height, bool fullscreen) {
[1ce9afe]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
[7fc5e27]56 windowWidth = vmode->width;
57 windowHeight = vmode->height;
58 } else {
59 windowWidth = width;
60 windowHeight = height;
[1ce9afe]61 }
[0e6ecf3]62
[7fc5e27]63 window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), mon, nullptr);
[1ce9afe]64 //glfwMakeContextCurrent(window);
65
[7bf5433]66 glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
[1ce9afe]67 glfwSetKeyCallback(window, glfw_key_callback);
68
[7bf5433]69 // fill(s_keyState, s_keyState + GLFW_KEY_LAST, RTWO_KEY_EVENT_NONE);
70 // fill(s_keyDown, s_keyDown + GLFW_KEY_LAST, false);
[0e6ecf3]71
72 return window;
73}
74
[7fc5e27]75void GameGui_GLFW::destroyWindow() {
[0e6ecf3]76 // TODO: This function can throw some errors. They should be handled
77 glfwDestroyWindow(window);
78}
79
[7bf5433]80/*
81void GameGui_GLFW::processEvents() {
82 fill(s_keyState, s_keyState + GLFW_KEY_LAST, RTWO_KEY_EVENT_NONE);
83
84 glfwPollEvents();
85}
86
87unsigned char GameGui_GLFW::getKeyEvent(unsigned int key) {
88 return s_keyState[key];
89}
90
91bool GameGui_GLFW::isKeyPressed(unsigned int key) {
92 return s_keyDown[key];
93}
94
95int 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
[4eb4d0a]107#ifdef GAMEGUI_INCLUDE_VULKAN
108
[7fc5e27]109bool GameGui_GLFW::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
[0e6ecf3]110 return glfwCreateWindowSurface(instance, window, nullptr, surface) == VK_SUCCESS ?
111 RTWO_SUCCESS : RTWO_ERROR;
112}
[8667f76]113
[4eb4d0a]114#endif
115
[7fc5e27]116vector<const char*> GameGui_GLFW::getRequiredExtensions() {
[8667f76]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
[7fc5e27]127void 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;
[8667f76]133}
[1ce9afe]134
135void glfw_error_callback(int error, const char* description) {
136 GameGui_GLFW::s_errorMessage = description;
137}
138
[7bf5433]139void 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
[1ce9afe]150void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
151 GameGui_GLFW::s_keyState[key] = action;
[7bf5433]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 }
[1ce9afe]164
165 // should be true for GLFW_PRESS and GLFW_REPEAT
166 GameGui_GLFW::s_keyDown[key] = (action != GLFW_RELEASE);
[7bf5433]167 */
[1ce9afe]168}
Note: See TracBrowser for help on using the repository browser.