Sprite system
Since we're working on a 2D game, the most likely candidate for the way graphics are going to be done is a sprite sheet. Unifying the way sprite sheet cropping and animations are handled is key to not only minimizing code, but also creating a simple, neat interface that's easy to interact with. Let us take a look at how that can be done:
class SpriteSheet{ public: ... void CropSprite(const sf::IntRect& l_rect); const sf::Vector2u& GetSpriteSize()const; const sf::Vector2f& GetSpritePosition()const; void SetSpriteSize(const sf::Vector2u& l_size); void SetSpritePosition(const sf::Vector2f& l_pos); void SetDirection(const Direction& l_dir); Direction GetDirection() const; void SetSheetPadding(const sf::Vector2f& l_padding); void SetSpriteSpacing(const sf::Vector2f& l_spacing); const sf::Vector2f& GetSheetPadding()const; const sf::Vector2f& GetSpriteSpacing()const; bool LoadSheet(const std::string& l_file); void ReleaseSheet(); Anim_Base* GetCurrentAnim(); bool SetAnimation(const std::string& l_name, bool l_play = false, bool l_loop = false); void Update(float l_dT); void Draw(sf::RenderWindow* l_wnd); private: ... Animations m_animations; };
The SpriteSheet
class itself isn't really that complex. It offers helper methods for cropping the sheet down to a specific rectangle, altering the stored direction, defining different attributes, such as spacing, padding, and so on, and manipulating the animation data.
Animations are stored in this class by name:
using Animations = std::unordered_map<std::string, std::unique_ptr<Anim_Base>>;
The interface of an animation class looks like this:
class Anim_Base{ friend class SpriteSheet; public: ... void SetSpriteSheet(SpriteSheet* l_sheet); bool SetFrame(Frame l_frame); void SetStartFrame(Frame l_frame); void SetEndFrame(Frame l_frame); void SetFrameRow(unsigned int l_row); void SetActionStart(Frame l_frame); void SetActionEnd(Frame l_frame); void SetFrameTime(float l_time); void SetLooping(bool l_loop); void SetName(const std::string& l_name); SpriteSheet* GetSpriteSheet(); Frame GetFrame() const; Frame GetStartFrame() const; Frame GetEndFrame() const; unsigned int GetFrameRow() const; int GetActionStart() const; int GetActionEnd() const; float GetFrameTime() const; float GetElapsedTime() const; bool IsLooping() const; bool IsPlaying() const; bool IsInAction() const; bool CheckMoved(); std::string GetName() const; void Play(); void Pause(); void Stop(); void Reset(); virtual void Update(float l_dT); friend std::stringstream& operator >>( std::stringstream&l_stream, Anim_Base& a){ ... } protected: virtual void FrameStep() = 0; virtual void CropSprite() = 0; virtual void ReadIn(std::stringstream& l_stream) = 0; ... };
First, the Frame
data type is simply a type definition of an integer. This class keeps track of all necessary animation data, and even provides a way to set up specific frame ranges (also referred to as actions), which can be used for something such as an entity only attacking something if the attack animation is within that specific action range.
The obvious thing about this class is that it does not represent any single type of animation, but rather all the common elements of every type. This is why three different purely virtual methods are provided, so that different types of animation can define how the frame step is handled, define the specific method, the location of cropping, and the exact process of the animation being loaded from a file. This helps us separate directional animations, where every row represents a character facing a different way, from simple, sequential animations of frames following each other in a linear order.