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

How a texture is cut into frames, numbered from 0. More...

#include <aegis2d/render/sprite_sheet.hpp>

Public Member Functions

 SpriteSheet (TextureSize texture)
 A sheet whose only frame is the whole texture.
 
 SpriteSheet (TextureSize texture, std::vector< PixelRect > frames)
 Frames at the given rectangles, for sheets packed by hand or by a tool.
 
std::size_t size () const noexcept
 Number of frames.
 
TextureSize texture () const noexcept
 Size of the texture.
 
PixelRect frame (std::size_t index) const
 Rectangle of a frame.
 
Rect source (std::size_t index) const
 Part of the texture to draw for a frame, as Sprite::source takes it.
 

Static Public Member Functions

static SpriteSheet grid (TextureSize texture, TextureSize frame, std::size_t count=0, int spacing=0)
 Frames of the same size laid out in a grid, numbered left to right, then top to bottom.
 

Detailed Description

How a texture is cut into frames, numbered from 0.

Animations show the frames in turn (ecs::Animation); a sprite draws one with source() as its Sprite::source:

// player.png: 4 frames of 16x9 pixels side by side.
const auto sheet = render::SpriteSheet::grid(renderer.texture_size(player), {16, 9});
renderer.draw({.texture = player, .position = ..., .size = ..., .source = sheet.source(2)});
static SpriteSheet grid(TextureSize texture, TextureSize frame, std::size_t count=0, int spacing=0)
Frames of the same size laid out in a grid, numbered left to right, then top to bottom.

TextureManager keeps the sheet of each texture (TextureManager::load_sheet()).

Constructor & Destructor Documentation

◆ SpriteSheet() [1/2]

aegis2d::render::SpriteSheet::SpriteSheet ( TextureSize  texture)
explicit

A sheet whose only frame is the whole texture.

Parameters
textureSize of the texture.
Exceptions
std::invalid_argumentif a dimension is not above 0.

◆ SpriteSheet() [2/2]

aegis2d::render::SpriteSheet::SpriteSheet ( TextureSize  texture,
std::vector< PixelRect >  frames 
)

Frames at the given rectangles, for sheets packed by hand or by a tool.

Parameters
textureSize of the texture.
framesRectangle of each frame, in the order of their numbers.
Exceptions
std::invalid_argumentif a texture dimension is not above 0, frames is empty, or a frame is empty or goes past the texture.

Member Function Documentation

◆ frame()

PixelRect aegis2d::render::SpriteSheet::frame ( std::size_t  index) const

Rectangle of a frame.

Parameters
indexFrame number, from 0.
Returns
The frame, in pixels.
Exceptions
std::out_of_rangeif index is not below size().

◆ grid()

static SpriteSheet aegis2d::render::SpriteSheet::grid ( TextureSize  texture,
TextureSize  frame,
std::size_t  count = 0,
int  spacing = 0 
)
static

Frames of the same size laid out in a grid, numbered left to right, then top to bottom.

Parameters
textureSize of the texture.
frameSize of one frame.
countNumber of frames; 0 for every whole frame of the grid, so a sheet whose last row is not full lists the frames it has.
spacingPixels between two neighbouring frames, on both axes; none around the grid.
Returns
The sheet.
Exceptions
std::invalid_argumentif a size is not above 0, spacing is negative, not even one frame fits, or count is more than the frames that fit.

◆ size()

std::size_t aegis2d::render::SpriteSheet::size ( ) const
noexcept

Number of frames.

Returns
At least 1.

◆ source()

Rect aegis2d::render::SpriteSheet::source ( std::size_t  index) const

Part of the texture to draw for a frame, as Sprite::source takes it.

Parameters
indexFrame number, from 0.
Returns
The frame in normalized texture coordinates.
Exceptions
std::out_of_rangeif index is not below size().

◆ texture()

TextureSize aegis2d::render::SpriteSheet::texture ( ) const
noexcept

Size of the texture.

Returns
The size given when the sheet was made.

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