generated from sigonasr2/CPlusPlusProjectTemplate
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.4 KiB
126 lines
3.4 KiB
2 years ago
|
|
||
|
#include "pixelGameEngine.h"
|
||
|
#include <strstream>
|
||
|
|
||
|
using namespace olc;
|
||
|
|
||
|
struct XMLTag{
|
||
|
std::string tag;
|
||
|
std::map<std::string,std::string> data;
|
||
|
const std::string FormatTagData(std::map<std::string,std::string>tiles) {
|
||
|
std::string displayStr="";
|
||
|
for (std::map<std::string,std::string>::iterator it=data.begin();it!=data.end();it++) {
|
||
|
displayStr+=" "+it->first+": "+it->second+"\n";
|
||
|
}
|
||
|
return displayStr;
|
||
|
}
|
||
|
friend std::ostream& operator << (std::ostream& os, XMLTag& rhs) {
|
||
|
os <<
|
||
|
rhs.tag <<"\n"<<
|
||
|
rhs.FormatTagData(rhs.data) <<"\n";
|
||
|
|
||
|
return os; }
|
||
|
};
|
||
|
|
||
|
struct LayerTag{
|
||
|
XMLTag tag;
|
||
|
std::vector<std::vector<int>> tiles;
|
||
|
std::string str() {
|
||
|
std::string displayStr=tag.tag+"\n"+tag.FormatTagData(tag.data);
|
||
|
displayStr+=" DATA\n";
|
||
|
for (int row=0;row<tiles.size();row++) {
|
||
|
displayStr+=" ";
|
||
|
for (int col=0;col<tiles[row].size();col++) {
|
||
|
displayStr+=std::to_string(tiles[row][col])+",";
|
||
|
}
|
||
|
displayStr+="\n";
|
||
|
}
|
||
|
return displayStr;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
struct Map{
|
||
|
XMLTag MapData;
|
||
|
XMLTag TilesetData;
|
||
|
std::vector<LayerTag> LayerData;
|
||
|
std::string FormatLayerData(std::ostream& os, std::vector<LayerTag>tiles) {
|
||
|
for (int i=0;i<LayerData.size();i++) {
|
||
|
return LayerData[i].str();
|
||
|
}
|
||
|
}
|
||
|
friend std::ostream& operator << (std::ostream& os, Map& rhs) {
|
||
|
os <<
|
||
|
rhs.MapData <<"\n"<<
|
||
|
rhs.TilesetData <<"\n"<<
|
||
|
rhs.FormatLayerData(os,rhs.LayerData) <<"\n";
|
||
|
|
||
|
return os; }
|
||
|
};
|
||
|
|
||
|
class TMXParser{
|
||
|
private:
|
||
|
|
||
|
Map parsedMapInfo;
|
||
|
LayerTag*activeLayer;
|
||
|
|
||
|
void 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()) {
|
||
|
s>>data;
|
||
|
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);
|
||
|
newTag.data[key]=value;
|
||
|
std::cout<<" "<<key<<":"<<newTag.data[key]<<"\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (newTag.tag=="map") {
|
||
|
parsedMapInfo.MapData=newTag;
|
||
|
} else
|
||
|
if (newTag.tag=="tileset") {
|
||
|
parsedMapInfo.TilesetData=newTag;
|
||
|
} else
|
||
|
if (newTag.tag=="layer") {
|
||
|
parsedMapInfo.LayerData.push_back({newTag});
|
||
|
} else {
|
||
|
std::cout<<"Unsupported tag format! Ignoring."<<"\n";
|
||
|
}
|
||
|
std::cout<<"\n"<<"=============\n";
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
TMXParser() {
|
||
|
std::ifstream f("00_test_room.tmx",std::ios::in);
|
||
|
|
||
|
std::string accumulator="";
|
||
|
|
||
|
while (f.good()) {
|
||
|
std::string data;
|
||
|
f>>data;
|
||
|
|
||
|
if (accumulator.length()>0) {
|
||
|
//We're accumulating strings until we find '>'
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::cout<<"Parsed Map Data:\n"<<parsedMapInfo<<"\n";
|
||
|
}
|
||
|
};
|