Skip to content

Commit 25cfb2d

Browse files
committed
Reworked the Engine to allow for better abstraction and more features to be neatly organized.
There are plans to hopefully improve it and make UI Object management much more automatic and robust.
1 parent fd14dc0 commit 25cfb2d

File tree

10 files changed

+313
-198
lines changed

10 files changed

+313
-198
lines changed

include/camera.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Camera
5252
Camera(float posX = 0.0f, float posY = 0.0f, float posZ = 0.0f, float upX = 0.0f, float upY = 0.0f, float upZ = 1.0f, float yaw = YAW, float pitch = PITCH);
5353

5454
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
55-
glm::mat4 GetViewMatrix();
55+
glm::mat4 GetViewMatrix() const;
5656

5757
// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
5858
void ProcessKeyboard(const Camera_Movement& direction, float deltaTime);

include/common.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <SDL3/SDL_stdinc.h>
1414
#include <array>
1515

16-
class Engine;
16+
class Renderer;
1717

1818
struct BufferAndMemory {
1919
VkBuffer buffer;
@@ -42,7 +42,7 @@ struct TextureImageAndMemory {
4242
};
4343

4444
struct EngineSharedContext {
45-
Engine *engine;
45+
Renderer *engine;
4646

4747
VkDevice engineDevice;
4848
VkPhysicalDevice physicalDevice;
@@ -88,6 +88,8 @@ namespace UI {
8888

8989
class GenericElement {
9090
public:
91+
std::string id;
92+
9193
ElementType genericType;
9294
ElementType type;
9395

include/engine.hpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ui/button.hpp"
88
#include <future>
99
#include <mutex>
10+
#include <unordered_map>
1011
#ifndef VK_EXT_DEBUG_REPORT_EXTENSION_NAME
1112
#define VK_EXT_DEBUG_REPORT_EXTENSION_NAME "VK_EXT_debug_report"
1213
#endif
@@ -61,7 +62,7 @@ const std::vector<const char *> requiredInstanceExtensions = {
6162

6263
const std::vector<const char *> requiredLayerExtensions {
6364
#if DEBUG
64-
// "VK_LAYER_KHRONOS_validation",
65+
// "VK_LAYER_KHRONOS_validation",
6566
#endif
6667
};
6768

@@ -90,14 +91,15 @@ struct UIArrowsUBO {
9091
glm::vec3 Color;
9192
};
9293

94+
/* Got a weird bug with floats and found out it was because I didn't put alignas(16), I am now extremely paranoid and will put this in every single UBO with a vec2/float. */
9395
struct UIPanelUBO {
94-
glm::vec4 Dimensions;
95-
float Depth;
96+
alignas(16) glm::vec4 Dimensions;
97+
alignas(16) float Depth;
9698
};
9799

98100
struct UILabelPositionUBO {
99-
glm::vec2 PositionOffset;
100-
float Depth;
101+
alignas(16) glm::vec2 PositionOffset;
102+
alignas(16) float Depth;
101103
};
102104

103105
struct RenderModel {
@@ -172,10 +174,10 @@ struct RenderPass {
172174
PipelineAndLayout graphicsPipeline;
173175
};
174176

175-
class Engine {
177+
class Renderer {
176178
public:
177-
Engine(Settings &settings, Camera *primaryCam) : m_PrimaryCamera(primaryCam), m_Settings(settings) {};
178-
~Engine();
179+
Renderer(Settings &settings, const Camera *primaryCam) : m_PrimaryCamera(primaryCam), m_Settings(settings) {};
180+
~Renderer();
179181

180182
void SetMouseCaptureState(bool capturing);
181183

@@ -208,6 +210,9 @@ class Engine {
208210
// Fixed Updates are called 60 times a second.
209211
void RegisterFixedUpdateFunction(const std::function<void(std::array<bool, 322>)> &func);
210212

213+
/* DO NOT 'OR' MULTIPLE EVENT TYPES, REGISTER THE SAME FUNCTION WITH A DIFFERENT TYPE IF YOU WANT THAT. */
214+
void RegisterSDLEventListener(const std::function<void(SDL_Event *)> &func, SDL_EventType types);
215+
211216
void SetPrimaryCamera(Camera &cam);
212217

213218
Glyph GenerateGlyph(EngineSharedContext &sharedContext, FT_Face ftFace, char c, float &x, float &y, float depth);
@@ -256,9 +261,11 @@ class Engine {
256261
inline bool HasStencilAttachment(VkFormat format) {return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;};
257262

258263
/* Cameras, high-level stuff. */
259-
Camera *m_PrimaryCamera;
264+
const Camera *m_PrimaryCamera;
260265
Settings &m_Settings;
261266

267+
std::unordered_map<SDL_EventType, std::vector<std::function<void(SDL_Event *)>>> m_SDLEventListeners;
268+
262269
std::vector<Glyph> m_GlyphCache;
263270

264271
std::vector<RenderUIWaypoint> m_RenderUIWaypoints;
@@ -352,7 +359,36 @@ class Engine {
352359
std::vector<VkDeviceMemory> m_AllocatedMemory;
353360
std::vector<VkImageView> m_CreatedImageViews;
354361
std::vector<VkSampler> m_CreatedSamplers;
362+
};
363+
364+
class Engine {
365+
public:
366+
Engine() = default;
367+
368+
void InitRenderer(Settings &settings, const Camera *primaryCamera);
369+
370+
/* UI::Button listeners will receive events when any button is pressed, along with its ID. */
371+
/* Due to how it works, this function can be called before the renderer is initialized. */
372+
void RegisterUIButtonListener(const std::function<void(std::string)> listener);
373+
374+
Renderer *GetRenderer();
375+
376+
void StartRenderer();
377+
378+
void LoadUIFile(const std::string &name);
379+
380+
/* Register a button to the Engine, so that it can forward any clicks inside of it to the UIButton Listeners */
381+
void RegisterUIButton(UI::Button *button);
382+
void UnregisterUIButton(UI::Button *button);
383+
private:
384+
/* Systems */
385+
Renderer *m_Renderer = nullptr;
386+
Settings *m_Settings;
387+
388+
void CheckButtonClicks(SDL_Event *event);
355389

390+
std::vector<std::function<void(std::string)>> m_UIButtonListeners;
391+
std::vector<UI::Button *> m_UIButtons;
356392
};
357393

358394
#endif

include/util.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <array>
88
#include <vector>
99
#include <string>
10+
#include <rapidxml.hpp>
11+
12+
1013

1114
#define UTILASSERT(expr) (static_cast<bool>(expr) \
1215
? void(0) \
@@ -16,5 +19,11 @@ bool intersects(const glm::vec3 &origin, const glm::vec3 &fr
1619
std::vector<std::string> split(const std::string_view text, const char delim);
1720
bool endsWith(const std::string_view fullString, const std::string_view ending);
1821
glm::vec2 adjustScaleToFitType(UI::Scalable *self, glm::vec2 scale, UI::FitType fitType = UI::UNSET);
22+
rapidxml::xml_node<char>*getPropertiesNode(rapidxml::xml_node<char> *uiObjectNode);
23+
std::string getID(rapidxml::xml_node<char> *propertiesNode);
24+
glm::vec3 getColor(rapidxml::xml_node<char> *propertiesNode);
25+
glm::vec2 getPosition(rapidxml::xml_node<char> *propertiesNode);
26+
glm::vec2 getScale(rapidxml::xml_node<char> *propertiesNode);
27+
float getZDepth(rapidxml::xml_node<char> *propertiesNode, float depthDefault = 1.0f);
1928

2029
#endif

src/camera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float u
2020
}
2121

2222
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
23-
glm::mat4 Camera::GetViewMatrix()
23+
glm::mat4 Camera::GetViewMatrix() const
2424
{
2525
return glm::lookAt(Position, Position + Front, Up);
2626
}

0 commit comments

Comments
 (0)