mirror of
https://github.com/ScrelliCopter/tmx2gba.git
synced 2025-02-21 03:29:25 +11:00
uncringe comments somewhat
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
#include "tmxlayer.hpp"
|
#include "tmxlayer.hpp"
|
||||||
#include "tmxobject.hpp"
|
#include "tmxobject.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -75,7 +74,7 @@ void ParseArgs(int argc, char** argv, Arguments& p)
|
|||||||
|
|
||||||
bool CheckArgs(const Arguments& params)
|
bool CheckArgs(const Arguments& params)
|
||||||
{
|
{
|
||||||
// Check my paranoia.
|
// Check my paranoia
|
||||||
if (params.inPath.empty())
|
if (params.inPath.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "No input file specified." << std::endl;
|
std::cerr << "No input file specified." << std::endl;
|
||||||
@@ -215,7 +214,7 @@ int main(int argc, char** argv)
|
|||||||
if (!CheckArgs(p))
|
if (!CheckArgs(p))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// Object mappings.
|
// Object mappings
|
||||||
std::map<std::string, uint32_t> objMapping;
|
std::map<std::string, uint32_t> objMapping;
|
||||||
if (p.objExport)
|
if (p.objExport)
|
||||||
{
|
{
|
||||||
@@ -242,7 +241,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open & read input file.
|
// Open & read input file
|
||||||
TmxReader tmx;
|
TmxReader tmx;
|
||||||
std::ifstream fin(p.inPath);
|
std::ifstream fin(p.inPath);
|
||||||
if (!fin.is_open())
|
if (!fin.is_open())
|
||||||
@@ -252,7 +251,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
tmx.Open(fin);
|
tmx.Open(fin);
|
||||||
|
|
||||||
// Get layers.
|
// Get layers
|
||||||
if (tmx.GetLayerCount() == 0)
|
if (tmx.GetLayerCount() == 0)
|
||||||
{
|
{
|
||||||
std::cerr << "No layers found." << std::endl;
|
std::cerr << "No layers found." << std::endl;
|
||||||
@@ -274,7 +273,7 @@ int main(int argc, char** argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open output files.
|
// Open output files
|
||||||
std::ofstream foutS(p.outPath + ".s");
|
std::ofstream foutS(p.outPath + ".s");
|
||||||
std::ofstream foutH(p.outPath + ".h");
|
std::ofstream foutH(p.outPath + ".h");
|
||||||
if (!foutS.is_open() || !foutH.is_open())
|
if (!foutS.is_open() || !foutH.is_open())
|
||||||
@@ -288,7 +287,7 @@ int main(int argc, char** argv)
|
|||||||
if (slashPos != -1)
|
if (slashPos != -1)
|
||||||
name = name.substr(slashPos + 1);
|
name = name.substr(slashPos + 1);
|
||||||
|
|
||||||
// Write header guards.
|
// Write header guards
|
||||||
std::string guard = "TMX2GBA_" + name;
|
std::string guard = "TMX2GBA_" + name;
|
||||||
for (auto& c: guard)
|
for (auto& c: guard)
|
||||||
c = static_cast<char>(toupper(c));
|
c = static_cast<char>(toupper(c));
|
||||||
@@ -299,7 +298,7 @@ int main(int argc, char** argv)
|
|||||||
foutH << "#define " << name << "Height " << tmx.GetHeight() << std::endl;
|
foutH << "#define " << name << "Height " << tmx.GetHeight() << std::endl;
|
||||||
foutH << std::endl;
|
foutH << std::endl;
|
||||||
|
|
||||||
// Convert to GBA-friendly charmap data.
|
// Convert to GBA-friendly charmap data
|
||||||
const uint32_t* gfxTiles = layerGfx->GetData();
|
const uint32_t* gfxTiles = layerGfx->GetData();
|
||||||
const uint32_t* palTiles = (layerPal == nullptr) ? nullptr : layerPal->GetData();
|
const uint32_t* palTiles = (layerPal == nullptr) ? nullptr : layerPal->GetData();
|
||||||
std::vector<uint16_t> charDat;
|
std::vector<uint16_t> charDat;
|
||||||
@@ -316,7 +315,7 @@ int main(int argc, char** argv)
|
|||||||
flags |= (read & TmxLayer::FLIP_HORZ) ? 0x4 : 0x0;
|
flags |= (read & TmxLayer::FLIP_HORZ) ? 0x4 : 0x0;
|
||||||
flags |= (read & TmxLayer::FLIP_VERT) ? 0x8 : 0x0;
|
flags |= (read & TmxLayer::FLIP_VERT) ? 0x8 : 0x0;
|
||||||
|
|
||||||
// Determine palette ID.
|
// Determine palette ID
|
||||||
uint32_t idx = 0;
|
uint32_t idx = 0;
|
||||||
if (palTiles != nullptr)
|
if (palTiles != nullptr)
|
||||||
idx = tmx.LidFromGid((*palTiles++) & ~TmxLayer::FLIP_MASK);
|
idx = tmx.LidFromGid((*palTiles++) & ~TmxLayer::FLIP_MASK);
|
||||||
@@ -327,7 +326,7 @@ int main(int argc, char** argv)
|
|||||||
charDat.push_back(tile | (static_cast<uint16_t>(flags) << 8));
|
charDat.push_back(tile | (static_cast<uint16_t>(flags) << 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save out charmap.
|
// Write out charmap
|
||||||
foutH << "#define " << name << "TilesLen " << charDat.size() * 2 << std::endl;
|
foutH << "#define " << name << "TilesLen " << charDat.size() * 2 << std::endl;
|
||||||
foutH << "extern const unsigned short " << name << "Tiles[" << charDat.size() << "];" << std::endl;
|
foutH << "extern const unsigned short " << name << "Tiles[" << charDat.size() << "];" << std::endl;
|
||||||
foutH << std::endl;
|
foutH << std::endl;
|
||||||
@@ -340,7 +339,7 @@ int main(int argc, char** argv)
|
|||||||
WriteArray<uint16_t>(foutS, charDat);
|
WriteArray<uint16_t>(foutS, charDat);
|
||||||
foutS << std::endl;
|
foutS << std::endl;
|
||||||
|
|
||||||
// Convert collision map & save it out.
|
// Convert collision map & write it out
|
||||||
if (layerCls != nullptr)
|
if (layerCls != nullptr)
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> vucCollisionDat;
|
std::vector<uint8_t> vucCollisionDat;
|
||||||
@@ -353,7 +352,7 @@ int main(int argc, char** argv)
|
|||||||
vucCollisionDat.push_back(ucTile);
|
vucCollisionDat.push_back(ucTile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to nicely append "_collision" to the output name.
|
// Try to nicely append "_collision" to the output name
|
||||||
std::string path;
|
std::string path;
|
||||||
size_t extPos = p.outPath.find_last_of('.');
|
size_t extPos = p.outPath.find_last_of('.');
|
||||||
if (extPos != std::string::npos)
|
if (extPos != std::string::npos)
|
||||||
@@ -361,7 +360,7 @@ int main(int argc, char** argv)
|
|||||||
else
|
else
|
||||||
path = p.outPath + "_collision";
|
path = p.outPath + "_collision";
|
||||||
|
|
||||||
// Save it out.
|
// Write collision
|
||||||
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size() << std::endl;
|
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size() << std::endl;
|
||||||
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size() << "];" << std::endl;
|
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size() << "];" << std::endl;
|
||||||
foutH << std::endl;
|
foutH << std::endl;
|
||||||
@@ -393,7 +392,7 @@ int main(int argc, char** argv)
|
|||||||
objDat.push_back((int)(y * 256.0f));
|
objDat.push_back((int)(y * 256.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save it out.
|
// Write objects
|
||||||
foutH << "#define " << name << "ObjCount " << objDat.size() / 3 << std::endl;
|
foutH << "#define " << name << "ObjCount " << objDat.size() / 3 << std::endl;
|
||||||
foutH << "#define " << name << "ObjdatLen " << objDat.size() * sizeof(int) << std::endl;
|
foutH << "#define " << name << "ObjdatLen " << objDat.size() * sizeof(int) << std::endl;
|
||||||
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size() << "];" << std::endl;
|
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size() << "];" << std::endl;
|
||||||
|
|||||||
@@ -14,12 +14,12 @@
|
|||||||
|
|
||||||
TmxReader::~TmxReader()
|
TmxReader::~TmxReader()
|
||||||
{
|
{
|
||||||
// Delete old tilesets.
|
// Delete old tilesets
|
||||||
for (auto pTileset : mTilesets)
|
for (auto pTileset : mTilesets)
|
||||||
delete pTileset;
|
delete pTileset;
|
||||||
mTilesets.clear();
|
mTilesets.clear();
|
||||||
|
|
||||||
// Delete old layers.
|
// Delete old layers
|
||||||
for (auto pLay : mLayers)
|
for (auto pLay : mLayers)
|
||||||
delete pLay;
|
delete pLay;
|
||||||
mLayers.clear();
|
mLayers.clear();
|
||||||
@@ -37,10 +37,10 @@ bool TmxReader::DecodeMap(uint32_t* aOut, size_t aOutSize, const std::string& aB
|
|||||||
std::size_t endOff = std::distance(end, aBase64Dat.rend()) - begOff;
|
std::size_t endOff = std::distance(end, aBase64Dat.rend()) - begOff;
|
||||||
auto trimmed = aBase64Dat.substr(begOff, endOff);
|
auto trimmed = aBase64Dat.substr(begOff, endOff);
|
||||||
|
|
||||||
// Decode base64 string.
|
// Decode base64 string
|
||||||
std::string decoded = base64_decode(trimmed);
|
std::string decoded = base64_decode(trimmed);
|
||||||
|
|
||||||
// Decompress compressed data.
|
// Decompress compressed data
|
||||||
auto dstSize = static_cast<mz_ulong>(aOutSize);
|
auto dstSize = static_cast<mz_ulong>(aOutSize);
|
||||||
int res = uncompress(
|
int res = uncompress(
|
||||||
(unsigned char*)aOut,
|
(unsigned char*)aOut,
|
||||||
@@ -62,17 +62,17 @@ void TmxReader::ReadTileset(rapidxml::xml_node<>* aXNode)
|
|||||||
const char* source = "";
|
const char* source = "";
|
||||||
uint32_t firstGid = 0;
|
uint32_t firstGid = 0;
|
||||||
|
|
||||||
// Read name.
|
// Read name
|
||||||
xAttrib = aXNode->first_attribute("name");
|
xAttrib = aXNode->first_attribute("name");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
name = xAttrib->value();
|
name = xAttrib->value();
|
||||||
|
|
||||||
// Read source.
|
// Read source
|
||||||
xAttrib = aXNode->first_attribute("source");
|
xAttrib = aXNode->first_attribute("source");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
source = xAttrib->value();
|
source = xAttrib->value();
|
||||||
|
|
||||||
// Read first global ID.
|
// Read first global ID
|
||||||
xAttrib = aXNode->first_attribute("firstgid");
|
xAttrib = aXNode->first_attribute("firstgid");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
firstGid = std::stoul(xAttrib->value());
|
firstGid = std::stoul(xAttrib->value());
|
||||||
@@ -88,26 +88,26 @@ void TmxReader::ReadLayer(rapidxml::xml_node<>* aXNode)
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
uint32_t* tileDat = nullptr;
|
uint32_t* tileDat = nullptr;
|
||||||
|
|
||||||
// Read name.
|
// Read name
|
||||||
xAttrib = aXNode->first_attribute("name");
|
xAttrib = aXNode->first_attribute("name");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
name = xAttrib->value();
|
name = xAttrib->value();
|
||||||
|
|
||||||
// Read width.
|
// Read width
|
||||||
xAttrib = aXNode->first_attribute("width");
|
xAttrib = aXNode->first_attribute("width");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
width = std::stoi(xAttrib->value());
|
width = std::stoi(xAttrib->value());
|
||||||
|
|
||||||
// Read height.
|
// Read height
|
||||||
xAttrib = aXNode->first_attribute("height");
|
xAttrib = aXNode->first_attribute("height");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
height = std::stoi(xAttrib->value());
|
height = std::stoi(xAttrib->value());
|
||||||
|
|
||||||
// Read tile data.
|
// Read tile data
|
||||||
auto xData = aXNode->first_node("data");
|
auto xData = aXNode->first_node("data");
|
||||||
if (xData != nullptr)
|
if (xData != nullptr)
|
||||||
{
|
{
|
||||||
// TODO: don't assume base64 & zlib.
|
// TODO: don't assume base64 & zlib
|
||||||
tileDat = new uint32_t[width * height];
|
tileDat = new uint32_t[width * height];
|
||||||
if (!DecodeMap(tileDat, width * height * sizeof(uint32_t), std::string(xData->value())))
|
if (!DecodeMap(tileDat, width * height * sizeof(uint32_t), std::string(xData->value())))
|
||||||
{
|
{
|
||||||
@@ -131,17 +131,17 @@ void TmxReader::ReadObjects(rapidxml::xml_node<>* aXNode)
|
|||||||
float x = 0.0f;
|
float x = 0.0f;
|
||||||
float y = 0.0f;
|
float y = 0.0f;
|
||||||
|
|
||||||
// Read name.
|
// Read name
|
||||||
xAttrib = xNode->first_attribute("name");
|
xAttrib = xNode->first_attribute("name");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
name = xAttrib->value();
|
name = xAttrib->value();
|
||||||
|
|
||||||
// Read X pos.
|
// Read X pos
|
||||||
xAttrib = xNode->first_attribute("x");
|
xAttrib = xNode->first_attribute("x");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
x = std::stof(xAttrib->value());
|
x = std::stof(xAttrib->value());
|
||||||
|
|
||||||
// Read Y pos.
|
// Read Y pos
|
||||||
xAttrib = xNode->first_attribute("y");
|
xAttrib = xNode->first_attribute("y");
|
||||||
if (xAttrib != nullptr)
|
if (xAttrib != nullptr)
|
||||||
y = std::stof(xAttrib->value());
|
y = std::stof(xAttrib->value());
|
||||||
@@ -152,44 +152,44 @@ void TmxReader::ReadObjects(rapidxml::xml_node<>* aXNode)
|
|||||||
|
|
||||||
void TmxReader::Open(std::istream& aIn)
|
void TmxReader::Open(std::istream& aIn)
|
||||||
{
|
{
|
||||||
// Delete old tilesets.
|
// Delete old tilesets
|
||||||
for (auto tileset : mTilesets)
|
for (auto tileset : mTilesets)
|
||||||
delete tileset;
|
delete tileset;
|
||||||
mTilesets.clear();
|
mTilesets.clear();
|
||||||
|
|
||||||
// Delete old layers.
|
// Delete old layers
|
||||||
for (auto layer : mLayers)
|
for (auto layer : mLayers)
|
||||||
delete layer;
|
delete layer;
|
||||||
mLayers.clear();
|
mLayers.clear();
|
||||||
|
|
||||||
mGidTable.clear();
|
mGidTable.clear();
|
||||||
|
|
||||||
// Read string into a buffer.
|
// Read string into a buffer
|
||||||
std::stringstream buf;
|
std::stringstream buf;
|
||||||
buf << aIn.rdbuf();
|
buf << aIn.rdbuf();
|
||||||
std::string strXml = buf.str();
|
std::string strXml = buf.str();
|
||||||
buf.clear();
|
buf.clear();
|
||||||
|
|
||||||
// Parse document.
|
// Parse document
|
||||||
rapidxml::xml_document<> xDoc;
|
rapidxml::xml_document<> xDoc;
|
||||||
xDoc.parse<0>((char*)strXml.c_str());
|
xDoc.parse<0>((char*)strXml.c_str());
|
||||||
|
|
||||||
// Get map node.
|
// Get map node
|
||||||
auto xMap = xDoc.first_node("map");
|
auto xMap = xDoc.first_node("map");
|
||||||
if (xMap == nullptr)
|
if (xMap == nullptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Read map attribs.
|
// Read map attribs
|
||||||
rapidxml::xml_attribute<>* xAttrib = nullptr;
|
rapidxml::xml_attribute<>* xAttrib = nullptr;
|
||||||
if ((xAttrib = xMap->first_attribute("width")) != nullptr)
|
if ((xAttrib = xMap->first_attribute("width")) != nullptr)
|
||||||
mWidth = std::stoi(xAttrib->value());
|
mWidth = std::stoi(xAttrib->value());
|
||||||
if ((xAttrib = xMap->first_attribute("height")) != nullptr)
|
if ((xAttrib = xMap->first_attribute("height")) != nullptr)
|
||||||
mHeight = std::stoi(xAttrib->value());
|
mHeight = std::stoi(xAttrib->value());
|
||||||
|
|
||||||
// Read nodes.
|
// Read nodes
|
||||||
for (auto xNode = xMap->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
|
for (auto xNode = xMap->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
|
||||||
{
|
{
|
||||||
// Read layer nodes.
|
// Read layer nodes
|
||||||
if (strcmp(xNode->name(), "layer") == 0)
|
if (strcmp(xNode->name(), "layer") == 0)
|
||||||
ReadLayer(xNode);
|
ReadLayer(xNode);
|
||||||
else
|
else
|
||||||
@@ -200,7 +200,7 @@ void TmxReader::Open(std::istream& aIn)
|
|||||||
ReadObjects(xNode);
|
ReadObjects(xNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate global id table.
|
// Generate global id table
|
||||||
for (auto tileset : mTilesets)
|
for (auto tileset : mTilesets)
|
||||||
mGidTable.push_back(tileset->GetFirstGid());
|
mGidTable.push_back(tileset->GetFirstGid());
|
||||||
std::sort(mGidTable.rbegin(), mGidTable.rend());
|
std::sort(mGidTable.rbegin(), mGidTable.rend());
|
||||||
|
|||||||
Reference in New Issue
Block a user