Sprite¶
Description¶
Sprite is the primary class for 2D images and sprite sheets. It renders a textured quad, supports texture atlas frames for manual sprite selection, and provides a simple frame animation player for looping sequences.
Inherits: Mesh → Object → EntityHandle
Properties¶
| Type | Name | Default | Languages |
|---|---|---|---|
| unsigned int | width | 0 |
C++ | Lua |
| unsigned int | height | 0 |
C++ | Lua |
| bool | flipY | false |
C++ | Lua |
| float | textureScaleFactor | 1.0 |
C++ | Lua |
| Rect | textureRect | full texture | C++ | Lua |
| PivotPreset | pivotPreset | CENTER |
C++ | Lua |
Methods¶
| Returns | Name | Languages |
|---|---|---|
| bool | createSprite | C++ | Lua |
| void | setSize | C++ | Lua |
| void | setWidth | C++ | Lua |
| void | setHeight | C++ | Lua |
| unsigned int | getWidth | C++ | Lua |
| unsigned int | getHeight | C++ | Lua |
| void | setFlipY | C++ | Lua |
| bool | isFlipY | C++ | Lua |
| void | setTextureScaleFactor | C++ | Lua |
| float | getTextureScaleFactor | C++ | Lua |
| void | setTextureRect | C++ | Lua |
| Rect | getTextureRect | C++ | Lua |
| void | setPivotPreset | C++ | Lua |
| PivotPreset | getPivotPreset | C++ | Lua |
| void | addFrame | C++ | Lua |
| void | removeFrame | C++ | Lua |
| void | setFrame | C++ | Lua |
| void | startAnimation | C++ | Lua |
| void | pauseAnimation | C++ | Lua |
| void | resumeAnimation | C++ | Lua |
| void | stopAnimation | C++ | Lua |
Enumerations¶
PivotPreset¶
Controls the origin point used for position and rotation.
- CENTER — Origin at the geometric center of the sprite (default).
- TOP_LEFT — Origin at the top-left corner.
- TOP_CENTER — Origin at the top-center.
- TOP_RIGHT — Origin at the top-right corner.
- CENTER_LEFT — Origin at the center-left edge.
- CENTER_RIGHT — Origin at the center-right edge.
- BOTTOM_LEFT — Origin at the bottom-left corner.
- BOTTOM_CENTER — Origin at the bottom-center.
- BOTTOM_RIGHT — Origin at the bottom-right corner.
Property details¶
width / height¶
- Setter:
void setWidth(unsigned int width)/void setHeight(unsigned int height) - Setter combined:
void setSize(unsigned int width, unsigned int height) - Getter:
unsigned int getWidth() const/unsigned int getHeight() const
Physical size of the rendered quad in world units. If both are zero, the sprite uses the texture dimensions automatically after createSprite().
flipY¶
- Setter:
void setFlipY(bool flipY) - Getter:
bool isFlipY() const
Flips the texture vertically. Useful when loading textures from APIs that use a top-left origin (e.g. some framebuffer captures).
textureScaleFactor¶
- Setter:
void setTextureScaleFactor(float textureScaleFactor) - Getter:
float getTextureScaleFactor() const
Scales the texture coordinates, effectively tiling or shrinking the mapped texture without changing the quad size.
textureRect¶
- Setter:
void setTextureRect(float x, float y, float width, float height) - Setter:
void setTextureRect(Rect textureRect) - Getter:
Rect getTextureRect() const
Defines the sub-rectangle of the texture to display, in pixel coordinates. Use to select a region from a texture atlas without defining named frames.
pivotPreset¶
- Setter:
void setPivotPreset(PivotPreset pivotPreset) - Getter:
PivotPreset getPivotPreset() const
Sets the pivot (origin) point used for position placement and rotation. See PivotPreset.
Method details¶
createSprite¶
bool createSprite()
Builds the quad geometry and uploads it to the GPU. Must be called at least once after setting the texture and size. Returns true on success.
setSize¶
void setSize(unsigned int width, unsigned int height)
Sets both width and height in a single call. Equivalent to calling setWidth and setHeight separately.
setTextureRect¶
void setTextureRect(float x, float y, float width, float height)void setTextureRect(Rect textureRect)
Restricts rendering to a sub-region of the texture. Coordinates are in pixels from the top-left of the texture image.
addFrame¶
void addFrame(int id, const std::string& name, Rect rect)void addFrame(const std::string& name, float x, float y, float width, float height)void addFrame(float x, float y, float width, float height)(auto-increment id)void addFrame(Rect rect)(auto-increment id)
Registers a named frame in the sprite sheet. Frames are used by setFrame and startAnimation.
removeFrame¶
void removeFrame(int id)void removeFrame(const std::string& name)
Removes a registered frame by ID or name.
setFrame¶
void setFrame(int id)void setFrame(const std::string& name)
Immediately displays the registered frame with the given ID or name, overriding any active animation.
startAnimation¶
void startAnimation(std::vector<int> frames, std::vector<int> framesTime, bool loop)void startAnimation(int startFrame, int endFrame, int interval, bool loop)void startAnimation(const std::string& name, int interval, bool loop)
Plays a frame animation. Time values are in milliseconds.
- The first overload accepts explicit frame ID lists and per-frame durations.
- The second overload plays a contiguous range of frame IDs at a fixed interval.
- The third overload plays all frames whose names begin with
nameat a fixed interval.
pauseAnimation¶
void pauseAnimation()
Freezes the animation at the current frame. Call resumeAnimation to continue.
resumeAnimation¶
void resumeAnimation()
Resumes a paused animation from where it was stopped.
stopAnimation¶
void stopAnimation()
Stops the animation and resets to the first frame of the sequence.