Peoplemon  0.1.0
Peoplemon 3 game source documentation
Tileset.hpp
Go to the documentation of this file.
1 #ifndef CORE_MAPS_TILESET_HPP
2 #define CORE_MAPS_TILESET_HPP
3 
4 #include <BLIB/Graphics.hpp>
5 #include <BLIB/Resources.hpp>
6 #include <BLIB/Serialization.hpp>
7 #include <BLIB/Util/ImageStitcher.hpp>
8 #include <Core/Maps/Tile.hpp>
9 #include <SFML/Graphics.hpp>
10 #include <optional>
11 #include <string>
12 #include <unordered_map>
13 
14 namespace core
15 {
16 namespace map
17 {
23 class Tileset {
24 public:
25  using TileStore = std::unordered_map<Tile::IdType, bl::resource::Ref<sf::Image>>;
26  using AnimStore =
27  std::unordered_map<Tile::IdType, bl::resource::Ref<bl::gfx::a2d::AnimationData>>;
28 
32  Tileset();
33 
40  Tile::IdType addTexture(const std::string& uri);
41 
48  void removeTexture(Tile::IdType id);
49 
56  Tile::IdType addAnimation(const std::string& uri);
57 
65 
73  unsigned int tileHeight(Tile::IdType id, bool isAnim) const;
74 
81  bool loadDev(std::istream& input);
82 
89  bool loadProd(bl::serial::binary::InputStream& input);
90 
97  bool save(const std::string& file) const;
98 
106  bool saveBundle(bl::serial::binary::OutputStream& output,
107  bl::resource::bundle::FileHandlerContext& ctx) const;
108 
114  void activate(bl::engine::Engine& engine);
115 
122  bl::resource::Ref<sf::Image> getTile(Tile::IdType id) const;
123 
127  std::vector<TileStore::const_iterator> getTiles() const;
128 
135  bl::resource::Ref<bl::gfx::a2d::AnimationData> getAnim(Tile::IdType id) const;
136 
140  std::vector<AnimStore::const_iterator> getAnims() const;
141 
148  sf::FloatRect getTileTextureBounds(Tile::IdType tid) const;
149 
156  static std::string getFullPath(const std::string& path);
157 
158 private:
159  std::unordered_map<Tile::IdType, std::string> textureFiles;
160  std::unordered_map<Tile::IdType, std::string> animFiles;
161  Tile::IdType nextTextureId;
162  Tile::IdType nextAnimationId;
163 
164  TileStore textures;
165  AnimStore anims;
166  bl::engine::Engine* enginePtr;
167  std::optional<bl::util::ImageStitcher> textureStitcher;
168  std::unordered_map<Tile::IdType, glm::u32vec2> textureAtlas;
169  bl::rc::res::TextureRef combinedTextures;
170  std::unordered_map<Tile::IdType, bl::gfx::DiscreteAnimation2DPlayer> sharedAnimations;
171 
172  void clear();
173  void finishLoad();
174 
175  friend class Map;
176  friend struct bl::serial::SerializableObject<Tileset>;
177 };
178 
179 } // namespace map
180 } // namespace core
181 
182 namespace bl
183 {
184 namespace serial
185 {
186 template<>
187 struct SerializableObject<core::map::Tileset> : public SerializableObjectBase {
190 
191  SerializableField<1, TS, std::unordered_map<T::IdType, std::string>> textureFiles;
192  SerializableField<2, TS, std::unordered_map<T::IdType, std::string>> animFiles;
193 
195  : SerializableObjectBase("Tileset")
196  , textureFiles("textures", *this, &TS::textureFiles, SerializableFieldBase::Required{})
197  , animFiles("anims", *this, &TS::animFiles, SerializableFieldBase::Required{}) {}
198 };
199 
200 } // namespace serial
201 } // namespace bl
202 
203 #endif
Core classes and functionality for both the editor and game.
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
Data representation of a tile in a Map TileLayer.
Definition: Tile.hpp:30
std::uint16_t IdType
Definition: Tile.hpp:33
Stores the collection of images and animations used by Tiles in a Map.
Definition: Tileset.hpp:23
unsigned int tileHeight(Tile::IdType id, bool isAnim) const
Returns the height of the requested tile, in pixels.
Definition: Tileset.cpp:83
Tile::IdType addAnimation(const std::string &uri)
Adds the given animation to the tileset and returns its id.
Definition: Tileset.cpp:35
bool save(const std::string &file) const
Saves the tileset to the given file. No media is saved.
Definition: Tileset.cpp:138
static std::string getFullPath(const std::string &path)
Generates the full path to the given tileset file.
Definition: Tileset.cpp:190
Tile::IdType addTexture(const std::string &uri)
Adds a texture to the tileset and returns its id.
Definition: Tileset.cpp:14
std::vector< TileStore::const_iterator > getTiles() const
Returns all contained tiles.
Definition: Tileset.cpp:165
void activate(bl::engine::Engine &engine)
Prepares renderer resources and starts playing all shared animations.
Definition: Tileset.cpp:56
Tileset()
Creates an empty Tileset.
Definition: Tileset.cpp:9
bool loadDev(std::istream &input)
Loads the tileset from the development format data.
Definition: Tileset.cpp:97
sf::FloatRect getTileTextureBounds(Tile::IdType tid) const
Returns the normalized texture coordinates for the given tile id.
Definition: Tileset.cpp:194
bl::resource::Ref< bl::gfx::a2d::AnimationData > getAnim(Tile::IdType id) const
Returns an animation from the set. Returns nullptr if not found.
Definition: Tileset.cpp:175
void removeTexture(Tile::IdType id)
Removes the given texture from the tileset. Undefined behavior if any tiles reference it....
Definition: Tileset.cpp:30
bl::resource::Ref< sf::Image > getTile(Tile::IdType id) const
Returns a tile from the set. Returns nullptr if not found.
Definition: Tileset.cpp:160
std::unordered_map< Tile::IdType, bl::resource::Ref< sf::Image > > TileStore
Definition: Tileset.hpp:25
void removeAnimation(Tile::IdType id)
Removes the given animation from the tileset. Undefined behavior if any tiles reference it....
Definition: Tileset.cpp:50
std::unordered_map< Tile::IdType, bl::resource::Ref< bl::gfx::a2d::AnimationData > > AnimStore
Definition: Tileset.hpp:27
bool saveBundle(bl::serial::binary::OutputStream &output, bl::resource::bundle::FileHandlerContext &ctx) const
Saves the data from this object to the given bundle and registers dependency files.
Definition: Tileset.cpp:145
std::vector< AnimStore::const_iterator > getAnims() const
Returns all contained animations.
Definition: Tileset.cpp:180
bool loadProd(bl::serial::binary::InputStream &input)
Loads the tileset from the production format data.
Definition: Tileset.cpp:113
SerializableField< 1, TS, std::unordered_map< T::IdType, std::string > > textureFiles
Definition: Tileset.hpp:191
SerializableField< 2, TS, std::unordered_map< T::IdType, std::string > > animFiles
Definition: Tileset.hpp:192