Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::SpriteSheet Class Reference

Loads an image and exposes individual sprite regions. More...

#include <spk_sprite_sheet.hpp>

Inheritance diagram for spk::SpriteSheet:
Inheritance graph
Collaboration diagram for spk::SpriteSheet:
Collaboration graph

Public Types

using Sprite = Image::Region
 Alias describing a rectangular sprite region.
Public Types inherited from spk::Image
using Format = OpenGL::TextureObject::Format
 Pixel format accepted by the underlying texture.
using Filtering = OpenGL::TextureObject::Filtering
 Filtering modes for minification and magnification.
using Filter = OpenGL::TextureObject::Filter
 Pairing of min and mag filtering options.
using Wrap = OpenGL::TextureObject::Wrap
 Texture coordinate wrapping modes.
using Wrapper = OpenGL::TextureObject::Wrapper
 Pairing of wrap modes for the U and V axes.
using Mipmap = OpenGL::TextureObject::Mipmap
 Mipmap generation policy.
Public Types inherited from spk::OpenGL::TextureObject
enum class  Format : GLenum {
  RGB = GL_RGB , RGBA = GL_RGBA , BGR = GL_BGR , BGRA = GL_BGRA ,
  GreyLevel = GL_RED , DualChannel = GL_RG , DepthComponent = GL_DEPTH_COMPONENT , DepthStencil = GL_DEPTH_STENCIL ,
  Error = GL_NONE
}
 Supported GPU pixel formats.
enum class  Filtering : GLenum { Nearest = GL_NEAREST , Linear = GL_LINEAR }
 Texture sampling algorithms for minification/magnification.
enum class  Wrap : GLenum { Repeat = GL_REPEAT , ClampToEdge = GL_CLAMP_TO_EDGE , ClampToBorder = GL_CLAMP_TO_BORDER }
 Texture coordinate wrapping strategies.
using Mipmap = spk::ActivationStatus
 Indicates whether mipmaps are active.
Public Types inherited from spk::ActivableObject
using Contract = StatefulObject<ActivationStatus>::Contract
 Subscription handle controlling callback lifetime.
using Job = StatefulObject<ActivationStatus>::Job
 Callback signature executed on activation state changes.
Public Types inherited from spk::StatefulObject< ActivationStatus >
using Contract
 Handle allowing subscribers to manage callback lifetimes.
using Job
 Callable invoked when the object transitions to a state.

Public Member Functions

 SpriteSheet ()=default
 Builds an empty sprite sheet with no image data loaded.
 SpriteSheet (const std::filesystem::path &p_path, const spk::Vector2UInt &p_nbSprite, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Loads an image and splits it into an N x M grid of sprites.
const spk::Vector2UIntnbSprite () const
 Retrieves the number of sprites along each axis.
const spk::Vector2unit () const
 Provides the normalized unit step separating sprites.
const std::vector< Sprite > & sprites () const
 Accesses every sprite region composing the sheet.
std::size_t spriteID (const spk::Vector2UInt &p_spriteCoord) const
 Converts a sprite coordinate to its linear index.
const Spritesprite (const spk::Vector2UInt &p_spriteCoord) const
 Provides a sprite region by grid coordinate.
const Spritesprite (const std::size_t &p_spriteID) const
 Provides a sprite region by linear index.
Public Member Functions inherited from spk::Image
 Image (const std::vector< uint8_t > &p_pixelsAndEncodingData, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Builds an image from encoded pixel data already in memory.
 Image (const std::filesystem::path &p_path, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Loads an image from disk.
Public Member Functions inherited from spk::OpenGL::TextureObject
 TextureObject (Format p_format=Format::RGBA, Filter p_filter={Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper={Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Constructs a texture with format, filtering, and wrapping options.
 TextureObject (const TextureObject &p_other)
 Copy-constructs a texture object including CPU-side data.
TextureObjectoperator= (const TextureObject &p_other)
 Copy-assigns a texture object including CPU-side data.
 TextureObject (TextureObject &&p_other) noexcept
 Move-constructs a texture object transferring ownership.
TextureObjectoperator= (TextureObject &&p_other) noexcept
 Move-assigns a texture object transferring ownership.
void activate () const
 Activates this texture for rendering.
void deactivate () const
 Deactivates this texture after rendering.
Format format () const
 Gets the current texture format.
Filter filter () const
 Gets filtering parameters.
Wrapper wrapper () const
 Gets wrapping parameters.
Mipmap mipmap () const
 Gets mipmap activation state.
void setFormat (Format p_format)
 Sets the texture format (affects upload interpretation).
void setFiltering (Filter p_filter)
 Sets min/mag filtering together.
void setFiltering (Filtering p_minFilter, Filtering p_magFilter)
 Sets minification and magnification filters independently.
void setWrap (Wrapper p_wrapper)
 Sets wrapping for both axes.
void setWrap (Wrap p_wrapS, Wrap p_wrapT)
 Sets wrapping for each axis independently.
void setMipmap (Mipmap p_mipmap)
 Sets mipmap activation state.
spk::Vector2UInt size () const
 Returns texture dimensions.
size_t sizeAsBytes () const
 Returns CPU data size in bytes.
GLuint id () const
 Returns GL texture id, creating if needed.
void setData (const uint8_t *p_data, const spk::Vector2UInt &p_size, Format p_format=Format::RGBA)
 Uploads pixel data from raw pointer.
void setData (std::vector< uint8_t > p_data, const spk::Vector2UInt &p_size, Format p_format=Format::RGBA)
 Uploads pixel data by move.
void clearData ()
 Clears CPU data and marks for sync.
void resize (const spk::Vector2UInt &p_size)
 Resizes CPU data and marks for sync.
std::vector< uint8_t > & data ()
 Gets CPU-side pixel data.
const std::vector< uint8_t > & data () const
 Gets CPU-side pixel data.
void flip (const spk::Orientation &p_orientation)
 Flips stored pixel data along the given orientation.
void activate ()
 Sets the state to Activated.
void deactivate ()
 Sets the state to Deactivated.
Public Member Functions inherited from spk::ActivableObject
 ActivableObject ()
 Builds a deactivated object.
 ActivableObject (ActivationStatus p_initial)
 Builds with a custom initial activation state.
void activate ()
 Sets the state to Activated.
void deactivate ()
 Sets the state to Deactivated.
void toggle ()
 Toggles between activated and deactivated states.
bool isActive () const
 Checks whether the object is currently activated.
Contract addActivationCallback (const Job &p_callback)
 Registers a callback executed when the object activates.
Contract addDeactivationCallback (const Job &p_callback)
 Registers a callback executed when the object deactivates.
Public Member Functions inherited from spk::StatefulObject< ActivationStatus >
 StatefulObject (const ActivationStatus &p_initialState)
 Builds with an initial state.
StatefulObjectoperator= (const StatefulObject &p_other)=delete
void setState (const ActivationStatus &p_newState)
 Sets a new state and triggers callbacks registered for it.
ActivationStatus state () const
 Returns the current state.
Contract addCallback (const ActivationStatus &p_state, const Job &p_callback)
 Subscribes a callback for a specific state.
Public Member Functions inherited from spk::SynchronizableObject
void requestSynchronization () noexcept
 Marks the object as needing synchronization.
bool needsSynchronization () const noexcept
 Checks if synchronization is pending.
void synchronize ()
 Performs synchronization if requested.
void forceSynchronization ()
 Forces synchronization regardless of current flag.

Static Public Member Functions

static SpriteSheet fromRawData (const std::vector< uint8_t > &p_pixelsAndEncodingData, const spk::Vector2UInt &p_nbSprite, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Builds a sprite sheet directly from encoded pixel data.
static SpriteSheet fromFile (const std::filesystem::path &p_path, const spk::Vector2UInt &p_nbSprite, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Loads a sprite sheet from a file on disk.
Static Public Member Functions inherited from spk::Image
static Image fromFile (const std::filesystem::path &p_path, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Convenience factory that loads from a file path.
static Image fromRawData (const std::vector< uint8_t > &p_pixelsAndEncodingData, Filter p_filter=Filter{Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper=Wrapper{Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
 Convenience factory that builds from encoded pixel data.

Additional Inherited Members

Static Public Attributes inherited from spk::Image
static Region Whole = Region{.anchor = {0, 0}, .size = {1, 1}}
 Represents the entire texture area.
Protected Member Functions inherited from spk::Image
void _loadFromFile (const std::filesystem::path &p_path)
 Populates the texture from an image stored on disk.
void _loadFromData (const std::vector< uint8_t > &p_pixelsAndEncodingData)
 Populates the texture from encoded bytes already in memory.
Protected Member Functions inherited from spk::OpenGL::TextureObject
void _onSynchronize () override
 Synchronizes CPU-side data with the GPU texture.

Detailed Description

Loads an image and exposes individual sprite regions.

See also
spk::Image
spk::SpriteSheet sheet{"tiles.png", {8, 8}};
const auto &grass = sheet.sprite({0u, 1u});
Loads an image and exposes individual sprite regions.
Definition spk_sprite_sheet.hpp:21
const Sprite & sprite(const spk::Vector2UInt &p_spriteCoord) const
Provides a sprite region by grid coordinate.
Definition spk_sprite_sheet.cpp:90

Constructor & Destructor Documentation

◆ SpriteSheet()

spk::SpriteSheet::SpriteSheet ( const std::filesystem::path & p_path,
const spk::Vector2UInt & p_nbSprite,
Filter p_filter = Filter{Filtering::Linear, Filtering::Linear},
Wrapper p_wrapper = Wrapper{Wrap::Repeat, Wrap::Repeat},
Mipmap p_mipmap = Mipmap::Activated )

Loads an image and splits it into an N x M grid of sprites.

Parameters
p_pathFile system path to the source image.
p_nbSpriteNumber of sprites across (x) and down (y).
p_filterSampling configuration for minification/magnification.
p_wrapperWrapping configuration for the texture coordinates.
p_mipmapWhether mipmaps are generated.
Here is the call graph for this function:

Member Function Documentation

◆ fromFile()

SpriteSheet spk::SpriteSheet::fromFile ( const std::filesystem::path & p_path,
const spk::Vector2UInt & p_nbSprite,
Filter p_filter = Filter{Filtering::Linear, Filtering::Linear},
Wrapper p_wrapper = Wrapper{Wrap::Repeat, Wrap::Repeat},
Mipmap p_mipmap = Mipmap::Activated )
static

Loads a sprite sheet from a file on disk.

Parameters
p_pathFile system path to the source image.
p_nbSpriteNumber of sprites across (x) and down (y).
p_filterSampling configuration for minification/magnification.
p_wrapperWrapping configuration for the texture coordinates.
p_mipmapWhether mipmaps are generated.
Returns
Constructed sprite sheet ready for sampling.
Here is the call graph for this function:

◆ fromRawData()

SpriteSheet spk::SpriteSheet::fromRawData ( const std::vector< uint8_t > & p_pixelsAndEncodingData,
const spk::Vector2UInt & p_nbSprite,
Filter p_filter = Filter{Filtering::Linear, Filtering::Linear},
Wrapper p_wrapper = Wrapper{Wrap::Repeat, Wrap::Repeat},
Mipmap p_mipmap = Mipmap::Activated )
static

Builds a sprite sheet directly from encoded pixel data.

Parameters
p_pixelsAndEncodingDataRaw pixel buffer containing format metadata.
p_nbSpriteNumber of sprites across (x) and down (y).
p_filterSampling configuration for minification/magnification.
p_wrapperWrapping configuration for the texture coordinates.
p_mipmapWhether mipmaps are generated.
Returns
Constructed sprite sheet ready for sampling.
Here is the call graph for this function:

◆ nbSprite()

const spk::Vector2UInt & spk::SpriteSheet::nbSprite ( ) const

Retrieves the number of sprites along each axis.

Returns
Grid dimensions as {columns, rows}.

◆ sprite() [1/2]

const SpriteSheet::Sprite & spk::SpriteSheet::sprite ( const spk::Vector2UInt & p_spriteCoord) const

Provides a sprite region by grid coordinate.

Parameters
p_spriteCoordCoordinate of the sprite in the grid.
Returns
Region describing the requested sprite.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sprite() [2/2]

const SpriteSheet::Sprite & spk::SpriteSheet::sprite ( const std::size_t & p_spriteID) const

Provides a sprite region by linear index.

Parameters
p_spriteIDZero-based identifier for the sprite.
Returns
Region describing the requested sprite.

◆ spriteID()

std::size_t spk::SpriteSheet::spriteID ( const spk::Vector2UInt & p_spriteCoord) const

Converts a sprite coordinate to its linear index.

Parameters
p_spriteCoordCoordinate of the sprite in the grid.
Returns
Zero-based identifier for the sprite.
Here is the caller graph for this function:

◆ sprites()

const std::vector< SpriteSheet::Sprite > & spk::SpriteSheet::sprites ( ) const

Accesses every sprite region composing the sheet.

Returns
Collection of sprite regions ordered row-major.

◆ unit()

const spk::Vector2 & spk::SpriteSheet::unit ( ) const

Provides the normalized unit step separating sprites.

Returns
Normalized width/height of a sprite in UV space.

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