1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00

Compare commits

2 Commits

Author SHA1 Message Date
677d59f096 fix tilesets & implement xml tile reader 2024-04-10 19:37:32 +10:00
b6308816ae msvc fixes 2024-04-10 12:46:34 +10:00
5 changed files with 38 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
add_library(base64
base64.cpp base64.h)
add_library(base64::base64 ALIAS base64)
set_target_properties(base64 PROPERTIES CXX_STANDARD 17)
set_target_properties(base64 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
target_compile_options(base64 PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/Zc:__cplusplus>)
target_include_directories(base64
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -15,7 +15,9 @@ configure_file(config.h.in config.h @ONLY)
target_sources(tmx2gba PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/config.h)
target_include_directories(tmx2gba PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(tmx2gba PROPERTIES CXX_STANDARD 20)
set_target_properties(tmx2gba PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON)
target_compile_definitions(${PROJECT_NAME} PRIVATE
$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS> # disable msvc warning

View File

@@ -13,6 +13,7 @@
#include <limits>
#include <cerrno>
#include <optional>
#include <algorithm>
template <typename T>
@@ -74,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);
@@ -122,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()),
@@ -149,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;
}
}
@@ -159,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)
@@ -171,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())
@@ -191,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;
}
@@ -205,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

View File

@@ -61,7 +61,7 @@ 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())
@@ -69,7 +69,7 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
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())
@@ -77,15 +77,15 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
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
/*

View File

@@ -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