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/spritesheet.cpp | |
parent | 41bd601ef62d967c80cc0591eaf6b4290baae425 (diff) |
working resource manager and textures and sprite to new standard
Diffstat (limited to 'src/crepe/api/spritesheet.cpp')
-rw-r--r-- | src/crepe/api/spritesheet.cpp | 36 |
1 files changed, 32 insertions, 4 deletions
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); +} |