Commit d76afe3d authored by louiz’'s avatar louiz’

Introduce the concept of animation and sprite sheets

parent 95e04ac5
#include <gui/animation.hpp>
void Animation::update(const utils::Duration& dt)
{
this->time += dt;
while (this->time >= this->frame_duration)
{
this->frame++;
if (this->frame == this->number_of_frames)
this->frame = 0;
this->time -= this->frame_duration;
}
}
std::size_t Animation::get_current_frame() const
{
return this->frame;
}
void Animation::reset()
{
this->frame = 0;
}
#ifndef ANIMATION_HPP_INCLUDED
#define ANIMATION_HPP_INCLUDED
/**
* Given the number of frames of an animation, and the frame duration: keeps
* track of the current frame we should display on-screen.
*/
#include <utils/time.hpp>
class Animation
{
public:
Animation(const std::size_t number_of_frames, const utils::Duration& frame_duration):
number_of_frames(number_of_frames),
frame_duration(frame_duration),
time(0s),
frame(0)
{
}
~Animation() = default;
void update(const utils::Duration& dt);
std::size_t get_current_frame() const;
void reset();
private:
const std::size_t number_of_frames;
utils::Duration frame_duration;
utils::Duration time;
std::size_t frame;
Animation(const Animation&) = delete;
Animation(Animation&&) = delete;
Animation& operator=(const Animation&) = delete;
Animation& operator=(Animation&&) = delete;
};
#endif /* ANIMATION_HPP_INCLUDED */
#ifndef ORIENTED_SPRITE_SHEETS_HPP_INCLUDED
#define ORIENTED_SPRITE_SHEETS_HPP_INCLUDED
/**
* Contains 8 sprite sheets, one for each direction
*/
#include <world/direction.hpp>
#include <gui/sprite_sheet.hpp>
class OrientedSpriteSheets
{
public:
OrientedSpriteSheets() = default;
~OrientedSpriteSheets() = default;
void load(const std::string& directory, const std::string& prefix,
const sf::Vector2<int> size)
{
sheets[0].load(directory + prefix + "_E.png", size);
sheets[1].load(directory + prefix + "_SE.png", size);
sheets[2].load(directory + prefix + "_S.png", size);
sheets[3].load(directory + prefix + "_SW.png", size);
sheets[4].load(directory + prefix + "_W.png", size);
sheets[5].load(directory + prefix + "_NW.png", size);
sheets[6].load(directory + prefix + "_N.png", size);
sheets[7].load(directory + prefix + "_NE.png", size);
}
const sf::Texture& get_texture(Direction& direction, const std::size_t index) const
{
return this->sheets[static_cast<unsigned short>(direction)].get_texture(index);
}
std::size_t get_nb_textures() const
{
return this->sheets[0].get_nb_textures();
}
private:
std::array<SpriteSheet, static_cast<unsigned short>(Direction::count)> sheets;
OrientedSpriteSheets(const OrientedSpriteSheets&) = delete;
OrientedSpriteSheets(OrientedSpriteSheets&&) = delete;
OrientedSpriteSheets& operator=(const OrientedSpriteSheets&) = delete;
OrientedSpriteSheets& operator=(OrientedSpriteSheets&&) = delete;
};
#endif /* ORIENTED_SPRITE_SHEETS_HPP_INCLUDED */
#ifndef SPRITE_SHEET_HPP_INCLUDED
#define SPRITE_SHEET_HPP_INCLUDED
/**
* Class managing a sprite sheet. This is a single image, loaded and cut as
* an array of textures, representing (for examples) the different frames of
* an animation.
*/
#include <SFML/Graphics.hpp>
#include <vector>
#include <cassert>
#include <logging/logging.hpp>
class SpriteSheet
{
public:
SpriteSheet() = default;
~SpriteSheet() = default;
/**
* Takes the image of the sprite sheet to use, and the size of each
* texture to cut in it.
*/
void load(const std::string& image_path, const sf::Vector2<int> size)
{
sf::Image image;
if (!image.loadFromFile(image_path))
{
log_error("Failed to load image " << image_path);
return;
}
// Check if the image size is a multiple of the sub-images’ size
auto image_size = image.getSize();
assert(image_size.x % size.x == 0);
this->textures.resize(image_size.x / size.x);
for (auto i = 0u; i < this->textures.size(); ++i)
{
if (!this->textures[i].loadFromImage(image,
{{size.x * static_cast<int>(i), 0}, {size.x, size.y}}))
{
log_error("Failed to create texture from image " << image_path);
return;
}
}
}
const sf::Texture& get_texture(const std::size_t index) const
{
assert(index < this->textures.size());
return this->textures[index];
}
std::size_t get_nb_textures() const
{
return this->textures.size();
}
private:
std::vector<sf::Texture> textures;
SpriteSheet(const SpriteSheet&) = delete;
SpriteSheet(SpriteSheet&&) = delete;
SpriteSheet& operator=(const SpriteSheet&) = delete;
SpriteSheet& operator=(SpriteSheet&&) = delete;
};
#endif /* SPRITE_SHEET_HPP_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment