forked from KhronosGroup/Vulkan-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimgui_system.h
More file actions
230 lines (193 loc) · 6.36 KB
/
imgui_system.h
File metadata and controls
230 lines (193 loc) · 6.36 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <vulkan/vulkan_raii.hpp>
#include <vulkan/vk_platform.h>
#include <stdexcept>
// Forward declarations
class Renderer;
class AudioSystem;
class AudioSource;
struct ImGuiContext;
/**
* @brief Class for managing ImGui integration with Vulkan.
*
* This class implements the ImGui integration as described in the GUI chapter:
* @see en/Building_a_Simple_Engine/GUI/02_imgui_setup.adoc
*/
class ImGuiSystem {
public:
/**
* @brief Default constructor.
*/
ImGuiSystem();
// Constructor-based initialization to replace separate Initialize() calls
ImGuiSystem(Renderer* renderer, uint32_t width, uint32_t height) {
if (!Initialize(renderer, width, height)) {
throw std::runtime_error("ImGuiSystem: initialization failed");
}
}
/**
* @brief Destructor for proper cleanup.
*/
~ImGuiSystem();
/**
* @brief Initialize the ImGui system.
* @param renderer Pointer to the renderer.
* @param width The width of the window.
* @param height The height of the window.
* @return True if initialization was successful, false otherwise.
*/
bool Initialize(Renderer* renderer, uint32_t width, uint32_t height);
/**
* @brief Clean up ImGui resources.
*/
void Cleanup();
/**
* @brief Start a new ImGui frame.
*/
void NewFrame();
/**
* @brief Render the ImGui frame.
* @param commandBuffer The command buffer to record rendering commands to.
*/
void Render(vk::raii::CommandBuffer & commandBuffer, uint32_t frameIndex);
/**
* @brief Handle mouse input.
* @param x The x-coordinate of the mouse.
* @param y The y-coordinate of the mouse.
* @param buttons The state of the mouse buttons.
*/
void HandleMouse(float x, float y, uint32_t buttons);
/**
* @brief Handle keyboard input.
* @param key The key code.
* @param pressed Whether the key was pressed or released.
*/
void HandleKeyboard(uint32_t key, bool pressed);
/**
* @brief Handle character input.
* @param c The character.
*/
void HandleChar(uint32_t c);
/**
* @brief Handle window resize.
* @param width The new width of the window.
* @param height The new height of the window.
*/
void HandleResize(uint32_t width, uint32_t height);
/**
* @brief Check if ImGui wants to capture keyboard input.
* @return True if ImGui wants to capture keyboard input, false otherwise.
*/
bool WantCaptureKeyboard() const;
/**
* @brief Check if ImGui wants to capture mouse input.
* @return True if ImGui wants to capture mouse input, false otherwise.
*/
bool WantCaptureMouse() const;
/**
* @brief Set the audio system reference for audio controls.
* @param audioSystem Pointer to the audio system.
*/
void SetAudioSystem(AudioSystem* audioSystem);
/**
* @brief Get the current PBR rendering state.
* @return True if PBR rendering is enabled, false otherwise.
*/
bool IsPBREnabled() const { return pbrEnabled; }
/**
* @brief Get the current ball-only rendering state.
* @return True if ball-only rendering is enabled, false otherwise.
*/
bool IsBallOnlyRenderingEnabled() const { return ballOnlyRenderingEnabled; }
/**
* @brief Get the current camera tracking state.
* @return True if camera tracking is enabled, false otherwise.
*/
bool IsCameraTrackingEnabled() const { return cameraTrackingEnabled; }
private:
// ImGui context
ImGuiContext* context = nullptr;
// Renderer reference
Renderer* renderer = nullptr;
// Audio system reference
AudioSystem* audioSystem = nullptr;
AudioSource* audioSource = nullptr;
AudioSource* debugPingSource = nullptr;
// Audio position tracking
float audioSourceX = 1.0f;
float audioSourceY = 0.0f;
float audioSourceZ = 0.0f;
// Vulkan resources
vk::raii::DescriptorPool descriptorPool = nullptr;
vk::raii::DescriptorSetLayout descriptorSetLayout = nullptr;
vk::raii::DescriptorSet descriptorSet = nullptr;
vk::raii::PipelineLayout pipelineLayout = nullptr;
vk::raii::Pipeline pipeline = nullptr;
vk::raii::Sampler fontSampler = nullptr;
vk::raii::Image fontImage = nullptr;
vk::raii::DeviceMemory fontMemory = nullptr;
vk::raii::ImageView fontView = nullptr;
// Per-frame dynamic buffers to avoid GPU/CPU contention when frames are in flight
std::vector<vk::raii::Buffer> vertexBuffers;
std::vector<vk::raii::DeviceMemory> vertexBufferMemories;
std::vector<vk::raii::Buffer> indexBuffers;
std::vector<vk::raii::DeviceMemory> indexBufferMemories;
std::vector<uint32_t> vertexCounts;
std::vector<uint32_t> indexCounts;
// Window dimensions
uint32_t width = 0;
uint32_t height = 0;
// Mouse state
float mouseX = 0.0f;
float mouseY = 0.0f;
uint32_t mouseButtons = 0;
// Initialization flag
bool initialized = false;
// PBR rendering state
bool pbrEnabled = true;
// Ball-only rendering and camera tracking state
bool ballOnlyRenderingEnabled = false;
bool cameraTrackingEnabled = false;
/**
* @brief Create Vulkan resources for ImGui.
* @return True if creation was successful, false otherwise.
*/
bool createResources();
/**
* @brief Create font texture.
* @return True if creation was successful, false otherwise.
*/
bool createFontTexture();
/**
* @brief Create descriptor set layout.
* @return True if creation was successful, false otherwise.
*/
bool createDescriptorSetLayout();
/**
* @brief Create descriptor pool.
* @return True if creation was successful, false otherwise.
*/
bool createDescriptorPool();
/**
* @brief Create descriptor set.
* @return True if creation was successful, false otherwise.
*/
bool createDescriptorSet();
/**
* @brief Create pipeline layout.
* @return True if creation was successful, false otherwise.
*/
bool createPipelineLayout();
/**
* @brief Create pipeline.
* @return True if creation was successful, false otherwise.
*/
bool createPipeline();
/**
* @brief Update vertex and index buffers.
*/
void updateBuffers(uint32_t frameIndex);
};