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

What the game draws with, whatever the graphics API behind it. More...

#include <aegis2d/render/renderer.hpp>

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

Public Member Functions

 IRenderer ()=default
 
 IRenderer (const IRenderer &)=delete
 
IRenderer & operator= (const IRenderer &)=delete
 
 IRenderer (IRenderer &&)=delete
 
IRenderer & operator= (IRenderer &&)=delete
 
virtual ~IRenderer ()
 Releases the backend resources.
 
virtual TextureId create_texture (const Image &image)=0
 Makes a texture of an image, to draw sprites with.
 
virtual void update_texture (TextureId texture, const Image &image)=0
 Replaces the image of a texture, which keeps its identifier.
 
virtual TextureSize texture_size (TextureId texture) const =0
 Size of a texture.
 
virtual void begin_frame (Color clear)=0
 Starts a frame and fills the screen with one color.
 
virtual void draw (const Sprite &sprite)=0
 Adds a sprite to the current frame.
 
virtual void end_frame ()=0
 Finishes the current frame and shows it.
 
virtual bool in_frame () const noexcept=0
 Tells whether a frame is started.
 
virtual FrameStats stats () const noexcept=0
 Counters of the last finished frame.
 
virtual void set_camera (const std::optional< Camera > &camera)=0
 Places the sprites drawn from now on with a camera, or on the screen as given.
 
virtual const std::optional< Camera > & camera () const noexcept=0
 The camera applied to the sprites drawn.
 

Detailed Description

What the game draws with, whatever the graphics API behind it.

A frame is begin_frame(), any number of draw(), then end_frame(), which shows it. Sprites are drawn by layer, lowest first; within a layer, sprites of the same texture keep the order they were drawn in, and textures follow each other in the order they were created (see batch_sprites()), so overlapping sprites that must stack in a given order go on different layers. Calling them out of this order throws std::logic_error. With a camera (set_camera()), the sprites drawn are in world coordinates, placed on screen by the camera; without one, the default, they are in screen coordinates. Game code takes an IRenderer&, so it runs unchanged with the OpenGL backend (opengl::Renderer) or without any window (HeadlessRenderer, for tests and tools).

Constructor & Destructor Documentation

◆ IRenderer() [1/3]

aegis2d::render::IRenderer::IRenderer ( )
default

◆ IRenderer() [2/3]

aegis2d::render::IRenderer::IRenderer ( const IRenderer &  )
delete

◆ IRenderer() [3/3]

aegis2d::render::IRenderer::IRenderer ( IRenderer &&  )
delete

◆ ~IRenderer()

virtual aegis2d::render::IRenderer::~IRenderer ( )
virtual

Releases the backend resources.

Member Function Documentation

◆ begin_frame()

virtual void aegis2d::render::IRenderer::begin_frame ( Color  clear)
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ camera()

virtual const std::optional< Camera > & aegis2d::render::IRenderer::camera ( ) const
pure virtualnoexcept

The camera applied to the sprites drawn.

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

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ create_texture()

virtual TextureId aegis2d::render::IRenderer::create_texture ( const Image &  image)
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ draw()

virtual void aegis2d::render::IRenderer::draw ( const Sprite &  sprite)
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ end_frame()

virtual void aegis2d::render::IRenderer::end_frame ( )
pure virtual

Finishes the current frame and shows it.

Exceptions
std::logic_erroroutside a frame.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ in_frame()

virtual bool aegis2d::render::IRenderer::in_frame ( ) const
pure virtualnoexcept

Tells whether a frame is started.

Returns
true between begin_frame() and end_frame().

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ operator=() [1/2]

IRenderer & aegis2d::render::IRenderer::operator= ( const IRenderer &  )
delete

◆ operator=() [2/2]

IRenderer & aegis2d::render::IRenderer::operator= ( IRenderer &&  )
delete

◆ set_camera()

virtual void aegis2d::render::IRenderer::set_camera ( const std::optional< Camera > &  camera)
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ stats()

virtual FrameStats aegis2d::render::IRenderer::stats ( ) const
pure virtualnoexcept

Counters of the last finished frame.

Returns
Zero counters before the first end_frame().

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ texture_size()

virtual TextureSize aegis2d::render::IRenderer::texture_size ( TextureId  texture) const
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.

◆ update_texture()

virtual void aegis2d::render::IRenderer::update_texture ( TextureId  texture,
const Image &  image 
)
pure virtual

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.

Implemented in aegis2d::render::HeadlessRenderer, and aegis2d::render::opengl::Renderer.


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