12 , enginePtr(nullptr) {}
15 textureFiles.emplace(nextTextureId, uri);
16 const auto it = textures
17 .emplace(nextTextureId,
22 textureAtlas.try_emplace(nextTextureId, textureStitcher.value().addImage(*it->second));
23 combinedTextures->ensureSize({textureStitcher.value().getStitchedImage().getSize().x,
24 textureStitcher.value().getStitchedImage().getSize().y});
25 combinedTextures.asBindlessTexture()->update(textureStitcher.value().getStitchedImage());
27 return nextTextureId++;
31 textureFiles.erase(
id);
36 animFiles.emplace(nextAnimationId, uri);
38 .emplace(nextAnimationId,
39 AnimationManager::load(
42 if (it->second->isLooping() && enginePtr) {
43 sharedAnimations.try_emplace(nextAnimationId)
44 .first->second.create(
45 *enginePtr, it->second, bl::gfx::DiscreteAnimation2DPlayer::Auto,
true,
true);
47 return nextAnimationId++;
53 sharedAnimations.erase(
id);
57 if (enginePtr !=
nullptr) {
return; }
59 BL_LOG_INFO <<
"Activating tileset...";
62 BL_LOG_INFO <<
"Creating shared animation players...";
63 for (
const auto& apair : anims) {
64 if (apair.second->isLooping()) {
65 sharedAnimations.try_emplace(apair.first)
66 .first->second.create(
67 engine, apair.second, bl::gfx::DiscreteAnimation2DPlayer::Auto,
true,
true);
71 BL_LOG_INFO <<
"Stitching tiles into atlas...";
72 textureStitcher.emplace(engine.renderer());
73 for (
const auto& tpair : textures) {
74 textureAtlas.try_emplace(tpair.first, textureStitcher.value().addImage(*tpair.second));
76 combinedTextures = engine.renderer().texturePool().createTexture(
77 textureStitcher.value().getStitchedImage(),
78 engine.renderer().vulkanState().samplerCache.noFilterEdgeClamped());
80 BL_LOG_INFO <<
"Tileset activated";
85 auto anim = anims.find(
id);
86 if (anim != anims.end()) {
87 if (anim->second->frameCount() > 0)
return anim->second->getFrameSize(0).y;
91 auto txtr = textures.find(
id);
92 if (txtr != textures.end()) {
return txtr->second->getSize().y; }
99 if (!bl::serial::json::Serializer<Tileset>::deserializeStream(input, *
this))
return false;
104 void Tileset::clear() {
105 textureFiles.clear();
107 nextTextureId = nextAnimationId = 1;
110 sharedAnimations.clear();
115 if (!bl::serial::binary::Serializer<Tileset>::deserialize(input, *
this))
return false;
120 void Tileset::finishLoad() {
121 for (
const auto& tpair : textureFiles) {
122 textures.emplace(tpair.first,
125 if (tpair.first >= nextTextureId) nextTextureId = tpair.first + 1;
128 for (
const auto& apair : animFiles) {
130 .emplace(apair.first,
131 AnimationManager::load(bl::util::FileUtil::joinPath(
134 if (apair.first >= nextAnimationId) nextAnimationId = apair.first + 1;
142 return bl::serial::json::Serializer<Tileset>::serializeStream(output, *
this, 4, 0);
146 bl::resource::bundle::FileHandlerContext& ctx)
const {
147 if (!bl::serial::binary::Serializer<Tileset>::serialize(output, *
this))
return false;
148 for (
const auto& tp : textureFiles) {
150 if (bl::util::FileUtil::exists(p)) { ctx.addDependencyFile(p); }
152 for (
const auto& ap : animFiles) {
153 const std::string p =
155 if (bl::util::FileUtil::exists(p)) { ctx.addDependencyFile(p); }
161 auto it = textures.find(
id);
162 return it != textures.end() ? it->second : bl::resource::Ref<sf::Image>{};
166 std::vector<TileStore::const_iterator> result;
167 result.reserve(textures.size());
169 auto it = textures.find(
id);
170 if (it != textures.end()) { result.emplace_back(it); }
176 auto it = anims.find(
id);
177 return it != anims.end() ? it->second : bl::resource::Ref<bl::gfx::a2d::AnimationData>{};
181 std::vector<AnimStore::const_iterator> result;
182 result.reserve(anims.size());
184 auto it = anims.find(
id);
185 if (it != anims.end()) { result.emplace_back(it); }
195 const auto tit = textures.find(tid);
196 if (tit == textures.end()) {
197 BL_LOG_ERROR <<
"Unable to find texture with id: " << tid;
198 return {-1.f, -1.f, -1.f, -1.f};
201 const auto ait = textureAtlas.find(tid);
202 if (ait == textureAtlas.end()) {
203 BL_LOG_ERROR <<
"Texture with id " << tid <<
" is missing from atlas";
204 return {-1.f, -1.f, -1.f, -1.f};
207 return {
static_cast<float>(ait->second.x),
208 static_cast<float>(ait->second.y),
209 static_cast<float>(tit->second->getSize().x),
210 static_cast<float>(tit->second->getSize().y)};
Core classes and functionality for both the editor and game.
unsigned int tileHeight(Tile::IdType id, bool isAnim) const
Returns the height of the requested tile, in pixels.
Tile::IdType addAnimation(const std::string &uri)
Adds the given animation to the tileset and returns its id.
bool save(const std::string &file) const
Saves the tileset to the given file. No media is saved.
static std::string getFullPath(const std::string &path)
Generates the full path to the given tileset file.
Tile::IdType addTexture(const std::string &uri)
Adds a texture to the tileset and returns its id.
std::vector< TileStore::const_iterator > getTiles() const
Returns all contained tiles.
void activate(bl::engine::Engine &engine)
Prepares renderer resources and starts playing all shared animations.
Tileset()
Creates an empty Tileset.
bool loadDev(std::istream &input)
Loads the tileset from the development format data.
sf::FloatRect getTileTextureBounds(Tile::IdType tid) const
Returns the normalized texture coordinates for the given tile id.
bl::resource::Ref< bl::gfx::a2d::AnimationData > getAnim(Tile::IdType id) const
Returns an animation from the set. Returns nullptr if not found.
void removeTexture(Tile::IdType id)
Removes the given texture from the tileset. Undefined behavior if any tiles reference it....
bl::resource::Ref< sf::Image > getTile(Tile::IdType id) const
Returns a tile from the set. Returns nullptr if not found.
void removeAnimation(Tile::IdType id)
Removes the given animation from the tileset. Undefined behavior if any tiles reference it....
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.
std::vector< AnimStore::const_iterator > getAnims() const
Returns all contained animations.
bool loadProd(bl::serial::binary::InputStream &input)
Loads the tileset from the production format data.
static const std::string & MapAnimationPath()
static const std::string & MapTilePath()
static const std::string & TilesetPath()