Skip to content

Commit 80af942

Browse files
Raelrjonjondev
authored andcommitted
Separated GLFW functions from Window class, added input streaming
1 parent 5e8b7d9 commit 80af942

File tree

21 files changed

+603
-293
lines changed

21 files changed

+603
-293
lines changed

engine/render/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ renderSources := $(call rwildcard,$(renderSrcDir)/,*.cpp)
1414
renderObjects := $(call findobjs,$(renderSrcDir),$(renderBinDir),$(renderSources))
1515
renderDepends := $(patsubst %.o, %.d, $(call rwildcard,$(renderBinDir)/,*.o))
1616
renderBuildDir := $(renderBinDir)/build
17-
renderLibs := $(vendorDir)/zlib/build/lib/libz.a $(vendorDir)/libpng/build/libpng.a \
18-
$(vendorDir)/freetype/build/libfreetype.a
17+
renderLibs := $(vendorDir)/zlib/build/lib/libz.a $(vendorDir)/libpng/build/libpng.a $(vendorDir)/freetype/build/libfreetype.a
1918

2019
# Set shader build vars
2120
vertSources := $(call rwildcard,assets/shaders,*.vert)

engine/render/renderer/Renderer.cpp

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

1111
#include "Renderer.h"
1212

13-
#include <GLFW/glfw3.h>
14-
1513
#include "platform/vulkan/Font.h"
1614
#include "platform/vulkan/utils/Types.h"
1715
#include "statics/Statics.h"
@@ -28,15 +26,9 @@ Renderer::Renderer(Window& window) : window {window}
2826

2927
if (instance == nullptr) instance = this;
3028

31-
auto createWindowSurface = [&window](VkInstance instance, VkSurfaceKHR* surface) -> bool {
32-
return glfwCreateWindowSurface(instance,
33-
reinterpret_cast<GLFWwindow*>(window.GetWindow()),
34-
nullptr,
35-
surface) == VK_SUCCESS;
36-
};
37-
3829
auto extent = window.GetExtents();
39-
context.Init({extent.width, extent.height}, Window::GetRequiredExtensions, createWindowSurface);
30+
31+
context.Init(window);
4032

4133
DescriptorPool::AddPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024);
4234
DescriptorPool::AddPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1024);
@@ -129,7 +121,7 @@ void Renderer::DrawQuad(const Vec2 position,
129121
renderer2D.DrawQuad(position, scale, colour, rotation, zIndex, texture);
130122
}
131123

132-
void Renderer::DrawText2D(const char* const text,
124+
void Renderer::DrawText2D(const char* text,
133125
Vulkan::Font& font,
134126
const Vec2 position,
135127
const Vec2 scale,

engine/render/renderer/Renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Renderer
6464
float rotation = 0.f,
6565
const uint8_t zIndex = 0,
6666
Vulkan::Texture2D* texture = nullptr);
67-
void DrawText2D(const char* const text,
67+
void DrawText2D(const char* text,
6868
Vulkan::Font& font,
6969
const Vec2 position,
7070
const Vec2 scale,

engine/render/renderer/platform/vulkan/Context.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Context.h"
1111

1212
#include <utils/Logging.h>
13+
#include <GLFW/glfw3.h>
1314

1415
#include "render/renderer/Renderer.h"
1516

@@ -23,22 +24,24 @@ Context::~Context()
2324
vulkanInstance.~Instance();
2425
}
2526

26-
void Context::Init(const Utils::Extent2D& extent,
27-
Instance::GetSurfaceExtensionsCallback surfaceExtensionsCallback,
28-
const GetWindowSurfaceCallBack& windowSurfaceCallback)
27+
void Context::Init(Window& window)
2928
{
3029
CC_ASSERT(volkInitialize() == VK_SUCCESS, "Unable to initialise Volk!")
3130

32-
vulkanInstance = Instance(surfaceExtensionsCallback);
31+
vulkanInstance = Instance(window.GetRequiredExtensions());
32+
glfwCreateWindowSurface(vulkanInstance.GetInstance(),
33+
reinterpret_cast<GLFWwindow*>(window.GetRawWindow()),
34+
nullptr,
35+
&surface);
3336

34-
CC_ASSERT(windowSurfaceCallback(vulkanInstance.GetInstance(), &surface),
35-
"Unable to create window surface!")
37+
CC_ASSERT(surface != VK_NULL_HANDLE, "Unable to create surface!")
3638

3739
physicalDevice = PhysicalDevice(surface, vulkanInstance);
3840

3941
logicalDevice = LogicalDevice(surface, physicalDevice);
4042

41-
swapchain = Swapchain(extent);
43+
auto extents = window.GetExtents();
44+
swapchain = Swapchain({extents.width, extents.height});
4245
}
4346

4447
Context& Context::Get()

engine/render/renderer/platform/vulkan/Context.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@
1313
#include "LogicalDevice.h"
1414
#include "PhysicalDevice.h"
1515
#include "Swapchain.h"
16+
#include "window/Window.h"
1617

1718
namespace Siege::Vulkan
1819
{
1920
class Context
2021
{
2122
public:
2223

23-
typedef std::function<bool(VkInstance, VkSurfaceKHR*)> getSurfaceCallback;
24-
typedef getSurfaceCallback GetWindowSurfaceCallBack;
25-
2624
Context() = default;
2725
~Context();
2826

29-
void Init(const Utils::Extent2D& extent,
30-
Instance::GetSurfaceExtensionsCallback surfaceExtensionsCallback,
31-
const GetWindowSurfaceCallBack& windowSurfaceCallback);
27+
void Init(Window& window);
3228

3329
static Context& Get();
3430

engine/render/renderer/platform/vulkan/Instance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464

6565
namespace Siege::Vulkan
6666
{
67-
Instance::Instance(GetSurfaceExtensionsCallback surfaceExtensionsCallback)
67+
Instance::Instance(MHArray<const char*> surfaceExtensions)
6868
{
69-
CreateInstance(surfaceExtensionsCallback());
69+
CreateInstance(surfaceExtensions);
7070

7171
SETUP_UTILS_MESSENGER
7272
}

engine/render/renderer/platform/vulkan/Instance.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ class Instance
1919
{
2020
public:
2121

22-
typedef Siege::MHArray<const char*> (*surfaceCallback)(void);
23-
typedef surfaceCallback GetSurfaceExtensionsCallback;
24-
2522
Instance() = default;
26-
Instance(GetSurfaceExtensionsCallback surfaceExtensionsCallback);
23+
Instance(MHArray<const char*> surfaceExtensions);
2724
Instance(Instance&& other);
2825
~Instance();
2926

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ void Renderer2D::Initialise(const char* const globalDataName)
3838
.FromFragmentShader("assets/shaders/Quad2DInstanced.frag.spv")
3939
.WithTexture("texture", 0, 16)
4040
.WithDefaultTexture(&defaultTexture)
41-
.Build());
41+
.Build(),
42+
false);
4243

4344
textMaterial =
4445
Material(Shader::Builder()
@@ -56,7 +57,8 @@ void Renderer2D::Initialise(const char* const globalDataName)
5657
.FromFragmentShader("assets/shaders/Text2DInstanced.frag.spv")
5758
.WithTexture("texture", 0, 16)
5859
.WithDefaultTexture(&defaultTexture)
59-
.Build());
60+
.Build(),
61+
false);
6062

6163
gridMaterial =
6264
Material(Shader::Builder()

engine/window/Input.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,37 @@
88

99
#include "Input.h"
1010

11-
#include <GLFW/glfw3.h>
12-
#include <utils/math/Float.h>
11+
#include "window/platform/glfw/Input.h"
1312

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)
13+
namespace Siege
2114
{
22-
currentMouseCoordinates.x = static_cast<float>(xpos);
23-
currentMouseCoordinates.y = static_cast<float>(ypos);
24-
}
25-
26-
static GLFWwindow* AsGlfwWindow(void* window)
15+
Glfw::Window Input::primaryWindow = nullptr;
16+
int Input::latestKey {-1};
17+
int Input::latestChar {-1};
18+
bool Input::keyUpdated {false};
19+
bool Input::charUpdated {false};
20+
std::map<int, int> Input::keyMap;
21+
void Input::SetInputWindowSource(Glfw::Window window)
2722
{
28-
return reinterpret_cast<GLFWwindow*>(window);
29-
}
23+
primaryWindow = window;
3024

31-
namespace Siege
32-
{
25+
using namespace Glfw;
3326

34-
void Input::SetWindowPointer(Window* window)
35-
{
36-
// TODO: Fail if inputted pointer is nullptr
37-
windowPtr = window;
38-
glfwSetCursorPosCallback(AsGlfwWindow(window->GetWindow()), GetCursorPositionCallback);
27+
SetOnTextKeyPressedCallback(window, [](Window, unsigned int key) {
28+
latestChar = key;
29+
charUpdated = true;
30+
});
31+
32+
SetOnKeyPressedCallback(window, [](Window, int key, int scancode, int action, int mod) {
33+
latestKey = ((action == ACTION_PRESSED || action == ACTION_REPEAT) * key) +
34+
(action == ACTION_RELEASED * -1);
35+
keyUpdated = true;
36+
});
3937
}
38+
4039
bool Input::IsKeyDown(int key)
4140
{
42-
bool hasKey = (glfwGetKey(AsGlfwWindow(windowPtr->GetWindow()), key) == GLFW_PRESS);
41+
bool hasKey = Glfw::IsKeyDown(primaryWindow, key);
4342

4443
if (keyMap.find(key) != keyMap.end())
4544
{
@@ -54,8 +53,7 @@ bool Input::IsKeyDown(int key)
5453

5554
bool Input::IsKeyJustPressed(int key)
5655
{
57-
bool hasKey =
58-
glfwGetKey(reinterpret_cast<GLFWwindow*>(windowPtr->GetWindow()), key) == GLFW_PRESS;
56+
bool hasKey = Glfw::IsKeyDown(primaryWindow, key);
5957

6058
bool keyEntryExists = keyMap.find(key) != keyMap.end();
6159
if (keyEntryExists && hasKey)
@@ -77,20 +75,22 @@ bool Input::IsKeyJustPressed(int key)
7775
return false;
7876
}
7977

80-
const Input::MouseCoordinates& Input::GetCursorPosition()
78+
const MousePosition Input::GetCursorPosition()
8179
{
82-
return currentMouseCoordinates;
80+
return Glfw::GetMousePosition(primaryWindow);
8381
}
8482

85-
Input::MouseCoordinates Input::GetNormalisedMousePosition()
83+
int Input::GetLatestKey()
8684
{
87-
using namespace Siege::Float;
88-
return {Float::Clamp(Float::Normalise(currentMouseCoordinates.x, 0, windowPtr->GetWidth()),
89-
-1.f,
90-
1.f),
91-
Float::Clamp(Float::Normalise(currentMouseCoordinates.y, 0, windowPtr->GetHeight()),
92-
-1.f,
93-
1.f)};
85+
auto key = (keyUpdated * latestKey) + (!keyUpdated * -1);
86+
keyUpdated = false;
87+
return key;
9488
}
9589

90+
int Input::GetLatestChar()
91+
{
92+
auto key = (charUpdated * latestChar) + (!charUpdated * -1);
93+
charUpdated = false;
94+
return key;
95+
}
9696
} // namespace Siege

engine/window/Input.h

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,36 @@
99
#ifndef SIEGE_ENGINE_INPUT_H
1010
#define SIEGE_ENGINE_INPUT_H
1111

12-
#include <map>
13-
14-
#include "Window.h"
12+
#include <utils/collections/HeapArray.h>
1513

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
14+
#include <map>
2815

29-
#define KEY_ESCAPE 256
16+
#include "Key.h"
17+
#include "window/platform/glfw/Types.h"
18+
#include "window/utils/Types.h"
3019

3120
namespace Siege
3221
{
3322
class Input
3423
{
3524
public:
3625

37-
struct MouseCoordinates
38-
{
39-
double x;
40-
double y;
41-
};
42-
static void SetWindowPointer(Window* window);
26+
static void SetInputWindowSource(Glfw::Window window);
4327
static bool IsKeyDown(int key);
4428
static bool IsKeyJustPressed(int key);
4529

46-
static const MouseCoordinates& GetCursorPosition();
47-
static MouseCoordinates GetNormalisedMousePosition();
30+
static const MousePosition GetCursorPosition();
31+
static int GetLatestKey();
32+
static int GetLatestChar();
33+
34+
private:
35+
36+
static int latestKey;
37+
static int latestChar;
38+
static bool keyUpdated;
39+
static bool charUpdated;
40+
static Glfw::Window primaryWindow;
41+
static std::map<int, int> keyMap;
4842
};
4943
} // namespace Siege
5044

0 commit comments

Comments
 (0)