-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
76 lines (64 loc) · 1.85 KB
/
Copy pathWindow.cpp
File metadata and controls
76 lines (64 loc) · 1.85 KB
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
#include "Window.h"
Window::Window(const std::string& title, int width, int height) :
m_windowPtr(nullptr),
m_width(width),
m_height(height)
{
glfwInit();
m_windowPtr = glfwCreateWindow(m_width, m_height, title.c_str(), NULL, NULL);
glfwMakeContextCurrent(m_windowPtr);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glViewport(0, 0, width, height);
glfwSetInputMode(m_windowPtr, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(m_windowPtr, mouseCallback);
glEnable(GL_DEPTH_TEST);
}
Window::~Window() {
glfwDestroyWindow(m_windowPtr);
glfwTerminate();
}
bool Window::isOpen() const {
if (glfwWindowShouldClose(m_windowPtr)){
return false;
}
return true;
}
void Window::update() const {
glfwSwapBuffers(m_windowPtr);
glfwPollEvents();
processKeyInputs();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
int Window::width() const {
return m_width;
}
int Window::height() const {
return m_height;
}
void Window::setPointer(void* object) const {
glfwSetWindowUserPointer(m_windowPtr, object);
}
void Window::processKeyInputs() const {
Camera* camera = reinterpret_cast<Camera*>(glfwGetWindowUserPointer(m_windowPtr));
if (glfwGetKey(m_windowPtr, GLFW_KEY_W) == GLFW_PRESS)
camera->move(Forward, 0.05);
if (glfwGetKey(m_windowPtr, GLFW_KEY_S) == GLFW_PRESS)
camera->move(Backward, 0.05);
if (glfwGetKey(m_windowPtr, GLFW_KEY_A) == GLFW_PRESS)
camera->move(Left, 0.05);
if (glfwGetKey(m_windowPtr, GLFW_KEY_D) == GLFW_PRESS)
camera->move(Right, 0.05);
}
void mouseCallback(GLFWwindow* window, double xPos, double yPos) {
static bool firstMouse = true;
static float lastx, lasty;
Camera* camera = reinterpret_cast<Camera*>(glfwGetWindowUserPointer(window));
if (firstMouse) {
lastx = xPos;
lasty = yPos;
firstMouse = false;
}
camera->rotate((xPos - lastx), (lasty - yPos));
lastx = xPos;
lasty = yPos;
}