From 1a0bd2ea4e3a858f39834ee219c2b53cae1685fa Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Wed, 26 Jul 2023 15:13:45 -0400 Subject: [PATCH] Tileson fully refactored into engine --- assets/maps/testmap.json | 2 +- src/ecs/TileMapComponent.h | 173 +++++++++++++++++-------------------- src/game/Game.cpp | 87 +++---------------- 3 files changed, 92 insertions(+), 170 deletions(-) diff --git a/assets/maps/testmap.json b/assets/maps/testmap.json index 6673eb8..2f04078 100644 --- a/assets/maps/testmap.json +++ b/assets/maps/testmap.json @@ -45,4 +45,4 @@ "type":"map", "version":"1.8", "width":42 -} +} \ No newline at end of file diff --git a/src/ecs/TileMapComponent.h b/src/ecs/TileMapComponent.h index 972a814..f28e846 100644 --- a/src/ecs/TileMapComponent.h +++ b/src/ecs/TileMapComponent.h @@ -17,7 +17,6 @@ #include #include #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 map; int globalScale; std::vector tileSet; std::vector destRects; @@ -50,7 +50,7 @@ public: //tson::Tileson t; const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); - std::unique_ptr 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(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;rgetType() == tson::LayerType::TileLayer) - { + { destRects.resize(tilesWide*tilesHigh); initialPositions.resize(tilesWide*tilesHigh); -// ================================================= -// for (int r = 0;r 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 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;rgetLayer("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 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;rgetLayer("Tile Layer 1"); + tson::Layer *collisionLayer = map->getLayer("Collision"); + for (int r = 0;rgetSize().y;r++){ // Next cycle through each column or tile in that row: - for (int c = 0;cgetSize().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]); } } } - }*/ + } } diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 183b069..f99acd9 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -137,27 +137,6 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError()); } - -// --------------------------------------- -// Trying out the halsafar/libtmx-parser -/* tmxparser::TmxReturn error; - tmxparser::TmxMap map; - error = tmxparser::parseFromFile("assets/maps/testmapb64.tmx", &map, "assets/textures/tiles/"); - - if (!error) - { -// printf("Yay! Tile map loaded with no errors.\n"); - tmxparser::TmxLayer layer = map.layerCollection[0]; - levelMap.x = 0; - levelMap.y = 0; - levelMap.w = map.width*map.tileWidth*gScale; - levelMap.h = map.height*map.tileHeight*gScale; -// std::cout << "Map Width Tiles: " << map.width << " Map Tile Width: " << map.tileWidth <AddTexture("player", "assets/textures/actors/firefighter.png"); assets->AddTexture("font", "assets/textures/ui/ui-font-cloud-sans.png"); @@ -243,49 +222,11 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g player.addComponent(); player.addGroup(groupPlayers); + levelMap.x = 0; + levelMap.y = 0; + gameScene.addComponent("assets/maps/testmap.json",gScale,player.getComponent().position.x+player.getComponent().width/2,player.getComponent().position.y+player.getComponent().height/2); //150,100 gameScene.addGroup(groupMap); - - // --------------------------------------- - // Tileson tryout - // printf("\nTrying out Tileson, does it work?\n"); - // // - // // - // tson::Map myMap; - // //std::string jsonStr = "assets/maps/testmap.json"; - // const char * jsonStr = "assets/maps/testmap.json"; - // const std::filesystem::path jsonPath = std::filesystem::u8path(jsonStr); - // std::unique_ptr map = t.parse(jsonPath); - // //myMap.parse("./assets/maps/testmap.json"); - // // std::unique_ptr map = t.parse(tson_files::_ULTIMATE_TEST_JSON, tson_files::_ULTIMATE_TEST_JSON_SIZE); - // std::cout << jsonPath << std::endl; - // if(map->getStatus() == tson::ParseStatus::OK) - // { - // printf("Tileson successfully parsed the tilemap\n"); - // std::cout << map->getStatusMessage(); - // //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); - // } - // } - // - // } else { - // printf("Failed to load Tileson map\n"); - // std::cout << map->getStatusMessage(); - // //printf(map->getStatus()); - // } playerPosition = Vector2D().Zero(); pVel = Vector2D().Zero(); @@ -301,7 +242,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g } // ====== Setup groups -//auto& tiles(manager.getGroup(Game::groupMap)); +auto& tiles(manager.getGroup(Game::groupMap)); auto& players(manager.getGroup(Game::groupPlayers)); auto& colliders(manager.getGroup(Game::groupColliders)); auto& objects(manager.getGroup(Game::groupObjects)); @@ -407,13 +348,13 @@ void Game::update() uiPlayerXInfo.getComponent().updateString(std::to_string(playerX)); uiPlayerYInfo.getComponent().updateString(std::to_string(playerY)); - int * foundBoundaries = predictCollisions(); - int max = foundBoundaries[0]; - for (int b=0;b<3;b++){ - if (foundBoundaries[b]>max){ - max = foundBoundaries[b]; - } - } + // int * foundBoundaries = predictCollisions(); + // int max = foundBoundaries[0]; + // for (int b=0;b<3;b++){ + // if (foundBoundaries[b]>max){ + // max = foundBoundaries[b]; + // } + // } float desiredMovementX = -1*player.getComponent().speed*0.016; // int desiredMovementY = player.getComponent().velocity.y*player.getComponent().speed*player.getComponent().scale; // uiBoundary1Info.getComponent().updateString(std::to_string((int)foundBoundaries[0])); @@ -426,7 +367,7 @@ void Game::update() // uiBoundary2Info.getComponent().updateString(std::to_string((int)foundBoundaries[1])); float difference = player.getComponent().position.x+desiredMovementX; uiBoundary2Info.getComponent().updateString(std::to_string((float)difference)); - uiBoundary3Info.getComponent().updateString(std::to_string((int)(max*gScale))); + //uiBoundary3Info.getComponent().updateString(std::to_string((int)(max*gScale))); // uiBoundary3Info.getComponent().updateString(std::to_string((int)foundBoundaries[2])); @@ -473,10 +414,10 @@ void Game::render() // { // printf("Core Game state\n"); // } -/* for (auto& t : tiles) + for (auto& t : tiles) { t->draw(); - } */ + } for (auto& c : colliders) {