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

Revert "port to tmxlite"

This reverts commit 35abaf71
This commit is contained in:
2024-04-08 02:54:44 +10:00
parent f9928df187
commit ebe83ffb68
44 changed files with 3656 additions and 4586 deletions

View File

@@ -1,12 +1,191 @@
/* tmxreader.cpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */
#include "tmxreader.hpp"
#include "tmxlite/Map.hpp"
#include "tmxlite/TileLayer.hpp"
#include "tmxlite/ObjectGroup.hpp"
#include "tmxtileset.hpp"
#include "tmxobject.hpp"
#include "tmxlayer.hpp"
#include <optional>
#include <algorithm>
bool TmxReader::DecodeMap(uint32_t* aOut, size_t aOutSize, const std::string& aBase64Dat)
{
// Cut leading & trailing whitespace (including newlines)
auto beg = std::find_if_not(aBase64Dat.begin(), aBase64Dat.end(), ::isspace);
if (beg == std::end(aBase64Dat))
return false;
auto end = std::find_if_not(aBase64Dat.rbegin(), aBase64Dat.rend(), ::isspace);
std::size_t begOff = std::distance(aBase64Dat.begin(), beg);
std::size_t endOff = std::distance(end, aBase64Dat.rend()) - begOff;
auto trimmed = aBase64Dat.substr(begOff, endOff);
// Decode base64 string
std::string decoded = base64_decode(trimmed);
// Decompress compressed data
auto dstSize = static_cast<mz_ulong>(aOutSize);
int res = uncompress(
reinterpret_cast<unsigned char*>(aOut),
&dstSize,
reinterpret_cast<const unsigned char*>(decoded.data()),
static_cast<mz_ulong>(decoded.size()));
decoded.clear();
if (res < 0)
return false;
return true;
}
void TmxReader::ReadTileset(rapidxml::xml_node<>* aXNode)
{
rapidxml::xml_attribute<>* xAttrib;
const char* name = "";
const char* source = "";
uint32_t firstGid = 0;
// Read name
xAttrib = aXNode->first_attribute("name");
if (xAttrib != nullptr)
name = xAttrib->value();
// Read source
xAttrib = aXNode->first_attribute("source");
if (xAttrib != nullptr)
source = xAttrib->value();
// Read first global ID
xAttrib = aXNode->first_attribute("firstgid");
if (xAttrib != nullptr)
firstGid = static_cast<uint32_t>(std::stoul(xAttrib->value()));
mTilesets.push_back(new TmxTileset(name, source, firstGid));
}
void TmxReader::ReadLayer(rapidxml::xml_node<>* aXNode)
{
rapidxml::xml_attribute<>* xAttrib;
const char* name = "";
int width = 0;
int height = 0;
uint32_t* tileDat = nullptr;
// Read name
xAttrib = aXNode->first_attribute("name");
if (xAttrib != nullptr)
name = xAttrib->value();
// Read width
xAttrib = aXNode->first_attribute("width");
if (xAttrib != nullptr)
width = std::stoi(xAttrib->value());
// Read height
xAttrib = aXNode->first_attribute("height");
if (xAttrib != nullptr)
height = std::stoi(xAttrib->value());
// Read tile data
auto xData = aXNode->first_node("data");
if (xData != nullptr)
{
// TODO: don't assume base64 & zlib
tileDat = new uint32_t[width * height];
if (!DecodeMap(tileDat, width * height * sizeof(uint32_t), std::string(xData->value())))
{
delete[] tileDat;
tileDat = nullptr;
}
}
mLayers.push_back(new TmxLayer(width, height, name, tileDat));
}
void TmxReader::ReadObjects(rapidxml::xml_node<>* aXNode)
{
for (auto xNode = aXNode->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
{
if (strcmp(xNode->name(), "object") != 0)
continue;
rapidxml::xml_attribute<>* xAttrib;
const char* name = "";
float x = 0.0f;
float y = 0.0f;
// Read name
xAttrib = xNode->first_attribute("name");
if (xAttrib != nullptr)
name = xAttrib->value();
// Read X pos
xAttrib = xNode->first_attribute("x");
if (xAttrib != nullptr)
x = std::stof(xAttrib->value());
// Read Y pos
xAttrib = xNode->first_attribute("y");
if (xAttrib != nullptr)
y = std::stof(xAttrib->value());
mObjects.push_back(new TmxObject(name, x, y));
}
}
void TmxReader::Open(std::istream& aIn)
{
// Delete old tilesets
for (auto tileset : mTilesets)
delete tileset;
mTilesets.clear();
// Delete old layers
for (auto layer : mLayers)
delete layer;
mLayers.clear();
mGidTable.clear();
// Read string into a buffer
std::stringstream buf;
buf << aIn.rdbuf();
std::string strXml = buf.str();
buf.clear();
// Parse document
rapidxml::xml_document<> xDoc;
xDoc.parse<0>(const_cast<char*>(strXml.c_str()));
// Get map node
auto xMap = xDoc.first_node("map");
if (xMap == nullptr)
return;
// Read map attribs
rapidxml::xml_attribute<>* xAttrib = nullptr;
if ((xAttrib = xMap->first_attribute("width")) != nullptr)
mWidth = std::stoi(xAttrib->value());
if ((xAttrib = xMap->first_attribute("height")) != nullptr)
mHeight = std::stoi(xAttrib->value());
// Read nodes
for (auto xNode = xMap->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
{
// Read layer nodes
if (strcmp(xNode->name(), "layer") == 0)
ReadLayer(xNode);
else
if (strcmp(xNode->name(), "tileset") == 0)
ReadTileset(xNode);
else
if (strcmp(xNode->name(), "objectgroup") == 0)
ReadObjects(xNode);
}
// Generate global id table
for (auto tileset : mTilesets)
mGidTable.push_back(tileset->GetFirstGid());
std::sort(mGidTable.rbegin(), mGidTable.rend());
}
TmxReader::Error TmxReader::Open(const std::string& inPath,
const std::string_view graphicsName,
@@ -67,7 +246,7 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
return Error::PALETTE_NOTFOUND;
// Read TMX map
mSize = Size { map.getTileCount().x, map.getTileCount().y };
mSize = Size{ map.getTileCount().x, map.getTileCount().y };
size_t numTiles = static_cast<size_t>(mSize.width) * static_cast<size_t>(mSize.height);
// Read graphics layer