Skip to content

Sprite Renderer

Imagment edited this page Mar 27, 2025 · 6 revisions

Explanation

The SpriteRenderer class handles how objects appear on screen. It manages:

  • The visual shape of objects
  • Pivot points (center or custom anchor positions)
  • Transparency and colors
  • Special text formatting

Constructors

Constructor Description
SpriteRenderer() Default constructor.
SpriteRenderer(std::string newShape) Initializes the sprite with a specific shape.
SpriteRenderer(std::string newShape, Vector2 newPivot) Initializes the sprite with a specific shape and pivot.
SpriteRenderer(bool useRelative, Vector2 newPivot, std::string newShape) Initializes the sprite with a specific shape, pivot, and a flag indicating whether the pivot is relative.
SpriteRenderer(bool useRelative, Vector2 newPivot, std::string newShape, bool transparent, bool markdown, Color newColor) Initializes the sprite with shape, pivot, transparency, markdown support, and color.
SpriteRenderer(const SpriteRenderer& other) Copy constructor. Initializes the sprite by copying another SpriteRenderer instance.
SpriteRenderer& operator=(const SpriteRenderer& other) Copy assignment operator, creates a copy of another SpriteRenderer instance.

Member Variables

Variable Name Type Description
useRelativePivot bool A boolean indicating whether the pivot is relative.
pivot Vector2 A Vector2 that defines the position of the pivot.
pivotFactor Vector2 A Vector2 that represents the default pivot factor.
isTransparent bool A boolean that determines if the sprite is transparent.
spriteColor Color A Color representing the color of the sprite.
useMarkdown bool A boolean that indicates whether markdown support is used for the sprite.

Member Functions

Function Name Description
std::shared_ptr<Component> Clone() const Creates and returns a copy of this SpriteRenderer object.
std::string getShape() Returns the shape of the sprite as a string.
void setShape(std::string) Sets the shape of the sprite to the specified string.
void alignShapeTo(double) Aligns the shape to the specified alignment value.
Vector2 GetSize() Returns the size of the sprite as a Vector2.
Vector2 GetPivot() Returns the pivot of the sprite as a Vector2.
std::tuple<int, int, int, int> GetPivotBounds() Returns the bounding box of the pivot as a tuple of four integers.
std::string GetCellString(int, int) Returns the string representation of the sprite at the specified column and line.
std::tuple<int, int, int, int> CalculatePivotExpansion() Calculates the expansion of the sprite around the pivot.

Private Member Variables

Variable Name Type Description
shape std::string The shape of the sprite.
cleanShape std::string A clean version of the shape.
spriteWidth int The width of the sprite.
spriteHeight int The height of the sprite.
ss std::stringstream A stringstream used for storing sprite-related data.
ansiExtracted std::vector<std::vector<std::string>> A vector storing the extracted ANSI data for the sprite.

Related Functions

Function Name Description
StripAnsi(const std::string& input) Strips ANSI escape codes from the input string, returning the clean string without formatting.
ExtractAnsi(const std::string& input) Extracts ANSI escape codes from the input string, returning a 2D vector of strings containing the ANSI sequences.

Notes

The shape variable represents the sprite’s visual appearance and needs to be formatted properly for correct display. Since the shape is not frequently changed, we made it private. This helps improve efficiency by eliminating the need to reapply formatting to objects that haven't been modified.

Overview

Cheatsheets

Structure of the engine

Silver Mathematics

Components

Clone this wiki locally