TileSet Parsing and Setup Refactored

This commit is contained in:
Alan Youngblood 2023-07-25 16:48:12 -04:00
parent b15316c40e
commit b4b83f098d

View File

@ -1,4 +1,5 @@
/* /*
*
* TileMapComponent.h * TileMapComponent.h
* *
* Created on: Mar 21, 2020 * Created on: Mar 21, 2020
@ -24,7 +25,7 @@ public:
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
//tmxparser::TmxMap map; //tmxparser::TmxMap map;
//tileson::Tileson map; tson::Tileson t;
int globalScale; int globalScale;
std::vector<SDL_Rect> tileSet; std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects; std::vector<SDL_Rect> destRects;
@ -47,7 +48,7 @@ public:
// *********************************************************************************** // ***********************************************************************************
// TILESON ~~~~~~~~~~~ // TILESON ~~~~~~~~~~~
tson::Tileson t; //tson::Tileson t;
const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
std::unique_ptr<tson::Map> map = t.parse(jsonPath); std::unique_ptr<tson::Map> map = t.parse(jsonPath);
@ -56,51 +57,109 @@ public:
{ {
printf("Tileson successfully parsed the tilemap\n"); printf("Tileson successfully parsed the tilemap\n");
std::cout << map->getStatusMessage() << std::endl; std::cout << map->getStatusMessage() << std::endl;
//Gets the layer called "Object Layer" from the "ultimate_demo.json map
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//Example from a Tile Layer
//I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
//if(tileLayer->getType() == tson::LayerType::TileLayer)
//{
//pos = position in tile units
// printf("Tileson found layer of tiles");
// for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
// {
// tson::Tileset *tileset = tileObject.getTile()->getTileset();
// tson::Rect drawingRect = tileObject.getDrawingRect();
// tson::Vector2f position = tileObject.getPosition();
// //printf("X: %f, Y: %f\n",position.x,position.y);
// }
//}
// *******************************************************************************************
//map = loadedMap; //map = loadedMap;
tson::Tileset *tileset = map->getTileset("br-tiles"); tson::Tileset *tileset = map->getTileset("br-tiles");
std::cout << "Image Path: " << tileset->getImage() << std::endl; std::cout << "Image Path: " << tileset->getImage() << std::endl;
std::string fullPath = tileset->getImage();
size_t charPos = fullPath.find("assets");
fullPath.erase(0,charPos);
//std::cout << "Updated path: " << fullPath << std::endl;
//tson::Tile *tile = tileset->getTile(1); //tson::Tile *tile = tileset->getTile(1);
//std::string myTexPath = tile->getImage(); //std::string myTexPath = fullPath;
//tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is a Layer
//std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl; //std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl;
//std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png"; //std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
//Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str()); std::string texturePath = fullPath;
//setTex(map.tilesetCollection[0].name); Game::assets->AddTexture("terrain", texturePath.c_str());
setTex("terrain");
globalScale = gScale; globalScale = gScale;
//colliders.resize(map.height, std::vector<int>(map.width, 0)); //colliders.resize(map.height, std::vector<int>(map.width, 0));
tilesWide = map->getSize().x; tilesWide = map->getSize().x;
//printf("tilesWide: %d\n",tilesWide); printf("tilesWide: %d\n",tilesWide);
tilesHigh = map->getSize().y; tilesHigh = map->getSize().y;
//printf("tilesHigh: %d\n",tilesHigh); printf("tilesHigh: %d\n",tilesHigh);
tileWidth = map->getTileSize().x; tileWidth = map->getTileSize().x;
//printf("tileSizeWidth: %d\n",tileWidth); printf("tileSizeWidth: %d\n",tileWidth);
// =========== Setup Tile Set =========== // =========== Setup Tile Set ===========
//tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
//tileSet.resize(tileSetTotal); tileSetTotal = tileset->getTileCount();
int tileSetCols = tileset->getColumns();
std::cout << "tileSet Cols: " << tileSetCols << std::endl;
int tileSetRows = tileSetTotal/tileSetCols;
std::cout << "tileSet Rows: " << tileSetRows << std::endl;
tileSet.resize(tileSetTotal);
std::cout << tileSetTotal << std::endl;
for (int r=0;r<tileSetRows;r++){
for (int c=0;c<tileSetCols;c++){
srcRect.x = c*tileWidth;
srcRect.y = r*tileWidth;
srcRect.w = srcRect.h = tileWidth;
int element = r*tileSetCols+c;
tileSet[element] = srcRect;
}
}
if(tileLayer->getType() == tson::LayerType::TileLayer)
{
destRects.resize(tilesWide*tilesHigh);
initialPositions.resize(tilesWide*tilesHigh);
// =================================================
// for (int r = 0;r<map.height;r++){
// for (int c = 0;c<map.width;c++){
// int elem = c+r*map.width;
// SDL_Rect thisRect = SDL_Rect();
// thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
// thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
// thisRect.w = thisRect.h = map.tileWidth * globalScale;
// std::tuple<int,int> ogPos = std::make_tuple(thisRect.x,thisRect.y);
// initialPositions[elem] = ogPos;
// thisRect.x = thisRect.x-offsetX*globalScale;
// thisRect.y = thisRect.y-offsetY*globalScale;
// destRects[elem] = thisRect;
// colliders[r][c] = map.layerCollection[1].tiles[elem].gid;
// // std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl;
// }
// }
//=================================================
int col = 0;
int row = 0;
for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
{
//int elem = 0;
tson::Tileset *tileset = tileObject.getTile()->getTileset();
tson::Rect drawingRect = tileObject.getDrawingRect();
tson::Vector2f position = tileObject.getPosition();
//int thiscount = tileObject.getTileCount();
//std::cout << "Tile id: " << tileObject.getTile()->getId() << std::endl;
//std::cout << "Tile column: " << col << std::endl;
//std::cout << "Tile row: " << row << std::endl;
//std::cout << "X: " << drawingRect.x << std::endl;
//std::cout << "Y: " << drawingRect.y << std::endl;
//std::cout << "Width: " << drawingRect.width << std::endl;
//std::cout << "height: " << drawingRect.height << std::endl;
//std::cout << "col % tilesWide: " << col%tilesWide << std::endl;
if ( col%tilesWide == 0) {
col=0;
//printf("New Row\n");
row++;
}
col++;
}
}
/* /*
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){ for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
for (int c=0;c<map.tilesetCollection[0].colCount;c++){ for (int c=0;c<map.tilesetCollection[0].colCount;c++){
@ -130,13 +189,12 @@ public:
colliders[r][c] = map.layerCollection[1].tiles[elem].gid; colliders[r][c] = map.layerCollection[1].tiles[elem].gid;
// std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl; // std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl;
} }
} }*/
destRect.w = destRect.h = map.tileWidth * gScale; destRect.w = destRect.h = tileWidth * gScale;
*/
} else { } else {
printf("Failed to load Tileson map\n"); printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage(); std::cout << map->getStatusMessage();
//printf(map->getStatus());
} }
} }