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

OpenGL renderer: sprites, text and camera (client only). More...

Namespaces

namespace  opengl
 OpenGL backend of the renderer.
 

Classes

class  BitmapFont
 Monospaced font whose glyphs are cells of a texture, drawn as sprites. More...
 
class  Camera
 The part of the world shown on screen: where it looks, how close, within which bounds. More...
 
struct  Color
 RGBA color with components in [0, 1]. More...
 
struct  ControlsTableStyle
 Styles and spacing of a controls table. More...
 
struct  EffectSettings
 The player's choice about screen flashes and shake. More...
 
struct  EmitterConfig
 How an emitter spawns its particles, and how they change over their life. More...
 
struct  FlowLayout
 Boxes placed by flow_layout(). More...
 
class  FrameMetrics
 Durations of the last frames, and the frame rate they make. More...
 
struct  FrameStats
 Counters of the last finished frame. More...
 
struct  GlyphGrid
 Layout of a font's atlas: a grid of cells holding consecutive characters. More...
 
class  HeadlessRenderer
 IRenderer without window nor graphics API, which keeps the sprites of each frame. More...
 
struct  Image
 RGBA image with 8 bits per channel, ready to become a texture. More...
 
class  ImageError
 Thrown when an image cannot be read, decoded or written. More...
 
class  IRenderer
 What the game draws with, whatever the graphics API behind it. More...
 
struct  Letterbox
 Where the logical screen goes in the window's framebuffer. More...
 
struct  LogicalScreen
 The game's screen at a fixed size, whatever the window's. More...
 
struct  MapLayer
 Layer of tiles or of objects. More...
 
class  MetricsOverlay
 Overlay of the frame metrics, for developers: frames per second, frame times, entities, sprites and draw calls of the frame before, the memory of the process, the game's own lines (its network numbers...), and a bar per frame kept. More...
 
struct  MetricsOverlayStyle
 Where and how the metrics overlay is drawn. More...
 
struct  Particle
 One particle of an emitter. More...
 
class  ParticleEmitter
 Spawns particles at a point, moves them and removes them at the end of their life. More...
 
struct  PixelRect
 Rectangle of a texture, in whole pixels from its top-left corner. More...
 
struct  PlayerStyle
 How one player is drawn: the tint of their sprites (their character and shots), and their marker. More...
 
struct  Range
 Values picked at random between two bounds, both included. More...
 
struct  Rect
 Axis-aligned rectangle. More...
 
class  ScreenEffects
 Full-screen flashes and camera shake triggered by gameplay (explosions, hits). More...
 
struct  ShownCue
 A cue on screen and the time it has left. More...
 
struct  SoundCue
 What an important sound tells the player, shown while it plays. More...
 
class  SoundCues
 Captions and direction arrows for the sounds a deaf or hard-of-hearing player, or anyone playing muted, would miss. More...
 
struct  Sprite
 One textured quad to draw. More...
 
struct  SpriteBatch
 Run of consecutive sprites drawn with one texture on one layer: one draw call. More...
 
class  SpriteSheet
 How a texture is cut into frames, numbered from 0. More...
 
class  Starfield
 Stars scrolling across the screen in layers, each at its own speed: a parallax background for a side-scrolling shooter. More...
 
struct  StarLayer
 One layer of a starfield: stars at one depth. More...
 
struct  TextSize
 Size of the HUD and menu text chosen by the player, from 100% to 200%. More...
 
struct  TextStyle
 How a text is drawn. More...
 
class  TextureManager
 The game's textures, each loaded once and found by a name such as "player". More...
 
struct  TextureSize
 Width and height of a texture, in pixels. More...
 
struct  TileMap
 Orthogonal, finite map made in Tiled, saved as JSON. More...
 
class  TileMapError
 Thrown when a map or one of its tilesets cannot be read or is broken. More...
 
class  TileMapView
 A TileMap ready to draw: the textures of its tilesets and where each tile is in them. More...
 
struct  TileObject
 Shape placed on the map: a spawn point, a trigger area, a tile placed freely. More...
 
struct  Tileset
 Image cut into tiles; its tiles are the global ids first_gid to first_gid + tile_count - 1. More...
 

Typedefs

using Vec2 = aegis2d::Vec2
 2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
 
using TextureId = std::uint32_t
 Identifier of a texture loaded by the resource manager.
 
using TileProperty = std::variant< bool, std::int64_t, double, std::string >
 Value of a custom property: bool, int (and object, its id), float, or string (and color, file).
 
using TileProperties = std::map< std::string, TileProperty, std::less<> >
 Custom properties of a map, layer, object or tileset, by name.
 

Enumerations

enum class  ColorVision : std::uint8_t {
  Typical , Protanopia , Deuteranopia , Tritanopia ,
  Achromatopsia
}
 Kind of color vision. More...
 
enum class  ScaleMode : std::uint8_t { Fit , Integer }
 How a logical screen is scaled to the window. More...
 
enum class  MarkerShape : std::uint8_t { Circle , Triangle , Square , Diamond }
 Shape drawn next to a player, so players are told apart without color. More...
 

Functions

constexpr Color rgb (std::uint32_t hex, float alpha=1.0F) noexcept
 Color from its hexadecimal sRGB code, as design tools and palettes give it.
 
Color simulate (Color color, ColorVision vision) noexcept
 Color as seen with a color vision.
 
float relative_luminance (Color color) noexcept
 Relative luminance of a color, as defined by WCAG 2.
 
float contrast_ratio (Color first, Color second) noexcept
 WCAG 2 contrast ratio between two colors.
 
float lightness (Color color) noexcept
 Perceived lightness of a color, CIE L*.
 
float color_difference (Color first, Color second) noexcept
 Perceived difference between two colors, CIE76 ΔE*ab in CIELAB (D65).
 
Vec2 measure_controls (const BitmapFont &font, std::span< const platform::ControlLine > lines, const ControlsTableStyle &style) noexcept
 Size a controls table takes.
 
Vec2 draw_controls (IRenderer &renderer, const BitmapFont &font, std::span< const platform::ControlLine > lines, Vec2 position, const ControlsTableStyle &style)
 Draws a controls table: actions in a column, their keys aligned in a second one.
 
Letterbox letterbox (Vec2 logical, int width, int height, ScaleMode mode=ScaleMode::Fit) noexcept
 Fits a logical screen into a framebuffer, centered, keeping its aspect ratio.
 
Vec2 to_logical (const Letterbox &box, Vec2 framebuffer) noexcept
 Logical pixel under a point of the framebuffer, such as the mouse.
 
std::string metrics_text (const FrameMetrics &metrics, FrameStats stats, std::optional< std::size_t > entities, std::optional< std::size_t > memory=std::nullopt, std::span< const std::string > lines={})
 Text of the metrics overlay.
 
std::string_view name () noexcept
 Name of the module, used to check that it is linked.
 
PlayerStyle player_style (std::size_t player)
 Style of one player.
 
std::vector< std::uint8_t > marker_image (MarkerShape shape, int size)
 Image of a marker, to load as a texture and draw tinted with the player's color.
 
std::vector< SpriteBatch > batch_sprites (std::vector< Sprite > &sprites)
 Orders sprites for drawing and groups those that share a layer and a texture.
 
bool overlaps (const Rect &first, const Rect &second) noexcept
 Tells whether two rectangles overlap.
 
bool contains (const Rect &outer, const Rect &inner) noexcept
 Tells whether a rectangle lies within another.
 
FlowLayout flow_layout (std::span< const Vec2 > sizes, const Rect &area, Vec2 gap)
 Places boxes left to right in an area, starting a new row when one does not fit.
 

Variables

constexpr std::array color_visions
 Every ColorVision, to check a palette against each of them.
 
constexpr std::size_t max_players = 4
 Number of players a game can tell apart with player_styles.
 
constexpr std::array< PlayerStyle, max_players > player_styles
 Style of each player, the first player first.
 
constexpr TextureId no_texture = 0
 Texture identifier meaning "no texture": the sprite is a plain tinted rectangle.
 
constexpr std::array< StarLayer, 3 > default_star_layers
 Three layers, from the farthest: many small dim stars scrolling slowly, then fewer, larger, brighter and faster ones.
 
constexpr std::uint32_t flip_horizontal = 0x8000'0000U
 Bit of a global tile id set when the tile is flipped left to right.
 
constexpr std::uint32_t flip_vertical = 0x4000'0000U
 Bit of a global tile id set when the tile is flipped top to bottom.
 
constexpr std::uint32_t flip_diagonal = 0x2000'0000U
 Bit of a global tile id set when the tile is flipped along its diagonal.
 
constexpr std::uint32_t tile_id_mask = 0x0FFF'FFFFU
 Bits of a global tile id that are the id, without the flip bits.
 

Detailed Description

OpenGL renderer: sprites, text and camera (client only).

Typedef Documentation

◆ TextureId

using aegis2d::render::TextureId = typedef std::uint32_t

Identifier of a texture loaded by the resource manager.

◆ TileProperties

using aegis2d::render::TileProperties = typedef std::map<std::string, TileProperty, std::less<> >

Custom properties of a map, layer, object or tileset, by name.

◆ TileProperty

using aegis2d::render::TileProperty = typedef std::variant<bool, std::int64_t, double, std::string>

Value of a custom property: bool, int (and object, its id), float, or string (and color, file).

◆ Vec2

2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.

Enumeration Type Documentation

◆ ColorVision

enum class aegis2d::render::ColorVision : std::uint8_t
strong

Kind of color vision.

The three dichromacies are the strongest forms of the common deficiencies: about 8% of men and 0.5% of women see red and green less well (protan, deutan), tritan deficiencies are rare. Achromatopsia sees no color at all, as a grayscale screenshot.

Enumerator
Typical 

Typical trichromatic vision.

Protanopia 

No long-wavelength (red) cones.

Deuteranopia 

No medium-wavelength (green) cones.

Tritanopia 

No short-wavelength (blue) cones.

Achromatopsia 

No color: only lightness, as in grayscale.

◆ MarkerShape

enum class aegis2d::render::MarkerShape : std::uint8_t
strong

Shape drawn next to a player, so players are told apart without color.

Enumerator
Circle 

Disc.

Triangle 

Triangle pointing up.

Square 

Square.

Diamond 

Square standing on a corner.

◆ ScaleMode

enum class aegis2d::render::ScaleMode : std::uint8_t
strong

How a logical screen is scaled to the window.

Enumerator
Fit 

As large as the window allows: the whole window is used on one axis.

Integer 

The largest whole scale (x1, x2, x3...), so every logical pixel covers the same number of window pixels and pixel art stays even; Fit when the window is smaller than the logical screen.

Function Documentation

◆ batch_sprites()

std::vector< SpriteBatch > aegis2d::render::batch_sprites ( std::vector< Sprite > &  sprites)

Orders sprites for drawing and groups those that share a layer and a texture.

Sprites are sorted by layer, then by texture; the sort is stable, so sprites of the same layer and texture keep the order they were drawn in. Each run of sprites with the same layer and texture is one batch, which a backend draws with one call.

Parameters
spritesSprites of a frame, sorted in place.
Returns
The batches, in drawing order; empty for no sprite.

◆ color_difference()

float aegis2d::render::color_difference ( Color  first,
Color  second 
)
noexcept

Perceived difference between two colors, CIE76 ΔE*ab in CIELAB (D65).

About 2.3 is just noticeable side by side; 20 and more tells colors apart at a glance.

Parameters
firstOne color; alpha is ignored.
secondThe other color; alpha is ignored.
Returns
0 for the same color, about 100 between black and white.

◆ contains()

bool aegis2d::render::contains ( const Rect &  outer,
const Rect &  inner 
)
noexcept

Tells whether a rectangle lies within another.

Parameters
outerEnclosing rectangle, such as the screen.
innerRectangle to test.
Returns
true if inner is entirely inside outer, edges included.

◆ contrast_ratio()

float aegis2d::render::contrast_ratio ( Color  first,
Color  second 
)
noexcept

WCAG 2 contrast ratio between two colors.

WCAG asks for 3:1 between graphical objects and what surrounds them, 4.5:1 for text.

Parameters
firstOne color.
secondThe other color.
Returns
From 1 (same luminance) to 21 (black and white), whatever the order.

◆ draw_controls()

Vec2 aegis2d::render::draw_controls ( IRenderer &  renderer,
const BitmapFont &  font,
std::span< const platform::ControlLine >  lines,
Vec2  position,
const ControlsTableStyle &  style 
)

Draws a controls table: actions in a column, their keys aligned in a second one.

Parameters
rendererRenderer in a frame.
fontFont of the table.
linesLines from platform::control_lines().
positionTop-left corner of the table, in logical pixels.
styleStyles and spacing of the table.
Returns
The size of the table, as measure_controls().
Exceptions
std::logic_erroroutside a frame (as IRenderer::draw()).

◆ flow_layout()

FlowLayout aegis2d::render::flow_layout ( std::span< const Vec2 >  sizes,
const Rect &  area,
Vec2  gap 
)

Places boxes left to right in an area, starting a new row when one does not fit.

As text grows, the boxes that no longer fit move to the next row, so boxes of a layout never overlap. A box wider than the area gets a row of its own. Rows are as tall as their tallest box.

Parameters
sizesWidth and height of each box, such as BitmapFont::measure() of a text.
areaRectangle to fill from its top-left corner; its height is not enforced.
gapHorizontal space between boxes of a row, and vertical space between rows.
Returns
The position of each box and the size of the whole layout.

◆ letterbox()

Letterbox aegis2d::render::letterbox ( Vec2  logical,
int  width,
int  height,
ScaleMode  mode = ScaleMode::Fit 
)
noexcept

Fits a logical screen into a framebuffer, centered, keeping its aspect ratio.

The scale is the largest that fits on both axes (ScaleMode::Fit), or its whole part (ScaleMode::Integer, when at least 1). The area is rounded to whole pixels and centered; the bars take the rest, the extra pixel of an odd remainder going below or on the right.

Parameters
logicalSize of the logical screen; components must be above 0 and finite.
widthWidth of the framebuffer, in pixels.
heightHeight of the framebuffer, in pixels.
modeHow to scale.
Returns
The area of the logical screen; empty when the framebuffer or the logical size has no area.

◆ lightness()

float aegis2d::render::lightness ( Color  color)
noexcept

Perceived lightness of a color, CIE L*.

Two colors whose lightness is close look alike in grayscale.

Parameters
colorsRGB color; alpha is ignored.
Returns
0 for black to 100 for white.

◆ marker_image()

std::vector< std::uint8_t > aegis2d::render::marker_image ( MarkerShape  shape,
int  size 
)

Image of a marker, to load as a texture and draw tinted with the player's color.

White shape with a black outline on a transparent background, antialiased: the tint colors the shape and leaves the outline black, so the marker reads on any background. Pixels are RGBA, 8 bits per channel, not premultiplied, row by row from the top-left corner.

Parameters
shapeShape to draw, centered.
sizeWidth and height in pixels, 8 to 1024.
Returns
size * size * 4 bytes.
Exceptions
std::invalid_argumentif size is out of range.

◆ measure_controls()

Vec2 aegis2d::render::measure_controls ( const BitmapFont &  font,
std::span< const platform::ControlLine >  lines,
const ControlsTableStyle &  style 
)
noexcept

Size a controls table takes.

Parameters
fontFont of the table.
linesLines from platform::control_lines().
styleStyles and spacing of the table.
Returns
Width (widest action, gap, widest keys) and height of all rows; zero for no line.

◆ metrics_text()

std::string aegis2d::render::metrics_text ( const FrameMetrics &  metrics,
FrameStats  stats,
std::optional< std::size_t >  entities,
std::optional< std::size_t >  memory = std::nullopt,
std::span< const std::string >  lines = {} 
)

Text of the metrics overlay.

Parameters
metricsFrame times.
statsCounters of the frame drawn, as MetricsOverlay::game_stats() gives them.
entitiesNumber of entities, or nothing to leave the line out.
memoryMemory of the process in bytes (core::memory_use()), or nothing to leave the line out.
linesThe game's own lines, after the others, such as its network numbers.
Returns
Such as "FPS 60 16.7 MS MAX 18.2 MS\nENTITIES 42\nSPRITES 300 DRAW CALLS 4\nMEMORY 54.2 MB", then lines.

◆ name()

std::string_view aegis2d::render::name ( )
noexcept

Name of the module, used to check that it is linked.

Returns
"render", a view of a static string.

◆ overlaps()

bool aegis2d::render::overlaps ( const Rect &  first,
const Rect &  second 
)
noexcept

Tells whether two rectangles overlap.

Parameters
firstOne rectangle.
secondThe other rectangle.
Returns
true if they share some area; rectangles that only touch do not overlap.

◆ player_style()

PlayerStyle aegis2d::render::player_style ( std::size_t  player)

Style of one player.

Parameters
playerIndex of the player, from 0 to max_players - 1.
Returns
A copy of its entry of player_styles.
Exceptions
std::out_of_rangeif player is max_players or more.

◆ relative_luminance()

float aegis2d::render::relative_luminance ( Color  color)
noexcept

Relative luminance of a color, as defined by WCAG 2.

Parameters
colorsRGB color; alpha is ignored.
Returns
0 for black to 1 for white.

◆ rgb()

constexpr Color aegis2d::render::rgb ( std::uint32_t  hex,
float  alpha = 1.0F 
)
constexprnoexcept

Color from its hexadecimal sRGB code, as design tools and palettes give it.

Parameters
hex0xRRGGBB, such as 0x0072B2.
alphaAlpha component, 1 for opaque.
Returns
The color, each component divided by 255.

◆ simulate()

Color aegis2d::render::simulate ( Color  color,
ColorVision  vision 
)
noexcept

Color as seen with a color vision.

Dichromacies use the model of Machado, Oliveira and Fernandes (2009) at full severity, applied to linear RGB; achromatopsia keeps the relative luminance.

Parameters
colorsRGB color.
visionColor vision to simulate.
Returns
The sRGB color a typical viewer would need to see to perceive what vision perceives; alpha is kept.

◆ to_logical()

Vec2 aegis2d::render::to_logical ( const Letterbox &  box,
Vec2  framebuffer 
)
noexcept

Logical pixel under a point of the framebuffer, such as the mouse.

Undoes the letterbox: removes the bars and divides by the scale. A point on a bar gives a position outside (0, 0) to the logical size, which the game can ignore.

Parameters
boxArea of the logical screen in the framebuffer, from letterbox().
framebufferPoint in framebuffer pixels, from the top-left corner.
Returns
The point in logical pixels; (0, 0) when box is empty.

Variable Documentation

◆ color_visions

constexpr std::array aegis2d::render::color_visions
inlineconstexpr
Initial value:
{ColorVision::Typical, ColorVision::Protanopia,
ColorVision::Deuteranopia, ColorVision::Tritanopia,
ColorVision::Achromatopsia}

Every ColorVision, to check a palette against each of them.

◆ default_star_layers

constexpr std::array<StarLayer, 3> aegis2d::render::default_star_layers
inlineconstexpr
Initial value:
{{
{.count = 160, .speed = 25.0F, .size = 1.0F, .color = rgb(0x50506A)},
{.count = 90, .speed = 70.0F, .size = 2.0F, .color = rgb(0x8080A0)},
{.count = 40, .speed = 160.0F, .size = 3.0F, .color = rgb(0xC8C8E6)},
}}
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25

Three layers, from the farthest: many small dim stars scrolling slowly, then fewer, larger, brighter and faster ones.

◆ flip_diagonal

constexpr std::uint32_t aegis2d::render::flip_diagonal = 0x2000'0000U
inlineconstexpr

Bit of a global tile id set when the tile is flipped along its diagonal.

◆ flip_horizontal

constexpr std::uint32_t aegis2d::render::flip_horizontal = 0x8000'0000U
inlineconstexpr

Bit of a global tile id set when the tile is flipped left to right.

The three flip bits are masked out with gid & tile_id_mask to look the tile up.

◆ flip_vertical

constexpr std::uint32_t aegis2d::render::flip_vertical = 0x4000'0000U
inlineconstexpr

Bit of a global tile id set when the tile is flipped top to bottom.

◆ max_players

constexpr std::size_t aegis2d::render::max_players = 4
inlineconstexpr

Number of players a game can tell apart with player_styles.

◆ no_texture

constexpr TextureId aegis2d::render::no_texture = 0
inlineconstexpr

Texture identifier meaning "no texture": the sprite is a plain tinted rectangle.

◆ player_styles

constexpr std::array<PlayerStyle, max_players> aegis2d::render::player_styles
inlineconstexpr
Initial value:
{{
{.color = rgb(0x0072B2), .marker = MarkerShape::Circle},
{.color = rgb(0xEE6677), .marker = MarkerShape::Triangle},
{.color = rgb(0xF0E442), .marker = MarkerShape::Square},
{.color = rgb(0x66CCEE), .marker = MarkerShape::Diamond},
}}

Style of each player, the first player first.

Colors from the Okabe-Ito and Paul Tol colorblind-safe palettes: blue #0072B2, red #EE6677, yellow #F0E442, cyan #66CCEE. Any two stay apart (CIE76 ΔE above 30) for typical vision, protanopia, deuteranopia and tritanopia; their lightness steps (L* 46, 61, 89, 77) keep them apart in grayscale; each has a 3:1 contrast or more on a dark background. The marker shapes tell them apart even without any color.

◆ tile_id_mask

constexpr std::uint32_t aegis2d::render::tile_id_mask = 0x0FFF'FFFFU
inlineconstexpr

Bits of a global tile id that are the id, without the flip bits.