Loading...
Searching...
No Matches
ml::TextureSlicer Class Reference

Divides a texture into a uniform grid of sub-rectangles. More...

#include <TextureSlicer.h>

Static Public Member Functions

static ml::ImageRects getImageRects (const sf::Texture &texture, int rows, int cols)
 Slice a texture into a rows × cols grid of equal rectangles.

Detailed Description

Divides a texture into a uniform grid of sub-rectangles.

TextureSlicer calculates the pixel bounds of each cell when a texture is divided into rows rows and cols columns of equal size, returning the results as an ImageRects collection.

This is the primary utility for working with sprite sheets and tile atlases — slice once, then use the resulting rects with sf::Sprite::setTextureRect to display individual frames or tiles.

Usage

// A sprite sheet with 4 rows and 8 columns of frames
const sf::Texture& sheet = ml::TextureManager<MyManifest>::get(MyManifest::Images::Player);
// Display frame at row 0, column 2
sprite.setTexture(sheet);
sprite.setTextureRect(rects.getIntRect(0, 2));
A grid-indexed collection of sf::IntRect regions.
Definition ImageRects.h:37
const sf::IntRect & getIntRect(int row, int col)
Return the rect for a given row and column.
static const sf::Texture & get(const Asset &asset)
static ml::ImageRects getImageRects(const sf::Texture &texture, int rows, int cols)
Slice a texture into a rows × cols grid of equal rectangles.
See also
ImageRects, TextureManager

Definition at line 38 of file TextureSlicer.h.

Member Function Documentation

◆ getImageRects()

ml::ImageRects ml::TextureSlicer::getImageRects ( const sf::Texture & texture,
int rows,
int cols )
static

Slice a texture into a rows × cols grid of equal rectangles.

Divides the texture's pixel dimensions evenly, computing one sf::IntRect per cell in row-major order. The rects are stored in the returned ImageRects object.

Parameters
textureThe source texture to slice. Must be fully loaded.
rowsNumber of rows to divide the texture into.
colsNumber of columns to divide the texture into.
Returns
An ImageRects containing rows × cols rects, accessible by (row, col) index.

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