Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
renderer.hpp
Go to the documentation of this file.
1
3#pragma once
4
9#include <aegis2d/render/export.hpp>
14
15#include <cstdint>
16#include <functional>
17#include <memory>
18#include <optional>
19#include <span>
20#include <vector>
21
23
25enum class DrawMode : std::uint8_t {
28 Batched,
31 Naive,
32};
33
47class AEGIS2D_RENDER_EXPORT Renderer final : public IRenderer {
48public:
55 explicit Renderer(const platform::Window& window, const ContextConfig& config = {});
56
57 Renderer(const Renderer&) = delete;
58 Renderer& operator=(const Renderer&) = delete;
59 Renderer(Renderer&&) = delete;
62 ~Renderer() override;
63
65 TextureId create_texture(const Image& image) override;
67 void update_texture(TextureId texture, const Image& image) override;
69 [[nodiscard]] TextureSize texture_size(TextureId texture) const override;
71 void begin_frame(Color clear) override;
73 void draw(const Sprite& sprite) override;
75 void end_frame() override;
77 [[nodiscard]] bool in_frame() const noexcept override;
79 [[nodiscard]] FrameStats stats() const noexcept override;
81 void set_camera(const std::optional<Camera>& camera) override;
83 [[nodiscard]] const std::optional<Camera>& camera() const noexcept override;
84
94 void set_logical_screen(const std::optional<LogicalScreen>& screen);
95
98 [[nodiscard]] const std::optional<LogicalScreen>& logical_screen() const noexcept;
99
103 [[nodiscard]] Letterbox letterbox() const;
104
115 [[nodiscard]] Vec2 to_logical(Vec2 window) const;
116
121 void set_draw_mode(DrawMode mode) noexcept;
122
125 [[nodiscard]] DrawMode draw_mode() const noexcept;
126
132 void set_gpu_timing(bool enabled);
133
137 [[nodiscard]] std::optional<core::Duration> gpu_time() const noexcept;
138
141 [[nodiscard]] const Context& context() const noexcept;
142
146 [[nodiscard]] std::span<const Sprite> pending() const noexcept;
147
151 void capture_next_frame() noexcept;
152
156 [[nodiscard]] const std::optional<Image>& captured() const noexcept;
157
158private:
159 struct Pipeline;
160
161 std::reference_wrapper<const platform::Window> window_;
162 Context context_;
163 std::unique_ptr<Pipeline> pipeline_;
164 std::vector<Sprite> pending_;
165 FrameStats stats_;
166 std::optional<Image> captured_;
167 std::optional<LogicalScreen> logical_;
168 std::optional<Camera> camera_;
169 Letterbox frame_box_;
170 int frame_height_ = 0;
171 bool in_frame_ = false;
172 bool capture_ = false;
173};
174
175} // namespace aegis2d::render::opengl
2D camera: the part of a world larger than the screen that is shown.
Desktop window: creation, size and events. Drawing is left to a render backend.
Definition window.hpp:61
The part of the world shown on screen: where it looks, how close, within which bounds.
Definition camera.hpp:31
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
OpenGL core profile context of a window, with every OpenGL function loaded.
Definition context.hpp:77
IRenderer drawing into a window with OpenGL.
Definition renderer.hpp:47
void update_texture(TextureId texture, const Image &image) override
Replaces the image of a texture, which keeps its identifier.
TextureId create_texture(const Image &image) override
Makes a texture of an image, to draw sprites with.
TextureSize texture_size(TextureId texture) const override
Size of a texture.
Renderer & operator=(const Renderer &)=delete
bool in_frame() const noexcept override
Tells whether a frame is started.
Renderer(const platform::Window &window, const ContextConfig &config={})
Creates the OpenGL context of window and the sprite pipeline.
~Renderer() override
Deletes the textures and buffers, then the context.
Renderer(const Renderer &)=delete
void end_frame() override
Finishes the current frame and shows it.
Renderer & operator=(Renderer &&)=delete
void draw(const Sprite &sprite) override
Adds a sprite to the current frame.
void begin_frame(Color clear) override
Starts a frame and fills the screen with one color.
Fixed-timestep accumulator and monotonic stopwatch.
RGBA color used by the renderer.
Images in memory: loaded from files, generated by the engine, saved as PNG.
A fixed logical screen scaled into a window of any size, keeping its aspect ratio.
OpenGL backend of the renderer.
Definition context.hpp:18
DrawMode
How end_frame() sends the sprites to the GPU.
Definition renderer.hpp:25
@ Naive
One upload, texture bind and draw call per sprite, in the same order: what a renderer without batchin...
@ Batched
The vertices of the whole frame sent at once, then one draw call per run of sprites sharing a layer a...
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
std::uint32_t TextureId
Identifier of a texture loaded by the resource manager.
Definition renderer.hpp:32
Definition endpoint.hpp:85
OpenGL core context attached to a window, with its functions loaded.
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
Counters of the last finished frame.
Definition renderer.hpp:64
RGBA image with 8 bits per channel, ready to become a texture.
Definition image.hpp:25
Where the logical screen goes in the window's framebuffer.
Definition letterbox.hpp:38
The game's screen at a fixed size, whatever the window's.
Definition letterbox.hpp:28
One textured quad to draw.
Definition renderer.hpp:46
Width and height of a texture, in pixels.
Definition renderer.hpp:38
Settings of a new context.
Definition context.hpp:52
SDL3 desktop window with its events, keyboard, mouse and gamepads, independent of the graphics API.