|
Aegis2D
Modular C++26 2D game engine for networked games
|
IRenderer drawing into a window with OpenGL. More...
#include <aegis2d/render/opengl/renderer.hpp>
Public Member Functions | |
| Renderer (const platform::Window &window, const ContextConfig &config={}) | |
Creates the OpenGL context of window and the sprite pipeline. | |
| Renderer (const Renderer &)=delete | |
| Renderer & | operator= (const Renderer &)=delete |
| Renderer (Renderer &&)=delete | |
| Renderer & | operator= (Renderer &&)=delete |
| ~Renderer () override | |
| Deletes the textures and buffers, then the context. | |
| TextureId | create_texture (const Image &image) override |
| Makes a texture of an image, to draw sprites with. | |
| void | update_texture (TextureId texture, const Image &image) override |
| Replaces the image of a texture, which keeps its identifier. | |
| TextureSize | texture_size (TextureId texture) const override |
| Size of a texture. | |
| void | begin_frame (Color clear) override |
| Starts a frame and fills the screen with one color. | |
| void | draw (const Sprite &sprite) override |
| Adds a sprite to the current frame. | |
| void | end_frame () override |
| Finishes the current frame and shows it. | |
| bool | in_frame () const noexcept override |
| Tells whether a frame is started. | |
| FrameStats | stats () const noexcept override |
| Counters of the last finished frame. | |
| void | set_camera (const std::optional< Camera > &camera) override |
| Places the sprites drawn from now on with a camera, or on the screen as given. | |
| const std::optional< Camera > & | camera () const noexcept override |
| The camera applied to the sprites drawn. | |
| void | set_logical_screen (const std::optional< LogicalScreen > &screen) |
| Draws at a fixed logical size, scaled to the window, or at the window's size. | |
| const std::optional< LogicalScreen > & | logical_screen () const noexcept |
| The logical screen in use. | |
| Letterbox | letterbox () const |
| Where the frame goes in the window's framebuffer, for the window's size now. | |
| Vec2 | to_logical (Vec2 window) const |
| Logical pixel under a point of the window, such as the mouse. | |
| void | set_draw_mode (DrawMode mode) noexcept |
| Chooses how the sprites are sent to the GPU, from the next end_frame(). | |
| DrawMode | draw_mode () const noexcept |
| How the sprites are sent to the GPU. | |
| void | set_gpu_timing (bool enabled) |
| Measures, or stops measuring, the time the GPU takes to draw each frame. | |
| std::optional< core::Duration > | gpu_time () const noexcept |
| GPU time of the latest measured frame. | |
| const Context & | context () const noexcept |
| OpenGL context of the renderer. | |
| std::span< const Sprite > | pending () const noexcept |
| Sprites collected in the current frame. | |
| void | capture_next_frame () noexcept |
| Asks for a copy of the next frame, as it is shown. | |
| const std::optional< Image > & | captured () const noexcept |
| The last frame captured. | |
Public Member Functions inherited from aegis2d::render::IRenderer | |
| IRenderer ()=default | |
| IRenderer (const IRenderer &)=delete | |
| IRenderer & | operator= (const IRenderer &)=delete |
| IRenderer (IRenderer &&)=delete | |
| IRenderer & | operator= (IRenderer &&)=delete |
| virtual | ~IRenderer () |
| Releases the backend resources. | |
IRenderer drawing into a window with OpenGL.
Owns the OpenGL Context of the window. begin_frame() clears the framebuffer; draw() collects sprites; end_frame() sorts them with batch_sprites(), sends the vertices of the whole frame to the GPU at once, then issues one draw call per batch (one texture on one layer) and swaps the buffers. Sprites are in logical pixels from the top-left corner: by default the window spans its size in screen coordinates; with a logical screen (set_logical_screen()), it spans that fixed size, scaled to the window with bars so the aspect ratio never changes when the window is resized. Textures are sampled with the nearest texel (crisp pixel art and text) and blended with their alpha.
|
explicit |
Creates the OpenGL context of window and the sprite pipeline.
| window | Window created with platform::GraphicsApi::OpenGL; it must outlive the renderer. |
| config | Swap behaviour of the context. |
| ContextError | if the context cannot be created (see Context). |
| ShaderError | if the sprite shader does not build on the driver. |
|
delete |
|
delete |
|
override |
Deletes the textures and buffers, then the context.
|
overridevirtual |
Starts a frame and fills the screen with one color.
| clear | Background color of the frame. |
| std::logic_error | if a frame is already started. |
Implements aegis2d::render::IRenderer.
|
overridevirtualnoexcept |
The camera applied to the sprites drawn.
Implements aegis2d::render::IRenderer.
|
noexcept |
Asks for a copy of the next frame, as it is shown.
The next end_frame() reads the framebuffer back before swapping, which stalls the GPU once: for screenshots and tests, not every frame.
|
noexcept |
The last frame captured.
|
noexcept |
OpenGL context of the renderer.
Makes a texture of an image, to draw sprites with.
Allowed inside and outside frames. Textures live as long as the renderer; a TextureManager makes sure each file is loaded once.
| image | RGBA image, not empty. |
| std::invalid_argument | if image is empty. |
Implements aegis2d::render::IRenderer.
|
overridevirtual |
Adds a sprite to the current frame.
| sprite | Sprite to draw; it is copied, the caller may reuse it. |
| std::logic_error | outside a frame. |
Implements aegis2d::render::IRenderer.
|
noexcept |
How the sprites are sent to the GPU.
|
overridevirtual |
Finishes the current frame and shows it.
| std::logic_error | outside a frame. |
Implements aegis2d::render::IRenderer.
|
noexcept |
GPU time of the latest measured frame.
|
overridevirtualnoexcept |
Tells whether a frame is started.
true between begin_frame() and end_frame(). Implements aegis2d::render::IRenderer.
| Letterbox aegis2d::render::opengl::Renderer::letterbox | ( | ) | const |
Where the frame goes in the window's framebuffer, for the window's size now.
|
noexcept |
The logical screen in use.
|
noexcept |
Sprites collected in the current frame.
|
overridevirtual |
Places the sprites drawn from now on with a camera, or on the screen as given.
Takes effect at the next draw(), so a frame can draw its world with the camera, then its HUD without it. The camera is copied: set it again after moving it.
| camera | The camera to apply; nothing to draw in screen coordinates. |
Implements aegis2d::render::IRenderer.
|
noexcept |
Chooses how the sprites are sent to the GPU, from the next end_frame().
Both modes draw the same picture; FrameStats::draw_calls tells the difference.
| mode | DrawMode::Batched (the default) or DrawMode::Naive. |
| void aegis2d::render::opengl::Renderer::set_gpu_timing | ( | bool | enabled | ) |
Measures, or stops measuring, the time the GPU takes to draw each frame.
Uses OpenGL timer queries around the drawing of end_frame(). The results come back a few frames later, without waiting for the GPU: gpu_time() gives the latest one.
| enabled | true to measure. |
| void aegis2d::render::opengl::Renderer::set_logical_screen | ( | const std::optional< LogicalScreen > & | screen | ) |
Draws at a fixed logical size, scaled to the window, or at the window's size.
With a logical screen, sprites are placed in its logical pixels whatever the window's size: the screen is scaled to fit the window (letterbox()), centered, and the rest of the window shows the bars; sprites outside the screen are cut at its edges. Takes effect at the next begin_frame().
| screen | Size, scale mode and bar color; nothing to draw in the window's own coordinates, as by default. |
| std::invalid_argument | if a component of the size is not above 0 or not finite. |
|
overridevirtualnoexcept |
Counters of the last finished frame.
Implements aegis2d::render::IRenderer.
|
overridevirtual |
Size of a texture.
| texture | Identifier returned by create_texture(). |
| std::out_of_range | if texture was not created by this renderer. |
Implements aegis2d::render::IRenderer.
Logical pixel under a point of the window, such as the mouse.
Turns window coordinates (platform::Mouse::position()) into the coordinates sprites are drawn in: the logical screen's, bars removed, or the window's own without a logical screen. With a camera, Camera::to_world() then gives the point in the world.
| window | Point in window coordinates, from the top-left corner. |
(0, 0) when the window shows nothing (minimized).
|
overridevirtual |
Replaces the image of a texture, which keeps its identifier.
Every sprite drawn with the texture shows the new image from then on, as for a texture reloaded while the game runs. Allowed inside and outside frames.
| texture | Identifier returned by create_texture(). |
| image | RGBA image, not empty; it may have another size. |
| std::out_of_range | if texture was not created by this renderer. |
| std::invalid_argument | if image is empty. |
Implements aegis2d::render::IRenderer.