1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <iostream>
- #include "rendering/wrapper/Window.h"
- Window::Window(WindowSize& size, Controller& controller, const Options& options) : window(nullptr), size(size),
- controller(controller) {
- glfwDefaultWindowHints();
- glfwWindowHint(GLFW_VISIBLE, 0);
- glfwWindowHint(GLFW_RESIZABLE, 1);
- glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
- glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
- glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- GLFWmonitor* monitor = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
- window = glfwCreateWindow(size.width, size.height, options.name, monitor, nullptr);
- if(window == nullptr) {
- std::cout << "could not create window\n";
- return;
- }
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(options.vsync);
- glfwSetWindowUserPointer(window, this);
- glfwSetFramebufferSizeCallback(window, [](GLFWwindow* glfwWindow, int newWidth, int newHeight) {
- glViewport(0, 0, newWidth, newHeight);
- void* data = glfwGetWindowUserPointer(glfwWindow);
- if(data == nullptr) {
- return;
- }
- Window& w = *static_cast<Window*> (data);
- w.size.width = newWidth;
- w.size.height = newHeight;
- });
- glfwSetKeyCallback(window, [](GLFWwindow* glfwWindow, int key, int, int action, int) {
- void* data = glfwGetWindowUserPointer(glfwWindow);
- if(data == nullptr) {
- return;
- }
- Window& w = *static_cast<Window*> (data);
- if(action == GLFW_PRESS) {
- w.controller.press(key);
- } else if(action == GLFW_RELEASE) {
- w.controller.release(key);
- }
- });
- }
- Window::~Window() {
- if(window != nullptr) {
- glfwDestroyWindow(window);
- }
- }
- bool Window::hasError() const {
- return window == nullptr;
- }
- void Window::show() {
- glfwShowWindow(window);
- }
- bool Window::shouldClose() const {
- return glfwWindowShouldClose(window);
- }
- void Window::swapBuffers() {
- glfwSwapBuffers(window);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- }
|