1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00
This commit is contained in:
2022-09-07 02:34:09 +10:00
parent 6c2cca3fd5
commit a5ac1d9bfe
12 changed files with 470 additions and 735 deletions

View File

@@ -20,9 +20,9 @@
*/
#include "tmxreader.h"
#include "tmxlayer.h"
#include "tmxobject.h"
#include "tmxreader.hpp"
#include "tmxlayer.hpp"
#include "tmxobject.hpp"
#include <iostream>
#include <iomanip>
#include <fstream>
@@ -34,22 +34,9 @@
#include <ultragetopt.h>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::ifstream;
using std::ofstream;
using std::stoi;
using std::min;
using std::max;
using std::ios;
static const string g_strUsage = "Usage: tmx2gba [-h] [-f file] [-r offset] [-lyc name] [-p 0-15] [-m name;id] <-i inpath> <-o outpath>";
static const string g_strHelpShort = "Run 'tmx2gba -h' to view all available options.";
static const string g_strHelpFull = R"(
const std::string helpUsage = "Usage: tmx2gba [-h] [-f file] [-r offset] [-lyc name] [-p 0-15] [-m name;id] <-i inpath> <-o outpath>";
const std::string helpShort = "Run 'tmx2gba -h' to view all available options.";
const std::string helpFull = R"(
-h ------------ Display this help & command info.
-l <name> ----- Name of layer to use (default first layer in TMX).
-y <name> ----- Layer for palette mappings.
@@ -61,66 +48,44 @@ static const string g_strHelpFull = R"(
-o <path> ----- Path to output files.
-f <file> ----- Specify a file to use for flags, will override any options specified on the command line.)";
struct SParams
struct Arguments
{
bool help = false;
string inPath, outPath;
string layer, collisionlay, paletteLay;
string flagFile;
std::string inPath, outPath;
std::string layer, collisionlay, paletteLay;
std::string flagFile;
int offset = 0;
int palette = 0;
vector<string> objMappings;
std::vector<std::string> objMappings;
bool objExport = false;
};
void ParseArgs ( int argc, char** argv, SParams* params )
void ParseArgs(int argc, char** argv, Arguments& p)
{
char cOption;
int opt;
optreset = 1;
while ( ( cOption = (char)getopt ( argc, argv, "hr:l:c:p:y:m:i:o:f:" ) ) > 0 )
while ((opt = getopt(argc, argv, "hr:l:c:p:y:m:i:o:f:")) > 0)
{
switch ( cOption )
switch (opt)
{
case ( 'h' ):
params->help = true;
case ('h'):
p.help = true;
return;
case ( 'l' ):
params->layer = optarg;
case ('l'): p.layer = optarg; break;
case ('c'): p.collisionlay = optarg; break;
case ('y'): p.paletteLay = optarg; break;
case ('r'): p.offset = std::stoi(optarg); break;
case ('p'): p.palette = std::stoi(optarg); break;
case ('m'):
p.objExport = true;
p.objMappings.emplace_back(optarg);
break;
case ( 'c' ):
params->collisionlay = optarg;
break;
case ( 'y' ):
params->paletteLay = optarg;
break;
case ( 'r' ):
params->offset = stoi ( optarg );
break;
case ( 'p' ):
params->palette = stoi ( optarg );
break;
case ( 'm' ):
params->objExport = true;
params->objMappings.push_back ( optarg );
break;
case ( 'i' ):
params->inPath = optarg;
break;
case ( 'o' ):
params->outPath = optarg;
break;
case ( 'f' ):
params->flagFile = optarg;
break;
case ('i'): p.inPath = optarg; break;
case ('o'): p.outPath = optarg; break;
case ('f'): p.flagFile = optarg; break;
default:
break;
@@ -128,25 +93,25 @@ void ParseArgs ( int argc, char** argv, SParams* params )
}
}
bool CheckArgs ( const SParams* params )
bool CheckArgs(const Arguments& params)
{
// Check my paranoia.
if ( params->inPath.empty () )
if (params.inPath.empty())
{
cerr << "No input file specified." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
std::cerr << "No input file specified." << std::endl;
std::cout << helpUsage << std::endl << helpShort << std::endl;
return false;
}
if ( params->outPath.empty () )
if (params.outPath.empty())
{
cerr << "No output file specified." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
std::cerr << "No output file specified." << std::endl;
std::cout << helpUsage << std::endl << helpShort << std::endl;
return false;
}
if ( params->palette < 0 || params->palette > 15 )
if (params.palette < 0 || params.palette > 15)
{
cerr << "Invalid palette index." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
std::cerr << "Invalid palette index." << std::endl;
std::cout << helpUsage << std::endl << helpShort << std::endl;
return false;
}
@@ -154,50 +119,36 @@ bool CheckArgs ( const SParams* params )
}
template <typename T> constexpr const char* DatType();
template <> constexpr const char* DatType<uint8_t>() { return ".byte"; }
template <> constexpr const char* DatType<uint16_t>() { return ".hword"; }
template <> constexpr const char* DatType<uint32_t>() { return ".word"; }
template <typename T>
void WriteArray ( ofstream& a_fout, const vector<T>& a_dat, int a_perCol = 16 )
void WriteArray(std::ofstream& aOut, const std::vector<T>& aDat, int aPerCol = 16)
{
const int w = sizeof(T) * 2;
int col = 0;
string datType = "ERR";
if ( sizeof(T) == 1 )
{
datType = ".byte";
}
else
if ( sizeof(T) == 2 )
{
datType = ".hword";
}
else
if ( sizeof(T) == 4 )
{
datType = ".word";
}
a_fout.setf ( ios::hex, ios::basefield );
a_fout.setf ( ios::showbase );
aOut.setf(std::ios::hex, std::ios::basefield);
aOut.setf(std::ios::showbase);
size_t i = 0;
for ( T element : a_dat )
for (T element : aDat)
{
if ( col == 0 )
{
a_fout << "\t" << datType << " ";
}
if (col == 0)
aOut << "\t" << DatType<T>() << " ";
a_fout << std::hex << (int)element;
aOut << std::hex << (int)element;
if ( i < a_dat.size () - 1 )
if (i < aDat.size() - 1)
{
if ( ++col < a_perCol )
if (++col < aPerCol)
{
a_fout << ",";
aOut << ",";
}
else
{
a_fout << "" << endl;
aOut << "" << std::endl;
col = 0;
}
}
@@ -206,292 +157,282 @@ void WriteArray ( ofstream& a_fout, const vector<T>& a_dat, int a_perCol = 16 )
}
}
int main ( int argc, char** argv )
int main(int argc, char** argv)
{
SParams params;
ParseArgs ( argc, argv, &params );
Arguments p;
ParseArgs(argc, argv, p);
if ( params.help )
if (p.help)
{
cout << g_strUsage << endl << g_strHelpFull << endl;
std::cout << helpUsage << std::endl << helpFull << std::endl;
return 0;
}
if ( params.flagFile.length () != 0 )
if (!p.flagFile.empty())
{
ifstream paramFile ( params.flagFile );
if ( !paramFile.is_open () )
std::ifstream paramFile(p.flagFile);
if (!paramFile.is_open())
{
cerr << "Failed to open param file." << endl;
std::cerr << "Failed to open param file." << std::endl;
return -1;
}
vector<string> fileArgTokens;
fileArgTokens.push_back ( "auu~~" );
std::vector<std::string> fileArgTokens;
fileArgTokens.push_back("auu~~");
bool carry = false;
string token;
while ( !paramFile.eof () )
std::string rawToken;
while (!paramFile.eof())
{
if ( carry )
if (carry)
{
string tmp;
std::string tmp;
paramFile >> tmp;
token += " ";
token += tmp;
rawToken += " ";
rawToken += tmp;
}
else
{
token.clear ();
paramFile >> token;
rawToken.clear();
paramFile >> rawToken;
}
if ( token == "" )
{
if (rawToken.empty())
continue;
}
bool qFr = token[0] == '"';
bool qBk = token[token.length () - 1] == '"';
if ( qFr && qBk )
bool qFr = rawToken[0] == '"';
bool qBk = rawToken[rawToken.length() - 1] == '"';
if (qFr && qBk)
{
fileArgTokens.push_back ( token.substr ( 1, token.length () - 2 ) );
fileArgTokens.push_back(rawToken.substr(1, rawToken.length() - 2));
}
else
if ( qFr )
if (qFr)
{
fileArgTokens.push_back ( token.substr ( 1, token.length () - 1 ) );
fileArgTokens.push_back(rawToken.substr(1, rawToken.length() - 1));
carry = true;
}
else
if ( qBk )
if (qBk)
{
fileArgTokens.push_back ( token.substr ( 0, token.length () - 1 ) );
fileArgTokens.push_back(rawToken.substr(0, rawToken.length() - 1));
carry = false;
}
else
{
fileArgTokens.push_back ( token );
fileArgTokens.push_back(rawToken);
}
}
vector<const char*> fileArgs;
fileArgs.reserve ( fileArgTokens.size () );
for ( auto& token : fileArgTokens )
{
fileArgs.push_back ( token.c_str () );
}
std::vector<const char*> fileArgs;
fileArgs.reserve(fileArgTokens.size());
for (const auto& token : fileArgTokens)
fileArgs.push_back(token.c_str());
fileArgs.push_back(nullptr);
ParseArgs ( fileArgs.size () - 1, (char**)fileArgs.data (), &params );
ParseArgs(static_cast<int>(fileArgs.size()) - 1, (char**)fileArgs.data(), p);
}
if ( !CheckArgs ( &params ) )
{
if (!CheckArgs(p))
return -1;
}
// Object mappings.
map<string, uint32_t> objMapping;
if ( params.objExport )
std::map<std::string, uint32_t> objMapping;
if (p.objExport)
{
for ( auto token : params.objMappings )
for (const auto& objToken : p.objMappings)
{
int splitter = token.find_last_of ( ';' );
if ( splitter == -1 )
auto splitter = objToken.find_last_of(';');
if (splitter == std::string::npos)
{
cerr << "Malformed mapping (missing a splitter)." << endl;
std::cerr << "Malformed mapping (missing a splitter)." << std::endl;
return -1;
}
try
{
string name = token.substr ( 0, splitter );
int id = stoi ( token.substr ( splitter + 1 ) );
std::string name = objToken.substr(0, splitter);
int id = std::stoi(objToken.substr(splitter + 1));
objMapping[name] = id;
}
catch ( std::exception )
catch (std::exception&)
{
cerr << "Malformed mapping, make sure id is numeric." << endl;
std::cerr << "Malformed mapping, make sure id is numeric." << std::endl;
}
}
}
// Open & read input file.
CTmxReader tmx;
ifstream fin ( params.inPath );
if ( !fin.is_open () )
TmxReader tmx;
std::ifstream fin(p.inPath);
if (!fin.is_open())
{
cerr << "Failed to open input file." << endl;
std::cerr << "Failed to open input file." << std::endl;
return -1;
}
tmx.Open ( fin );
tmx.Open(fin);
// Get layers.
if ( tmx.GetLayerCount () == 0 )
if (tmx.GetLayerCount() == 0)
{
cerr << "No layers found." << endl;
std::cerr << "No layers found." << std::endl;
return -1;
}
const CTmxLayer* pLayerGfx = params.layer.empty () ? tmx.GetLayer ( 0 ) : tmx.GetLayer ( params.layer );
const CTmxLayer* pLayerCls = params.collisionlay.empty () ? nullptr : tmx.GetLayer ( params.collisionlay );
const CTmxLayer* pLayerPal = params.paletteLay.empty () ? nullptr : tmx.GetLayer ( params.paletteLay );
const TmxLayer* layerGfx = p.layer.empty()
? tmx.GetLayer(0)
: tmx.GetLayer(p.layer);
const TmxLayer* layerCls = p.collisionlay.empty()
? nullptr
: tmx.GetLayer(p.collisionlay);
const TmxLayer* layerPal = p.paletteLay.empty()
? nullptr
: tmx.GetLayer(p.paletteLay);
if ( pLayerGfx == nullptr )
if (layerGfx == nullptr)
{
cerr << "Input layer not found." << endl;
std::cerr << "Input layer not found." << std::endl;
return -1;
}
// Open output files.
ofstream foutS ( params.outPath + ".s" );
ofstream foutH ( params.outPath + ".h" );
if ( !foutS.is_open () || !foutH.is_open () )
std::ofstream foutS(p.outPath + ".s");
std::ofstream foutH(p.outPath + ".h");
if (!foutS.is_open() || !foutH.is_open())
{
cerr << "Failed to create output file(s).";
std::cerr << "Failed to create output file(s).";
return -1;
}
int slashPos = max ( (int)params.outPath.find_last_of ( '/' ), (int)params.outPath.find_last_of ( '\\' ) );
string name = params.outPath;
if ( slashPos != -1 )
{
name = name.substr ( slashPos + 1 );
}
int slashPos = std::max((int)p.outPath.find_last_of('/'), (int)p.outPath.find_last_of('\\'));
std::string name = p.outPath;
if (slashPos != -1)
name = name.substr(slashPos + 1);
// Write header guards.
string guard = "__TMX2GBA_" + name + "__";
for ( auto& c: guard ) c = (char)toupper ( (int)c );
foutH << "#ifndef " << guard << endl;
foutH << "#define " << guard << endl;
foutH << endl;
foutH << "#define " << name << "Width " << tmx.GetWidth () << endl;
foutH << "#define " << name << "Height " << tmx.GetHeight () << endl;
foutH << endl;
std::string guard = "TMX2GBA_" + name;
for (auto& c: guard)
c = static_cast<char>(toupper(c));
foutH << "#ifndef " << guard << std::endl;
foutH << "#define " << guard << std::endl;
foutH << std::endl;
foutH << "#define " << name << "Width " << tmx.GetWidth() << std::endl;
foutH << "#define " << name << "Height " << tmx.GetHeight() << std::endl;
foutH << std::endl;
// Convert to GBA-friendly charmap data.
const uint32_t* pRead = pLayerGfx->GetData ();
const uint32_t* pPalRead = pLayerPal == nullptr ? nullptr : pLayerPal->GetData ();
vector<uint16_t> vucCharDat;
vucCharDat.reserve ( pLayerGfx->GetWidth () * pLayerGfx->GetHeight () );
for ( size_t i = 0; i < size_t(pLayerGfx->GetWidth () * pLayerGfx->GetHeight ()); ++i )
const uint32_t* gfxTiles = layerGfx->GetData();
const uint32_t* palTiles = (layerPal == nullptr) ? nullptr : layerPal->GetData();
std::vector<uint16_t> charDat;
size_t numTiles = static_cast<size_t>(layerGfx->GetWidth()) * static_cast<size_t>(layerGfx->GetHeight());
charDat.reserve(numTiles);
for (size_t i = 0; i < numTiles; ++i)
{
uint32_t uiRead = (*pRead++);
uint32_t read = (*gfxTiles++);
uint16_t usTile = (uint16_t)max<int32_t> ( 0, tmx.LidFromGid ( uiRead & ~FLIP_MASK ) + params.offset );
uint8_t ucFlags = 0x0;
uint16_t tile = (uint16_t)std::max<int32_t>(0, tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK) + p.offset);
uint8_t flags = 0x0;
// Get flipped!
ucFlags |= ( uiRead & FLIP_HORZ ) ? 0x4 : 0x0;
ucFlags |= ( uiRead & FLIP_VERT ) ? 0x8 : 0x0;
flags |= (read & TmxLayer::FLIP_HORZ) ? 0x4 : 0x0;
flags |= (read & TmxLayer::FLIP_VERT) ? 0x8 : 0x0;
// Determine palette ID.
uint32_t uiIndex = 0;
if ( pPalRead != nullptr )
{
uiIndex = tmx.LidFromGid ( (*pPalRead++) & ~FLIP_MASK );
}
if ( uiIndex == 0 )
{
uiIndex = params.palette + 1;
}
ucFlags |= (uint8_t)(uiIndex - 1) << 4;
uint32_t idx = 0;
if (palTiles != nullptr)
idx = tmx.LidFromGid((*palTiles++) & ~TmxLayer::FLIP_MASK);
if (idx == 0)
idx = p.palette + 1;
flags |= static_cast<uint8_t>(idx - 1) << 4;
vucCharDat.push_back ( usTile | ( uint16_t(ucFlags) << 8 ) );
charDat.push_back(tile | (static_cast<uint16_t>(flags) << 8));
}
// Save out charmap.
foutH << "#define " << name << "TilesLen " << vucCharDat.size () * 2 << endl;
foutH << "extern const unsigned short " << name << "Tiles[" << vucCharDat.size () << "];" << endl;
foutH << endl;
foutH << "#define " << name << "TilesLen " << charDat.size() * 2 << std::endl;
foutH << "extern const unsigned short " << name << "Tiles[" << charDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Tiles" << endl;
foutS << "\t.hidden " << name << "Tiles" << endl;
foutS << name << "Tiles" << ":" << endl;
WriteArray<uint16_t> ( foutS, vucCharDat );
foutS << endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Tiles" << std::endl;
foutS << "\t.hidden " << name << "Tiles" << std::endl;
foutS << name << "Tiles" << ":" << std::endl;
WriteArray<uint16_t>(foutS, charDat);
foutS << std::endl;
// Convert collision map & save it out.
if ( pLayerCls != nullptr )
if (layerCls != nullptr)
{
vector<uint8_t> vucCollisionDat;
vucCollisionDat.reserve ( pLayerCls->GetWidth () * pLayerCls->GetHeight () );
std::vector<uint8_t> vucCollisionDat;
vucCollisionDat.reserve(layerCls->GetWidth() * layerCls->GetHeight());
pRead = pLayerCls->GetData ();
for ( int i = 0; i < pLayerCls->GetWidth () * pLayerCls->GetHeight (); ++i )
gfxTiles = layerCls->GetData();
for (int i = 0; i < layerCls->GetWidth() * layerCls->GetHeight(); ++i)
{
uint8_t ucTile = (uint8_t)tmx.LidFromGid ( (*pRead++) & ~FLIP_MASK );
vucCollisionDat.push_back ( ucTile );
uint8_t ucTile = (uint8_t)tmx.LidFromGid((*gfxTiles++) & ~TmxLayer::FLIP_MASK);
vucCollisionDat.push_back(ucTile);
}
// Try to nicely append "_collision" to the output name.
string strPath;
size_t extPos = params.outPath.find_last_of ( '.' );
if ( extPos != string::npos )
{
strPath = params.outPath.insert ( extPos, "_collision" );
}
std::string path;
size_t extPos = p.outPath.find_last_of('.');
if (extPos != std::string::npos)
path = p.outPath.insert(extPos, "_collision");
else
{
strPath = params.outPath + "_collision";
}
path = p.outPath + "_collision";
// Save it out.
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size () << endl;
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size () << "];" << endl;
foutH << endl;
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size() << std::endl;
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << endl;
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Collision" << endl;
foutS << "\t.hidden " << name << "Collision" << endl;
foutS << name << "Collision" << ":" << endl;
WriteArray<uint8_t> ( foutS, vucCollisionDat );
foutS << endl;
foutS << std::endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Collision" << std::endl;
foutS << "\t.hidden " << name << "Collision" << std::endl;
foutS << name << "Collision" << ":" << std::endl;
WriteArray<uint8_t>(foutS, vucCollisionDat);
foutS << std::endl;
}
if ( params.objExport )
if (p.objExport)
{
vector<uint32_t> objDat;
for ( int i = 0; i < tmx.GetObjectCount (); ++i )
std::vector<uint32_t> objDat;
for (size_t i = 0; i < tmx.GetObjectCount(); ++i)
{
auto obj = tmx.GetObject ( i );
auto it = objMapping.find ( obj->GetName () );
if ( it == objMapping.end () )
{
auto obj = tmx.GetObject(i);
auto it = objMapping.find(obj->GetName());
if (it == objMapping.end())
continue;
}
float x, y;
obj->GetPos ( &x, &y );
objDat.push_back ( it->second );
objDat.push_back ( (int)( x * 256.0f ) );
objDat.push_back ( (int)( y * 256.0f ) );
obj->GetPos(x, y);
objDat.push_back(it->second);
objDat.push_back((int)(x * 256.0f));
objDat.push_back((int)(y * 256.0f));
}
// Save it out.
foutH << "#define " << name << "ObjCount " << objDat.size () / 3 << endl;
foutH << "#define " << name << "ObjdatLen " << objDat.size () * sizeof(int) << endl;
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size () << "];" << endl;
foutH << endl;
foutH << "#define " << name << "ObjCount " << objDat.size() / 3 << std::endl;
foutH << "#define " << name << "ObjdatLen " << objDat.size() * sizeof(int) << std::endl;
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << endl;
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Objdat" << endl;
foutS << "\t.hidden " << name << "Objdat" << endl;
foutS << name << "Objdat" << ":" << endl;
WriteArray<uint32_t> ( foutS, objDat );
foutS << endl;
foutS << std::endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Objdat" << std::endl;
foutS << "\t.hidden " << name << "Objdat" << std::endl;
foutS << name << "Objdat" << ":" << std::endl;
WriteArray<uint32_t>(foutS, objDat);
foutS << std::endl;
}
foutH << "#endif//" << guard << endl;
foutH << "#endif//" << guard << std::endl;
foutH.close ();
foutS.close ();
foutH.close();
foutS.close();
return 0;
}