#pragma once #include "olcPixelGameEngine.h" #include "olcUTIL_Geometry2D.h" #include using namespace olc; struct XMLTag{ std::string tag; std::map data; const std::string FormatTagData(std::maptiles); friend std::ostream& operator << (std::ostream& os, XMLTag& rhs); std::string str(); int GetInteger(std::string dataTag); float GetFloat(std::string dataTag); double GetDouble(std::string dataTag); bool GetBool(std::string dataTag); }; struct MapTag{ int width,height; vi2d playerSpawnLocation; friend std::ostream& operator << (std::ostream& os, MapTag& rhs); }; struct LayerTag{ XMLTag tag; std::vector> tiles; std::string str(); }; struct SpawnerTag{ XMLTag ObjectData; std::vectorproperties; std::string str(); friend std::ostream& operator << (std::ostream& os, SpawnerTag& rhs); }; struct Map{ MapTag MapData; std::vector TilesetData; std::vector LayerData; std::vector SpawnerData; std::map>> ZoneData; std::string FormatLayerData(std::ostream& os, std::vectortiles); std::string FormatSpawnerData(std::ostream& os, std::vectortiles); friend std::ostream& operator << (std::ostream& os, Map& rhs); friend std::ostream& operator << (std::ostream& os, std::vector& rhs); }; class TMXParser{ public: Map GetData(); private: Map parsedMapInfo; bool buildingSpawner=false; SpawnerTag obj; void ParseTag(std::string tag); public: TMXParser(std::string file); }; #ifdef TMX_PARSER_SETUP #undef TMX_PARSER_SETUP const std::string XMLTag::FormatTagData(std::maptiles){ std::string displayStr=""; for (std::map::iterator it=data.begin();it!=data.end();it++) { displayStr+=" "+it->first+": "+it->second+"\n"; } return displayStr; } std::ostream& operator << (std::ostream& os, XMLTag& rhs){ os << rhs.tag <<"\n"<< rhs.FormatTagData(rhs.data) <<"\n"; return os; } std::ostream& operator << (std::ostream& os, MapTag& rhs){ os << "(width:"<tiles) { std::string displayStr; for (int i=0;itiles) { std::string displayStr; for (int i=0;i& rhs) { for(XMLTag&tag:rhs){ os << tag<<"\n"; } return os; } std::ostream& operator <<(std::ostream& os, Map& rhs) { os << rhs.MapData <<"\n"<< rhs.TilesetData <<"\n"<< rhs.FormatLayerData(os,rhs.LayerData) <<"\n"<< rhs.FormatSpawnerData(os,rhs.SpawnerData)<<"\n"; return os; } Map TMXParser::GetData() { return parsedMapInfo; } void TMXParser::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==' '&"ationMarkCount%2==0){ valid=true; break; } data+=character; if(pastEquals&"ationMarkCount%2==0){ valid=true; break; } if(character=='='&"ationMarkCount%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: "<>&zones=parsedMapInfo.ZoneData[newTag.data["type"]]; zones.push_back({{newTag.GetInteger("x"),newTag.GetInteger("y")},{newTag.GetInteger("width"),newTag.GetInteger("height")}}); } else { std::vector>zones; zones.push_back({{newTag.GetInteger("x"),newTag.GetInteger("y")},{newTag.GetInteger("width"),newTag.GetInteger("height")}}); parsedMapInfo.ZoneData[newTag.data["type"]]=zones; } } if (newTag.tag=="property"&&buildingSpawner) { if(newTag.data["propertytype"]=="MonsterName"){ obj.properties.push_back(newTag); } goto spawnerResetSkip; } else { std::cout<<"Unsupported tag format! Ignoring."<<"\n"; } if(buildingSpawner){ parsedMapInfo.SpawnerData.push_back(obj); } buildingSpawner=false; spawnerResetSkip: std::cout<<"\n"<<"=============\n"; } TMXParser::TMXParser(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! } } else { //Start reading in data for this layer. std::vectorrowData; while (data.find(",")!=std::string::npos) { std::string datapiece = data.substr(0,data.find(",")); data = data.substr(data.find(",")+1,std::string::npos); rowData.push_back(stoi(datapiece)); } if (data.length()) { rowData.push_back(stoi(data)); } parsedMapInfo.LayerData[parsedMapInfo.LayerData.size()-1].tiles.push_back(rowData); } } if(buildingSpawner){ parsedMapInfo.SpawnerData.push_back(obj); } std::cout<<"Parsed Map Data:\n"<