Skip to content
Snippets Groups Projects
Commit 9ebbaedb authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh :speech_balloon:
Browse files

Had to write something to load tsx tileset specs for maps. I am really not a...

Had to write something to load tsx tileset specs for maps. I am really not a fan of Tiled's format. :P
parent f00a2d9d
Branches
Tags
No related merge requests found
Pipeline #555628889 passed with warnings
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.2" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="20" height="12" tilewidth="20" tileheight="20" infinite="0" nextlayerid="5" nextobjectid="1">
<map version="1.2" tiledversion="1.3.2" orientation="orthogonal" renderorder="right-down" compressionlevel="-1" width="20" height="12" tilewidth="20" tileheight="20" infinite="0" nextlayerid="6" nextobjectid="1">
<tileset firstgid="1" source="sheet-terrain.tsx"/>
<tileset firstgid="193" source="sheet-objects.tsx"/>
<layer id="1" name="FLOOR-LAYER" width="20" height="12">
......@@ -55,13 +55,13 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,215,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,241,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,220,0,0,
0,0,0,0,0,0,210,0,0,0,213,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,210,0,0,0,0,0,0,0,0,0,211,0,0,0,0,
0,0,0,0,0,0,0,209,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,230,0,0,0,0,0,0,0,0,0,206,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,213,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,211,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,206,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
......
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.3.2" name="sheet-objects" tilewidth="20" tileheight="20" tilecount="192" columns="12">
<image source="sheet-objects.png" width="240" height="320"/>
</tileset>
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.3.2" name="sheet-terrain" tilewidth="20" tileheight="20" tilecount="192" columns="12">
<image source="sheet-terrain.png" width="240" height="320"/>
</tileset>
......@@ -46,11 +46,13 @@ void GameState::Setup()
std::string level = chalo::Messager::Get( "LEVEL" );
std::string levelFilename = "LEVEL_" + Helper::ToString( world + "-" + level ) + ".tmx";
Logger::OutValue( "levelFilename", levelFilename );
levelFilename = "Content/Maps/" + levelFilename;
m_map.Load( levelFilename );
m_map.Load( "Content/Maps/", levelFilename );
m_map.Debug();
ChaloMap test;
test.ConvertFrom( m_map );
// XmlDocument doc = XmlParser::Parse( levelFilename );
// doc.Debug();
......
......@@ -9,6 +9,7 @@
#include "../chalo-engine/UI/UIButton.hpp"
#include "../chalo-engine/UI/UILabel.hpp"
#include "../chalo-engine/Maps/TmxMap.hpp"
#include "../chalo-engine/Maps/ChaloMap.hpp"
#include <vector>
......
#include "ChaloMap.hpp"
#include "../Utilities/Logger.hpp"
std::string ChaloMap::s_className = "ChaloMap";
void ChaloMap::ConvertFrom( const TmxMap& tmxMap )
{
Logger::StackPush( s_className + "::" + __func__ );
m_tileDimensions.x = tmxMap.m_tileWidth;
m_tileDimensions.y = tmxMap.m_tileHeight;
m_mapDimensions.x = tmxMap.m_width;
m_mapDimensions.x = tmxMap.m_height;
// Figure out spritesheet offsets; terrain can be used as-is,
// but objects are special items and require special loading.
int terrainTileOffset = 0;
int objectTileOffset = 0;
TmxTileset terrainTileset;
TmxTileset objectTileset;
for ( auto& tileset : tmxMap.m_tilesets )
{
if ( Helper::Contains( tileset.tsxSource, "object", false ) )
{
objectTileOffset = tileset.firstGid;
objectTileset = tileset;
}
else if ( Helper::Contains( tileset.tsxSource, "terrain", false ) )
{
terrainTileOffset = tileset.firstGid;
terrainTileset = tileset;
m_tilesetName = tileset.imgSource;
}
}
Logger::OutValue( "terrainTileOffset", terrainTileOffset, s_className + "::" + __func__ );
Logger::OutValue( "objectTileOffset", objectTileOffset, s_className + "::" + __func__ );
for ( auto& layer : tmxMap.m_layers )
{
if ( Helper::Contains( layer.name, "object", false ) )
{
// Object placement layer
}
else
{
// Terrain layer
int zIndex = layer.id;
for ( size_t index = 0; index < layer.data.size(); index++ )
{
// Position of the tile on the map
int xPos = index % layer.width;
int yPos = index / layer.width;
// Logger::Out( "Tile Index: " + Helper::ToString( index )
// + ", xPos: " + Helper::ToString( xPos )
// + ", yPos: " + Helper::ToString( yPos )
// , s_className + "::" + __func__ );
// Position of the tile's coordinates on the tileset
int tileId = layer.data[index] - terrainTileOffset;
int tilesetX = tileId % terrainTileset.columns;
int tilesetY = tileId / terrainTileset.columns;
// Logger::Out( "Tilesheet Index: " + Helper::ToString( tileId )
// + ", tilesetX: " + Helper::ToString( tilesetX )
// + ", tilesetY: " + Helper::ToString( tilesetY )
// , s_className + "::" + __func__ );
}
}
}
Logger::StackPop();
}
std::string ChaloMap::GetTilesetName()
{
return m_tilesetName;
}
#ifndef _CHALO_MAP_HPP
#define _CHALO_MAP_HPP
#include "TmxMap.hpp"
#include <SFML/Graphics.hpp>
// TODO: Create Tile class for ChaloMap
class ChaloMap
{
public:
void ConvertFrom( const TmxMap& tmxMap );
std::string GetTilesetName();
private:
static std::string s_className;
std::string m_tilesetName;
sf::Vector2i m_tileDimensions;
sf::Vector2i m_mapDimensions;
};
#endif
......@@ -4,12 +4,12 @@
std::string TmxMap::s_className = "TmxMap";
void TmxMap::Load( std::string path )
void TmxMap::Load( std::string path, std::string mapFile )
{
Logger::StackPush( s_className + "::" + __func__ );
Logger::Out( "Load TmxMap: \"" + path + "\"", s_className + "::" + __func__ );
Logger::Out( "Load TmxMap. path: \"" + path + "\", mapFile: \"" + mapFile + "\"", s_className + "::" + __func__ );
XmlDocument mapXml = XmlParser::Parse( path );
XmlDocument mapXml = XmlParser::Parse( path + mapFile );
mapXml.Debug();
// <map version="1.2" tiledversion="1.3.2" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="20" height="12" tilewidth="20" tileheight="20" infinite="0" nextlayerid="5" nextobjectid="1">
......@@ -25,7 +25,27 @@ void TmxMap::Load( std::string path )
std::vector<XmlElement> tilesetData = mapXml.FindElementsWithPath( "map/tileset/" );
for ( auto& t : tilesetData )
{
m_tilesetSources.push_back( t.attributes["source"] );
TmxTileset tt;
tt.firstGid = Helper::StringToInt( t.attributes["firstgid"] );
tt.tsxSource = t.attributes["source"];
// Gotta load the tileset data, too. -_-
XmlDocument tilesetDoc = XmlParser::Parse( path + tt.tsxSource );
tilesetDoc.Debug();
XmlElement tilesetData = tilesetDoc.FindElementsWithPath( "tileset/" )[0];
tt.columns = Helper::StringToInt( tilesetData.attributes["columns"] );
tt.tileCount = Helper::StringToInt( tilesetData.attributes["tileCount"] );
tt.width = Helper::StringToInt( tilesetData.attributes["width"] );
tt.height = Helper::StringToInt( tilesetData.attributes["height"] );
tt.tileWidth = Helper::StringToInt( tilesetData.attributes["columns"] );
tt.tileHeight = Helper::StringToInt( tilesetData.attributes["columns"] );
XmlElement tilesetImageData = tilesetDoc.FindElementsWithPath( "tileset/image/" )[0];
tt.imageWidth = Helper::StringToInt( tilesetImageData.attributes["width"] );
tt.imageHeight = Helper::StringToInt( tilesetImageData.attributes["height"] );
m_tilesets.push_back( tt );
}
// <layer id="1" name="FLOOR-LAYER" width="20" height="12">
......@@ -70,9 +90,20 @@ void TmxMap::Debug()
mapInfo += " <br> m_tileHeight: " + Helper::ToString( m_tileHeight );
mapInfo += "<br><br> TILESET DATA...";
for ( auto& t : m_tilesetSources )
for ( auto& t : m_tilesets )
{
mapInfo += "<br> - " + t;
mapInfo += "<br> tsxSource: \"" + t.tsxSource + "\"";
mapInfo += "<br> imgSource: \"" + t.imgSource + "\"";
mapInfo += "<br> firstgid: " + Helper::ToString( t.firstGid );
mapInfo += "<br> width: " + Helper::ToString( t.width );
mapInfo += "<br> height: " + Helper::ToString( t.height );
mapInfo += "<br> tileWidth: " + Helper::ToString( t.tileWidth );
mapInfo += "<br> tileHeight: " + Helper::ToString( t.tileHeight );
mapInfo += "<br> imageWidth: " + Helper::ToString( t.imageWidth );
mapInfo += "<br> imageHeight: " + Helper::ToString( t.imageWidth );
mapInfo += "<br> tileCount: " + Helper::ToString( t.tileCount );
mapInfo += "<br> columns: " + Helper::ToString( t.columns );
mapInfo += "<br>";
}
mapInfo += "<br><br> LAYERS...";
......
......@@ -14,10 +14,25 @@ struct TmxMapLayer
std::vector<int> data;
};
struct TmxTileset
{
std::string tsxSource;
std::string imgSource;
int firstGid;
int width;
int height;
int tileCount;
int columns;
int tileWidth;
int tileHeight;
int imageWidth;
int imageHeight;
};
class TmxMap
{
public:
void Load( std::string path );
void Load( std::string path, std::string mapFile );
void Debug();
private:
......@@ -29,8 +44,10 @@ private:
int m_height;
int m_tileWidth;
int m_tileHeight;
std::vector<std::string> m_tilesetSources;
std::vector<TmxTileset> m_tilesets;
std::vector<TmxMapLayer> m_layers;
friend class ChaloMap;
};
#endif
......@@ -3,6 +3,8 @@
#include "Logger.hpp"
#include <list>
std::string XmlParser::s_className = "XmlParser";
void XmlDocument::Debug()
{
for ( size_t e = 0; e < elements.size(); e++ )
......@@ -56,10 +58,14 @@ std::vector<XmlElement> XmlDocument::FindElementsContainingPath( std::string pat
XmlDocument XmlParser::Parse( string filepath )
{
Logger::StackPush( s_className + "::" + __func__ );
Logger::Out( "Parse XML file: \"" + filepath + "\"", s_className + "::" + __func__ );
ifstream input( filepath );
if ( input.fail() )
{
cout << "ERROR: Couldn't open \"" << filepath << "\"!" << endl;
Logger::Error( "ERROR: Couldn't open \"" + filepath + "\"!", s_className + "::" + __func__ );
Logger::StackPop();
throw runtime_error( "File not found!" );
}
......@@ -126,6 +132,7 @@ XmlDocument XmlParser::Parse( string filepath )
}
}
Logger::StackPop();
return doc;
}
......
......@@ -30,6 +30,9 @@ class XmlParser
public:
static XmlDocument Parse( std::string filepath );
static std::string StackString( std::vector<std::string> pathStack );
private:
static std::string s_className;
};
#endif
......@@ -105,6 +105,10 @@
<Unit filename="chalo-engine/Managers/TextureManager.hpp" />
<Unit filename="chalo-engine/Managers/UIManager.cpp" />
<Unit filename="chalo-engine/Managers/UIManager.hpp" />
<Unit filename="chalo-engine/Maps/ChaloMap.cpp" />
<Unit filename="chalo-engine/Maps/ChaloMap.hpp" />
<Unit filename="chalo-engine/Maps/TmxMap.cpp" />
<Unit filename="chalo-engine/Maps/TmxMap.hpp" />
<Unit filename="chalo-engine/States/DefaultState.cpp" />
<Unit filename="chalo-engine/States/DefaultState.hpp" />
<Unit filename="chalo-engine/States/IState.cpp" />
......
......@@ -760,7 +760,7 @@
1653198499 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/Objects/Properties/Positionable.hpp
<SFML/Graphics.hpp>
1654322722 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/States/GameState.hpp
1654324610 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/States/GameState.hpp
"../chalo-engine/States/IState.hpp"
"../chalo-engine/Managers/TextureManager.hpp"
"../chalo-engine/Managers/FontManager.hpp"
......@@ -769,6 +769,7 @@
"../chalo-engine/UI/UIButton.hpp"
"../chalo-engine/UI/UILabel.hpp"
"../chalo-engine/Maps/TmxMap.hpp"
"../chalo-engine/Maps/ChaloMap.hpp"
<vector>
"../Objects/RawrMap.hpp"
"../Objects/Enums.hpp"
......@@ -1170,7 +1171,7 @@
"../chalo-engine/Utilities/Helper.hpp"
"../chalo-engine/Utilities_SFML/SFMLHelper.hpp"
1654322924 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/States/GameState.cpp
1654325989 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/States/GameState.cpp
"GameState.hpp"
"../chalo-engine/Managers/MenuManager.hpp"
"../chalo-engine/Managers/InputManager.hpp"
......@@ -1247,12 +1248,12 @@
"../chalo-engine/Utilities/Messager.hpp"
"../chalo-engine/Utilities/Logger.hpp"
1654323690 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Utilities/XmlParser.cpp
1654326263 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Utilities/XmlParser.cpp
"XmlParser.hpp"
"Logger.hpp"
<list>
1654323479 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Utilities/XmlParser.hpp
1654326090 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Utilities/XmlParser.hpp
"Helper.hpp"
<string>
<fstream>
......@@ -1260,12 +1261,20 @@
<vector>
<stack>
1654323815 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/TmxMap.hpp
1654326468 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/TmxMap.hpp
"../Utilities/CsvParser.hpp"
"../Utilities/XmlParser.hpp"
<string>
1654323659 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/TmxMap.cpp
1654326502 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/TmxMap.cpp
"TmxMap.hpp"
"../Utilities/Logger.hpp"
1654325179 /media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/ChaloMap.hpp
"TmxMap.hpp"
<SFML/Graphics.hpp>
1654326064 source:/media/rachelwil/Rachel/PROJECTS/GAMES AND APPS/rawr-rinth/source/chalo-engine/Maps/ChaloMap.cpp
"ChaloMap.hpp"
"../Utilities/Logger.hpp"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment