#include "rendering/Window.h" int Window::glfwCounter = 0; int Window::glewCounter = 0; Window::Window(TextInput*& textInput, const WindowOptions& options) : textInput(textInput), window(nullptr), size(options.size) { if(glfwCounter == 0 && !glfwInit()) { error.message.clear().append("could not initialize GLFW"); return; } glfwCounter++; glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, false); glfwWindowHint(GLFW_RESIZABLE, true); glfwWindowHint(GLFW_DECORATED, !options.fullscreen); glfwWindowHint(GLFW_DOUBLEBUFFER, true); if(options.es) { glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); } else { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion); GLFWmonitor* monitor = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr; window = glfwCreateWindow(options.size.width, options.size.height, options.name, monitor, nullptr); if(window == nullptr) { error.message.clear().append("could not create window"); return; } glfwSetWindowUserPointer(window, this); glfwSetKeyCallback(window, onKey); glfwSetCharCallback(window, onChar); glfwSetFramebufferSizeCallback(window, onResize); glfwSetMouseButtonCallback(window, onMouse); glfwSetCursorPosCallback(window, onMouseMove); glfwMakeContextCurrent(window); glfwSwapInterval(options.vsync); if(glewCounter == 0) { GLenum err = glewInit(); if(err != GLEW_OK) { error.message.clear() .append("could not initialize GLEW: ") .append(glewGetErrorString(err)); return; } } glewCounter++; } Window::~Window() { if(window != nullptr) { glfwDestroyWindow(window); } glfwCounter--; if(glfwCounter <= 0) { glfwTerminate(); } } const Error& Window::getError() const { return error; } const Clock& Window::getFrameClock() const { return fps; } const Clock& Window::getTickClock() const { return tps; } const Size& Window::getSize() const { return size; } Buttons& Window::getButtons() { return buttons; } void Window::trapCursor(bool trap) { glfwSetInputMode(window, GLFW_CURSOR, trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); } void Window::onKey(GLFWwindow* w, int key, int scancode, int action, int mods) { void* p = glfwGetWindowUserPointer(w); if(p != nullptr) { Window* rw = static_cast(p); TextInput* input = rw->textInput; if(input != nullptr) { input->onKeyEvent(key, scancode, action, mods); } rw->buttons.onKey(key, scancode, action, mods); } } void Window::onChar(GLFWwindow* w, unsigned int codepoint) { void* p = glfwGetWindowUserPointer(w); if(p != nullptr) { TextInput* input = static_cast(p)->textInput; if(input != nullptr) { input->onCharEvent(codepoint); } } } void Window::onResize(GLFWwindow* w, int width, int height) { void* p = glfwGetWindowUserPointer(w); if(p != nullptr) { Window* rw = static_cast(p); rw->size.width = width; rw->size.height = height; } } void Window::onMouse(GLFWwindow* w, int button, int action, int mods) { void* p = glfwGetWindowUserPointer(w); if(p != nullptr) { static_cast(p)->buttons.onMouse(button, action, mods); } } void Window::onMouseMove(GLFWwindow* w, double x, double y) { void* p = glfwGetWindowUserPointer(w); if(p != nullptr) { static_cast(p)->buttons.onMouseMove(x, y); } }