summary refs log tree commit diff
path: root/src/GFX/Window.cpp
blob: 33e76d5233b7de1c411706a7113c07fc17ab1631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdexcept>
#include "../Common/Sizes.hpp"
#include "../Common/Assert.hpp"
#include "../ThreadRole.hpp"
#include "Window.hpp"

namespace MC::GFX {

Window::Window(const Char* title, U32 width, U32 height) {
    ASSERT_MAIN_THREAD();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);

    m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
    if (m_window == nullptr) {
        throw std::runtime_error("Failed to create window.");
    }

    glfwMakeContextCurrent(m_window);
    glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}

Window::~Window() {
    ASSERT_MAIN_THREAD();
    glfwDestroyWindow(m_window);
}

Bool Window::should_close() const {
    return glfwWindowShouldClose(m_window);
}

GLFWwindow* Window::get() const {
    return m_window;
}

void Window::close() {
    glfwSetWindowShouldClose(m_window, true);
}

Vector<2> Window::mouse_delta() {
    return m_mouse.update(m_window);
}

Bool Window::key(I32 key, I32 type) const {
    return glfwGetKey(m_window, key) == type;
}

Bool Window::mouse(I32 key, I32 type) const {
    return glfwGetMouseButton(m_window, key) == type;
}

void Window::start_render() {
    glfwSwapBuffers(m_window);
}

void Window::poll_events() {
    ASSERT_MAIN_THREAD();
    glfwPollEvents();
}

void Window::on_size_change(void (callback)(GLFWwindow*, I32, I32)) {
    glfwSetFramebufferSizeCallback(m_window, callback);
}

void Window::attach() const {
    glfwMakeContextCurrent(m_window);
}

void Window::detach() const {
    ASSERT(glfwGetCurrentContext() == m_window, "Cannot detach window that is not current");
    glfwMakeContextCurrent(nullptr);
}

}