-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwindow.cpp
More file actions
210 lines (185 loc) · 5.28 KB
/
window.cpp
File metadata and controls
210 lines (185 loc) · 5.28 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "window.h"
#include <iostream>
Window::Window() : width{800},
height{600},
mouseFirstMoved{true},
leftButtonPressed{false},
rightButtonPressed{false},
xChange{0.0f},
yChange{0.0f},
scrollY{0.0f}
{
pKeys = std::make_shared<std::array<bool, 1024>>();
pKeys->fill(false);
}
Window::Window(GLint windowWidth, GLint windowHeight) : width{windowWidth},
height{windowHeight},
mouseFirstMoved{true},
leftButtonPressed{false},
rightButtonPressed{false},
xChange{0.0f},
yChange{0.0f},
scrollY{0.0f}
{
pKeys = std::make_shared<std::array<bool, 1024>>();
pKeys->fill(false);
}
int Window::Initialise()
{
if (!glfwInit())
{
printf("Error Initialising GLFW");
glfwTerminate();
return 1;
}
// Setup GLFW: desktop OpenGL 3.3 core; Emscripten uses embedded WebGL2 (GLES3)
#ifdef __EMSCRIPTEN__
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#else
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);
#endif
// Create the window
mainWindow = glfwCreateWindow(width, height, "Test Window", NULL, NULL);
if (!mainWindow)
{
printf("Error creating GLFW window!");
glfwTerminate();
return 1;
}
// Get buffer size information
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
// Set the current context
glfwMakeContextCurrent(mainWindow);
// Handle key presses + mouse input
createCallbacks();
// glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor
#ifndef __EMSCRIPTEN__
glewExperimental = GL_TRUE;
GLenum error = glewInit();
if (error != GLEW_OK)
{
printf("Error: %s", glewGetErrorString(error));
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
#endif
glEnable(GL_DEPTH_TEST);
// Create Viewport
glViewport(0, 0, bufferWidth, bufferHeight);
glfwSetWindowUserPointer(mainWindow, this);
return 0;
}
void Window::syncFramebufferSize()
{
if (!mainWindow)
return;
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
if (bufferWidth < 1)
bufferWidth = 1;
if (bufferHeight < 1)
bufferHeight = 1;
glViewport(0, 0, bufferWidth, bufferHeight);
}
Window::~Window()
{
glfwDestroyWindow(mainWindow);
glfwTerminate();
}
void Window::handleMouseButton(GLFWwindow *window, int button, int action, int mods)
{
Window *theWindow = static_cast<Window *>(glfwGetWindowUserPointer(window));
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
theWindow->leftButtonPressed = true;
}
else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
theWindow->leftButtonPressed = false;
theWindow->mouseFirstMoved = true;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
{
theWindow->rightButtonPressed = true;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
{
theWindow->rightButtonPressed = false;
theWindow->mouseFirstMoved = true;
}
}
void Window::handleKeys(GLFWwindow *window, int key, int code, int action, int mode)
{
Window *theWindow = static_cast<Window *>(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key >= 0 && key < theWindow->pKeys->size())
{
if (action == GLFW_PRESS)
{
theWindow->pKeys->at(key) = true;
// std::cout << "Key Pressed: " << key << std::endl;
}
else if (action == GLFW_RELEASE)
{
theWindow->pKeys->at(key) = false;
// std::cout << "Key Released: " << key << std::endl;
}
}
}
void Window::handleScroll(GLFWwindow *window, double xOffset, double yOffset)
{
(void)xOffset;
Window *theWindow = static_cast<Window *>(glfwGetWindowUserPointer(window));
theWindow->scrollY += static_cast<GLfloat>(yOffset);
}
void Window::handleMouseMovement(GLFWwindow *window, double xPos, double yPos)
{
Window *theWindow = static_cast<Window *>(glfwGetWindowUserPointer(window));
if (!theWindow->leftButtonPressed && !theWindow->rightButtonPressed)
return;
if (theWindow->mouseFirstMoved)
{
theWindow->lastX = xPos;
theWindow->lastY = yPos;
theWindow->mouseFirstMoved = false;
}
theWindow->xChange = xPos - theWindow->lastX;
theWindow->yChange = theWindow->lastY - yPos;
theWindow->lastX = xPos;
theWindow->lastY = yPos;
// std::cout << "Mouse X: " << xPos << " Mouse Y: " << yPos << std::endl;
// std::cout << "X Change: " << theWindow->xChange << " Y Change: " << theWindow->yChange << std::endl;
}
void Window::createCallbacks()
{
glfwSetKeyCallback(mainWindow, handleKeys);
glfwSetCursorPosCallback(mainWindow, handleMouseMovement);
glfwSetMouseButtonCallback(mainWindow, handleMouseButton);
glfwSetScrollCallback(mainWindow, handleScroll);
}
GLfloat Window::getXChange()
{
GLfloat aux = xChange;
xChange = 0;
return aux;
}
GLfloat Window::getYChange()
{
GLfloat aux = yChange;
yChange = 0;
return aux;
}
GLfloat Window::getScrollY()
{
GLfloat aux = scrollY;
scrollY = 0.0f;
return aux;
}