Calamity Engine 1.0.0
A cross-platform 2D game engine written in C++ and SDL3.
Loading...
Searching...
No Matches
Sprite Class Reference

#include <components.hpp>

Inheritance diagram for Sprite:
Component

Public Member Functions

 Sprite ()
 
 Sprite (const std::string &texturePath, std::shared_ptr< Window > window, TextureScaling scaling=TextureScaling::NEAREST)
 
void postLoad () override
 
void render (std::shared_ptr< Window >) override
 
void initialize () override
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 
- Public Member Functions inherited from Component
virtual ~Component ()=default
 
virtual void update (float deltaTime)
 
virtual void physicsUpdate ()
 
virtual void input (InputEvent &event)
 
virtual void exit ()
 
NodegetNode () const
 
void setNode (Node *n)
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 

Public Attributes

Vector2 origin = {0.5f, 0.5f}
 
Texture texture
 
Rect sourceRect
 
bool visible = true
 
bool screenSpace = false
 
Color modulate = Color::WHITE
 
bool flipH = false
 
bool flipV = false
 

Detailed Description

Sprite component

The sprite component renders a texture to the screen.

Example usage:

std::shared_ptr<Node> spriteNode = std::make_shared<Node>("spriteNode");
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>("res://path/to/texture.png", window);
spriteNode->addComponent(sprite);

Properties and usages

You can also modify it's origin:

sprite->origin = {0.0f, 0.0f} // instead of the center of the sprite being at the position of the node, now the top left will be at the position of the node.

The sprites Texture also has a width and height attribute. These define the size of the final rendered texture on the screen.

sprite->texture.width = 64;
sprite->texture.height = 64;

When you define the sprite, you can also define the TextureScaling mode:

std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>("res://path/to/texture.png", window, TextureScaling::PIXELART);

You can also define the sprites source rectangle in a texture atlas. This allows you to pack a bunch of textures in one image which you can load all at once!

sprite->sourceRect = Rect{Vector2{0.0f, 0.0f}, Vector2{16.0f, 16.0f}};
Definition definitions.hpp:294
Definition definitions.hpp:77

Sprites also have a modulate property, which allows you to modify their color when theyre rendered:

sprite->modulate = Color::BLUE; // The sprite will now become blue
static const Color BLUE
Definition definitions.hpp:62

You can also make sprites render using screen space positioning. This can be used for UI elements and things like that:

sprite->screenSpace = true; // Now, the position of the sprite will directly translate to screen coordinates!

Sprites also define flipH and flipV variables. You can use these when animating a sprite, for example.

sprite->flipV = true; // The sprite will now render vertically flipped!

Make sure to also check out the atlas example!

Constructor & Destructor Documentation

◆ Sprite() [1/2]

Sprite::Sprite ( )
default

◆ Sprite() [2/2]

Sprite::Sprite ( const std::string &  texturePath,
std::shared_ptr< Window window,
TextureScaling  scaling = TextureScaling::NEAREST 
)
explicit

Member Function Documentation

◆ initialize()

void Sprite::initialize ( )
overridevirtual

Reimplemented from Component.

◆ load()

template<class Archive >
void Sprite::load ( Archive &  ar)
inline

◆ postLoad()

void Sprite::postLoad ( )
overridevirtual

Reimplemented from Component.

◆ render()

void Sprite::render ( std::shared_ptr< Window window)
overridevirtual

Reimplemented from Component.

◆ save()

template<class Archive >
void Sprite::save ( Archive &  ar) const
inline

Member Data Documentation

◆ flipH

bool Sprite::flipH = false

◆ flipV

bool Sprite::flipV = false

◆ modulate

Color Sprite::modulate = Color::WHITE

◆ origin

Vector2 Sprite::origin = {0.5f, 0.5f}

◆ screenSpace

bool Sprite::screenSpace = false

◆ sourceRect

Rect Sprite::sourceRect

◆ texture

Texture Sprite::texture

◆ visible

bool Sprite::visible = true

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