-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.cpp
More file actions
100 lines (85 loc) · 2.72 KB
/
sprite.cpp
File metadata and controls
100 lines (85 loc) · 2.72 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
#include "sprite.h"
#include <iostream>
#include <SDL2/SDL_image.h>
#include <functional>
std::unordered_map<std::string, std::pair<SDL_Texture *, int>> Sprite::texCache;
Sprite::Sprite(const std::function<void()> &invalidateSort, RenderContext rc, std::string filename, int X, int Y, int Z)
: m_x{X}, m_y{Y}, m_z{Z}, m_oldx{X}, m_oldy{Y}, m_oldz{Z} {
m_renderContext = rc;
m_filename = filename;
fInvalidateSort = invalidateSort;
if (texCache.find(filename) == texCache.end()) {
load_texture();
} else {
m_texture = texCache[filename].first;
texCache[filename].second += 1;
}
Uint32 format;
int access;
if (SDL_QueryTexture(m_texture, &format, &access, &m_width, &m_height)) {
throw SpriteError("Failed to SDL_QueryTexture");
}
fInvalidateSort();
}
Sprite::~Sprite() {
texCache[m_filename].second -= 1;
if (texCache[m_filename].second == 0) {
std::cout << "All references to texture gone, deallocating" << std::endl;
SDL_DestroyTexture(m_texture);
texCache.erase(m_filename);
}
}
int Sprite::get_x() {
return m_x;
}
int Sprite::get_y() {
return m_y;
}
int Sprite::get_z() {
return m_z;
}
void Sprite::set_x(int v) {
m_x = v;
fInvalidateSort();
}
void Sprite::set_y(int v) {
m_y = v;
fInvalidateSort();
}
void Sprite::set_z(int v) {
m_z = v;
fInvalidateSort();
}
void Sprite::render(int screenWidth, int screenHeight) {
SDL_Rect rect = {m_x, m_y, m_width, m_height};
SDL_RenderCopy(m_renderContext.renderer, m_texture, NULL, &rect);
}
int Sprite::get_width() { return m_width; }
int Sprite::get_height() { return m_height; }
void Sprite::load_texture() {
SDL_Surface *img, *convertedImg;
SDL_PixelFormat *pixelFormat;
img = IMG_Load(m_filename.c_str());
if (img == nullptr) {
throw SpriteError(SDL_GetError());
}
pixelFormat = SDL_AllocFormat(SDL_GetWindowPixelFormat(m_renderContext.window));
convertedImg = SDL_ConvertSurface(img, pixelFormat, 0);
SDL_FreeFormat(pixelFormat);
if (convertedImg == nullptr) {
throw SpriteError("Failed to convert image pixel format to window pixel format");
}
SDL_FreeSurface(img);
m_texture = SDL_CreateTextureFromSurface(m_renderContext.renderer, convertedImg);
if (m_texture == nullptr) {
throw SpriteError("Failed to convert surface to texture");
}
SDL_FreeSurface(convertedImg);
texCache[m_filename] = std::make_pair(m_texture, 1);
}
SpriteError::SpriteError(std::string msg) {
m_msg = msg;
}
const char* SpriteError::what() const noexcept {
return m_msg.c_str();
}