GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::graphics::SpriteAtlas Class Reference

Expandable texture atlas for packing 2D images into a spritesheet to enable batch rendering. More...

#include <GREM/graphics/SpriteAtlas.hpp>

Classes

struct  Sprite
 Information about a specific image in the spritesheet. More...

Public Member Functions

 SpriteAtlas (Device &device, const SpriteAtlasOptions &options={})
 Construct an empty sprite atlas.
void clear () noexcept
 Clear all inserted sprites and images from the spritesheet.
SpriteID insertSprite (const resource::ImageView &image, const SpriteOptions &spriteOptions={})
 Add a new image to the spritesheet, possibly expanding the texture atlas in order to make space for it.
SpriteID createSubSprite (SpriteID baseSpriteID, Region2D region, SpriteOptions::Flip flip=SpriteOptions::NO_FLIP)
 Add a new sprite that is defined as a sub-region of an existing sprite.
const Sprite & getSprite (SpriteID id) const
 Get information about a specific image in the spritesheet.
Pair< vec2 > getSpriteTextureOffsetAndScale (SpriteID id) const noexcept
 Get the offset and scale of a specific sprite in the texture atlas.
const Texture & getAtlasTexture () const noexcept
 Get a reference to the internal texture atlas.
const SpriteAtlasOptions & getOptions () const noexcept
 Get the configuration options of this spritesheet.

Detailed Description

Expandable texture atlas for packing 2D images into a spritesheet to enable batch rendering.

Constructor & Destructor Documentation

◆ SpriteAtlas()

grem::graphics::SpriteAtlas::SpriteAtlas ( Device & device,
const SpriteAtlasOptions & options = {} )
inlineexplicit

Construct an empty sprite atlas.

Parameters
devicedevice to create the sprite atlas for. Must outlive the sprite atlas.
optionssprite atlas options, see SpriteAtlasOptions.

Member Function Documentation

◆ clear()

void grem::graphics::SpriteAtlas::clear ( )
inlinenoexcept

Clear all inserted sprites and images from the spritesheet.

◆ insertSprite()

SpriteID grem::graphics::SpriteAtlas::insertSprite ( const resource::ImageView & image,
const SpriteOptions & spriteOptions = {} )
inlinenodiscard

Add a new image to the spritesheet, possibly expanding the texture atlas in order to make space for it.

Parameters
imagenon-owning view over the image to copy into the spritesheet.
spriteOptionssprite options, see SpriteOptions.
Returns
an identifier for the inserted image.
Exceptions
graphics::Erroron failure to copy the image or expand the texture atlas.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
Note
The function will fail if the given image is not of a format that is compatible with the internal format of the texture atlas.
See also
createSubSprite()

◆ createSubSprite()

SpriteID grem::graphics::SpriteAtlas::createSubSprite ( SpriteID baseSpriteID,
Region2D region,
SpriteOptions::Flip flip = SpriteOptions::NO_FLIP )
inlinenodiscard

Add a new sprite that is defined as a sub-region of an existing sprite.

Parameters
baseSpriteIDidentifier for the existing sprite to create a sub-region of. Must have been obtained from a previous call to insert() or createSubSprite() on the same SpriteAtlas object as the one that this function is called on.
regionregion, in pixels, relative to the bottom left corner of the original sprite, where the new sprite will begin. Must fit within the original sprite image.
flipflags that describe how the sprite should be flipped when rendered.
Returns
an identifier for the new sub-sprite.
Exceptions
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
Note
This function does not grow the texture atlas.
See also
insert()

◆ getSprite()

const Sprite & grem::graphics::SpriteAtlas::getSprite ( SpriteID id) const
inlinenodiscard

Get information about a specific image in the spritesheet.

Parameters
ididentifier for the image to get the information of. Must have been obtained from a previous call to insert() or createSubSprite() on the same SpriteAtlas object as the one that this function is called on.
Returns
a read-only reference to the sprite information that is valid until the next call to insert(), or until the SpriteAtlas is destroyed, whichever happens first.

◆ getSpriteTextureOffsetAndScale()

Pair< vec2 > grem::graphics::SpriteAtlas::getSpriteTextureOffsetAndScale ( SpriteID id) const
inlinenodiscardnoexcept

Get the offset and scale of a specific sprite in the texture atlas.

Parameters
ididentifier for the image to get the information of. Must have been obtained from a previous call to insert() or createSubSprite() on the same SpriteAtlas object as the one that this function is called on.
Returns
a pair of:
  • the texture offset of the sprite, in normalized texture coordinates, and
  • the texture scale of the sprite, in normalized texture coordinates.

◆ getAtlasTexture()

const Texture & grem::graphics::SpriteAtlas::getAtlasTexture ( ) const
inlinenodiscardnoexcept

Get a reference to the internal texture atlas.

Returns
a read-only reference to the texture atlas containing the sprite image data that is valid until the next call to insert(), or until the SpriteAtlas is destroyed, whichever happens first.

◆ getOptions()

const SpriteAtlasOptions & grem::graphics::SpriteAtlas::getOptions ( ) const
inlinenodiscardnoexcept

Get the configuration options of this spritesheet.

Returns
a read-only reference to the current options.

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