From 765485ced528ca2f4cf644a1503b9446c5826731 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 22 Sep 2024 10:58:21 +0200 Subject: spritesheet werkt --- resource-manager/spritesheet.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 resource-manager/spritesheet.h (limited to 'resource-manager/spritesheet.h') diff --git a/resource-manager/spritesheet.h b/resource-manager/spritesheet.h new file mode 100644 index 0000000..022b6cf --- /dev/null +++ b/resource-manager/spritesheet.h @@ -0,0 +1,27 @@ +#pragma once + + + + +#include +#include +#include +#include +class SpriteSheet{ + + +public: + SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column); + ~SpriteSheet(); + + + void select_sprite(const int x, const int y); + void draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position); + SDL_Surface* getSurface() const; + +private: + SDL_Rect m_clip; + SDL_Surface* m_spritesheet_image; + SDL_Texture * texture; + +}; -- cgit v1.2.3 From 35224c07549448e919a18a463e5dec81bbabf7c6 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 25 Sep 2024 13:07:13 +0200 Subject: spritesheet and image changed --- resource-manager/Audio_asset.h | 5 +++++ resource-manager/Image_asset.cpp | 25 ++++++++++++++++++------- resource-manager/Image_asset.h | 13 ++++++++----- resource-manager/main.cpp | 22 ++++++++++------------ resource-manager/resource_fabricator.cpp | 22 ---------------------- resource-manager/resource_fabricator.h | 11 +++++++++-- resource-manager/resource_manager.cpp | 2 ++ resource-manager/resource_manager.h | 15 +++++++++++---- resource-manager/spritesheet.cpp | 19 ++++++++++--------- resource-manager/spritesheet.h | 14 +++++++++----- 10 files changed, 82 insertions(+), 66 deletions(-) (limited to 'resource-manager/spritesheet.h') diff --git a/resource-manager/Audio_asset.h b/resource-manager/Audio_asset.h index 87fa762..7f7f8cd 100644 --- a/resource-manager/Audio_asset.h +++ b/resource-manager/Audio_asset.h @@ -2,10 +2,15 @@ #include "resource.h" +#include class Audio : public Resource { +public: + Audio(const std::string& path); + ~Audio(); + diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp index 10b35f8..791e988 100644 --- a/resource-manager/Image_asset.cpp +++ b/resource-manager/Image_asset.cpp @@ -1,21 +1,32 @@ #include "Image_asset.h" -#include "spritesheet.h" #include #include +#include #include -Image::Image(const std::string& path){ - surface = IMG_Load(path.c_str()); +Texture::Texture(const std::string& path){ + m_surface = IMG_Load(path.c_str()); } +void Texture::setTexture(SDL_Renderer& renderer){ + m_texture = SDL_CreateTextureFromSurface(&renderer, m_surface); +} + + +Texture::~Texture(){ + SDL_FreeSurface(m_surface); + + if(m_texture) + SDL_DestroyTexture(m_texture); +} -Image::~Image(){ - SDL_FreeSurface(surface); +SDL_Surface* Texture::getSurface() const { + return m_surface; } -SDL_Surface* Image::getSurface() const { - return surface; +SDL_Texture* Texture::getTexture() const{ + return m_texture; } diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h index 8563352..88c0fb0 100644 --- a/resource-manager/Image_asset.h +++ b/resource-manager/Image_asset.h @@ -3,7 +3,7 @@ #include "resource.h" -#include "spritesheet.h" +#include #include #include @@ -11,15 +11,18 @@ -class Image : public Resource { +class Texture : public Resource { public: - Image(const std::string& path); - ~Image(); + Texture(const std::string& path); + ~Texture(); + void setTexture(SDL_Renderer& renderer); SDL_Surface* getSurface() const; + SDL_Texture* getTexture() const; private: - SDL_Surface* surface; + SDL_Surface* m_surface; + SDL_Texture* m_texture; }; diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp index 6dc362b..dc087c6 100644 --- a/resource-manager/main.cpp +++ b/resource-manager/main.cpp @@ -15,10 +15,6 @@ int main() { SDL_Init(SDL_INIT_VIDEO); - ResourceManager * rm = new ResourceManager; - - //Image* img = rm->Load("../spritesheet_test.png"); - //Resource* sound = rm->Load("/sound.ogg"); bool quit = false; @@ -28,18 +24,20 @@ int main() { = SDL_CreateWindow("Tessting resources", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); + SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0); - SpriteSheet spritesheet("../spritesheet_test.png", *renderer, 1, 4); + ResourceManager * rm = new ResourceManager(); + + Texture* img = rm->Load("../img.png"); + img->setTexture(*renderer); + + SpriteSheet* SS = rm->Load("../spritesheet_test.png"); + SDL_SetRenderDrawColor(renderer, 168, 230, 255, 255); SDL_RenderClear(renderer); while (!quit) { - Uint32 ticks = SDL_GetTicks(); - int sprite = (ticks / 100) % 4; - - //SDL_Rect srcrect = { sprite * 32, 0, 32, 64 }; - SDL_Rect dstrect = {10, 10, 32, 64}; while (SDL_PollEvent(&event) != NULL) { switch (event.type) { @@ -49,9 +47,9 @@ int main() { } } + SDL_RenderClear(renderer); - spritesheet.select_sprite(sprite, 0); - spritesheet.draw_selected_sprite(renderer, &dstrect); + SDL_RenderCopy(renderer, img->getTexture(), NULL, NULL); SDL_RenderPresent(renderer); } SDL_DestroyRenderer(renderer); diff --git a/resource-manager/resource_fabricator.cpp b/resource-manager/resource_fabricator.cpp index 2b1f5f7..36353e4 100644 --- a/resource-manager/resource_fabricator.cpp +++ b/resource-manager/resource_fabricator.cpp @@ -1,26 +1,4 @@ -#include -#include - #include "resource_fabricator.h" -#include "resource.h" -#include "Image_asset.h" -#include "Audio_asset.h" - -#include - - -Resource* ResourceFactory::create_resource(const Constants::FILE_PATH &file_path){ - - std::string extension = std::filesystem::path(file_path).extension(); - - if( extension == Constants::PNG_EXT ) { - return new Image(file_path); - } - else if ( extension == Constants::OGG_EXT ){ - return new Audio; - } - return nullptr; -} diff --git a/resource-manager/resource_fabricator.h b/resource-manager/resource_fabricator.h index 3489341..2d46b00 100644 --- a/resource-manager/resource_fabricator.h +++ b/resource-manager/resource_fabricator.h @@ -2,8 +2,13 @@ +#include "Audio_asset.h" +#include "Image_asset.h" #include "resource.h" #include "constants.h" +#include "spritesheet.h" +#include + using namespace crepe; @@ -13,8 +18,10 @@ class ResourceFactory { public: - static Resource* create_resource(const Constants::FILE_PATH& file_path); - + template + static Resource* create_resource(const Constants::FILE_PATH& file_path){ + return new T(file_path); + } }; diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp index e8828a4..99918d1 100644 --- a/resource-manager/resource_manager.cpp +++ b/resource-manager/resource_manager.cpp @@ -5,6 +5,7 @@ #include "resource.h" #include "resource_fabricator.h" #include +#include #include @@ -31,3 +32,4 @@ void ResourceManager::Unload(const Constants::FILE_PATH& file_path){ m_resources.erase(itr); } } + diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h index fd6657c..5570313 100644 --- a/resource-manager/resource_manager.h +++ b/resource-manager/resource_manager.h @@ -2,8 +2,7 @@ -#include -#include +#include #include @@ -11,6 +10,15 @@ #include "constants.h" #include "resource_fabricator.h" + +enum class asset_type{ + TEXTURE, + SPRITESHEET, + AUDIO, + MAP, + UNKNOWN, +}; + using namespace crepe; @@ -29,7 +37,7 @@ public: return static_cast(m_resources[file_path]); } - Resource* resource = ResourceFactory::create_resource(file_path); + Resource* resource = ResourceFactory::create_resource(file_path); if (resource) { m_resources[file_path] = std::move(resource); } @@ -42,5 +50,4 @@ private: std::unordered_map m_resources; - }; diff --git a/resource-manager/spritesheet.cpp b/resource-manager/spritesheet.cpp index c8e1b70..c0e4d06 100644 --- a/resource-manager/spritesheet.cpp +++ b/resource-manager/spritesheet.cpp @@ -1,6 +1,7 @@ #include "spritesheet.h" +#include "Image_asset.h" #include #include @@ -10,17 +11,17 @@ #include -SpriteSheet::SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column){ - m_spritesheet_image = IMG_Load(path.c_str()); - texture = SDL_CreateTextureFromSurface(&renderer, m_spritesheet_image); - m_clip.w = m_spritesheet_image->w / column; - m_clip.h = m_spritesheet_image->h / row; +SpriteSheet::SpriteSheet(const std::string& path){ + m_spritesheet = new Texture(path); +} +void SpriteSheet::set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column){ + m_clip.w = m_spritesheet->getSurface()->w / column; + m_clip.h = m_spritesheet->getSurface()->h / row; } SpriteSheet::~SpriteSheet(){ - SDL_DestroyTexture(texture); - SDL_FreeSurface(m_spritesheet_image); + delete m_spritesheet; } void SpriteSheet::select_sprite(const int x, const int y){ @@ -29,10 +30,10 @@ void SpriteSheet::select_sprite(const int x, const int y){ } void SpriteSheet::draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position){ - SDL_RenderCopy(window_surface, texture, &m_clip, position); + SDL_RenderCopy(window_surface, m_spritesheet->getTexture(), &m_clip, position); } SDL_Surface* SpriteSheet::getSurface() const { - return m_spritesheet_image; + return m_spritesheet->getSurface(); } diff --git a/resource-manager/spritesheet.h b/resource-manager/spritesheet.h index 022b6cf..be1a051 100644 --- a/resource-manager/spritesheet.h +++ b/resource-manager/spritesheet.h @@ -3,17 +3,23 @@ +#include "Image_asset.h" +#include "resource.h" #include #include #include #include -class SpriteSheet{ + + +class SpriteSheet : public Resource{ public: - SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column); + SpriteSheet(const std::string& path); ~SpriteSheet(); + void set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column); + void select_sprite(const int x, const int y); void draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position); @@ -21,7 +27,5 @@ public: private: SDL_Rect m_clip; - SDL_Surface* m_spritesheet_image; - SDL_Texture * texture; - + Texture* m_spritesheet; }; -- cgit v1.2.3 From a08f5acf483d5371ccab70ba75c39ac6a5f96ce7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 30 Sep 2024 10:56:13 +0200 Subject: changed resource manager based on feedback --- resource-manager/Audio_asset.cpp | 15 +++++++ resource-manager/Audio_asset.h | 2 +- resource-manager/CMakeLists.txt | 2 + resource-manager/Image_asset.cpp | 23 ++-------- resource-manager/Image_asset.h | 14 +------ resource-manager/main.cpp | 29 ++++++++----- resource-manager/map_asset.cpp | 65 +--------------------------- resource-manager/map_asset.h | 25 +---------- resource-manager/resource.h | 9 +++- resource-manager/resource_converter.cpp | 37 ++++++++++++++++ resource-manager/resource_converter.h | 24 +++++++++++ resource-manager/resource_fabricator.cpp | 22 ++++++++++ resource-manager/resource_fabricator.h | 10 +++-- resource-manager/resource_manager.cpp | 8 ---- resource-manager/resource_manager.h | 11 ----- resource-manager/spritesheet.cpp | 29 +------------ resource-manager/spritesheet.h | 17 +------- resource-manager/tiledMap.cpp | 72 ++++++++++++++++++++++++++++++++ resource-manager/tiledMap.h | 32 ++++++++++++++ 19 files changed, 248 insertions(+), 198 deletions(-) create mode 100644 resource-manager/resource_converter.cpp create mode 100644 resource-manager/resource_converter.h create mode 100644 resource-manager/tiledMap.cpp create mode 100644 resource-manager/tiledMap.h (limited to 'resource-manager/spritesheet.h') diff --git a/resource-manager/Audio_asset.cpp b/resource-manager/Audio_asset.cpp index e69de29..056034f 100644 --- a/resource-manager/Audio_asset.cpp +++ b/resource-manager/Audio_asset.cpp @@ -0,0 +1,15 @@ + + + + +#include "Audio_asset.h" +#include + + + +Audio::Audio(const std::string& content){ + this->m_content = content; +} + +Audio::~Audio(){ +} diff --git a/resource-manager/Audio_asset.h b/resource-manager/Audio_asset.h index 7f7f8cd..d25c52f 100644 --- a/resource-manager/Audio_asset.h +++ b/resource-manager/Audio_asset.h @@ -8,7 +8,7 @@ class Audio : public Resource { public: - Audio(const std::string& path); + Audio(const std::string&); ~Audio(); diff --git a/resource-manager/CMakeLists.txt b/resource-manager/CMakeLists.txt index cf883ae..bbee3dc 100644 --- a/resource-manager/CMakeLists.txt +++ b/resource-manager/CMakeLists.txt @@ -24,7 +24,9 @@ add_executable(main Audio_asset.cpp Image_asset.cpp resource_fabricator.cpp + tiledMap.cpp resource_manager.cpp + resource_converter.cpp spritesheet.cpp constants.cpp map_asset.cpp diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp index c5599e9..0b3afad 100644 --- a/resource-manager/Image_asset.cpp +++ b/resource-manager/Image_asset.cpp @@ -3,31 +3,14 @@ #include "Image_asset.h" #include #include +#include -Texture::Texture(const std::string& path){ - m_surface = IMG_Load(path.c_str()); +Texture::Texture(const std::string& content){ + this->m_content = content; } -void Texture::setTexture(SDL_Renderer& renderer){ - m_texture = SDL_CreateTextureFromSurface(&renderer, m_surface); -} Texture::~Texture(){ - if (m_surface) { - SDL_FreeSurface(m_surface); - } - - if(m_texture){ - SDL_DestroyTexture(m_texture); - } -} - -SDL_Surface* Texture::getSurface() const { - return m_surface; -} - -SDL_Texture* Texture::getTexture() const{ - return m_texture; } diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h index 93617e6..97daca1 100644 --- a/resource-manager/Image_asset.h +++ b/resource-manager/Image_asset.h @@ -3,26 +3,14 @@ #include "resource.h" -#include -#include #include - - class Texture : public Resource { public: - Texture(const std::string& path); + Texture(const std::string&); ~Texture(); - - void setTexture(SDL_Renderer& renderer); - SDL_Surface* getSurface() const; - SDL_Texture* getTexture() const; - -private: - SDL_Surface* m_surface; - SDL_Texture* m_texture; }; diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp index a301114..6b3a23e 100644 --- a/resource-manager/main.cpp +++ b/resource-manager/main.cpp @@ -2,6 +2,7 @@ #include "Image_asset.h" #include "map_asset.h" +#include "resource_converter.h" #include "resource_manager.h" #include "spritesheet.h" #include @@ -14,6 +15,8 @@ #include #include #include +#include +#include int main() { SDL_Init(SDL_INIT_VIDEO); @@ -27,20 +30,25 @@ int main() { SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); + SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0); ResourceManager * rm = new ResourceManager(); + ResourceConverter resource_converter; Texture* img = rm->Load("../img.png"); - img->setTexture(*renderer); SpriteSheet* SS = rm->Load("../spritesheet_test.png"); - SS->set_spritesheet_data(*renderer, 1, 4); - Map* map = rm->Load("../../asset/tiled/demo.tmx"); - map->SetRenderer(*renderer); + + SDL_Surface* surface = resource_converter.FromStringToImage(*img); + SDL_Texture* m_texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + TiledMap testingTiledMap = resource_converter.FromStringToMap(*map); + testingTiledMap.SetRenderer(*renderer); + SDL_SetRenderDrawColor(renderer, 168, 230, 255, 255); SDL_RenderClear(renderer); @@ -55,19 +63,20 @@ int main() { quit = true; break; } - } + } SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, img->getTexture(), NULL, NULL); - SS->select_sprite(sprite, 0); - SDL_Rect pos = {10,10,32,64}; - SS->draw_selected_sprite(renderer, &pos); - map->draw(); + + SDL_RenderCopy(renderer, m_texture, NULL, NULL); + testingTiledMap.draw(); + SDL_RenderPresent(renderer); } delete rm; + + SDL_DestroyTexture(m_texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); diff --git a/resource-manager/map_asset.cpp b/resource-manager/map_asset.cpp index 342edc3..bbabe2b 100644 --- a/resource-manager/map_asset.cpp +++ b/resource-manager/map_asset.cpp @@ -3,71 +3,10 @@ #include "map_asset.h" -#include "TextureMap.h" -#include "map_layer.h" -#include -#include -#include -#include -#include -#include -#include -#include - -Map::Map(const std::string& path){ - if(m_Map.load(path)){ - std::cout << "Map loaded correctly " << std::endl; - } +Map::Map(const std::string& content){ + this->m_content = content; } 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()); - - std::cout << "Processing SetMapTextures " << std::endl; - - 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(); - - std::cout << "Processing SetMapLayers " << std::endl; - 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); - } - } } diff --git a/resource-manager/map_asset.h b/resource-manager/map_asset.h index 39cb8f4..082d84d 100644 --- a/resource-manager/map_asset.h +++ b/resource-manager/map_asset.h @@ -1,35 +1,12 @@ #pragma once - -#include "TextureMap.h" -#include "map_layer.h" #include "resource.h" #include -#include -#include class Map : public Resource { public: - Map(const std::string& path); + Map(const std::string& ); ~Map(); - - void SetRenderer(SDL_Renderer&); - void draw() const; - - -private: - void SetMapTextures(); - void SetMapLayers(); - - -private: - tmx::Map m_Map; - std::vector m_MapTextures; - std::vector m_RenderLayers; - - SDL_Renderer* m_Renderer; - - }; diff --git a/resource-manager/resource.h b/resource-manager/resource.h index 6bc44ed..4f112e3 100644 --- a/resource-manager/resource.h +++ b/resource-manager/resource.h @@ -3,9 +3,16 @@ +#include class Resource{ public: - + virtual ~Resource() =default; + const std::string& getContent() const{ + return this->m_content; + } + +protected: + std::string m_content; }; diff --git a/resource-manager/resource_converter.cpp b/resource-manager/resource_converter.cpp new file mode 100644 index 0000000..63b7491 --- /dev/null +++ b/resource-manager/resource_converter.cpp @@ -0,0 +1,37 @@ + + +#include "resource_converter.h" +#include "Image_asset.h" +#include "map_asset.h" +#include "resource.h" +#include +#include +#include +#include + +ResourceConverter::ResourceConverter() { IMG_Init(IMG_INIT_PNG); } + +ResourceConverter::~ResourceConverter() { IMG_Quit(); } + +SDL_Surface * +ResourceConverter::FromStringToImage(const Texture& resource) { + const std::string& content = resource.getContent(); + SDL_RWops * rw = SDL_RWFromConstMem(content.data(), content.size()); + if (!rw) { + std::cerr << "Failed to create SDL_RWops: " << SDL_GetError() + << std::endl; + return nullptr; + } + + SDL_Surface * surface = IMG_Load_RW(rw, 1); + if (!surface) { + std::cerr << "Failed to load image: " << IMG_GetError() << std::endl; + } + return surface; +} + + +TiledMap ResourceConverter::FromStringToMap(const Map& resource){ + const std::string& content = resource.getContent(); + return TiledMap(content); +} diff --git a/resource-manager/resource_converter.h b/resource-manager/resource_converter.h new file mode 100644 index 0000000..d771163 --- /dev/null +++ b/resource-manager/resource_converter.h @@ -0,0 +1,24 @@ +#pragma once + + + +#include "Image_asset.h" +#include "map_asset.h" +#include "tiledMap.h" + +#include + +class ResourceConverter{ + + +public: + ResourceConverter(); + ~ResourceConverter(); + + SDL_Surface* FromStringToImage(const Texture&); + TiledMap FromStringToMap(const Map&); + + //TODO: + //convertrs a string map into a real map: + +}; diff --git a/resource-manager/resource_fabricator.cpp b/resource-manager/resource_fabricator.cpp index 36353e4..0633a40 100644 --- a/resource-manager/resource_fabricator.cpp +++ b/resource-manager/resource_fabricator.cpp @@ -1,4 +1,26 @@ #include "resource_fabricator.h" +#include +#include +#include +#include + + + +std::string ResourceFactory::convert_file_to_string(const std::string& path){ + std::ifstream file(path, std::ios::binary | std::ios::ate); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << path << std::endl; + return ""; + } + + std::ifstream::pos_type fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector bytes(fileSize); + file.read(bytes.data(), fileSize); + + return std::string(bytes.begin(), bytes.end()); +} diff --git a/resource-manager/resource_fabricator.h b/resource-manager/resource_fabricator.h index 2d46b00..cb6929f 100644 --- a/resource-manager/resource_fabricator.h +++ b/resource-manager/resource_fabricator.h @@ -2,12 +2,10 @@ -#include "Audio_asset.h" -#include "Image_asset.h" #include "resource.h" #include "constants.h" -#include "spritesheet.h" #include +#include @@ -20,9 +18,13 @@ public: template static Resource* create_resource(const Constants::FILE_PATH& file_path){ - return new T(file_path); + + return new T(convert_file_to_string(file_path)); } +private: + static std::string convert_file_to_string(const std::string& path); + }; diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp index 99918d1..1412592 100644 --- a/resource-manager/resource_manager.cpp +++ b/resource-manager/resource_manager.cpp @@ -3,14 +3,10 @@ #include "resource_manager.h" #include "constants.h" #include "resource.h" -#include "resource_fabricator.h" -#include -#include #include ResourceManager::ResourceManager(){ - IMG_Init(IMG_INIT_PNG); } ResourceManager::~ResourceManager(){ @@ -18,10 +14,6 @@ ResourceManager::~ResourceManager(){ for(auto pair : m_resources){ delete pair.second; } - - m_resources.clear(); - - IMG_Quit(); } diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h index 5570313..ea9c80a 100644 --- a/resource-manager/resource_manager.h +++ b/resource-manager/resource_manager.h @@ -10,19 +10,8 @@ #include "constants.h" #include "resource_fabricator.h" - -enum class asset_type{ - TEXTURE, - SPRITESHEET, - AUDIO, - MAP, - UNKNOWN, -}; - using namespace crepe; - - class ResourceManager{ public: diff --git a/resource-manager/spritesheet.cpp b/resource-manager/spritesheet.cpp index b6ff60e..5ea4b65 100644 --- a/resource-manager/spritesheet.cpp +++ b/resource-manager/spritesheet.cpp @@ -1,40 +1,15 @@ #include "spritesheet.h" -#include "Image_asset.h" -#include -#include -#include -#include -#include #include -SpriteSheet::SpriteSheet(const std::string& path){ - m_spritesheet = new Texture(path); -} - -void SpriteSheet::set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column){ - m_spritesheet->setTexture(renderer); - m_clip.w = m_spritesheet->getSurface()->w / column; - m_clip.h = m_spritesheet->getSurface()->h / row; +SpriteSheet::SpriteSheet(const std::string& content){ + this->m_content = content; } SpriteSheet::~SpriteSheet(){ - delete m_spritesheet; } -void SpriteSheet::select_sprite(const int x, const int y){ - m_clip.x = x * m_clip.w; - m_clip.y = y * m_clip.h; -} -void SpriteSheet::draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position){ - SDL_RenderCopy(window_surface, m_spritesheet->getTexture(), &m_clip, position); -} - - -SDL_Surface* SpriteSheet::getSurface() const { - return m_spritesheet->getSurface(); -} diff --git a/resource-manager/spritesheet.h b/resource-manager/spritesheet.h index be1a051..9b222e7 100644 --- a/resource-manager/spritesheet.h +++ b/resource-manager/spritesheet.h @@ -3,29 +3,14 @@ -#include "Image_asset.h" #include "resource.h" -#include -#include -#include #include class SpriteSheet : public Resource{ - public: - SpriteSheet(const std::string& path); + SpriteSheet(const std::string&); ~SpriteSheet(); - void set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column); - - - void select_sprite(const int x, const int y); - void draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position); - SDL_Surface* getSurface() const; - -private: - SDL_Rect m_clip; - Texture* m_spritesheet; }; diff --git a/resource-manager/tiledMap.cpp b/resource-manager/tiledMap.cpp new file mode 100644 index 0000000..659c1fa --- /dev/null +++ b/resource-manager/tiledMap.cpp @@ -0,0 +1,72 @@ + + + + +#include "tiledMap.h" +#include "TextureMap.h" +#include "map_layer.h" +#include +#include +#include +#include +#include +#include +#include + + +TiledMap::TiledMap(const std::string& content){ + if(m_TmxMap.loadFromString(content, "../../asset/tiled/")){ + std::cout << "Map loaded correctly " << std::endl; + } +} + +TiledMap::~TiledMap(){ + for(const auto& r : m_MapTextures){ + delete r; + } + m_MapTextures.clear(); + + for(const auto& m : m_RenderLayers){ + delete m; + } + m_RenderLayers.clear(); +} + + +void TiledMap::SetRenderer(SDL_Renderer& renderer){ + this->m_Renderer = &renderer; + this->SetMapTextures(); + this->SetMapLayers(); +} + +void TiledMap::draw() const{ + for(const auto& l : m_RenderLayers){ + l->draw(m_Renderer); + } +} + +void TiledMap::SetMapTextures(){ + const auto& tileSets = m_TmxMap.getTilesets(); + assert(~tileSets.empty()); + + std::cout << "Processing SetMapTextures " << std::endl; + + 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 TiledMap::SetMapLayers(){ + const auto& mapLayers = m_TmxMap.getLayers(); + + std::cout << "Processing SetMapLayers " << std::endl; + 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_TmxMap, i, this->m_MapTextures); + } + } +} diff --git a/resource-manager/tiledMap.h b/resource-manager/tiledMap.h new file mode 100644 index 0000000..be5b6a6 --- /dev/null +++ b/resource-manager/tiledMap.h @@ -0,0 +1,32 @@ + + +#include "TextureMap.h" +#include "map_layer.h" +#include +#include +#include + +class TiledMap { + +public: + TiledMap(const std::string&); + ~TiledMap(); + + void SetRenderer(SDL_Renderer&); + void draw() const; + + +private: + void SetMapTextures(); + void SetMapLayers(); + + +private: + tmx::Map m_TmxMap; + std::vector m_MapTextures; + std::vector m_RenderLayers; + + SDL_Renderer* m_Renderer; + + +}; -- cgit v1.2.3