-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWindow.cpp
156 lines (130 loc) · 3.79 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
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
// Author : Jihong Shin (snowapril)
#include <VulkanFramework/pch.h>
#include <VulkanFramework/Window.h>
#include <VulkanFramework/DebugUtils.h>
#include <Common/Logger.h>
#include <cassert>
namespace vfs
{
void ProcessCursorPosCallback (GLFWwindow* window, double xpos, double ypos);
void ProcessWindowResizeCallback (GLFWwindow* window, int width, int height);
Window::Window(const char* title, uint32_t width, uint32_t height)
{
assert(initGLFW() == true);
assert(createWindow(title, width, height) == true);
assert(initEvents() == true);
}
Window::Window(const char* title, float aspectRatio)
{
assert(initGLFW() == true);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
const uint32_t width = static_cast<uint32_t>(mode->width * aspectRatio);
const uint32_t height = static_cast<uint32_t>(mode->height * aspectRatio);
assert(createWindow(title, width, height) == true);
assert(initEvents() == true);
}
Window::~Window()
{
destroyWindow();
}
void Window::destroyWindow(void)
{
if (_window != nullptr)
{
glfwDestroyWindow(_window);
}
glfwTerminate();
}
bool Window::initGLFW(void)
{
if (glfwInit() == GLFW_FALSE)
{
VFS_ERROR << "Failed to initialize GLFW";
return false;
}
if (glfwVulkanSupported() == 0)
{
VFS_ERROR << "This device does not support Vulkan";
return false;
}
return true;
}
bool Window::createWindow(const char* title, uint32_t width, uint32_t height)
{
_title = title;
_extent = glm::vec2(width, height);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
_window = glfwCreateWindow(_extent.x, _extent.y, _title, nullptr, nullptr);
if (_window == nullptr)
{
VFS_ERROR << "Failed to create GLFW window handle";
return false;
}
return true;
}
bool Window::initEvents(void)
{
glfwSetWindowUserPointer(_window, this);
glfwSetFramebufferSizeCallback(_window, ProcessWindowResizeCallback);
glfwSetCursorPosCallback(_window, ProcessCursorPosCallback);
glfwSetErrorCallback(DebugUtils::GlfwDebugCallback);
return true;
}
VkSurfaceKHR Window::createWindowSurface(VkInstance instance)
{
VkSurfaceKHR surface;
glfwCreateWindowSurface(instance, _window, nullptr, &surface);
return surface;
}
void Window::processKeyInput(void)
{
for (uint32_t key = GLFW_KEY_SPACE; key < GLFW_KEY_LAST; ++key)
{
for (KeyCallback& callback : _keyCallbacks)
{
callback(key, glfwGetKey(_window, key) == GLFW_PRESS);
}
}
}
void Window::processCursorPos(double xpos, double ypos)
{
for (CursorPosCallback& callback : _cursorPosCallbacks)
{
callback(xpos, ypos);
}
}
void Window::processWindowResize(int width, int height)
{
_extent = { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
for (WindowResizeCallback& callback : _windowResizeCallbacks)
{
callback(width, height);
}
}
void Window::operator+=(const KeyCallback& callback)
{
_keyCallbacks.emplace_back(callback);
}
void Window::operator+=(const CursorPosCallback& callback)
{
_cursorPosCallbacks.emplace_back(callback);
}
void Window::operator+=(const WindowResizeCallback& callback)
{
_windowResizeCallbacks.emplace_back(callback);
}
void ProcessCursorPosCallback(GLFWwindow* window, double xpos, double ypos)
{
Window* windowPtr = static_cast<Window*>(glfwGetWindowUserPointer(window));
assert(window != nullptr); // Jihong Shin : Window pointer must be valid
windowPtr->processCursorPos(xpos, ypos);
}
void ProcessWindowResizeCallback(GLFWwindow* window, int width, int height)
{
Window* windowPtr = static_cast<Window*>(glfwGetWindowUserPointer(window));
assert(window != nullptr); // Jihong Shin : Window pointer must be valid
windowPtr->processWindowResize(width, height);
windowPtr->setWindowResizedFlag(true);
}
}