generated from sigonasr2/CPlusPlusProjectTemplate
Basic TMXParser without the data piece yet
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
b4a2a8b99d
commit
344322ada4
1572
00_test_room.tmx
Normal file
1572
00_test_room.tmx
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
10
Map.h
10
Map.h
@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
class Player;
|
||||
|
||||
class Map{
|
||||
public:
|
||||
Player*p;
|
||||
void test();
|
||||
};
|
@ -1,6 +0,0 @@
|
||||
#include "Player.h"
|
||||
|
||||
void Player::test(){
|
||||
printf("Hello Player\n");
|
||||
}
|
||||
|
10
Player.h
10
Player.h
@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
class Map;
|
||||
|
||||
class Player{
|
||||
public:
|
||||
Map*map;
|
||||
void test();
|
||||
};
|
126
TMXParser.h
Normal file
126
TMXParser.h
Normal file
@ -0,0 +1,126 @@
|
||||
|
||||
#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";
|
||||
}
|
||||
};
|
41
main.cpp
41
main.cpp
@ -1,40 +1,11 @@
|
||||
#include <memory>
|
||||
#include <stdio.h>
|
||||
|
||||
class C2{
|
||||
public:
|
||||
int data=4;
|
||||
~C2(){
|
||||
printf("C2 destroyed\n");
|
||||
}
|
||||
};
|
||||
|
||||
class C1{
|
||||
public:
|
||||
~C1(){
|
||||
printf("C1 destroyed\n");
|
||||
}
|
||||
void PrintData(std::weak_ptr<C2>ptr) {
|
||||
printf("Data is %d\n",ptr.lock()->data);
|
||||
}
|
||||
};
|
||||
#define OLC_PGE_APPLICATION
|
||||
#include "pixelGameEngine.h"
|
||||
#include <strstream>
|
||||
#include "TMXParser.h"
|
||||
|
||||
using namespace olc;
|
||||
|
||||
int main(){
|
||||
|
||||
std::shared_ptr<C1> ptrA{std::make_shared<C1>()};
|
||||
|
||||
std::weak_ptr<C1>ptrA_2=ptrA;
|
||||
if (!ptrA_2.expired()) {
|
||||
printf("Count:%ld",ptrA.use_count());
|
||||
std::shared_ptr<C1> ptrA_3{ptrA};
|
||||
printf("Inside if statement.\n");
|
||||
std::shared_ptr<C2> ptrB{std::make_shared<C2>()};
|
||||
ptrA_2.lock()->PrintData(ptrB);
|
||||
printf("Count:%ld",ptrA.use_count());
|
||||
}
|
||||
printf("Count:%ld",ptrA.use_count());
|
||||
printf("Outside if statement.\n");
|
||||
|
||||
TMXParser parser;
|
||||
return -1;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user