Skip to main content

RenderTarget.ixx File

Defines the RenderTarget class, which encapsulates a destination for rendering operations. More...

Included Headers

#include <memory> #include <span> #include <stdexcept> #include <vector> #include <helios.rendering.Viewport>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

Classes Index

classRenderTarget

Represents a destination for rendering operations, such as a window's framebuffer. More...

Description

Defines the RenderTarget class, which encapsulates a destination for rendering operations.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file RenderTarget.ixx
3 * @brief Defines the RenderTarget class, which encapsulates a destination for rendering operations.
4 */
5module;
6
7#include <memory>
8#include <span>
9#include <stdexcept>
10#include <vector>
11
12export module helios.rendering.RenderTarget;
13
14import helios.rendering.Viewport;
15
16
17export namespace helios::rendering {
18
19 /**
20 * @brief Represents a destination for rendering operations, such as a window's framebuffer.
21 *
22 * A RenderTarget encapsulates a buffer (e.g., the default framebuffer or an off-screen texture)
23 * into which a scene can be rendered. It manages one or more `Viewport` objects, which define
24 * specific rectangular areas within the target.
25 *
26 * When the RenderTarget is resized, it notifies all its associated viewports so they can update
27 * their states accordingly (e.g., camera aspect ratio).
28 *
29 * @todo This implementation currently represents only the **default framebuffer**. Future extensions
30 * should allow it to represent arbitrary framebuffer objects (FBOs), using indices.
31 * @see glGenFramebuffers
32 */
34 private:
35 /**
36 * @brief The width of the render target in pixels.
37 */
38 unsigned int width_ = 0;
39
40 /**
41 * @brief The height of the render target in pixels.
42 */
43 unsigned int height_ = 0;
44
45 /**
46 * @brief Passkey instance to authorize `Viewport::setRenderTarget`.
47 */
49
50 /**
51 * @brief A list of viewports owned by this render target.
52 *
53 * @todo The list should be sorted after a meaningful key, like the viewport's z-Index.
54 */
55 std::vector<std::shared_ptr<helios::rendering::Viewport>> viewports_;
56
57 public:
58 /**
59 * @brief Default constructor.
60 */
61 RenderTarget() = default;
62
63 /**
64 * @brief Constructs a RenderTarget with an initial viewport and dimensions.
65 *
66 * @param viewport The default viewport for this RenderTarget. The viewport defines the
67 * rectangular area where the image is drawn.
68 * @param width The initial width of the render target in pixels.
69 * @param height The initial height of the render target in pixels.
70 *
71 * @throws std::invalid_argument if viewport is a nullptr.
72 *
73 * @see addViewport()
74 */
75 explicit RenderTarget(
76 std::shared_ptr<helios::rendering::Viewport> viewport,
77 unsigned int width = 0,
78 unsigned int height = 0)
79 : width_(width),
80 height_(height) {
81 if (!viewport) {
82 throw std::invalid_argument("Constructor received a null shared pointer");
83 }
84
85 addViewport(std::move(viewport));
86 }
87
88 /**
89 * @brief Adds a viewport to this render target and establishes a parent-child relationship.
90 *
91 * Makes sure the viewport is informed about any possible bounds updates by
92 * calling onRenderTargetResize afterwards.
93 *
94 * @param viewport A shared pointer to the `Viewport` to be added.
95 *
96 * @return The newly added viewport as a shared pointer.
97 *
98 * @throws std::invalid_argument if viewport is a nullptr, or if the viewport already
99 * has a parent RenderTarget.
100 */
101 std::shared_ptr<helios::rendering::Viewport> addViewport(
102 std::shared_ptr<helios::rendering::Viewport> viewport) {
103 if (!viewport) {
104 throw std::invalid_argument("addViewport() received a null shared pointer");
105 }
106
107 if (viewport->renderTarget()) {
108 throw std::invalid_argument("Viewport already belongs to a RenderTarget.");
109 }
110
111 viewport->setRenderTarget(&*this, viewportKey_);
112
113 // make sure viewport is initialized with the render target's size
114 viewport->onRenderTargetResize(width_, height_);
115
116 viewports_.emplace_back(viewport);
117
118 return viewport;
119 }
120
121 /**
122 * @brief Returns a read-only view of all viewports owned by this RenderTarget.
123 *
124 * @return A span of shared pointers to the viewports.
125 */
126 [[nodiscard]] std::span<const std::shared_ptr<helios::rendering::Viewport>> viewports() noexcept {
127 return viewports_;
128 }
129
130 /**
131 * @brief Resizes this RenderTarget to the specified dimensions and propagates the change to every viewport.
132 *
133 * @param width The new width of the render target in pixels.
134 * @param height The new height of the render target in pixels.
135 *
136 * @todo A LayoutManager could be introduced to manage the arrangement of multiple viewports.
137 *
138 * @see helios::rendering::Viewport::onRenderTargetResize()
139 */
140 void setSize(unsigned int width, unsigned int height) noexcept {
141 width_ = width;
142 height_ = height;
143
144 for (const auto& it : viewports_) {
145 it->onRenderTargetResize(width_, height_);
146 }
147 }
148
149 /**
150 * @brief Gets the width of the render target.
151 *
152 * @return The width in pixels.
153 */
154 [[nodiscard]] unsigned int width() const noexcept {
155 return width_;
156 }
157
158 /**
159 * @brief Gets the height of the render target.
160 *
161 * @return The height in pixels.
162 */
163 [[nodiscard]] unsigned int height() const noexcept {
164 return height_;
165 }
166
167 };
168
169} // namespace helios::rendering

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.