-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_handler.h
194 lines (153 loc) · 4.39 KB
/
window_handler.h
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
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "input.h"
#include <iostream>
#include <utility>
#include <vector>
const int INITIAL_WINDOW_WIDTH = 800;
const int INITIAL_WINDOW_HEIGHT = 600;
double mouseLastXpos = INITIAL_WINDOW_WIDTH / 2;
double mouseLastYpos = INITIAL_WINDOW_HEIGHT / 2;
bool firstMouseMovement = true;
struct WindowHandler {
static void framebufferResizeCallback(
GLFWwindow* window, int width, int height) {
auto self = reinterpret_cast<WindowHandler*>(
glfwGetWindowUserPointer(window));
self->framebufferResized_ = true;
// reset last mouse position
mouseLastXpos = width / 2;
mouseLastYpos = height / 2;
}
static void keyPressCallback(
GLFWwindow* window, int key, int scancode, int action, int mods) {
auto self = reinterpret_cast<WindowHandler*>(
glfwGetWindowUserPointer(window));
bool pressed = action != GLFW_RELEASE;
switch(key) {
case GLFW_KEY_W:
self->keyStates_.forward = pressed;
break;
case GLFW_KEY_S:
self->keyStates_.reverse = pressed;
break;
case GLFW_KEY_A:
self->keyStates_.left = pressed;
break;
case GLFW_KEY_D:
self->keyStates_.right = pressed;
break;
case GLFW_KEY_Q:
self->keyStates_.rise = pressed;
break;
case GLFW_KEY_E:
self->keyStates_.fall = pressed;
break;
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, true);
break;
default:
break;
}
if (key == GLFW_KEY_R && action == GLFW_PRESS) {
std::cout << "pressed the r key\n";
self->shouldRecreateSwapChain_ = true;
}
}
static void mousePositionCallback(
GLFWwindow* window, double xpos, double ypos) {
if (firstMouseMovement) {
mouseLastXpos = xpos;
mouseLastYpos = ypos;
firstMouseMovement = false;
return;
}
auto self = reinterpret_cast<WindowHandler*>(
glfwGetWindowUserPointer(window));
self->mouseState_.xOffset = (xpos - mouseLastXpos) / 10;
self->mouseState_.yOffset = (ypos - mouseLastYpos) / 10;
mouseLastXpos = xpos;
mouseLastYpos = ypos;
}
WindowHandler() {
this->window_ = this->createWindow();
glfwSetWindowUserPointer(window_, this);
glfwSetFramebufferSizeCallback(window_, framebufferResizeCallback);
glfwSetKeyCallback(window_, keyPressCallback);
// capture mouse
glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window_, mousePositionCallback);
}
GLFWwindow* createWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
return glfwCreateWindow(
INITIAL_WINDOW_WIDTH,
INITIAL_WINDOW_HEIGHT,
"Phalanx",
nullptr,
nullptr);
}
bool wasWindowClosed() {
return glfwWindowShouldClose(window_);
}
std::vector<const char*> getRequiredExtensions() {
// this is where we actually get required extensions
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions =
glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
// beginning and end iterators of array
std::vector<const char*> requiredExtensions(
glfwExtensions,
glfwExtensions + glfwExtensionCount);
return requiredExtensions;
}
VkResult createWindowSurface(VkInstance& instance, VkSurfaceKHR* surfacePtr) {
return glfwCreateWindowSurface(instance, window_, nullptr, surfacePtr);
}
bool framebufferWasResized() {
return framebufferResized_;
}
void resetFramebufferResized() {
framebufferResized_ = false;
}
bool shouldRecreateSwapchain() {
return shouldRecreateSwapChain_;
}
void resetShouldRecreateSwapchain() {
shouldRecreateSwapChain_ = false;
}
// Vulkan works with pixels, while GLFW's window size is measured in screen
// coordinates. On high DPI displays, those values won't be 1:1.
// glfwGetFrameBufferSize returns the window dimensions in pixels
std::pair<int, int> getFramebufferWidthHeight() {
int width;
int height;
glfwGetFramebufferSize(window_, &width, &height);
return std::pair<int, int>{width, height};
}
void pollEvents() {
mouseState_.reset();
glfwPollEvents();
}
void waitEvents() {
glfwWaitEvents();
}
void cleanup() {
glfwDestroyWindow(window_);
glfwTerminate();
}
Input::KeyStates& getKeyStates() {
return keyStates_;
}
Input::MouseState& getMouseState() {
return mouseState_;
}
GLFWwindow* window_;
bool framebufferResized_ = false;
bool shouldRecreateSwapChain_ = false;
Input::KeyStates keyStates_;
Input::MouseState mouseState_;
};