diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-09-25 16:31:22 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-09-25 16:31:22 +0200 |
commit | 9ab843567b27bd23eb7e0d2d4a48b78f7df93595 (patch) | |
tree | 5f580dfe75a2cf3148cdd3a9ff2b0b0930340ada /resource-manager/map_asset.cpp | |
parent | 35224c07549448e919a18a463e5dec81bbabf7c6 (diff) |
testing map implementation
Diffstat (limited to 'resource-manager/map_asset.cpp')
-rw-r--r-- | resource-manager/map_asset.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/resource-manager/map_asset.cpp b/resource-manager/map_asset.cpp new file mode 100644 index 0000000..ab19e59 --- /dev/null +++ b/resource-manager/map_asset.cpp @@ -0,0 +1,69 @@ + + + + +#include "map_asset.h" +#include "TextureMap.h" +#include "map_layer.h" +#include <SDL_render.h> +#include <cassert> +#include <iostream> +#include <ostream> +#include <string> +#include <tmxlite/Layer.hpp> +#include <tmxlite/Map.hpp> + + +Map::Map(const std::string& path){ + if(m_Map.load(path)){ + std::cout << "Map loaded correctly " << std::endl; + } +} + +Map::~Map(){ + for(const auto& r : m_MapTextures){ + delete r; + } + m_MapTextures.clear(); + + for(const auto& m : m_RenderLayers){ + delete m; + } + m_RenderLayers.clear(); +} + + +void Map::SetRenderer(SDL_Renderer& renderer){ + this->m_Renderer = &renderer; + this->SetMapTextures(); + this->SetMapLayers(); +} + +void Map::draw() const{ + for(const auto& l : m_RenderLayers){ + l->draw(m_Renderer); + } +} + +void Map::SetMapTextures(){ + const auto& tileSets = m_Map.getTilesets(); + assert(~tileSets.empty()); + + for (const auto& ts : tileSets ) { + m_MapTextures.emplace_back(new TextureMap); + if(!m_MapTextures.back()->loadFromFile(ts.getImagePath(), m_Renderer)){ + std::cerr << "Failed opening " << ts.getImagePath() << "\n"; + } + } +} + +void Map::SetMapLayers(){ + const auto& mapLayers = m_Map.getLayers(); + + for(auto i = 0u; i < mapLayers.size(); ++i){ + if (mapLayers[i]->getType() == tmx::Layer::Type::Tile) { + m_RenderLayers.emplace_back(new MapLayer); + m_RenderLayers.back()->create(this->m_Map, i, this->m_MapTextures); + } + } +} |