NasNas
An intuitive and beginner friendly 2D game framework for C++
SpriteSheet.hpp
1 
6 #pragma once
7 
8 #include <string>
9 #include <unordered_map>
10 #include <vector>
11 
12 #include <SFML/Graphics/Texture.hpp>
13 #include <SFML/System/Vector2.hpp>
14 
15 #include <NasNas/core/graphics/Anim.hpp>
16 
17 namespace ns {
18 
19  class SpriteSheet {
20  public:
21 
22  SpriteSheet() = default;
23 
24  explicit SpriteSheet(const sf::Texture& texture);
25 
33  SpriteSheet(const sf::Texture& texture, const std::vector<Anim*>& anims);
34 
38  ~SpriteSheet();
39 
40  void setTexture(const sf::Texture& texture);
41 
42  auto getTexture() const -> const sf::Texture*;
43 
44  void setGrid(const sf::Vector2i& cell_size, int columns_nb, int margin=0, int spacing=0);
45 
46  void addAnim(Anim* new_anim);
47 
48  void addAnim(const std::string& anim_name, unsigned int first_frame, unsigned int nb_of_frames, int duration, const sf::Vector2i& origin={0,0});
49  void addAnim(const std::string& anim_name, unsigned int first_frame, unsigned int nb_of_frames, int duration, const std::vector<sf::Vector2i>& origins);
50  void addAnim(const std::string& anim_name, unsigned int first_frame, unsigned int nb_of_frames, const std::vector<int>& durations, const sf::Vector2i& origin={0,0});
51  void addAnim(const std::string& anim_name, unsigned int first_frame, unsigned int nb_of_frames, const std::vector<int>& durations, const std::vector<sf::Vector2i>& origins);
52 
53  void addAnim(const std::string& anim_name, const std::vector<unsigned int>& frames, int duration, const sf::Vector2i& origin={0,0});
54  void addAnim(const std::string& anim_name, const std::vector<unsigned int>& frames, int duration, const std::vector<sf::Vector2i>& origins);
55  void addAnim(const std::string& anim_name, const std::vector<unsigned int>& frames, const std::vector<int>& durations, const sf::Vector2i& origin={0,0});
56  void addAnim(const std::string& anim_name, const std::vector<unsigned int>& frames, const std::vector<int>& durations, const std::vector<sf::Vector2i>& origins);
57 
62  auto getAnimsMap() -> const std::unordered_map<std::string, Anim*>&;
63 
69  auto getAnim(const std::string& anim_name) -> const Anim&;
70 
71  private:
72  const sf::Texture* m_texture = nullptr;
73  std::unordered_map<std::string, Anim*> m_anims_map;
74  sf::Vector2i m_cell_size;
75  int m_margin = 0;
76  int m_spacing = 0;
77  int m_columns = 1;
78 
79  auto getFrameTexCoords(unsigned int frame_index) const -> sf::Vector2i;
80  };
81 
82 }
auto getAnim(const std::string &anim_name) -> const Anim &
Get an Anim by its name.
auto getAnimsMap() -> const std::unordered_map< std::string, Anim *> &
Get a map containing all the Anim objects.
~SpriteSheet()
Delete all the Anim objects of this Spritesheet.