-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGLFWWindow.cpp
More file actions
81 lines (70 loc) · 3.07 KB
/
GLFWWindow.cpp
File metadata and controls
81 lines (70 loc) · 3.07 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
#include "GLFWWindow.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <glm/ext/matrix_transform.hpp>
GLFWWindow::GLFWWindow(std::string name, int width, int height) {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
}
VkSurfaceKHR GLFWWindow::createSurface(std::shared_ptr<VulkanContext> context) {
if (glfwCreateWindowSurface(context->instance.get(), static_cast<GLFWwindow *>(window), nullptr, &surface) !=
VK_SUCCESS) {
throw std::runtime_error("failed to create window surface!");
}
return surface;
}
std::array<bool, 3> GLFWWindow::getMouseButton() {
return {
glfwGetMouseButton(static_cast<GLFWwindow *>(window), GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS,
glfwGetMouseButton(static_cast<GLFWwindow *>(window), GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS,
glfwGetMouseButton(static_cast<GLFWwindow *>(window), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS
};
}
std::vector<std::string> GLFWWindow::getRequiredInstanceExtensions() {
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
auto extensions = std::vector<std::string>{};
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
extensions.emplace_back(glfwExtensions[i]);
}
return extensions;
}
std::pair<uint32_t, uint32_t> GLFWWindow::getFramebufferSize() const {
int width, height;
glfwGetFramebufferSize(static_cast<GLFWwindow *>(window), &width, &height);
return {width, height};
}
std::array<double, 2> GLFWWindow::getCursorTranslation() {
double x, y;
glfwGetCursorPos(static_cast<GLFWwindow *>(window), &x, &y);
const auto translation = std::array<double, 2>{x - lastX, y - lastY};
lastX = x;
lastY = y;
return translation;
}
std::array<bool, 9> GLFWWindow::getKeys() {
return {
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_W) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_A) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_S) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_D) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_SPACE) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_ESCAPE) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_Q) == GLFW_PRESS,
glfwGetKey(static_cast<GLFWwindow *>(window), GLFW_KEY_E) == GLFW_PRESS
};
}
void GLFWWindow::mouseCapture(bool capture) {
if (capture) {
glfwSetInputMode(static_cast<GLFWwindow *>(window), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(static_cast<GLFWwindow *>(window), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
bool GLFWWindow::tick() {
glfwPollEvents();
return !glfwWindowShouldClose(static_cast<GLFWwindow *>(window));
}