-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpritesheet.cpp
More file actions
111 lines (83 loc) · 3.56 KB
/
Spritesheet.cpp
File metadata and controls
111 lines (83 loc) · 3.56 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "Spritesheet.hpp"
Spritesheet::Spritesheet() {
w = h = 0;
rows = columns = 0;
sprite_size = 8;
scale = 1;
}
Spritesheet::Spritesheet(SDL_Renderer* renderer, SDL_Texture* spritesheet_texture, uint8_t sprite_size, uint8_t scale) {
this->renderer = renderer;
this->spritesheet_texture = spritesheet_texture;
// Get width and height of spritesheet
SDL_QueryTexture(spritesheet_texture, NULL, NULL, &w, &h);
rows = h / sprite_size;
columns = w / sprite_size;
this->sprite_size = sprite_size;
this->scale = scale;
}
void Spritesheet::sprite(uint16_t index, float x, float y, float scale) {
// Render sprite at index from texture to screen
SDL_Rect src_rect{ sprite_size * (index % columns), sprite_size * (index / columns), sprite_size, sprite_size };
SDL_Rect dst_rect{ static_cast<int>(x * scale), static_cast<int>(y * scale), static_cast<int>(sprite_size * scale), static_cast<int>(sprite_size * scale) };
SDL_RenderCopy(renderer, spritesheet_texture, &src_rect, &dst_rect);
}
void Spritesheet::sprite(uint16_t index, float x, float y, float scale, SDL_RendererFlip flip) {
sprite(index, x, y, scale, 0.0f, NULL, flip);
}
void Spritesheet::sprite(uint16_t index, float x, float y, float scale, float angle, SDL_Point* center, SDL_RendererFlip flip) {
// Render sprite at index from texture to screen
SDL_Rect src_rect{ sprite_size * (index % columns), sprite_size * (index / columns), sprite_size, sprite_size };
SDL_Rect dst_rect{ static_cast<int>(x * scale), static_cast<int>(y * scale), static_cast<int>(sprite_size * scale), static_cast<int>(sprite_size * scale) };
// Note: possible issue here, if center != NULL, the value at that address is altered, i.e. it's modified globally, not just locally
if (center != NULL) {
center->x *= scale;
center->y *= scale;
}
SDL_RenderCopyEx(renderer, spritesheet_texture, &src_rect, &dst_rect, angle, center, flip);
}
void Spritesheet::sprite_scaled(uint16_t index, float x, float y) {
// Render sprite at index from texture to screen
sprite(index, x, y, scale);
}
void Spritesheet::sprite_scaled(uint16_t index, float x, float y, SDL_RendererFlip flip) {
sprite_scaled(index, x, y, 0.0f, NULL, flip);
}
void Spritesheet::sprite_scaled(uint16_t index, float x, float y, float angle, SDL_Point* center, SDL_RendererFlip flip) {
// Render sprite at index from texture to screen
sprite(index, x, y, scale, angle, center, flip);
}
void Spritesheet::rect(SDL_Rect* src_rect, float x, float y) {
SDL_Rect dst_rect{ static_cast<int>(x), static_cast<int>(y), src_rect->w, src_rect->h };
SDL_RenderCopy(renderer, spritesheet_texture, src_rect, &dst_rect);
}
void Spritesheet::rect(SDL_Rect* src_rect, float x, float y, float scale) {
SDL_Rect dst_rect{ static_cast<int>(x * scale), static_cast<int>(y * scale), static_cast<int>(src_rect->w * scale), static_cast<int>(src_rect->h * scale) };
SDL_RenderCopy(renderer, spritesheet_texture, src_rect, &dst_rect);
}
void Spritesheet::rect_scaled(SDL_Rect* src_rect, float x, float y) {
rect(src_rect, x, y, scale);
}
void Spritesheet::set_blend_mode(SDL_BlendMode blending)
{
// Set blending type
SDL_SetTextureBlendMode(spritesheet_texture, blending);
}
void Spritesheet::set_alpha(uint8_t alpha)
{
// Set texture alpha
SDL_SetTextureAlphaMod(spritesheet_texture, alpha);
}
float Spritesheet::get_scale() {
return scale;
}
uint8_t Spritesheet::get_alpha() {
uint8_t alpha;
SDL_GetTextureAlphaMod(spritesheet_texture, &alpha);
return alpha;
}
SDL_Texture* Spritesheet::get_texture() {
return spritesheet_texture;
}
//uint8_t Spritesheet::get_sprite_size() {
// return sprite_size;
//}