From b15316c40e23ec5b1f28c890003dbd0caa8d4aec Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Mon, 24 Jul 2023 13:41:02 -0400 Subject: [PATCH] More work on Tileson integration --- src/ecs/TileMapComponent.h | 74 +++++++++++++++++++++++++++----------- src/game/Game.cpp | 73 ++++++++++++++++++------------------- 2 files changed, 91 insertions(+), 56 deletions(-) diff --git a/src/ecs/TileMapComponent.h b/src/ecs/TileMapComponent.h index 95acb44..1c8fccc 100644 --- a/src/ecs/TileMapComponent.h +++ b/src/ecs/TileMapComponent.h @@ -42,37 +42,65 @@ public: SDL_DestroyTexture(texture); } - TileMapComponent(/*tileson::Tileson loadedMap,*/ int gScale, int offsetX, int offsetY) + TileMapComponent(std::string mapPath, int gScale, int offsetX, int offsetY) { // *********************************************************************************** // TILESON ~~~~~~~~~~~ - tson::Tileson t; -std::unique_ptr map = t.parse(fs::path("../../assets/maps/testmap.json")); + tson::Tileson t; + const std::filesystem::path jsonPath = std::filesystem::u8path(mapPath); + std::unique_ptr map = t.parse(jsonPath); -if(map->getStatus() == tson::ParseStatus::OK) -{ - printf("Parsed the map file okay."); - //Gets the layer called "Object Layer" from the "ultimate_demo.json map - tson::Layer *objectLayer = map->getLayer("Object Layer"); //This is an Object Layer + std::cout << jsonPath << std::endl; + if(map->getStatus() == tson::ParseStatus::OK) + { + printf("Tileson successfully parsed the tilemap\n"); + 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; - std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png"; - Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str()); - setTex(map.tilesetCollection[0].name); + //map = loadedMap; + tson::Tileset *tileset = map->getTileset("br-tiles"); + std::cout << "Image Path: " << tileset->getImage() << std::endl; + + //tson::Tile *tile = tileset->getTile(1); + //std::string myTexPath = tile->getImage(); + //tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer + //std::cout << "\nTexturePath: \n" << myTexPath << "\n" << std::endl; + + + //std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png"; + //Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str()); + //setTex(map.tilesetCollection[0].name); globalScale = gScale; - colliders.resize(map.height, std::vector(map.width, 0)); - tilesWide = map.width; - tilesHigh = map.height; - tileWidth = map.tileWidth; + //colliders.resize(map.height, std::vector(map.width, 0)); + tilesWide = map->getSize().x; + //printf("tilesWide: %d\n",tilesWide); + tilesHigh = map->getSize().y; + //printf("tilesHigh: %d\n",tilesHigh); + tileWidth = map->getTileSize().x; + //printf("tileSizeWidth: %d\n",tileWidth); // =========== Setup Tile Set =========== - tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;*/ - tileSet.resize(tileSetTotal); + //tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount; + //tileSet.resize(tileSetTotal); /* for (int r=0;rgetStatus() == tson::ParseStatus::OK) } } destRect.w = destRect.h = map.tileWidth * gScale; - */} + */ + } else { + printf("Failed to load Tileson map\n"); + std::cout << map->getStatusMessage(); + //printf(map->getStatus()); + } + } void update() override { diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 87fe335..183b069 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -243,48 +243,49 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g player.addComponent(); player.addGroup(groupPlayers); - gameScene.addComponent(/*map,*/gScale,player.getComponent().position.x+player.getComponent().width/2,player.getComponent().position.y+player.getComponent().height/2); //150,100 + 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"); + // 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); + // } + // } // - 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(); - // }*/ - // } - - } else { - printf("Failed to load Tileson map\n"); - std::cout << map->getStatusMessage(); - //printf(map->getStatus()); - } + // } else { + // printf("Failed to load Tileson map\n"); + // std::cout << map->getStatusMessage(); + // //printf(map->getStatus()); + // } playerPosition = Vector2D().Zero(); pVel = Vector2D().Zero();