aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/spritesheet.cpp
blob: 93a2b6533a1ffffc9ed68f289bbdf4e8f5588a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "spritesheet.h"
#include "SDL_rect.h"
#include "SDL_render.h"
#include "api/Resource.h"
#include "facade/SdlContext.h"
#include <memory>


using namespace crepe::api;

Spritesheet::Spritesheet(const char* src, const int row, const int col){
	this->load(std::make_unique<api::Resource>(src), row, col);
}

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);
}