SDEngine
Game Engine
Loading...
Searching...
No Matches
View.hpp
Go to the documentation of this file.
1// TODO(docs): Add file-level Doxygen header
2// - @file View.hpp
3// - @brief View class - represents a renderable viewport/window content
4// - Relationship to Layer, LayerList, and VulkanWindow
5// - Aspect ratio and render mode handling
6#pragma once
7
8#include <imgui.h>
9#include <string>
10#include <vector>
11#include <vk_mem_alloc.h>
12
14#include "SD/core/LayerList.hpp"
15#include "SD/core/id_types.hpp"
18#include "VLA/Matrix.hpp"
19
20namespace sd {
21
22struct Application;
23
24// TODO(docs): Document ViewError enum
25// - Each error code's meaning
26// - How errors are propagated (std::expected)
32
33// TODO(docs): Document AspectMode enum
34// - Each mode's behavior
35// - When to use which mode
36enum class AspectMode {
37 FIXED_HEIGHT, // -aspect to +aspect
38 FIXED_WIDTH, // -1 to +1
39 BEST_FIT // Auto switch based on aspect
40};
41
42// TODO(docs): Document RenderMode enum
43enum class RenderMode {
44 SHADED,
46};
47
48// TODO(docs): Document View class thoroughly
49// - Purpose: A renderable region with its own layers and Vulkan resources
50// - Ownership model (layers, Vulkan resources)
51// - Integration with ImGui for viewport windows
52// - Aspect ratio management
53// - Render pass and framebuffer management
54// - Example: Creating a custom view with layers
56 View(const std::string& name, const EngineServices& services) :
57 m_name(name), m_view_id(0), m_vulkan_ctx(services.vulkan), m_imgui_ctx(services.imgui) {
58 m_camera_view_projection = VLA::Matrix4x4f::Ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
59 }
60 ~View();
61
62 void on_update(float dt) { m_layers.update(dt); }
63 void on_gui_render();
64
65 void on_event(EventVariant& e) { m_layers.on_event(e); }
66 void on_render(vk::CommandBuffer cmd);
67 void on_fixed_update(double dt) { m_layers.on_fixed_update(dt); }
68
69 // --- Layer management ---
70 template<typename T, typename... Args>
71 requires std::is_base_of_v<Layer, T>
72 T& push_layer(Args&&... args) {
73 auto& layer = m_layers.push_layer<T>(std::forward<Args>(args)...);
74 layer.view_id = m_view_id;
75 layer.view = this;
76 layer.app = m_app;
77 return layer;
78 }
79
80 template<typename T, typename... Args>
81 requires std::is_base_of_v<Layer, T>
83 auto& layer = m_layers.push_layer<T>(std::forward<Args>(args)...);
84 layer.stage_id = stageOrder;
85 layer.view_id = m_view_id;
86 layer.view = this;
87 layer.app = m_app;
88 return layer;
89 }
90
91 // --- Identity ---
92 const std::string& get_name() const { return m_name; }
93 [[nodiscard]] ViewId get_view_id() const { return m_view_id; }
94 [[nodiscard]] bool is_open() const { return m_open; }
95 void set_open(bool open) { m_open = open; }
96
97 // --- Layers ---
98 LayerList& get_layers() { return m_layers; }
99 const LayerList& get_layers() const { return m_layers; }
100
101 // --- Viewport state ---
102 vk::Extent2D get_extent() const { return m_extent; }
103 void resize(vk::Extent2D extent);
104
105 AspectMode get_aspect_mode() const { return m_aspect_mode; }
107 m_aspect_mode = mode;
108 resize(m_extent);
109 }
110
111 RenderMode get_render_mode() const { return m_render_mode; }
112 void set_render_mode(RenderMode mode) { m_render_mode = mode; }
113
114 const VLA::Matrix4x4f& get_projection() const { return m_camera_view_projection; }
115
118 bool changed = m_extent_changed;
119 m_extent_changed = false;
120 return changed;
121 }
122
123 // --- Diagnostics (ImGui region tracking) ---
124 ImVec2 get_content_region_pos() const { return m_content_region_pos; }
125 ImVec2 get_content_region_extent() const { return m_content_region_extent; }
126
127 // --- Viewport lifecycle ---
128 vk::Extent2D get_im_gui_extent();
129 vk::Format get_color_format() const { return m_color_format; }
130 vk::Format get_depth_format() const { return m_depth_format; }
131 vk::ImageView get_color_view() const { return *m_color_view; }
132 vk::ImageView get_depth_view() const { return *m_depth_view; }
133 VkDescriptorSet get_display_texture() const { return m_display_tex_ds; }
134
135 void create_viewport(VkExtent2D initialExtent = {1280, 720});
136 void destroy_viewport();
137
138
139 vk::Format find_depth_format();
140 void create_vulkan_resources();
141
142 std::string m_name;
144 bool m_open = true;
146
149 Application* m_app = nullptr;
150 // ImGui content region (updated each frame in OnGuiRender)
151 ImVec2 m_content_region_pos{0, 0};
152 ImVec2 m_content_region_extent{0, 0};
153
154 // Vulkan rendering resources
155 vk::Image m_color_image = VK_NULL_HANDLE;
156 VmaAllocation m_color_allocation = VK_NULL_HANDLE;
157 vk::UniqueImageView m_color_view;
158
159 vk::Image m_depth_image = VK_NULL_HANDLE;
160 VmaAllocation m_depth_allocation = VK_NULL_HANDLE;
161 vk::UniqueImageView m_depth_view;
162
163 VkDescriptorSet m_display_tex_ds = VK_NULL_HANDLE; // imgui::image
164
165 vk::Format m_color_format = vk::Format::eR8G8B8A8Unorm;
166 vk::Format m_depth_format = vk::Format::eUndefined;
168 vk::Extent2D m_extent = {1280, 720};
169 bool m_extent_changed = false;
170
171 AspectMode m_aspect_mode = AspectMode::BEST_FIT;
172 RenderMode m_render_mode = RenderMode::SHADED;
173
174 friend class Application; // needs mViewId assignment
175 friend class ViewManager;
176};
177
178
179} // namespace sd
Definition Application.hpp:22
AspectMode
Definition View.hpp:36
RenderMode
Definition View.hpp:43
ViewError
Definition View.hpp:27
@ VIEW_DOES_NOT_EXIST
Definition View.hpp:30
@ SUCCESS
Definition View.hpp:29
@ NAME_ALREADY_EXISTS
Definition View.hpp:28
Definition Application.hpp:42
Definition EngineServices.hpp:10
Definition EventVariant.hpp:19
Definition LayerList.hpp:15
Definition SDImGuiContext.hpp:27
Definition id_types.hpp:7
Definition ViewManager.hpp:21
Definition View.hpp:55
vk::Extent2D get_extent() const
Definition View.hpp:102
bool is_open() const
Definition View.hpp:94
const LayerList & get_layers() const
Definition View.hpp:99
VkDescriptorSet get_display_texture() const
Definition View.hpp:133
VulkanContext & m_vulkan_ctx
Definition View.hpp:147
RenderMode get_render_mode() const
Definition View.hpp:111
T & push_layer(Args &&... args)
Definition View.hpp:72
void set_aspect_mode(AspectMode mode)
Definition View.hpp:106
std::string m_name
Definition View.hpp:142
const VLA::Matrix4x4f & get_projection() const
Definition View.hpp:114
vk::Format get_depth_format() const
Definition View.hpp:130
VLA::Matrix4x4f m_camera_view_projection
Definition View.hpp:167
ImVec2 get_content_region_extent() const
Definition View.hpp:125
void on_fixed_update(double dt)
Definition View.hpp:67
vk::Format get_color_format() const
Definition View.hpp:129
LayerList m_layers
Definition View.hpp:143
T & push_layer(int stageOrder, Args &&... args)
Definition View.hpp:82
ViewId get_view_id() const
Definition View.hpp:93
View(const std::string &name, const EngineServices &services)
Definition View.hpp:56
bool consume_extent_changed()
Returns true (once) if the extent changed since the last call.
Definition View.hpp:117
void on_event(EventVariant &e)
Definition View.hpp:65
vk::ImageView get_color_view() const
Definition View.hpp:131
const std::string & get_name() const
Definition View.hpp:92
vk::UniqueImageView m_depth_view
Definition View.hpp:161
AspectMode get_aspect_mode() const
Definition View.hpp:105
void set_render_mode(RenderMode mode)
Definition View.hpp:112
ViewId m_view_id
Definition View.hpp:145
void on_update(float dt)
Definition View.hpp:62
vk::UniqueImageView m_color_view
Definition View.hpp:157
void set_open(bool open)
Definition View.hpp:95
SDImGuiContext & m_imgui_ctx
Definition View.hpp:148
vk::ImageView get_depth_view() const
Definition View.hpp:132
ImVec2 get_content_region_pos() const
Definition View.hpp:124
LayerList & get_layers()
Definition View.hpp:98
Definition VulkanContext.hpp:22
consteval U64 type_id_of()
Definition type_id.hpp:6