diff options
Diffstat (limited to 'resource-manager')
-rw-r--r-- | resource-manager/Audio_asset.cpp | 15 | ||||
-rw-r--r-- | resource-manager/Audio_asset.h | 2 | ||||
-rw-r--r-- | resource-manager/CMakeLists.txt | 2 | ||||
-rw-r--r-- | resource-manager/Image_asset.cpp | 23 | ||||
-rw-r--r-- | resource-manager/Image_asset.h | 14 | ||||
-rw-r--r-- | resource-manager/main.cpp | 29 | ||||
-rw-r--r-- | resource-manager/map_asset.cpp | 65 | ||||
-rw-r--r-- | resource-manager/map_asset.h | 25 | ||||
-rw-r--r-- | resource-manager/resource.h | 9 | ||||
-rw-r--r-- | resource-manager/resource_converter.cpp | 37 | ||||
-rw-r--r-- | resource-manager/resource_converter.h | 24 | ||||
-rw-r--r-- | resource-manager/resource_fabricator.cpp | 22 | ||||
-rw-r--r-- | resource-manager/resource_fabricator.h | 10 | ||||
-rw-r--r-- | resource-manager/resource_manager.cpp | 8 | ||||
-rw-r--r-- | resource-manager/resource_manager.h | 11 | ||||
-rw-r--r-- | resource-manager/spritesheet.cpp | 29 | ||||
-rw-r--r-- | resource-manager/spritesheet.h | 17 | ||||
-rw-r--r-- | resource-manager/tiledMap.cpp | 72 | ||||
-rw-r--r-- | resource-manager/tiledMap.h | 32 |
19 files changed, 248 insertions, 198 deletions
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 <string> + + + +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 <SDL_image.h> #include <SDL_surface.h> +#include <string> -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 <SDL_render.h> -#include <SDL_surface.h> #include <string> - - 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 <SDL.h> @@ -14,6 +15,8 @@ #include <SDL_timer.h> #include <SDL_video.h> #include <cstddef> +#include <iostream> +#include <ostream> 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<Texture>("../img.png"); - img->setTexture(*renderer); SpriteSheet* SS = rm->Load<SpriteSheet>("../spritesheet_test.png"); - SS->set_spritesheet_data(*renderer, 1, 4); - Map* map = rm->Load<Map>("../../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 <SDL_render.h> -#include <cassert> -#include <iostream> -#include <iterator> -#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(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 <string> -#include <tmxlite/Map.hpp> -#include <vector> 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<TextureMap*> m_MapTextures; - std::vector<MapLayer*> 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 <string> 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 <SDL_image.h> +#include <SDL_surface.h> +#include <iostream> +#include <string> + +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 <SDL_surface.h> + +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 <fstream> +#include <iostream> +#include <string> +#include <vector> + + + +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<char> 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 <SDL_render.h> +#include <string> @@ -20,9 +18,13 @@ public: template<typename T> 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 <SDL2/SDL_image.h> -#include <SDL_render.h> #include <unordered_map> 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 <SDL2/SDL.h> -#include <SDL_image.h> -#include <SDL_render.h> -#include <SDL_surface.h> -#include <cstdlib> #include <string> -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 <SDL_rect.h> -#include <SDL_render.h> -#include <SDL_surface.h> #include <string> 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 <SDL_render.h> +#include <cassert> +#include <iostream> +#include <ostream> +#include <string> +#include <tmxlite/Layer.hpp> +#include <tmxlite/Map.hpp> + + +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 <string> +#include <tmxlite/Map.hpp> +#include <vector> + +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<TextureMap*> m_MapTextures; + std::vector<MapLayer*> m_RenderLayers; + + SDL_Renderer* m_Renderer; + + +}; |