-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
79 lines (59 loc) · 2.05 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
#include "Window.h"
static void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
Window::Window(const int width, const int height, const std::string &title) : m_width(width),
m_height(height) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
this->m_window = glfwCreateWindow(this->m_width, this->m_height, title.c_str(), nullptr, nullptr);
if (this->m_window == nullptr) {
std::cout << "ERROR: Cant open null util." << std::endl;
glfwTerminate();
}
glfwMakeContextCurrent(this->m_window);
glfwSetFramebufferSizeCallback(this->m_window, framebuffer_size_callback);
// glfwSetWindowSizeCallback(this->m_window, window_resize_callback);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "ERROR: Failed to load GLAD" << std::endl;
glfwTerminate();
}
std::cout << glGetString(GL_VERSION) << std::endl;
}
Window::~Window() {
glfwTerminate();
}
void Window::use() {
double currentFrame = glfwGetTime();
m_deltaTime = currentFrame - m_prevFrame;
m_prevFrame = currentFrame;
processInput();
}
void Window::close() {
glfwDestroyWindow(this->m_window);
glfwTerminate();
}
void Window::poll() {
glfwSwapBuffers(this->m_window);
glfwPollEvents();
}
int Window::getWidth() const {
return this->m_width;
}
int Window::getHeight() const {
return this->m_height;
}
float Window::getDeltaTime() const {
return this->m_deltaTime;
}
bool Window::shouldClose() {
return glfwWindowShouldClose(this->m_window);
}
void Window::processInput() {
if (glfwGetKey(this->m_window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(this->m_window, true);
if (glfwGetKey(this->m_window, GLFW_KEY_SPACE) == GLFW_PRESS && m_isPaused)
this->m_isPaused = !m_isPaused;
}