Creating a SpriteComponent

The next component that we're going to make is a SpriteComponent. This will provide an object with either a static or an animated sprite. It's a behavior that is commonly reused through many objects so is a great candidate to be moved into a component.

Encapsulating sprite behavior

Currently, all the animation-related behavior is inherited from the base Object class. The following code consists of all the sprite- and animation-related functions and variables that we'll pull from Object into its own class:

public: virtual void Draw(sf::RenderWindow &window, float timeDelta); bool SetSprite(sf::Texture& texture, bool isSmooth, int frames = 1, int frameSpeed = 0); sf::Sprite& GetSprite(); int GetFrameCount() const; bool IsAnimated(); ...

Get Procedural Content Generation for C++ Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.