-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
83 lines (69 loc) · 2.24 KB
/
window.cpp
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
78
79
80
81
82
83
#include <iostream>
#include <cstdlib>
#include <utility>
#include "window.h"
void glfw_error_callback(int code, const char *err) {
std::cerr << "GLFW error " << code << ": " << err << std::endl;
}
void glfw_key_callback(GLFWwindow *handle, int key, int scancode, int action,
int mods) {
(void)scancode; // Silence unused warnings
(void)mods;
const auto window = static_cast<Window *>(glfwGetWindowUserPointer(handle));
if (key < 0)
return;
switch (action) {
case GLFW_PRESS:
window->keyboard.press(key);
break;
case GLFW_RELEASE:
window->keyboard.release(key);
break;
default:
}
}
Window::Window(const float width, const float height, const char *title,
UpdateFn update, RenderFn render)
: size{width, height}, update{std::move(update)}, render{std::move(render)},
time_delta{0} {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
std::exit(-1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
handle = glfwCreateWindow(static_cast<int>(size.x), static_cast<int>(size.y),
title, nullptr, nullptr);
if (!handle) {
glfwTerminate();
std::exit(-1);
}
glfwSetInputMode(handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(handle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
glfwSetWindowUserPointer(handle, this);
glfwSetKeyCallback(handle, glfw_key_callback);
glfwMakeContextCurrent(handle);
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
glfwTerminate();
std::exit(-1);
}
}
Window::~Window() { glfwTerminate(); }
glm::vec2 Window::dimensions() const { return size; }
void Window::loop() {
time_now = Clock::now();
time_prev = time_now;
while (!glfwWindowShouldClose(handle)) {
time_delta = std::chrono::duration_cast<std::chrono::duration<float>>(
time_now - time_prev)
.count();
time_prev = time_now;
time_now = Clock::now();
update();
render();
glfwSwapBuffers(handle);
glfwPollEvents();
}
}