-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.cpp
More file actions
242 lines (215 loc) · 6.59 KB
/
sprite.cpp
File metadata and controls
242 lines (215 loc) · 6.59 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "sprite.hpp"
namespace TerraNova {
//TTF_Font* Sprite::defaultFont;
SDL_Renderer* GFXManager::ren;
std::unordered_map<std::string, std::shared_ptr<Sprite>> GFXManager::loadedSprites;
SDL_Rect MakeSDLRect(const int x, const int y, const unsigned int w,
const unsigned int h) {
SDL_Rect ret;
ret.x = x;
ret.y = y;
ret.w = w;
ret.h = h;
return ret;
}
SDL_Color Sprite::SDLifyTextColor(const textcolor_t color){
SDL_Color colorCode;
switch(color){
enum textcolor_t { BLACK = 0, RED = 1, BLUE = 2, GREEN = 3,
LAST_COLOR = 4 };
case BLACK: colorCode.r = 0;
colorCode.g = 0;
colorCode.b = 0;
colorCode.a = 255;
break;
case RED : colorCode.r = 150;
colorCode.g = 25;
colorCode.b = 50;
colorCode.a = 255;
break;
case BLUE : colorCode.r = 50;
colorCode.g = 25;
colorCode.b = 150;
colorCode.a = 255;
break;
case GREEN: colorCode.r = 25;
colorCode.g = 150;
colorCode.b = 50;
colorCode.a = 255;
break;
default: colorCode.r = 0;
colorCode.g = 0;
colorCode.b = 0;
colorCode.a = 255;
break;
}
return colorCode;
}
SDL_Rect Sprite::PackIntoRect(const int x, const int y){
SDL_Rect ret;
ret.x = x;
ret.y = y;
return ret;
}
Sprite::Sprite(SDL_Renderer* ren, File::Path filePath,
SDL_Rect layout) {
image = IMG_LoadTexture(ren, filePath.c_str());
if (image == nullptr) {
LogSDLError(std::cout, "LoadTexture");
File::Path smPath = File::BasePath() / "sprites" / "sprite_missing.png";
image = IMG_LoadTexture(ren, smPath.c_str());
if (image == nullptr) {
std::cerr << "Error: failed to load " << smPath.c_str() <<std::endl;
throw std::runtime_error("Sprite files not found.");
}
}
SDL_QueryTexture(image, NULL, NULL, &layout.w, &layout.h);
// these two lines below seem very bad actually
if(layout.w >= SCREEN_WIDTH) layout.w = SCREEN_WIDTH - 1;
if(layout.h >= SCREEN_HEIGHT) layout.h = SCREEN_HEIGHT - 1;
}
Sprite::Sprite(SDL_Renderer* ren, const File::Path& filePath, const int x,
const int y):
Sprite(ren, filePath, PackIntoRect(x,y)) {}
Sprite::Sprite(SDL_Renderer* ren, const std::string& text,
SDL_Rect layout, const SDL_Color color, TTF_Font* font) {
if(font == nullptr) font = defaultFont;
/* std::cout << "Writing \"" << text << "\" at (" << layout.x << "," <<
layout.y << ") using the font at " << font << "." << std::endl;*/
SDL_Surface* surf = TTF_RenderText_Blended_Wrapped(font, text.c_str(), color,
9*layout.w/10);
if(surf == nullptr){
LogSDLError(std::cout, "TTF_RenderText");
} else {
image = SDL_CreateTextureFromSurface(ren, surf);
SDL_FreeSurface(surf);
if(image == nullptr) LogSDLError(std::cout, "CreateTexture");
}
SDL_QueryTexture(image, NULL, NULL, &layout.w, &layout.h);
/* std::cout << "The image we created from the text \"" << text <<
"\" is at (" << layout.x << "," << layout.y << ");(" <<
layout.w << "," << layout.h << "). Its color is (" <<
color.r << "," << color.g << "," << color.b << "," <<
color.a << ")." << std::endl;*/
layout.x -= layout.w/2;
layout.y -= layout.h/2;
if(layout.w >= SCREEN_WIDTH) layout.w = SCREEN_WIDTH - 1;
if(layout.h >= SCREEN_HEIGHT) layout.h = SCREEN_HEIGHT - 1;
}
Sprite::Sprite(Sprite&& other) noexcept: image(nullptr){
std::swap(image, other.image);
}
Sprite::~Sprite(){
SDL_Cleanup(image);
}
void Sprite::RenderTo(SDL_Renderer* ren, const SDL_Rect& layout) const {
int errorCode = SDL_RenderCopyEx(ren, image, nullptr, &layout, 0, nullptr,
orientation);
if (errorCode != 0) LogSDLError(std::cout, "RenderCopyEx");
/*std::cout << "Rendered the image at " << image <<
" to the renderer at " << ren << " in position (" <<
layout.x << "," << layout.y << "), (" << layout.w << "," << layout.h
<< ")." << std::endl;*/
}
void Sprite::RenderTo(const SDL_Rect& layout) const {
RenderTo(GFXManager::Ren(), layout);
}
void Sprite::MakeDefaultSize(SDL_Rect& layout) const{
SDL_QueryTexture(image, NULL, NULL, &layout.w, &layout.h);
}
void Sprite::FlipHorizontal() {
orientation = static_cast<SDL_RendererFlip>(
orientation | SDL_FLIP_HORIZONTAL );
}
void Sprite::UnflipHorizontal() {
orientation = static_cast<SDL_RendererFlip>(
orientation & ~SDL_FLIP_HORIZONTAL );
}
void Sprite::FlipVertical() {
orientation = static_cast<SDL_RendererFlip>(
orientation | SDL_FLIP_VERTICAL );
}
void Sprite::UnflipVertical() {
orientation = static_cast<SDL_RendererFlip>(
orientation & ~SDL_FLIP_VERTICAL );
}
void Sprite::Darken() {
unsigned char* oldR = nullptr;
unsigned char* oldG = nullptr;
unsigned char* oldB = nullptr;
int errorCode = SDL_GetTextureColorMod(image, oldR, oldG, oldB);
if (errorCode != 0) LogSDLError(std::cout, "GetTextureColorMod");
unsigned char r, g, b;
if (oldR == nullptr) {
r = 255 * 0.5;
} else {
r = *oldR * 0.5;
}
if (oldG == nullptr) {
g = 255 * 0.5;
} else {
g = *oldG * 0.5;
}
if (oldB == nullptr) {
b = 255 * 0.5;
} else {
b = *oldB * 0.5;
}
SDL_SetTextureColorMod(image, r, g, b);
}
void Sprite::Lighten() {
unsigned char* oldR = nullptr;
unsigned char* oldG = nullptr;
unsigned char* oldB = nullptr;
int errorCode = SDL_GetTextureColorMod(image, oldR, oldG, oldB);
if (errorCode != 0) LogSDLError(std::cout, "GetTextureColorMod");
unsigned char r, g, b;
if (oldR == nullptr) {
r = 255;
} else {
r = *oldR * 2;
if (r < *oldR) r = 255;
}
if (oldG == nullptr) {
g = 255;
} else {
g = *oldG * 2;
if (g < *oldG) g = 255;
}
if (oldB == nullptr) {
b = 255;
} else {
b = *oldB * 2;
if (b < *oldB) b = 255;
}
SDL_SetTextureColorMod(image, r, g, b);
}
void GFXManager::Initialize(SDL_Renderer* newRen) {
GFXManager::ren = newRen;
//std::cout << "GFXManager initialized." << std::endl;
}
File::Path GFXManager::GetSpritePath(const std::string& subDir) {
return File::BasePath() / "sprites" / subDir;
}
void GFXManager::LoadSprite(File::Path spritePath) {
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>(ren,
spritePath, SDL_Rect());
loadedSprites.insert({spritePath.string(), sprite});
/*std::cout << "Loaded the sprite \"" << name << "\" at position "
<< loadedSprites.size()-1 << "=" << loadedSpriteNames.size()-1 << "."
<< std::endl;*/
}
std::shared_ptr<Sprite> GFXManager::RequestSprite(File::Path spritePath) {
std::string name = spritePath.filename().string() + ".png";
boost::replace_all(name, " ", "_");
boost::to_lower(name);
// std::cout << "Replacing " << spritePath.filename() << " -> " << name
// << std::endl;
spritePath.remove_filename();
spritePath /= name;
if (loadedSprites.count(spritePath.string()) == 0) {
LoadSprite(spritePath);
}
return loadedSprites[spritePath.string()];
}
} // namespace TerraNova