Skip to content

Commit 5e8b7d9

Browse files
committed
Updated window module to obfuscate GLFW internal implementation details
1 parent 40ea0ff commit 5e8b7d9

File tree

6 files changed

+193
-160
lines changed

6 files changed

+193
-160
lines changed

engine/render/renderer/Renderer.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@
1010

1111
#include "Renderer.h"
1212

13+
#include <GLFW/glfw3.h>
14+
1315
#include "platform/vulkan/Font.h"
1416
#include "platform/vulkan/utils/Types.h"
1517
#include "statics/Statics.h"
1618

17-
// TODO look into why including via GLFW header + volk isn't defining this - JDM
18-
extern "C"
19-
{
20-
GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance,
21-
GLFWwindow* window,
22-
const VkAllocationCallbacks* allocator,
23-
VkSurfaceKHR* surface);
24-
}
25-
2619
namespace Siege
2720
{
2821
Renderer* Renderer::instance {nullptr};
@@ -36,8 +29,10 @@ Renderer::Renderer(Window& window) : window {window}
3629
if (instance == nullptr) instance = this;
3730

3831
auto createWindowSurface = [&window](VkInstance instance, VkSurfaceKHR* surface) -> bool {
39-
return glfwCreateWindowSurface(instance, window.GetGlfwWindow(), nullptr, surface) ==
40-
VK_SUCCESS;
32+
return glfwCreateWindowSurface(instance,
33+
reinterpret_cast<GLFWwindow*>(window.GetWindow()),
34+
nullptr,
35+
surface) == VK_SUCCESS;
4136
};
4237

4338
auto extent = window.GetExtents();

engine/window/Input.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,38 @@
88

99
#include "Input.h"
1010

11+
#include <GLFW/glfw3.h>
1112
#include <utils/math/Float.h>
1213

13-
namespace Siege
14+
static Siege::Window* windowPtr = nullptr;
15+
16+
static Siege::Input::MouseCoordinates currentMouseCoordinates;
17+
18+
static std::map<int, int> keyMap;
19+
20+
static void GetCursorPositionCallback(GLFWwindow* window, double xpos, double ypos)
21+
{
22+
currentMouseCoordinates.x = static_cast<float>(xpos);
23+
currentMouseCoordinates.y = static_cast<float>(ypos);
24+
}
25+
26+
static GLFWwindow* AsGlfwWindow(void* window)
1427
{
15-
Window* Input::windowPtr = nullptr;
16-
Input::MouseCoordinates Input::currentMouseCoordinates;
28+
return reinterpret_cast<GLFWwindow*>(window);
29+
}
1730

18-
std::map<int, int> Input::keyMap;
31+
namespace Siege
32+
{
1933

2034
void Input::SetWindowPointer(Window* window)
2135
{
2236
// TODO: Fail if inputted pointer is nullptr
2337
windowPtr = window;
24-
glfwSetCursorPosCallback(window->GetGlfwWindow(), GetCursorPositionCallback);
38+
glfwSetCursorPosCallback(AsGlfwWindow(window->GetWindow()), GetCursorPositionCallback);
2539
}
2640
bool Input::IsKeyDown(int key)
2741
{
28-
bool hasKey = (glfwGetKey(windowPtr->GetGlfwWindow(), key) == GLFW_PRESS);
42+
bool hasKey = (glfwGetKey(AsGlfwWindow(windowPtr->GetWindow()), key) == GLFW_PRESS);
2943

3044
if (keyMap.find(key) != keyMap.end())
3145
{
@@ -40,7 +54,8 @@ bool Input::IsKeyDown(int key)
4054

4155
bool Input::IsKeyJustPressed(int key)
4256
{
43-
bool hasKey = glfwGetKey(windowPtr->GetGlfwWindow(), key) == GLFW_PRESS;
57+
bool hasKey =
58+
glfwGetKey(reinterpret_cast<GLFWwindow*>(windowPtr->GetWindow()), key) == GLFW_PRESS;
4459

4560
bool keyEntryExists = keyMap.find(key) != keyMap.end();
4661
if (keyEntryExists && hasKey)
@@ -78,9 +93,4 @@ Input::MouseCoordinates Input::GetNormalisedMousePosition()
7893
1.f)};
7994
}
8095

81-
void Input::GetCursorPositionCallback(GLFWwindow* window, double xpos, double ypos)
82-
{
83-
currentMouseCoordinates.x = static_cast<float>(xpos);
84-
currentMouseCoordinates.y = static_cast<float>(ypos);
85-
}
8696
} // namespace Siege

engine/window/Input.h

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313

1414
#include "Window.h"
1515

16-
#define KEY_W GLFW_KEY_W
17-
#define KEY_A GLFW_KEY_A
18-
#define KEY_S GLFW_KEY_S
19-
#define KEY_D GLFW_KEY_D
20-
#define KEY_Q GLFW_KEY_Q
21-
#define KEY_E GLFW_KEY_E
22-
#define KEY_UP GLFW_KEY_UP
23-
#define KEY_DOWN GLFW_KEY_DOWN
24-
#define KEY_RIGHT GLFW_KEY_RIGHT
25-
#define KEY_LEFT GLFW_KEY_LEFT
26-
#define KEY_BACKTICK GLFW_KEY_GRAVE_ACCENT
27-
#define KEY_LEFT_SHIFT GLFW_KEY_LEFT_SHIFT
28-
29-
#define KEY_ESCAPE GLFW_KEY_ESCAPE
16+
#define KEY_W 87
17+
#define KEY_A 65
18+
#define KEY_S 83
19+
#define KEY_D 68
20+
#define KEY_Q 81
21+
#define KEY_E 69
22+
#define KEY_UP 265
23+
#define KEY_DOWN 264
24+
#define KEY_RIGHT 262
25+
#define KEY_LEFT 263
26+
#define KEY_BACKTICK 96
27+
#define KEY_LEFT_SHIFT 340
28+
29+
#define KEY_ESCAPE 256
3030

3131
namespace Siege
3232
{
@@ -45,15 +45,6 @@ class Input
4545

4646
static const MouseCoordinates& GetCursorPosition();
4747
static MouseCoordinates GetNormalisedMousePosition();
48-
49-
private:
50-
51-
static Window* windowPtr;
52-
static bool movedLastFrame;
53-
static MouseCoordinates currentMouseCoordinates;
54-
static std::map<int, int> keyMap;
55-
56-
static void GetCursorPositionCallback(GLFWwindow* window, double xpos, double ypos);
5748
};
5849
} // namespace Siege
5950

engine/window/Window.cpp

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,102 @@
88

99
#include "Window.h"
1010

11+
#include <GLFW/glfw3.h>
12+
13+
static GLFWwindow* window = nullptr;
14+
15+
static size_t windowCount = 0;
16+
17+
void WindowResizeCallback(GLFWwindow* windowPtr, int width, int height)
18+
{
19+
auto windowContext = reinterpret_cast<Siege::Window*>(glfwGetWindowUserPointer(windowPtr));
20+
windowContext->ResizeWindow({static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
21+
}
22+
1123
namespace Siege
1224
{
13-
GLFWwindow* Window::window = nullptr;
14-
bool Window::glfwInitialised = false;
15-
size_t Window::glfwWindows = 0;
25+
26+
Window::Window(const char* name, WindowExtents extents) : extents(extents)
27+
{
28+
static bool glfwInitialised = false;
29+
if (!glfwInitialised)
30+
{
31+
glfwInit();
32+
glfwInitialised = true;
33+
}
34+
35+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
36+
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
37+
38+
if (!window)
39+
{
40+
window = glfwCreateWindow(extents.width, extents.height, name, nullptr, nullptr);
41+
}
42+
43+
if (glfwRawMouseMotionSupported())
44+
{
45+
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
46+
}
47+
48+
glfwSetWindowIconifyCallback(window, [](GLFWwindow* window, int iconified) {
49+
auto windowContext = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
50+
windowContext->isVisible = !iconified;
51+
});
52+
53+
glfwSetWindowUserPointer(window, this);
54+
glfwSetWindowSizeCallback(window, WindowResizeCallback);
55+
windowCount++;
56+
57+
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &DPI.width, &DPI.height);
58+
}
59+
60+
Window::~Window()
61+
{
62+
glfwDestroyWindow(window);
63+
if (--windowCount <= 0) glfwTerminate();
64+
}
65+
66+
int Window::GetHeight() const
67+
{
68+
return extents.height;
69+
}
70+
71+
int Window::GetWidth() const
72+
{
73+
return extents.width;
74+
}
75+
76+
bool Window::IsVisible() const
77+
{
78+
return isVisible;
79+
}
80+
81+
uint32_t Window::GetDPI() const
82+
{
83+
// NOTE(Aryeh): Need to find a better solution when the DPI in different axes are not the
84+
// same
85+
return DPI.width;
86+
}
87+
88+
uint32_t Window::GetScaledWidth() const
89+
{
90+
return extents.width * DPI.width;
91+
}
92+
93+
uint32_t Window::GetScaledHeight() const
94+
{
95+
return extents.height * DPI.height;
96+
}
97+
98+
WindowExtents Window::GetExtents() const
99+
{
100+
return extents;
101+
}
102+
103+
void* Window::GetWindow()
104+
{
105+
return window;
106+
}
16107

17108
void Window::Update()
18109
{
@@ -24,6 +115,32 @@ bool Window::WindowShouldClose()
24115
return glfwWindowShouldClose(window);
25116
}
26117

118+
void Window::ResizeWindow(WindowExtents newExtents)
119+
{
120+
wasResized = true;
121+
extents = newExtents;
122+
}
123+
124+
bool Window::WasResized() const
125+
{
126+
return wasResized;
127+
}
128+
129+
void Window::ResetWindowResized()
130+
{
131+
wasResized = false;
132+
}
133+
134+
void Window::EnableCursor()
135+
{
136+
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
137+
}
138+
139+
void Window::DisableCursor()
140+
{
141+
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
142+
}
143+
27144
MHArray<const char*> Window::GetRequiredExtensions()
28145
{
29146
uint32_t glfwExtensionCount = 0;
@@ -32,10 +149,14 @@ MHArray<const char*> Window::GetRequiredExtensions()
32149
return MHArray<const char*>(glfwExtensions, glfwExtensionCount);
33150
}
34151

35-
void Window::ResizeCallback(GLFWwindow* windowPtr, int width, int height)
152+
void Window::ToggleCursor(bool state)
36153
{
37-
auto windowContext = reinterpret_cast<Window*>(glfwGetWindowUserPointer(windowPtr));
38-
windowContext->wasResized = true;
39-
windowContext->extents = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
154+
state ? DisableCursor() : EnableCursor();
40155
}
156+
157+
void Window::WaitEvents()
158+
{
159+
glfwWaitEvents();
160+
}
161+
41162
} // namespace Siege

0 commit comments

Comments
 (0)