Skip to content

Commit 6171e99

Browse files
Rinziikarnkaul
andauthored
Working on comments (#49)
* More comments added. Still WIP. * Minimize Gui interface; update docs * Move util/ring_buffer to vk/rotator, document all util types Also rename `RingBuffer` to `Rotator`. * Move DeferQueue from util to vk * Document scene, move Transform from scene to util - Remove rect.hpp (unused). - `Transform` was initially more complex: contained child transforms too. Since it is now a pure data component agnostic to the scene, it has been moved to util. * Document glfw * Document render Co-authored-by: Karn Kaul <karnkaul@gmail.com>
1 parent 8a91298 commit 6171e99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1133
-112
lines changed

lib/engine/src/engine.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ struct DearImGui : Gui {
129129
pool = init_imgui(info);
130130
}
131131

132-
void new_frame() final {
132+
void new_frame() {
133133
if (state == State::eEndFrame) { end_frame(); }
134134
ImGui_ImplVulkan_NewFrame();
135135
ImGui_ImplGlfw_NewFrame();
136136
ImGui::NewFrame();
137137
state = State::eEndFrame;
138138
}
139139

140-
void end_frame() final {
140+
void end_frame() {
141141
// ImGui::Render calls ImGui::EndFrame
142142
ImGui::Render();
143143
state = State::eNewFrame;
@@ -151,9 +151,9 @@ struct RenderWindow {
151151
Vulkan vulkan;
152152
Gfx gfx;
153153
Renderer renderer;
154-
std::unique_ptr<Gui> gui;
154+
std::unique_ptr<DearImGui> gui;
155155

156-
RenderWindow(UniqueWin window, std::unique_ptr<Gui> gui, std::uint8_t msaa, bool validation)
156+
RenderWindow(UniqueWin window, std::unique_ptr<DearImGui> gui, std::uint8_t msaa, bool validation)
157157
: window(std::move(window)), vulkan(GlfwWsi{this->window}, validation), gfx(vulkan.gfx()),
158158
renderer(gfx, this->window, gui.get(), Renderer::CreateInfo{command_buffers_v, msaa}), gui(std::move(gui)) {}
159159
};

lib/glfw/include/facade/glfw/glfw.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,106 @@
99
#include <vector>
1010

1111
namespace facade {
12+
///
13+
/// \brief Represents a GLFW instance.
14+
///
15+
/// GLFW is initialized and shutdown agnostic of any windows created.
16+
/// GLFW polls all windows for events simultaneously (ergo, they all share the same delta-time).
17+
/// The required Vulkan Device extensions are specific to a platform, independent of any windows in use.
18+
/// Glfw handles all these responsibilities.
19+
///
1220
struct Glfw {
1321
struct Window;
1422
struct Deleter;
1523
struct State;
1624

25+
///
26+
/// \brief Whether this instance is active.
27+
///
28+
/// Used for move semantics.
29+
///
1730
bool active{};
1831

32+
///
33+
/// \brief Obtain the Vulkan Device extensions for this windowing platform.
34+
///
1935
std::vector<char const*> vk_extensions() const;
36+
///
37+
/// \brief Poll all windows for events.
38+
///
2039
void poll_events();
40+
///
41+
/// \brief Reset delta time.
42+
///
2143
void reset_dt();
2244

2345
bool operator==(Glfw const&) const = default;
2446
};
2547

48+
///
49+
/// \brief RAII GLFW Window
50+
///
2651
using UniqueWin = Unique<Glfw::Window, Glfw::Deleter>;
2752

53+
///
54+
/// \brief A snapshot of the current window / input state.
55+
///
2856
struct Glfw::State {
57+
///
58+
/// \brief Input state.
59+
///
2960
Input input{};
61+
///
62+
/// \brief Paths of files dropped on the window this frame.
63+
///
64+
/// All paths are supplied in generic form (/path/to/file).
65+
///
3066
std::vector<std::string> file_drops{};
67+
///
68+
/// \brief Delta-time between polls.
69+
///
3170
float dt{};
3271

72+
///
73+
/// \brief Obtain the filename of a file drop path.
74+
/// \param path Path to extract filename from
75+
///
76+
/// Passed path must be in generic form.
77+
///
3378
static std::string to_filename(std::string_view path);
3479
};
3580

81+
///
82+
/// \brief Represents a GLFW Window.
83+
///
3684
struct Glfw::Window {
85+
///
86+
/// \brief Shared Glfw instance.
87+
///
3788
std::shared_ptr<Glfw> glfw{};
89+
///
90+
/// \brief GLFW window associated with this instance.
91+
///
3892
GLFWwindow* win{};
3993

94+
///
95+
/// \brief Create a new window.
96+
/// \returns RAII Glfw::Window
97+
///
4098
static UniqueWin make();
4199

100+
///
101+
/// \brief Obtain the extent (size) of the window.
102+
///
42103
glm::uvec2 window_extent() const;
104+
///
105+
/// \brief Obtain the extent (size) of the framebuffer.
106+
///
43107
glm::uvec2 framebuffer_extent() const;
44108

109+
///
110+
/// \brief Obtain the state since the last poll.
111+
///
45112
State const& state() const;
46113

47114
operator GLFWwindow*() const { return win; }

lib/glfw/include/facade/glfw/glfw_wsi.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
#include <facade/vk/wsi.hpp>
44

55
namespace facade {
6+
///
7+
/// \brief Concrete Wsi using GLFW.
8+
///
69
struct GlfwWsi : Wsi {
10+
///
11+
/// \brief The window in use.
12+
///
713
Glfw::Window window{};
814

9-
GlfwWsi(Glfw::Window window) : window(window) {}
15+
explicit GlfwWsi(Glfw::Window window) : window(window) {}
1016

1117
std::vector<char const*> extensions() const final;
1218
vk::UniqueSurfaceKHR make_surface(vk::Instance instance) const final;

lib/glfw/include/facade/glfw/input.hpp

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,71 @@
11
#pragma once
2+
#include <facade/util/enum_array.hpp>
23
#include <glm/vec2.hpp>
34
#include <array>
45
#include <string_view>
56

67
struct GLFWwindow;
78

89
namespace facade {
9-
enum class Action { eNone, ePress, eHeld, eRepeat, eRelease };
10-
11-
inline constexpr std::string_view action_name(Action action) {
12-
switch (action) {
13-
default:
14-
case Action::eNone: return "none";
15-
case Action::ePress: return "press";
16-
case Action::eHeld: return "held";
17-
case Action::eRepeat: return "repeat";
18-
case Action::eRelease: return "release";
19-
}
20-
}
10+
///
11+
/// \brief Kinds of Key / Button actions.
12+
///
13+
enum class Action { eNone, ePress, eHeld, eRepeat, eRelease, eCOUNT_ };
14+
constexpr auto action_str = EnumArray<Action, std::string_view>{
15+
"none", "press", "held", "repeat", "release",
16+
};
2117

18+
///
19+
/// \brief Storage for Key (or Button) state.
20+
/// \param Capacity size of state array
21+
///
2222
template <int Capacity>
2323
class KeyState {
2424
public:
2525
static constexpr int capacity_v{Capacity};
2626

27+
///
28+
/// \brief Obtain the action corresponding to key
29+
/// \param key GLFW key / button Id
30+
/// \returns Action for key
31+
///
2732
Action action(int key) const {
2833
if (key < 0 || key > capacity_v) { return Action::eNone; }
2934
return m_key_actions[static_cast<std::size_t>(key)];
3035
}
3136

37+
///
38+
/// \brief Check if a key was pressed.
39+
/// \param key GLFW key / button Id
40+
/// \returns true If key was pressed
41+
///
3242
bool pressed(int key) const { return action(key) == Action::ePress; }
43+
///
44+
/// \brief Check if a key was released.
45+
/// \param key GLFW key / button Id
46+
/// \returns true If key was released
47+
///
3348
bool released(int key) const { return action(key) == Action::eRelease; }
49+
///
50+
/// \brief Check if a key was repeated.
51+
/// \param key GLFW key / button Id
52+
/// \returns true If key was repeated
53+
///
3454
bool repeated(int key) const { return action(key) == Action::eRepeat; }
3555

56+
///
57+
/// \brief Check if a key is held.
58+
/// \param key GLFW key / button Id
59+
/// \returns true If key is held
60+
///
3661
bool held(int key) const {
3762
auto const ret = action(key);
3863
return ret == Action::ePress || ret == Action::eHeld || ret == Action::eRepeat;
3964
}
4065

66+
///
67+
/// \brief Update actions and swap states.
68+
///
4169
virtual void next_frame() {
4270
auto prev = m_key_actions;
4371
m_key_actions = {};
@@ -49,24 +77,61 @@ class KeyState {
4977
}
5078
}
5179

80+
///
81+
/// \brief Associate action with key.
82+
/// \param key GLFW key / button Id
83+
/// \param action Action to associate
84+
///
5285
void on_key(int key, Action action) { m_key_actions[static_cast<std::size_t>(key)] = action; }
5386

5487
private:
5588
std::array<Action, static_cast<std::size_t>(capacity_v)> m_key_actions{};
5689
};
5790

91+
///
92+
/// \brief Keyboard State
93+
///
5894
class Keyboard : public KeyState<512> {};
5995

96+
///
97+
/// \brief Mouse State
98+
///
6099
class Mouse : public KeyState<8> {
61100
public:
101+
///
102+
/// \brief Wrapper for on_key.
103+
///
62104
void on_button(int button, Action action) { on_key(button, action); }
105+
///
106+
/// \brief Store cursor position.
107+
/// \param position Cursor position.
108+
///
63109
void on_position(glm::vec2 position) { m_position = position; }
64-
void on_scroll(glm::vec2 scroll) { m_scroll += scroll; }
65-
110+
///
111+
/// \brief Add scroll delta.
112+
/// \param delta Amount scrolled in xy directions
113+
///
114+
void on_scroll(glm::vec2 delta) { m_scroll += delta; }
115+
116+
///
117+
/// \brief Obtain the cursor position.
118+
/// \returns Cursor position
119+
///
66120
glm::vec2 position() const { return m_position; }
121+
///
122+
/// \brief Obtain the difference in cursor positions from last state.
123+
/// \returns Difference in cursor positions.
124+
///
67125
glm::vec2 delta_pos() const { return m_position - m_prev_position; }
126+
///
127+
/// \brief Obtain the amount scrolled.
128+
/// \returns Scroll amount
129+
///
68130
glm::vec2 scroll() const { return m_scroll; }
69131

132+
///
133+
/// \brief Update and swap state.
134+
///
70135
void next_frame() override {
71136
KeyState<8>::next_frame();
72137
m_scroll = {};

lib/render/include/facade/render/gui.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <facade/vk/gfx.hpp>
55

66
namespace facade {
7+
///
8+
/// \brief Abstract interface for GUI (concrete DearImGui authored and owned by Engine).
9+
///
710
class Gui {
811
public:
912
struct InitInfo {
@@ -17,9 +20,6 @@ class Gui {
1720
virtual ~Gui() = default;
1821

1922
virtual void init(InitInfo const& info) = 0;
20-
21-
virtual void new_frame() = 0;
22-
virtual void end_frame() = 0;
2323
virtual void render(vk::CommandBuffer cb) = 0;
2424
};
2525
} // namespace facade

0 commit comments

Comments
 (0)