diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-02 15:57:59 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-02 15:57:59 +0200 |
commit | de2c2593f9f272c5151d74af4ff846fdd70a9bc7 (patch) | |
tree | 4001a60809734a60b75e39da5c994d9381b6efe2 /src/crepe/api | |
parent | 41bd601ef62d967c80cc0591eaf6b4290baae425 (diff) |
working resource manager and textures and sprite to new standard
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 13 | ||||
-rw-r--r-- | src/crepe/api/Image_asset.cpp | 14 | ||||
-rw-r--r-- | src/crepe/api/Image_asset.h | 18 | ||||
-rw-r--r-- | src/crepe/api/Resource.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/baseResource.h | 11 | ||||
-rw-r--r-- | src/crepe/api/game.cpp | 18 | ||||
-rw-r--r-- | src/crepe/api/game.h | 15 | ||||
-rw-r--r-- | src/crepe/api/resource_manager.h | 12 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.cpp | 36 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.h | 27 |
10 files changed, 115 insertions, 55 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 2cf0bcc..96b55cf 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,17 +1,18 @@ target_sources(crepe PUBLIC - #Image_asset.cpp #map_asset.cpp - #spritesheet.cpp - #resource_manager.cpp + spritesheet.cpp + resource_manager.cpp Resource.cpp + game.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Resource.h - #Image_asset.h + baseResource.h + game.h #map_asset.h - #spritesheet.h - #resource_manager.h + spritesheet.h + resource_manager.h Component.h AudioSource.h ) diff --git a/src/crepe/api/Image_asset.cpp b/src/crepe/api/Image_asset.cpp deleted file mode 100644 index 57431c4..0000000 --- a/src/crepe/api/Image_asset.cpp +++ /dev/null @@ -1,14 +0,0 @@ - - -#include "Image_asset.h" -#include <string> - -using namespace crepe::api; - -Texture::Texture(const std::string& content){ - this->m_content = content; -} - - -Texture::~Texture(){ -} diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h deleted file mode 100644 index 0a36b0b..0000000 --- a/src/crepe/api/Image_asset.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - - - -#include "Resource.h" -#include <string> - -namespace crepe::api { - - -class Texture : public Resource { - -public: - Texture(const std::string&); - ~Texture(); -}; - -} diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp index 1a647ce..f7f2516 100644 --- a/src/crepe/api/Resource.cpp +++ b/src/crepe/api/Resource.cpp @@ -1,11 +1,15 @@ #include <filesystem> +#include <iostream> +#include <iterator> #include "Resource.h" +#include "util/log.h" using namespace crepe::api; Resource::Resource(const std::string & src) { - this->src = std::filesystem::canonical(src); + dbg_trace(); + this->src = std::filesystem::path(src); this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); } diff --git a/src/crepe/api/baseResource.h b/src/crepe/api/baseResource.h new file mode 100644 index 0000000..2513f4d --- /dev/null +++ b/src/crepe/api/baseResource.h @@ -0,0 +1,11 @@ +#pragma once + +namespace crepe::api { + +class BaseResource { +public: + virtual ~BaseResource() = default; +}; + +} + diff --git a/src/crepe/api/game.cpp b/src/crepe/api/game.cpp new file mode 100644 index 0000000..02a0132 --- /dev/null +++ b/src/crepe/api/game.cpp @@ -0,0 +1,18 @@ + + + +#include "game.h" +#include "api/spritesheet.h" +#include "facade/SdlContext.h" +#include "facade/Texture.h" +#include <vector> + + +void game::render(std::vector<crepe::Texture*> & draw, std::vector<crepe::api::Spritesheet*> & ss){ + auto& ctx = crepe::SdlContext::get_instance(); + + ctx.loop(*draw[0], *ss[0]); +} + + + diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h new file mode 100644 index 0000000..7cde954 --- /dev/null +++ b/src/crepe/api/game.h @@ -0,0 +1,15 @@ +#pragma once + + +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include <vector> + +class game { + +public: + game(){} + ~game(){} + + void render(std::vector<crepe::Texture*>&, std::vector<crepe::api::Spritesheet*>&); +}; diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h index 5b0e0e1..a646d95 100644 --- a/src/crepe/api/resource_manager.h +++ b/src/crepe/api/resource_manager.h @@ -7,19 +7,17 @@ #include <unordered_map> #include <utility> +#include "api/baseResource.h" -#include "Resource.h" -#include "fabricator/resource_fabricator.h" - - namespace crepe::api{ +namespace crepe::api{ class ResourceManager{ private: - std::unordered_map< std::string, std::unique_ptr<api::Resource>> m_resources; + std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources; protected: @@ -44,10 +42,10 @@ public: return static_cast<T*>(m_resources[file_path].get()); } - std::unique_ptr<api::Resource> resource = ResourceFactory::create_resource<T>(file_path); + auto resource = std::make_unique<T>(file_path.c_str()); if (resource) { m_resources[file_path] = std::move(resource); - return static_cast<T*>(m_resources[file_path].get() ); + return static_cast<T*>(m_resources[file_path].get() ); } return nullptr; diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp index f42a782..93a2b65 100644 --- a/src/crepe/api/spritesheet.cpp +++ b/src/crepe/api/spritesheet.cpp @@ -1,16 +1,44 @@ #include "spritesheet.h" +#include "SDL_rect.h" +#include "SDL_render.h" +#include "api/Resource.h" +#include "facade/SdlContext.h" +#include <memory> -#include <string> using namespace crepe::api; -SpriteSheet::SpriteSheet(const std::string& content){ - this->m_content = content; +Spritesheet::Spritesheet(const char* src, const int row, const int col){ + this->load(std::make_unique<api::Resource>(src), row, col); } -SpriteSheet::~SpriteSheet(){ +Spritesheet::Spritesheet(std::unique_ptr<api::Resource> res, const int row, const int col){ + this->load(std::move(res), row, col); } +Spritesheet::~Spritesheet(){ + + if (this->m_spritesheet) { + SDL_DestroyTexture(this->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(const int x, const int y){ + auto& ctx = SdlContext::get_instance(); + SDL_Rect tmp = { x, y, m_clip.w, m_clip.h}; + SDL_RenderCopy(ctx.m_game_renderer, this->m_spritesheet, &this->m_clip, &tmp); +} + +void Spritesheet::load(std::unique_ptr<api::Resource> res, const int row, const int col){ + auto& ctx = SdlContext::get_instance(); + + this->m_spritesheet = ctx.setTextureFromPath(res->canonical(), this->m_clip, row, col); +} diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index e7530c2..7f46296 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -4,18 +4,35 @@ #include "Resource.h" -#include <string> +#include "SDL_rect.h" +#include "SDL_render.h" +#include <memory> namespace crepe::api { +class Spritesheet{ +public: + Spritesheet(const char * src, const int row , const int col); + Spritesheet(std::unique_ptr<api::Resource> res, const int row, const int col); + ~Spritesheet(); -class SpriteSheet : public Resource{ + void select_sprite(const int x, const int y); + void draw_selected_sprite(const int x, const int y); +private: + void load(std::unique_ptr<api::Resource> res, const int row, const int col);; + SDL_Texture* get_texture() const; -public: - SpriteSheet(const std::string&); - ~SpriteSheet(); + +private: + + SDL_Texture* m_spritesheet; + SDL_Rect m_clip; + + friend class SdlContext; }; + } + |