Project update #4

Done on May 22, 2017 – Animation loading

In this demo, I’m switching between 3 animation sets by pressing a key. Each of these sets have their own configuration. They are loaded and used by the same Player controller which has 3 states: Idle, Run and Attack.


For this fourth update, I revisited the animation loading I mentioned in the project update #2. There was a couple of shortcomings that needed to be addressed:

  • The sprite sheet was loaded multiple times
  • The animation parameters were hardcoded in the Player class

The sprite sheet contains the characters’ animation sets and the map’s tile set. This texture was loaded 4 times in memory: one time for each of the player’s animation Idle, Run and Attack and one additional time for the map.

I solved this issue by implementing a ResourceManager. Now, anytime an object needs a texture, it asks the ResourceManager which will check if the texture is already loaded, loads it if it’s not and returns a pointer to it.

Here’s an example:

bool Animation::LoadFromFile(std::string path)
    // Load the texture
    m_texture = ResourceManager::GetInstance().GetTexture(path);

For the second point, the animation parameters are now loaded from a file instead of being hard coded. Here’s what the Player’s animation file looks like:

# The format of an animation declaration is:
#   path_to_sprite_sheet
#   frame_count
#   frame_duration
#   first_sprite_horizontal_index
#   first_sprite_vertical_index
#   sprite_width
#   sprite_height
#   loop

# Idle
Data/Textures/Scavengers_SpriteSheet.png 6 0.250 0 0 32 32 1

# Attack
Data/Textures/Scavengers_SpriteSheet.png 2 0.120 0 5 32 32 0

# Run
Data/Textures/Scavengers_SpriteSheet.png 6 0.080 0 0 32 32 1

And here’s the sprite sheet:


Finally, I added the possibility to reload an animation while the game is running. It can be useful when tweaking the animation, frame duration etc. I simply edit the file above, save it and press the hot reload key in game.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s