mirror of
https://github.com/ScrelliCopter/tmx2gba.git
synced 2025-02-21 03:29:25 +11:00
fix tilesets & implement xml tile reader
This commit is contained in:
@@ -75,9 +75,10 @@ template <typename T>
|
|||||||
auto beg = std::find_if_not(base64.begin(), base64.end(), ::isspace);
|
auto beg = std::find_if_not(base64.begin(), base64.end(), ::isspace);
|
||||||
if (beg == std::end(base64)) { return std::nullopt; }
|
if (beg == std::end(base64)) { return std::nullopt; }
|
||||||
auto end = std::find_if_not(base64.rbegin(), base64.rend(), ::isspace);
|
auto end = std::find_if_not(base64.rbegin(), base64.rend(), ::isspace);
|
||||||
std::size_t begOff = std::distance(base64.begin(), beg);
|
auto begOff = std::distance(base64.begin(), beg);
|
||||||
std::size_t endOff = std::distance(end, base64.rend()) - begOff;
|
auto endOff = std::distance(end, base64.rend()) - begOff;
|
||||||
const auto trimmed = base64.substr(begOff, endOff);
|
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
|
// Decode base64 string
|
||||||
return base64_decode(trimmed);
|
return base64_decode(trimmed);
|
||||||
@@ -123,7 +124,6 @@ enum class Compression { NONE, GZIP, ZLIB, ZSTD, INVALID };
|
|||||||
case Compression::ZLIB:
|
case Compression::ZLIB:
|
||||||
{
|
{
|
||||||
// Decompress gzip/zlib data with zlib/zlib data miniz
|
// Decompress gzip/zlib data with zlib/zlib data miniz
|
||||||
auto dstSize = static_cast<uLongf>(sizeof(uint32_t) * destination.size());
|
|
||||||
z_stream s =
|
z_stream s =
|
||||||
{
|
{
|
||||||
.next_in = const_cast<Bytef*>(source.data()),
|
.next_in = const_cast<Bytef*>(source.data()),
|
||||||
@@ -150,7 +150,11 @@ enum class Compression { NONE, GZIP, ZLIB, ZSTD, INVALID };
|
|||||||
source.data(), source.size());
|
source.data(), source.size());
|
||||||
return !ZSTD_isError(res);
|
return !ZSTD_isError(res);
|
||||||
}
|
}
|
||||||
default: return false;
|
// Define all labels to shut up linters
|
||||||
|
case Compression::NONE:
|
||||||
|
case Compression::INVALID:
|
||||||
|
//default:
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,9 +164,11 @@ void TmxMap::ReadTileset(const pugi::xml_node& xNode)
|
|||||||
std::string_view source = xNode.attribute("source").value();
|
std::string_view source = xNode.attribute("source").value();
|
||||||
|
|
||||||
auto firstGid = UintFromStr<uint32_t>(xNode.attribute("firstgid").value()).value_or(0);
|
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)
|
void TmxMap::ReadLayer(const pugi::xml_node& xNode)
|
||||||
@@ -172,8 +178,8 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
|
|||||||
// Read layer size
|
// Read layer size
|
||||||
int width = IntFromStr<int>(xNode.attribute("width").value()).value_or(0);
|
int width = IntFromStr<int>(xNode.attribute("width").value()).value_or(0);
|
||||||
int height = IntFromStr<int>(xNode.attribute("height").value()).value_or(0);
|
int height = IntFromStr<int>(xNode.attribute("height").value()).value_or(0);
|
||||||
if (width <= 0 || height <= 0)
|
if (width <= 0 || height <= 0) { return; }
|
||||||
return;
|
const auto numTiles = static_cast<size_t>(width) * static_cast<size_t>(height);
|
||||||
|
|
||||||
auto xData = xNode.child("data");
|
auto xData = xNode.child("data");
|
||||||
if (xData.empty() || xData.first_child().empty())
|
if (xData.empty() || xData.first_child().empty())
|
||||||
@@ -192,7 +198,7 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
|
|||||||
auto compression = CompressionFromStr(xData.attribute("compression").value());
|
auto compression = CompressionFromStr(xData.attribute("compression").value());
|
||||||
if (compression == Compression::GZIP || compression == Compression::ZLIB || compression == Compression::ZSTD)
|
if (compression == Compression::GZIP || compression == Compression::ZLIB || compression == Compression::ZSTD)
|
||||||
{
|
{
|
||||||
tileDat.resize(width * height);
|
tileDat.resize(numTiles);
|
||||||
if (!Decompress(compression, tileDat, decoded.value()))
|
if (!Decompress(compression, tileDat, decoded.value()))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -206,6 +212,12 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
|
|||||||
return;
|
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
|
else
|
||||||
{
|
{
|
||||||
//TODO
|
//TODO
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
|
|||||||
// Read graphics layer
|
// Read graphics layer
|
||||||
mGraphics.reserve(numTiles);
|
mGraphics.reserve(numTiles);
|
||||||
for (auto tmxTile : layerGfx.value().get().Tiles())
|
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
|
// Read optional layers
|
||||||
if (layerPal.has_value())
|
if (layerPal.has_value())
|
||||||
@@ -69,7 +69,7 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
|
|||||||
std::vector<uint32_t> v;
|
std::vector<uint32_t> v;
|
||||||
v.reserve(numTiles);
|
v.reserve(numTiles);
|
||||||
for (auto tmxTile : layerPal.value().get().Tiles())
|
for (auto tmxTile : layerPal.value().get().Tiles())
|
||||||
v.emplace_back(tmxTile & ~FLIP_MASK);
|
v.emplace_back(tmxTile & ~TmxLayer::FLIP_MASK);
|
||||||
mPalette.emplace(v);
|
mPalette.emplace(v);
|
||||||
}
|
}
|
||||||
if (layerCls.has_value())
|
if (layerCls.has_value())
|
||||||
@@ -77,15 +77,15 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
|
|||||||
std::vector<uint32_t> v;
|
std::vector<uint32_t> v;
|
||||||
v.reserve(numTiles);
|
v.reserve(numTiles);
|
||||||
for (auto tmxTile : layerCls.value().get().Tiles())
|
for (auto tmxTile : layerCls.value().get().Tiles())
|
||||||
v.emplace_back(tmxTile & ~FLIP_MASK);
|
v.emplace_back(tmxTile & ~TmxLayer::FLIP_MASK);
|
||||||
mCollision.emplace(v);
|
mCollision.emplace(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read tilesets
|
// Read tilesets
|
||||||
const auto& tilesets = map.Tilesets();
|
const auto& tilesets = map.Tilesets();
|
||||||
mGidTable.reserve(tilesets.size());
|
mGidTable.reserve(tilesets.size());
|
||||||
for (const auto& set : tilesets)
|
std::ranges::transform(tilesets, std::back_inserter(mGidTable),
|
||||||
mGidTable.emplace_back(set.GidRange());
|
[](const auto& it) { return it.GidRange(); });
|
||||||
|
|
||||||
// Read objects
|
// Read objects
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -10,15 +10,16 @@
|
|||||||
class TmxTileset
|
class TmxTileset
|
||||||
{
|
{
|
||||||
std::string mName, mSource;
|
std::string mName, mSource;
|
||||||
uint32_t mFirstGid = 0, mLastGid = 0;
|
uint32_t mFirstGid = 0, mTileCount = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TmxTileset(const std::string_view name, const std::string_view source, uint32_t firstGid, uint32_t lastGid)
|
TmxTileset(const std::string_view name, const std::string_view source, uint32_t firstGid, uint32_t tileCount)
|
||||||
: mName(name), mSource(source), mFirstGid(firstGid), mLastGid(lastGid) {}
|
: mName(name), mSource(source), mFirstGid(firstGid), mTileCount(tileCount) {}
|
||||||
|
|
||||||
[[nodiscard]] const std::string_view Name() const noexcept { return mName; }
|
[[nodiscard]] const std::string_view Name() const noexcept { return mName; }
|
||||||
[[nodiscard]] const std::string_view Source() const noexcept { return mSource; }
|
[[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
|
#endif//TMXTILESET_HPP
|
||||||
|
|||||||
Reference in New Issue
Block a user