Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::HeadlessRenderer Class Referencefinal

IRenderer without window nor graphics API, which keeps the sprites of each frame. More...

#include <aegis2d/render/headless_renderer.hpp>

Inheritance diagram for aegis2d::render::HeadlessRenderer:
aegis2d::render::IRenderer

Public Member Functions

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.
 
std::span< const Sprite > sprites () const noexcept
 Sprites of the current frame, or of the last finished one between frames.
 
Color clear_color () const noexcept
 Background color of the current or last frame.
 
std::size_t textures () const noexcept
 Number of textures created.
 
std::size_t texture_updates (TextureId texture) const
 Number of update_texture() calls a texture went through.
 
std::uint64_t frames () const noexcept
 Number of frames finished since creation.
 
- 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.
 

Detailed Description

IRenderer without window nor graphics API, which keeps the sprites of each frame.

Lets rendering code run in unit tests, on CI runners without a display and in tools: a test draws a scene, then checks sprites() and clear_color(). Textures keep only their size; stats() counts the draw calls a GPU backend would make (batch_sprites()).

Member Function Documentation

◆ begin_frame()

void aegis2d::render::HeadlessRenderer::begin_frame ( Color  clear)
overridevirtual

Starts a frame and fills the screen with one color.

Parameters
clearBackground color of the frame.
Exceptions
std::logic_errorif a frame is already started.

Implements aegis2d::render::IRenderer.

◆ camera()

const std::optional< Camera > & aegis2d::render::HeadlessRenderer::camera ( ) const
overridevirtualnoexcept

The camera applied to the sprites drawn.

Returns
The value given to set_camera(); nothing by default.

Implements aegis2d::render::IRenderer.

◆ clear_color()

Color aegis2d::render::HeadlessRenderer::clear_color ( ) const
noexcept

Background color of the current or last frame.

Returns
The color given to the last begin_frame().

◆ create_texture()

TextureId aegis2d::render::HeadlessRenderer::create_texture ( const Image &  image)
overridevirtual

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.

Parameters
imageRGBA image, not empty.
Returns
Its identifier: 1 for the first texture, then 2, 3... never no_texture.
Exceptions
std::invalid_argumentif image is empty.

Implements aegis2d::render::IRenderer.

◆ draw()

void aegis2d::render::HeadlessRenderer::draw ( const Sprite &  sprite)
overridevirtual

Adds a sprite to the current frame.

Parameters
spriteSprite to draw; it is copied, the caller may reuse it.
Exceptions
std::logic_erroroutside a frame.

Implements aegis2d::render::IRenderer.

◆ end_frame()

void aegis2d::render::HeadlessRenderer::end_frame ( )
overridevirtual

Finishes the current frame and shows it.

Exceptions
std::logic_erroroutside a frame.

Implements aegis2d::render::IRenderer.

◆ frames()

std::uint64_t aegis2d::render::HeadlessRenderer::frames ( ) const
noexcept

Number of frames finished since creation.

Returns
The end_frame() count.

◆ in_frame()

bool aegis2d::render::HeadlessRenderer::in_frame ( ) const
overridevirtualnoexcept

Tells whether a frame is started.

Returns
true between begin_frame() and end_frame().

Implements aegis2d::render::IRenderer.

◆ set_camera()

void aegis2d::render::HeadlessRenderer::set_camera ( const std::optional< Camera > &  camera)
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.

Parameters
cameraThe camera to apply; nothing to draw in screen coordinates.

Implements aegis2d::render::IRenderer.

◆ sprites()

std::span< const Sprite > aegis2d::render::HeadlessRenderer::sprites ( ) const
noexcept

Sprites of the current frame, or of the last finished one between frames.

Returns
The sprites in submission order, as placed on screen by the camera; valid until the next begin_frame().

◆ stats()

FrameStats aegis2d::render::HeadlessRenderer::stats ( ) const
overridevirtualnoexcept

Counters of the last finished frame.

Returns
Zero counters before the first end_frame().

Implements aegis2d::render::IRenderer.

◆ texture_size()

TextureSize aegis2d::render::HeadlessRenderer::texture_size ( TextureId  texture) const
overridevirtual

Size of a texture.

Parameters
textureIdentifier returned by create_texture().
Returns
Its width and height in pixels.
Exceptions
std::out_of_rangeif texture was not created by this renderer.

Implements aegis2d::render::IRenderer.

◆ texture_updates()

std::size_t aegis2d::render::HeadlessRenderer::texture_updates ( TextureId  texture) const

Number of update_texture() calls a texture went through.

Parameters
textureIdentifier returned by create_texture().
Returns
0 for a texture never updated.
Exceptions
std::out_of_rangeif texture was not created by this renderer.

◆ textures()

std::size_t aegis2d::render::HeadlessRenderer::textures ( ) const
noexcept

Number of textures created.

Returns
The create_texture() count.

◆ update_texture()

void aegis2d::render::HeadlessRenderer::update_texture ( TextureId  texture,
const Image &  image 
)
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.

Parameters
textureIdentifier returned by create_texture().
imageRGBA image, not empty; it may have another size.
Exceptions
std::out_of_rangeif texture was not created by this renderer.
std::invalid_argumentif image is empty.

Implements aegis2d::render::IRenderer.


The documentation for this class was generated from the following file: