|
| 1 | +#include "os.hpp" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include "dispatcher.hpp" |
| 5 | + |
| 6 | +#ifdef __APPLE__ |
| 7 | +// Needed so we can disable retina support for our window. |
| 8 | +#define GLFW_EXPOSE_NATIVE_COCOA 1 |
| 9 | +#define GLFW_EXPOSE_NATIVE_NSGL 1 |
| 10 | +#include <GLFW/glfw3native.h> |
| 11 | +// We can't just include objc/runtime.h and objc/message.h because glfw is too forward thinking for its own good. |
| 12 | +typedef void* SEL; |
| 13 | +extern "C" id objc_msgSend(id self, SEL op, ...); |
| 14 | +extern "C" SEL sel_getUid(const char *str); |
| 15 | +#endif |
| 16 | + |
| 17 | +namespace vv { |
| 18 | + |
| 19 | + // Error helper function used by GLFW for error messaging. |
| 20 | + // Currently outputs to std::cout. |
| 21 | + static void ErrorCallback(int error, const char* description) { |
| 22 | + std::cout << description << std::endl; |
| 23 | + } |
| 24 | + |
| 25 | + bool OS::InitializeWindow(const int width, const int height, const std::string title, |
| 26 | + const unsigned int glMajor /*= 3*/, const unsigned int glMinor /*= 2*/) { |
| 27 | + glfwSetErrorCallback(ErrorCallback); |
| 28 | + |
| 29 | + // Initialize the library. |
| 30 | + if (glfwInit() != GL_TRUE) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); |
| 35 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, glMajor); |
| 36 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, glMinor); |
| 37 | + |
| 38 | +#ifndef __APPLE__ |
| 39 | +#ifdef _DEBUG |
| 40 | + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); |
| 41 | +#else |
| 42 | + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 43 | +#endif |
| 44 | +#else |
| 45 | + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 46 | +#endif |
| 47 | + |
| 48 | + // Create a windowed mode window and its OpenGL context. |
| 49 | + this->window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); |
| 50 | + |
| 51 | + if (!this->window) { |
| 52 | + glfwTerminate(); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + this->client_width = width; |
| 57 | + this->client_height = height; |
| 58 | + |
| 59 | +#ifdef __APPLE__ |
| 60 | + // Force retina displays to create a 1x framebuffer so we don't choke our fillrate. |
| 61 | + id cocoaWindow = glfwGetCocoaWindow(this->window); |
| 62 | + id cocoaGLView = ((id (*)(id, SEL)) objc_msgSend)(cocoaWindow, sel_getUid("contentView")); |
| 63 | + ((void (*)(id, SEL, bool)) objc_msgSend)(cocoaGLView, sel_getUid("setWantsBestResolutionOpenGLSurface:"), false); |
| 64 | +#endif |
| 65 | + |
| 66 | + // attach the context |
| 67 | + glfwMakeContextCurrent(this->window); |
| 68 | + |
| 69 | +#ifndef __APPLE__ |
| 70 | + // setting glewExperimental fixes a glfw context problem |
| 71 | + // (tested on Ubuntu 13.04) |
| 72 | + glewExperimental = GL_TRUE; |
| 73 | + |
| 74 | + // Init GLEW. |
| 75 | + GLuint error = glewInit(); |
| 76 | + if (error != GLEW_OK) { |
| 77 | + return false; |
| 78 | + } |
| 79 | +#endif |
| 80 | + |
| 81 | + // Associate a pointer for this instance with this window. |
| 82 | + glfwSetWindowUserPointer(this->window, this); |
| 83 | + |
| 84 | + // Set up some callbacks. |
| 85 | + glfwSetWindowSizeCallback(this->window, &OS::WindowResized); |
| 86 | + glfwSetKeyCallback(this->window, &OS::KeyboardEventCallback); |
| 87 | + glfwSetCursorPosCallback(this->window, &OS::MouseMoveEventCallback); |
| 88 | + glfwSetCharCallback(this->window, &OS::CharacterEventCallback); |
| 89 | + glfwSetMouseButtonCallback(this->window, &OS::MouseButtonEventCallback); |
| 90 | + glfwSetWindowFocusCallback(this->window, &OS::WindowFocusChangeCallback); |
| 91 | + |
| 92 | + glfwGetCursorPos(this->window, &this->old_mouse_x, &this->old_mouse_y); |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + void OS::MakeCurrent() { |
| 98 | + glfwMakeContextCurrent(this->window); |
| 99 | + } |
| 100 | + |
| 101 | + void OS::DetachContext() { |
| 102 | + glfwMakeContextCurrent(NULL); |
| 103 | + } |
| 104 | + |
| 105 | + void OS::Terminate() { |
| 106 | + glfwTerminate(); |
| 107 | + } |
| 108 | + |
| 109 | + bool OS::Closing() { |
| 110 | + return glfwWindowShouldClose(this->window) > 0; |
| 111 | + } |
| 112 | + |
| 113 | + void OS::SwapBuffers() { |
| 114 | + glfwSwapBuffers(this->window); |
| 115 | + } |
| 116 | + |
| 117 | + void OS::OSMessageLoop() { |
| 118 | + glfwPollEvents(); |
| 119 | + } |
| 120 | + |
| 121 | + int OS::GetWindowWidth() { |
| 122 | + return this->client_width; |
| 123 | + } |
| 124 | + |
| 125 | + int OS::GetWindowHeight() { |
| 126 | + return this->client_height; |
| 127 | + } |
| 128 | + |
| 129 | + double OS::GetDeltaTime() { |
| 130 | + double new_time = glfwGetTime(); |
| 131 | + double delta_time = new_time - this->last_time; |
| 132 | + this->last_time = new_time; |
| 133 | + return delta_time; |
| 134 | + } |
| 135 | + |
| 136 | + void OS::WindowResized(GLFWwindow* window, int width, int height) { |
| 137 | + // Get the user pointer and cast it. |
| 138 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 139 | + |
| 140 | + if (os) { |
| 141 | + os->UpdateWindowSize(width, height); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + void OS::KeyboardEventCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { |
| 146 | + // Get the user pointer and cast it. |
| 147 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 148 | + |
| 149 | + if (os) { |
| 150 | + os->DispatchKeyboardEvent(key, scancode, action, mods); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + void OS::CharacterEventCallback(GLFWwindow* window, unsigned int uchar) { |
| 155 | + // Get the user pointer and cast it. |
| 156 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 157 | + |
| 158 | + if (os) { |
| 159 | + os->DispatchCharacterEvent(uchar); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + void OS::MouseMoveEventCallback(GLFWwindow* window, double x, double y) { |
| 164 | + // Get the user pointer and cast it. |
| 165 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 166 | + |
| 167 | + if (os) { |
| 168 | + os->DispatchMouseMoveEvent(x, y); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + void OS::MouseButtonEventCallback(GLFWwindow* window, int button, int action, int mods) { |
| 173 | + // Get the user pointer and cast it. |
| 174 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 175 | + |
| 176 | + if (os) { |
| 177 | + os->DispatchMouseButtonEvent(button, action, mods); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + void OS::WindowFocusChangeCallback(GLFWwindow* window, int focused) { |
| 182 | + if (focused == GL_FALSE) { |
| 183 | + // Get the user pointer and cast it. |
| 184 | + OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window)); |
| 185 | + |
| 186 | + if (os) { |
| 187 | + // TODO: Implement a DispatchWindowFocusEvent() method in OS |
| 188 | + // TODO: Dispatch a focus changed event. |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + void OS::UpdateWindowSize(const int width, const int height) { |
| 194 | + this->client_width = width; |
| 195 | + this->client_height = height; |
| 196 | + } |
| 197 | + |
| 198 | + void OS::DispatchKeyboardEvent(const int key, const int scancode, const int action, const int mods) { |
| 199 | + KeyboardEvent key_event; |
| 200 | + if (action == GLFW_PRESS) { |
| 201 | + key_event = { key, scancode, KeyboardEvent::KEY_DOWN, mods }; |
| 202 | + } |
| 203 | + else if (action == GLFW_REPEAT) { |
| 204 | + key_event = { key, scancode, KeyboardEvent::KEY_REPEAT, mods }; |
| 205 | + } |
| 206 | + else if (action == GLFW_RELEASE) { |
| 207 | + key_event = { key, scancode, KeyboardEvent::KEY_UP, mods }; |
| 208 | + } |
| 209 | + |
| 210 | + Dispatcher<KeyboardEvent>::GetInstance()->NotifySubscribers(&key_event); |
| 211 | + } |
| 212 | + |
| 213 | + void OS::DispatchCharacterEvent(const unsigned int uchar) { |
| 214 | + KeyboardEvent key_event { (const int)uchar, 0, KeyboardEvent::KEY_CHAR, 0 }; |
| 215 | + Dispatcher<KeyboardEvent>::GetInstance()->NotifySubscribers(&key_event); |
| 216 | + } |
| 217 | + |
| 218 | + void OS::DispatchMouseMoveEvent(const double x, const double y) { |
| 219 | + MouseMoveEvent mmov_event = { |
| 220 | + static_cast<double>(x) / this->client_width, |
| 221 | + static_cast<double>(y) / this->client_height, |
| 222 | + static_cast<int>(this->old_mouse_x), |
| 223 | + static_cast<int>(this->old_mouse_y), |
| 224 | + static_cast<int>(x), |
| 225 | + static_cast<int>(y) |
| 226 | + }; |
| 227 | + Dispatcher<MouseMoveEvent>::GetInstance()->NotifySubscribers(&mmov_event); |
| 228 | + |
| 229 | + // If we are in mouse lock we will snap the mouse to the middle of the screen. |
| 230 | + if (this->mouse_lock) { |
| 231 | + this->old_mouse_x = this->client_width / 2; |
| 232 | + this->old_mouse_y = this->client_height / 2; |
| 233 | + glfwSetCursorPos(this->window, this->old_mouse_x, this->old_mouse_y); |
| 234 | + } |
| 235 | + else { |
| 236 | + this->old_mouse_x = x; |
| 237 | + this->old_mouse_y = y; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + void OS::DispatchMouseButtonEvent(const int button, const int action, const int mods) { |
| 242 | + MouseBtnEvent mbtn_event; |
| 243 | + if (action == GLFW_PRESS) { |
| 244 | + mbtn_event.action = MouseBtnEvent::DOWN; |
| 245 | + } |
| 246 | + else { |
| 247 | + mbtn_event.action = MouseBtnEvent::UP; |
| 248 | + } |
| 249 | + if (button == GLFW_MOUSE_BUTTON_LEFT) { |
| 250 | + mbtn_event.button = MouseBtnEvent::LEFT; |
| 251 | + } |
| 252 | + else if (button == GLFW_MOUSE_BUTTON_RIGHT) { |
| 253 | + mbtn_event.button = MouseBtnEvent::RIGHT; |
| 254 | + } |
| 255 | + else if (button == GLFW_MOUSE_BUTTON_MIDDLE) { |
| 256 | + mbtn_event.button = MouseBtnEvent::MIDDLE; |
| 257 | + } |
| 258 | + Dispatcher<MouseBtnEvent>::GetInstance()->NotifySubscribers(&mbtn_event); |
| 259 | + } |
| 260 | + |
| 261 | + void OS::ToggleMouseLock() { |
| 262 | + this->mouse_lock = !this->mouse_lock; |
| 263 | + if (this->mouse_lock) { |
| 264 | + glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); |
| 265 | + } |
| 266 | + else { |
| 267 | + glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + void OS::SetMousePosition(const double x, const double y) { |
| 272 | + glfwSetCursorPos(this->window, x, y); |
| 273 | + } |
| 274 | + |
| 275 | +} |
0 commit comments