Skip to content

Commit

Permalink
fix tilesets & implement xml tile reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrelliCopter committed Apr 10, 2024
1 parent b630881 commit 677d59f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
32 changes: 22 additions & 10 deletions src/tmxmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ template <typename T>
auto beg = std::find_if_not(base64.begin(), base64.end(), ::isspace);
if (beg == std::end(base64)) { return std::nullopt; }
auto end = std::find_if_not(base64.rbegin(), base64.rend(), ::isspace);
std::size_t begOff = std::distance(base64.begin(), beg);
std::size_t endOff = std::distance(end, base64.rend()) - begOff;
const auto trimmed = base64.substr(begOff, endOff);
auto begOff = std::distance(base64.begin(), beg);
auto endOff = std::distance(end, base64.rend()) - begOff;
using size_type = std::string::size_type;
const auto trimmed = base64.substr(static_cast<size_type>(begOff), static_cast<size_type>(endOff));

// Decode base64 string
return base64_decode(trimmed);
Expand Down Expand Up @@ -123,7 +124,6 @@ enum class Compression { NONE, GZIP, ZLIB, ZSTD, INVALID };
case Compression::ZLIB:
{
// Decompress gzip/zlib data with zlib/zlib data miniz
auto dstSize = static_cast<uLongf>(sizeof(uint32_t) * destination.size());
z_stream s =
{
.next_in = const_cast<Bytef*>(source.data()),
Expand All @@ -150,7 +150,11 @@ enum class Compression { NONE, GZIP, ZLIB, ZSTD, INVALID };
source.data(), source.size());
return !ZSTD_isError(res);
}
default: return false;
// Define all labels to shut up linters
case Compression::NONE:
case Compression::INVALID:
//default:
return false;
}
}

Expand All @@ -160,9 +164,11 @@ void TmxMap::ReadTileset(const pugi::xml_node& xNode)
std::string_view source = xNode.attribute("source").value();

auto firstGid = UintFromStr<uint32_t>(xNode.attribute("firstgid").value()).value_or(0);
auto lastGid = UintFromStr<uint32_t>(xNode.attribute("lastgid").value()).value_or(0);
auto numTiles = UintFromStr<uint32_t>(xNode.attribute("tilecount").value()).value_or(0);
if (numTiles == 0)
return; // FIXME: warn about empty tilesets or something

mTilesets.emplace_back(TmxTileset(name, source, firstGid, lastGid));
mTilesets.emplace_back(TmxTileset(name, source, firstGid, numTiles));
}

void TmxMap::ReadLayer(const pugi::xml_node& xNode)
Expand All @@ -172,8 +178,8 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
// Read layer size
int width = IntFromStr<int>(xNode.attribute("width").value()).value_or(0);
int height = IntFromStr<int>(xNode.attribute("height").value()).value_or(0);
if (width <= 0 || height <= 0)
return;
if (width <= 0 || height <= 0) { return; }
const auto numTiles = static_cast<size_t>(width) * static_cast<size_t>(height);

auto xData = xNode.child("data");
if (xData.empty() || xData.first_child().empty())
Expand All @@ -192,7 +198,7 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
auto compression = CompressionFromStr(xData.attribute("compression").value());
if (compression == Compression::GZIP || compression == Compression::ZLIB || compression == Compression::ZSTD)
{
tileDat.resize(width * height);
tileDat.resize(numTiles);
if (!Decompress(compression, tileDat, decoded.value()))
return;
}
Expand All @@ -206,6 +212,12 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
return;
}
}
else if (encoding == Encoding::XML)
{
tileDat.reserve(numTiles);
std::ranges::transform(xData.children("tile"), std::back_inserter(tileDat), [](auto it)
-> uint32_t { return UintFromStr<uint32_t>(it.attribute("gid").value()).value_or(0); });
}
else
{
//TODO
Expand Down
10 changes: 5 additions & 5 deletions src/tmxreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,31 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
// Read graphics layer
mGraphics.reserve(numTiles);
for (auto tmxTile : layerGfx.value().get().Tiles())
mGraphics.emplace_back(Tile{ tmxTile & ~FLIP_MASK, static_cast<uint8_t>((tmxTile & FLIP_MASK) >> 28) });
mGraphics.emplace_back(Tile{ tmxTile & ~TmxLayer::FLIP_MASK, static_cast<uint8_t>((tmxTile & TmxLayer::FLIP_MASK) >> 28) });

// Read optional layers
if (layerPal.has_value())
{
std::vector<uint32_t> v;
v.reserve(numTiles);
for (auto tmxTile : layerPal.value().get().Tiles())
v.emplace_back(tmxTile & ~FLIP_MASK);
v.emplace_back(tmxTile & ~TmxLayer::FLIP_MASK);
mPalette.emplace(v);
}
if (layerCls.has_value())
{
std::vector<uint32_t> v;
v.reserve(numTiles);
for (auto tmxTile : layerCls.value().get().Tiles())
v.emplace_back(tmxTile & ~FLIP_MASK);
v.emplace_back(tmxTile & ~TmxLayer::FLIP_MASK);
mCollision.emplace(v);
}

// Read tilesets
const auto& tilesets = map.Tilesets();
mGidTable.reserve(tilesets.size());
for (const auto& set : tilesets)
mGidTable.emplace_back(set.GidRange());
std::ranges::transform(tilesets, std::back_inserter(mGidTable),
[](const auto& it) { return it.GidRange(); });

// Read objects
/*
Expand Down
9 changes: 5 additions & 4 deletions src/tmxtileset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
class TmxTileset
{
std::string mName, mSource;
uint32_t mFirstGid = 0, mLastGid = 0;
uint32_t mFirstGid = 0, mTileCount = 0;

public:
TmxTileset(const std::string_view name, const std::string_view source, uint32_t firstGid, uint32_t lastGid)
: mName(name), mSource(source), mFirstGid(firstGid), mLastGid(lastGid) {}
TmxTileset(const std::string_view name, const std::string_view source, uint32_t firstGid, uint32_t tileCount)
: mName(name), mSource(source), mFirstGid(firstGid), mTileCount(tileCount) {}

[[nodiscard]] const std::string_view Name() const noexcept { return mName; }
[[nodiscard]] const std::string_view Source() const noexcept { return mSource; }
[[nodiscard]] constexpr const std::pair<uint32_t, uint32_t> GidRange() const noexcept { return { mFirstGid, mLastGid }; }
[[nodiscard]] constexpr const std::pair<uint32_t, uint32_t> GidRange() const noexcept
{ return { mFirstGid, mFirstGid + mTileCount - 1 }; }
};

#endif//TMXTILESET_HPP

0 comments on commit 677d59f

Please sign in to comment.