Tileson fully refactored into engine

This commit is contained in:
Alan Youngblood
2023-07-26 15:13:45 -04:00
parent b4b83f098d
commit 1a0bd2ea4e
3 changed files with 92 additions and 170 deletions

View File

@ -17,7 +17,6 @@
#include <string>
#include <tuple>
#include "../tileson/tileson.hpp"
//#include "../../libtmx-parser/src/tmxparser.h"
class TileMapComponent : public Component
{
@ -26,6 +25,7 @@ public:
SDL_Rect srcRect, destRect;
//tmxparser::TmxMap map;
tson::Tileson t;
std::unique_ptr<tson::Map> map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
@ -50,7 +50,7 @@ public:
//tson::Tileson t;
const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath);
std::unique_ptr<tson::Map> map = t.parse(jsonPath);
map = t.parse(jsonPath);
std::cout << jsonPath << std::endl;
if(map->getStatus() == tson::ParseStatus::OK)
@ -60,41 +60,52 @@ public:
//map = loadedMap;
tson::Tileset *tileset = map->getTileset("br-tiles");
std::cout << "Image Path: " << tileset->getImage() << std::endl;
//tson::Tileset *collisionTS = map->getTileset("Collision");
//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);
//std::string myTexPath = fullPath;
tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is a Layer
//std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl;
//std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
std::string texturePath = fullPath;
Game::assets->AddTexture("terrain", texturePath.c_str());
setTex("terrain");
std::string texName = tileLayer->getName();
std::cout << "texName: " << texName << std::endl;
std::cout << "fullPath: " << fullPath << std::endl;
//std::string texturePath = fullPath;
Game::assets->AddTexture(texName, fullPath.c_str());
setTex(texName);
globalScale = gScale;
tson::Layer *collisionLayer = map->getLayer("Collision");
// std::cout << "Collider 0,2: " << collisionLayer->getTileData(0,2)->getId() << std::endl;
// std::cout << "Collider 1,2: " << collisionLayer->getTileData(1,2)->getId() << std::endl;
// std::cout << "Collider 2,2: " << collisionLayer->getTileData(2,2)->getId() << std::endl;
// if (collisionLayer->getTileData(3,2)){
// std::cout << "Collider 3,2: " << collisionLayer->getTileData(3,2)->getId() << std::endl;
// } else {
// printf("null found instead of collider \n");
// }
//colliders.resize(map.height, std::vector<int>(map.width, 0));
tilesWide = map->getSize().x;
printf("tilesWide: %d\n",tilesWide);
// printf("tilesWide: %d\n",tilesWide);
tilesHigh = map->getSize().y;
printf("tilesHigh: %d\n",tilesHigh);
// printf("tilesHigh: %d\n",tilesHigh);
tileWidth = map->getTileSize().x;
printf("tileSizeWidth: %d\n",tileWidth);
// printf("tileSizeWidth: %d\n",tileWidth);
Game::levelMap.w = tilesWide*tileWidth*globalScale;
Game::levelMap.h = tilesHigh*tileWidth*globalScale;
// =========== Setup Tile Set ===========
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;
int tileSetCols = tileset->getColumns();
// std::cout << "tileSet Cols: " << tileSetCols << std::endl;
int tileSetRows = tileSetTotal/tileSetCols;
// std::cout << "tileSet Rows: " << tileSetRows << std::endl;
// std::cout << "TileSetTotal: " << tileSetTotal << std::endl;
for (int r=0;r<tileSetRows;r++){
for (int c=0;c<tileSetCols;c++){
@ -103,42 +114,37 @@ public:
srcRect.w = srcRect.h = tileWidth;
int element = r*tileSetCols+c;
tileSet[element] = srcRect;
//std::cout << "Element: " << element << " X: " << srcRect.x << " Y: " << srcRect.y << std::endl;
}
}
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;
// }
// }
//=================================================
for (int r=0;r<tilesHigh;r++){
for (int c=0;c<tilesWide;c++){
int elem = c+r*tilesWide;
SDL_Rect thisRect = SDL_Rect();
thisRect.x = c*tileWidth*globalScale;
thisRect.y = r*tileWidth*globalScale;
thisRect.w = thisRect.h = 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;
}
}
destRect.w = destRect.h = tileWidth * gScale;
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 << "destRect[0] .x: " << destRects[0].x << " .y " << destRects[0].y << " .w " << destRects[0].w << " .h " << destRects[0].h << std::endl;
// std::cout << "destRect[1] .x: " << destRects[1].x << " .y " << destRects[1].y << " .w " << destRects[1].w << " .h " << destRects[1].h << std::endl;
//
// std::cout << "tileSet[0] .x: " << tileSet[0].x << " .y " << tileSet[0].y << " .w " << tileSet[0].w << " .h " << tileSet[0].h << std::endl;
// std::cout << "tileSet[1] .x: " << tileSet[1].x << " .y " << tileSet[1].y << " .w " << tileSet[1].w << " .h " << tileSet[0].h << std::endl;
//std::cout << "Tile id: " << tileObject.getTile()->getId() << std::endl;
//std::cout << "Tile column: " << col << std::endl;
@ -148,54 +154,25 @@ public:
//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 c=0;c<map.tilesetCollection[0].colCount;c++){
srcRect.x = c*map.tilesetCollection[0].tileWidth;
srcRect.y = r*map.tilesetCollection[0].tileHeight;
srcRect.w = srcRect.h = map.tileWidth;
int element = r*map.tilesetCollection[0].colCount+c;
tileSet[element] = srcRect;
}
// tson::Layer *myLayer = map->getLayer("Tile Layer 1");
// tson::Tile *myTile1 = myLayer->getTileData(0,0);
// int tid1 = myTile1->getId()-1;
// tson::Tile *myTile2 = myLayer->getTileData(1,0);
// int tid2 = myTile2->getId()-1;
// tson::Tile *myTile3 = myLayer->getTileData(2,0);
// int tid3 = myTile3->getId()-1;
// tson::Tile *myTile4 = myLayer->getTileData(3,0);
// int tid4 = myTile4->getId()-1;
// std::cout << "First tile row's Ids: " << tid1 << ", " << tid2 << ", " << tid3 << ", " << tid4 << ", " << std::endl;
}
// =========== Setup Tile Map ============
destRects.resize(map.width*map.height);
initialPositions.resize(map.width*map.height);
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;
}
}*/
destRect.w = destRect.h = tileWidth * gScale;
} else {
printf("Failed to load Tileson map\n");
std::cout << map->getStatusMessage();
}
//std::cout << "texture var: " << texture << std::endl;
}
void update() override
@ -213,21 +190,25 @@ public:
{
//iterate through rows and columns of the map to draw the tiles
// First cycle through rows
/* for (int r = 0;r<map.height;r++){
tson::Layer *myLayer = map->getLayer("Tile Layer 1");
tson::Layer *collisionLayer = map->getLayer("Collision");
for (int r = 0;r<map->getSize().y;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map.width;c++){
int i = r*map.width+c;
int elem = c+r*map.width;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
for (int c = 0;c<map->getSize().x;c++){
int i = r*map->getSize().x+c;
int elem = c+r*map->getSize().x;
tson::Tile *myTile = myLayer->getTileData(c,r);
int tid = myTile->getId()-1;
int tileToDraw = tid;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
if (Game::debugMenu){
if (map.layerCollection[1].tiles[i].gid != 0) {
if (collisionLayer->getTileData(c,r)) {
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
SDL_RenderDrawRect(Game::renderer, &destRects[elem]);
}
}
}
}*/
}
}