From de2c2593f9f272c5151d74af4ff846fdd70a9bc7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 2 Oct 2024 15:57:59 +0200 Subject: working resource manager and textures and sprite to new standard --- src/crepe/api/CMakeLists.txt | 13 +++---- src/crepe/api/Image_asset.cpp | 14 -------- src/crepe/api/Image_asset.h | 18 ---------- src/crepe/api/Resource.cpp | 6 +++- src/crepe/api/baseResource.h | 11 ++++++ src/crepe/api/game.cpp | 18 ++++++++++ src/crepe/api/game.h | 15 +++++++++ src/crepe/api/resource_manager.h | 12 +++---- src/crepe/api/spritesheet.cpp | 36 +++++++++++++++++--- src/crepe/api/spritesheet.h | 27 ++++++++++++--- src/crepe/facade/SdlContext.cpp | 73 ++++++++++++++++++++++++++++++++++++++-- src/crepe/facade/SdlContext.h | 15 +++++++-- src/crepe/facade/Sound.h | 3 +- src/crepe/facade/Texture.cpp | 4 +++ src/crepe/facade/Texture.h | 8 +++-- 15 files changed, 210 insertions(+), 63 deletions(-) delete mode 100644 src/crepe/api/Image_asset.cpp delete mode 100644 src/crepe/api/Image_asset.h create mode 100644 src/crepe/api/baseResource.h create mode 100644 src/crepe/api/game.cpp create mode 100644 src/crepe/api/game.h (limited to 'src/crepe') 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 - -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 - -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 +#include +#include #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 + + +void game::render(std::vector & draw, std::vector & 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 + +class game { + +public: + game(){} + ~game(){} + + void render(std::vector&, std::vector&); +}; 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 #include +#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> m_resources; + std::unordered_map< std::string, std::unique_ptr> m_resources; protected: @@ -44,10 +42,10 @@ public: return static_cast(m_resources[file_path].get()); } - std::unique_ptr resource = ResourceFactory::create_resource(file_path); + auto resource = std::make_unique(file_path.c_str()); if (resource) { m_resources[file_path] = std::move(resource); - return static_cast(m_resources[file_path].get() ); + return static_cast(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 -#include 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(src), row, col); } -SpriteSheet::~SpriteSheet(){ +Spritesheet::Spritesheet(std::unique_ptr 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 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 +#include "SDL_rect.h" +#include "SDL_render.h" +#include namespace crepe::api { +class Spritesheet{ +public: + Spritesheet(const char * src, const int row , const int col); + Spritesheet(std::unique_ptr 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 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; }; + } + diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp index fc68b40..7e2d79f 100644 --- a/src/crepe/facade/SdlContext.cpp +++ b/src/crepe/facade/SdlContext.cpp @@ -1,11 +1,18 @@ #include "SdlContext.h" +#include "SDL_rect.h" +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include "util/log.h" #include #include #include #include #include +#include +#include +#include using namespace crepe; @@ -33,8 +40,11 @@ SdlContext::SdlContext(){ SDL_DestroyWindow(m_game_window); return; } - - IMG_Init(IMG_INIT_PNG); + int imgFlags = IMG_INIT_PNG; + if( !( IMG_Init( imgFlags ) & imgFlags ) ) + { + std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; + } } SdlContext::~SdlContext(){ @@ -45,13 +55,72 @@ SdlContext::~SdlContext(){ SDL_DestroyWindow(m_game_window); } IMG_Quit(); + SDL_Quit(); } +SDL_Texture* SdlContext::setTextureFromPath(const char* path, SDL_Rect& clip, const int row, const int col){ + dbg_trace(); + + SDL_Surface* tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } + + clip.w = tmp->w / col; + clip.h = tmp->h / row; + + SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} SDL_Texture* SdlContext::setTextureFromPath(const char* path){ + dbg_trace(); + SDL_Surface* tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + } SDL_FreeSurface(tmp); return CreatedTexture; } + + +void SdlContext::loop(const Texture& texture, api::Spritesheet& ss){ + SDL_RenderClear(m_game_renderer); + bool quit = false; + SDL_Event event; + + while (!quit) { + Uint32 ticks = SDL_GetTicks(); + int sprite = (ticks / 100) % 4; + ss.select_sprite(sprite, 0); + + while (SDL_PollEvent(&event) != NULL) { + switch (event.type) { + case SDL_QUIT: + quit = true; + break; + } + + } + + SDL_RenderClear(m_game_renderer); + SDL_RenderCopy(m_game_renderer, texture.get_texture(), NULL, NULL); + ss.draw_selected_sprite(10, 10); + SDL_RenderPresent(m_game_renderer); + } +} + + diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h index c275300..329a374 100644 --- a/src/crepe/facade/SdlContext.h +++ b/src/crepe/facade/SdlContext.h @@ -1,29 +1,38 @@ #pragma once +#include "SDL_rect.h" #include "Texture.h" +#include "api/spritesheet.h" #include #include -#include namespace crepe { +class Texture; +class Spritesheet; + class SdlContext { +public: + void loop(const Texture& , api::Spritesheet&); + + // singleton + static SdlContext & get_instance(); private: SdlContext(); virtual ~SdlContext(); - // singleton - static SdlContext & get_instance(); SdlContext(const SdlContext &) = delete; SdlContext(SdlContext &&) = delete; SdlContext & operator=(const SdlContext &) = delete; SdlContext & operator=(SdlContext &&) = delete; SDL_Texture* setTextureFromPath(const char*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: friend class Texture; + friend class api::Spritesheet; SDL_Window* m_game_window; SDL_Renderer* m_game_renderer; diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index ac93991..06e1932 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -6,10 +6,11 @@ #include #include "api/Resource.h" +#include "api/baseResource.h" namespace crepe { -class Sound { +class Sound : public api::BaseResource{ public: /** * \brief Pause this sample diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index c24312a..220ef2e 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -25,7 +25,11 @@ Texture::~Texture(){ } } void Texture::load(std::unique_ptr res) { + dbg_trace(); SdlContext& ctx = SdlContext::get_instance(); m_texture = ctx.setTextureFromPath(res->canonical()); } +SDL_Texture* Texture::get_texture() const{ + return m_texture; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index 3677f6e..a5fcca9 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,24 +1,28 @@ #pragma once +#include "api/baseResource.h" +#include "facade/SdlContext.h" #include "api/Resource.h" #include #include - namespace crepe { -class Texture { +class Texture : public api::BaseResource{ public: Texture(const char * src); Texture(std::unique_ptr res); ~Texture(); + SDL_Texture* get_texture() const; private: void load(std::unique_ptr res); private: SDL_Texture* m_texture; + + friend class SdlContext; }; } // namespace crepe -- cgit v1.2.3