aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/spritesheet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/spritesheet.cpp')
-rw-r--r--src/crepe/api/spritesheet.cpp36
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);
+}