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

IRenderer drawing into a window with OpenGL. More...

#include <aegis2d/render/opengl/renderer.hpp>

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

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.
 

Detailed Description

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.

opengl::Renderer renderer{window};
renderer.set_logical_screen(render::LogicalScreen{.size = {1280.0F, 720.0F}});
IRenderer drawing into a window with OpenGL.
Definition renderer.hpp:47
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.
The game's screen at a fixed size, whatever the window's.
Definition letterbox.hpp:28
Vec2 size
Width and height in logical pixels, such as 1280 x 720.
Definition letterbox.hpp:30

Constructor & Destructor Documentation

◆ Renderer() [1/3]

aegis2d::render::opengl::Renderer::Renderer ( const platform::Window &  window,
const ContextConfig &  config = {} 
)
explicit

Creates the OpenGL context of window and the sprite pipeline.

Parameters
windowWindow created with platform::GraphicsApi::OpenGL; it must outlive the renderer.
configSwap behaviour of the context.
Exceptions
ContextErrorif the context cannot be created (see Context).
ShaderErrorif the sprite shader does not build on the driver.

◆ Renderer() [2/3]

aegis2d::render::opengl::Renderer::Renderer ( const Renderer &  )
delete

◆ Renderer() [3/3]

aegis2d::render::opengl::Renderer::Renderer ( Renderer &&  )
delete

◆ ~Renderer()

aegis2d::render::opengl::Renderer::~Renderer ( )
override

Deletes the textures and buffers, then the context.

Member Function Documentation

◆ begin_frame()

void aegis2d::render::opengl::Renderer::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::opengl::Renderer::camera ( ) const
overridevirtualnoexcept

The camera applied to the sprites drawn.

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

Implements aegis2d::render::IRenderer.

◆ capture_next_frame()

void aegis2d::render::opengl::Renderer::capture_next_frame ( )
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.

◆ captured()

const std::optional< Image > & aegis2d::render::opengl::Renderer::captured ( ) const
noexcept

The last frame captured.

Returns
Its pixels at the window's pixel size, from the top-left corner, or nothing before the first capture.

◆ context()

const Context & aegis2d::render::opengl::Renderer::context ( ) const
noexcept

OpenGL context of the renderer.

Returns
The context, for version and driver information.

◆ create_texture()

TextureId aegis2d::render::opengl::Renderer::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::opengl::Renderer::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.

◆ draw_mode()

DrawMode aegis2d::render::opengl::Renderer::draw_mode ( ) const
noexcept

How the sprites are sent to the GPU.

Returns
The mode given to set_draw_mode(); DrawMode::Batched by default.

◆ end_frame()

void aegis2d::render::opengl::Renderer::end_frame ( )
overridevirtual

Finishes the current frame and shows it.

Exceptions
std::logic_erroroutside a frame.

Implements aegis2d::render::IRenderer.

◆ gpu_time()

std::optional< core::Duration > aegis2d::render::opengl::Renderer::gpu_time ( ) const
noexcept

GPU time of the latest measured frame.

Returns
The time the GPU spent drawing the sprites of a recent frame, usually one to three frames before the last; nothing while timing is off or before the first result.

◆ in_frame()

bool aegis2d::render::opengl::Renderer::in_frame ( ) const
overridevirtualnoexcept

Tells whether a frame is started.

Returns
true between begin_frame() and end_frame().

Implements aegis2d::render::IRenderer.

◆ letterbox()

Letterbox aegis2d::render::opengl::Renderer::letterbox ( ) const

Where the frame goes in the window's framebuffer, for the window's size now.

Returns
The logical screen's area, or the whole framebuffer without one, with the framebuffer pixels per logical pixel.

◆ logical_screen()

const std::optional< LogicalScreen > & aegis2d::render::opengl::Renderer::logical_screen ( ) const
noexcept

The logical screen in use.

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

◆ operator=() [1/2]

Renderer & aegis2d::render::opengl::Renderer::operator= ( const Renderer &  )
delete

◆ operator=() [2/2]

Renderer & aegis2d::render::opengl::Renderer::operator= ( Renderer &&  )
delete

◆ pending()

std::span< const Sprite > aegis2d::render::opengl::Renderer::pending ( ) const
noexcept

Sprites collected in the current frame.

Returns
The sprites in submission order, as placed on screen by the camera; empty between frames.

◆ set_camera()

void aegis2d::render::opengl::Renderer::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.

◆ set_draw_mode()

void aegis2d::render::opengl::Renderer::set_draw_mode ( DrawMode  mode)
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.

Parameters
modeDrawMode::Batched (the default) or DrawMode::Naive.

◆ set_gpu_timing()

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.

Parameters
enabledtrue to measure.

◆ set_logical_screen()

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().

Parameters
screenSize, scale mode and bar color; nothing to draw in the window's own coordinates, as by default.
Exceptions
std::invalid_argumentif a component of the size is not above 0 or not finite.

◆ stats()

FrameStats aegis2d::render::opengl::Renderer::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::opengl::Renderer::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.

◆ to_logical()

Vec2 aegis2d::render::opengl::Renderer::to_logical ( Vec2  window) const

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.

const Vec2 cursor = renderer.to_logical(window.mouse().position());
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
Parameters
windowPoint in window coordinates, from the top-left corner.
Returns
The point in logical pixels, outside the logical screen when it is on a bar; (0, 0) when the window shows nothing (minimized).

◆ update_texture()

void aegis2d::render::opengl::Renderer::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: