Parse TSX files and link map data to map tilesets in Tiled.

pull/28/head
sigonasr2 2 years ago
parent 8d5d509c7c
commit cf87c25f63
  1. 12
      Crawler/Crawler.cpp
  2. 1
      Crawler/Crawler.vcxproj
  3. 3
      Crawler/Crawler.vcxproj.filters
  4. 116
      Crawler/TSXParser.h
  5. 2
      Crawler/Version.h
  6. 4
      Crawler/pixelGameEngine.cpp

@ -7,6 +7,7 @@
#include "Class.h"
#include "Version.h"
#include "TMXParser.h"
#include "TSXParser.h"
#include "DEFINES.h"
//192x192
@ -770,6 +771,7 @@ void Crawler::RenderHud(){
DrawStringDecal(vf2d{ GetScreenSize() } - vf2d{ GetTextSize(versionStr) }*0.4 - vf2d{ 0.4,0 }, versionStr, WHITE, { 0.4,0.4 });
DrawStringDecal(vf2d{ GetScreenSize() } - vf2d{ GetTextSize(versionStr) }*0.4 - vf2d{ 0,0.4 }, versionStr, WHITE, { 0.4,0.4 });
DrawStringDecal(vf2d{ GetScreenSize() } - vf2d{ GetTextSize(versionStr) }*0.4, versionStr, BLACK,{0.4,0.4});
DrawDecal({0,0},MAP_TILESETS["assets/maps/"+MAP_DATA[LEVEL1].TilesetData[1].data["source"]]->Decal());
}
void Crawler::AddEffect(Effect foreground,Effect background){
@ -802,12 +804,10 @@ void Crawler::InitializeLevel(std::string mapFile,MapName map){
std::string baseDir=mapFile.substr(0,slashMarker+1);
MAP_DATA[map]=level.GetData();
for(XMLTag&tag:MAP_DATA[map].TilesetData){
if(tag.tag=="tileset"&&MAP_TILESETS.find(tag.data["source"])==MAP_TILESETS.end()){
Renderable*r=new Renderable();
MAP_TILESETS[tag.data["source"]]=r;
r->Load(baseDir+tag.data["source"]);
std::cout<<"Missing tileset:"<<baseDir+tag.data["source"]<<". Loaded from source."<<std::endl;
}
TSXParser tileset(baseDir+tag.data["source"]);
Renderable*r=new Renderable();
MAP_TILESETS[baseDir+tag.data["source"]]=r;
r->Load("assets/maps/"+tileset.GetData().ImageData.data["source"]);
}
}

@ -183,6 +183,7 @@
<ClInclude Include="resource1.h" />
<ClInclude Include="State.h" />
<ClInclude Include="TMXParser.h" />
<ClInclude Include="TSXParser.h" />
<ClInclude Include="Version.h" />
</ItemGroup>
<ItemGroup>

@ -84,6 +84,9 @@
<ClInclude Include="Map.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TSXParser.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Player.cpp">

@ -0,0 +1,116 @@
#pragma once
#include "olcPixelGameEngine.h"
#include <strstream>
#include "TMXParser.h"
using namespace olc;
struct Tileset{
XMLTag ImageData;
friend std::ostream& operator << (std::ostream& os, Tileset& rhs);
};
class TSXParser{
public:
Tileset GetData();
private:
Tileset parsedTilesetInfo;
void ParseTag(std::string tag);
public:
TSXParser(std::string file);
};
#ifdef TSX_PARSER_SETUP
#undef TSX_PARSER_SETUP
Tileset TSXParser::GetData() {
return parsedTilesetInfo;
}
std::ostream&operator<<(std::ostream& os, Tileset& rhs){
os<<rhs.ImageData.FormatTagData(rhs.ImageData.data)<<"\n";
return os;
}
void TSXParser::ParseTag(std::string tag) {
XMLTag newTag;
//First character is a '<' so we discard it.
tag.erase(0,1); tag.erase(tag.length()-1,1); //Erase the first and last characters in the tag. Now parse by spaces.
std::stringstream s(tag); //Turn it into a string stream to now parse into individual whitespaces.
std::string data;
while (s.good()) {
int quotationMarkCount=0;
bool pastEquals=false;
data="";
bool valid=false;
while(s.good()){
int character=s.get();
if(character=='"'){
quotationMarkCount++;
}
if(character==' '&&quotationMarkCount%2==0){
valid=true;
break;
}
data+=character;
if(pastEquals&&quotationMarkCount%2==0){
valid=true;
break;
}
if(character=='='&&quotationMarkCount%2==0){
pastEquals=true;
}
}
if(valid&&data.length()>0){
if (newTag.tag.length()==0) { //Tag's empty, so first line is the tag.
newTag.tag=data;
std::cout<<"Tag: "<<newTag.tag<<"\n";
} else {
std::string key = data.substr(0,data.find("="));
std::string value = data.substr(data.find("=")+1,std::string::npos);
//Strip Quotation marks.
value = value.substr(1,std::string::npos);
value = value.substr(0,value.length()-1);
newTag.data[key]=value;
std::cout<<" "<<key<<":"<<newTag.data[key]<<"\n";
}
}
}
if (newTag.tag=="image") {
parsedTilesetInfo.ImageData=newTag;
}
std::cout<<"\n"<<"=============\n";
}
TSXParser::TSXParser(std::string file){
std::ifstream f(file,std::ios::in);
std::string accumulator="";
while (f.good()) {
std::string data;
f>>data;
if (data.empty()) continue;
if (accumulator.length()>0) {
accumulator+=" "+data;
//Check if it ends with '>'
if (data[data.length()-1]=='>') {
ParseTag(accumulator);
accumulator="";
}
} else
if (data[0]=='<') {
//Beginning of XML tag.
accumulator=data;
if(accumulator.length()>1&&accumulator.at(1)=='/'){
accumulator=""; //Restart because this is an end tag.
}
if(accumulator.length()>1&&accumulator.find('>')!=std::string::npos){
accumulator=""; //Restart because this tag has nothing in it!
}
}
}
std::cout<<"Parsed Tileset Data:\n"<<parsedTilesetInfo<<"\n";
}
#endif

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 125
#define VERSION_BUILD 135
#define stringify(a) stringify_(a)
#define stringify_(a) #a

@ -3,4 +3,6 @@
#define OLC_PGEX_TRANSFORMEDVIEW
#include "olcPGEX_TransformedView.h"
#define TMX_PARSER_SETUP
#include "TMXParser.h"
#include "TMXParser.h"
#define TSX_PARSER_SETUP
#include "TSXParser.h"
Loading…
Cancel
Save