sprite_sheet.hpp 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#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 */