-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetexture.cpp
executable file
·396 lines (355 loc) · 12.4 KB
/
etexture.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "etexture.h"
#include <algorithm>
eTexture::eTexture() {}
eTexture::~eTexture() {
reset();
}
void eTexture::reset() {
if(mTex) SDL_DestroyTexture(mTex);
mTex = nullptr;
mWidth = 0;
mHeight = 0;
}
bool eTexture::create(SDL_Renderer* const r,
const int width, const int height) {
reset();
mTex = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET, width, height);
if(!mTex) return false;
SDL_SetTextureBlendMode(mTex, SDL_BLENDMODE_BLEND);
mWidth = width;
mHeight = height;
return true;
}
void eTexture::setAsRenderTarget(SDL_Renderer* const r) {
SDL_SetRenderTarget(r, mTex);
}
bool eTexture::load(SDL_Renderer* const r, const std::string& path) {
reset();
const auto surf = IMG_Load(path.c_str());
if(!surf) {
printf("Unable to load image %s! SDL_image Error: %s\n",
path.c_str(), IMG_GetError());
return false;
}
return load(r, surf);
}
bool eTexture::load(SDL_Renderer* const r,
SDL_Surface* const surf) {
reset();
mTex = SDL_CreateTextureFromSurface(r, surf);
mWidth = surf->w;
mHeight = surf->h;
SDL_FreeSurface(surf);
if(!mTex) {
printf("Unable to create texture from surface!"
"SDL Error: %s\n", SDL_GetError());
return false;
}
return true;
}
SDL_Texture* generateTextTexture(SDL_Renderer* const r,
const std::string& text,
const SDL_Color& color,
TTF_Font& font,
const int width,
int& w, int& h) {
SDL_Surface* surf;
if(width) {
surf = TTF_RenderUTF8_Blended_Wrapped(&font, text.c_str(), color, width);
} else {
surf = TTF_RenderUTF8_Blended(&font, text.c_str(), color);
}
if(!surf) {
printf("Unable to render text! SDL_ttf Error: %s\n",
TTF_GetError());
return nullptr;
}
const auto tex = SDL_CreateTextureFromSurface(r, surf);
if(!tex) {
printf("Unable to create texture from rendered text! "
"SDL Error: %s\n", SDL_GetError());
return nullptr;
}
w = surf->w;
h = surf->h;
SDL_FreeSurface(surf);
return tex;
}
std::vector<std::string> textLines(const std::string& text,
TTF_Font& font,
const int width) {
std::vector<std::string> result;
const int ts = text.size();
std::string word;
std::string line;
int index = 0;
while(index < ts) {
const bool isLast = index == ts - 1;
const auto c = text[index++];
if(c == ' ' || c == '\n' || isLast) {
if(isLast && c != ' ' && c != '\n') word += c;
int w = 0;
int h = 0;
const auto newLine = line + (line.empty() ? "" : " ") + word;
TTF_SizeUTF8(&font, newLine.c_str(), &w, &h);
if(line.empty() || w < width) {
line = newLine;
if(c == '\n') {
result.push_back(line);
line = "";
}
} else {
result.push_back(line);
if(c == '\n') {
result.push_back(word);
line = "";
} else {
line = word;
}
}
word = "";
} else {
word += c;
}
}
if(!word.empty()) line += (line.empty() ? "" : " ") + word;
if(!line.empty()) result.push_back(line);
return result;
}
bool eTexture::loadText(SDL_Renderer* const r,
const std::string& text,
const eFontColor color,
TTF_Font& font,
const int width,
const eAlignment align) {
reset();
if(width) {
int w;
int h;
TTF_SizeUTF8(&font, text.c_str(), &w, &h);
if(w > width) {
const auto lines = textLines(text, font, width);
mWidth = 0;
mHeight = 0;
std::vector<std::shared_ptr<eTexture>> texs;
for(const auto& l : lines) {
auto& tex = texs.emplace_back();
if(!l.empty()) {
tex = std::make_shared<eTexture>();
tex->loadText(r, l, color, font);
mHeight += tex->height();
mWidth = std::max(mWidth, tex->width());
} else {
mHeight += h;
}
}
mTex = SDL_CreateTexture(r, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_TARGET, mWidth, mHeight);
if(!mTex) {
printf("Unable to create texture! "
"SDL Error: %s\n", SDL_GetError());
return false;
}
{
SDL_SetRenderTarget(r, mTex);
const auto bm = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(mTex, bm);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(r, 0, 0, 0, 0);
SDL_RenderFillRect(r, NULL);
int y = 0;
for(const auto& tex : texs) {
if(tex) {
const int w = tex->width();
const int x = align == eAlignment::hcenter ?
(mWidth - w)/2 : 0;
const SDL_Rect dstRect{x, y, w, tex->height()};
SDL_RenderCopy(r, tex->mTex, NULL, &dstRect);
y += dstRect.h;
} else {
y += h;
}
}
SDL_SetRenderTarget(r, nullptr);
}
return true;
}
}
SDL_Color col1;
SDL_Color col2;
eFontColorHelpers::colors(color, col1, col2);
int w;
int h;
const auto tex1 = generateTextTexture(r, text, col1,
font, width, w, h);
if(!tex1) return false;
const auto tex2 = generateTextTexture(r, text, col2,
font, width, w, h);
if(!tex2) return false;
const int dx = 1;
const int dy = 1;
mWidth = w + dx;
mHeight = h + dy;
mTex = SDL_CreateTexture(r, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_TARGET, mWidth, mHeight);
if(!mTex) {
printf("Unable to create texture! "
"SDL Error: %s\n", SDL_GetError());
return false;
}
{
SDL_SetRenderTarget(r, mTex);
const auto bm = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(mTex, bm);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(r, 0, 0, 0, 0);
SDL_RenderFillRect(r, NULL);
const SDL_Rect dstRect2{0, 0, w, h};
SDL_RenderCopy(r, tex2, NULL, &dstRect2);
SDL_SetTextureAlphaMod(tex2, 128);
SDL_RenderCopy(r, tex2, NULL, &dstRect2);
const SDL_Rect dstRect1{dx, dy, w, h};
SDL_RenderCopy(r, tex1, NULL, &dstRect1);
SDL_SetTextureAlphaMod(tex1, 128);
SDL_RenderCopy(r, tex1, NULL, &dstRect1);
SDL_DestroyTexture(tex2);
SDL_DestroyTexture(tex1);
SDL_SetRenderTarget(r, nullptr);
}
return true;
}
bool eTexture::loadText(SDL_Renderer* const r,
const std::string& text,
const eFontColor color,
const eFont& font,
const int width,
const eAlignment align) {
const auto ttf = eFonts::requestFont(font);
if(!ttf) return false;
return loadText(r, text, color, *ttf, width, align);
}
void eTexture::render(SDL_Renderer* const r,
const SDL_Rect& srcRect,
const SDL_Rect& dstRect,
const bool flipped) const {
if(mFlipTex) {
mFlipTex->render(r, srcRect, dstRect, true);
} else if(mParentTex) {
mParentTex->render(r, srcRect, dstRect, flipped);
} else if(mTex) {
if(flipped) {
SDL_RenderCopyEx(r, mTex, &srcRect, &dstRect, 0, nullptr,
SDL_RendererFlip::SDL_FLIP_HORIZONTAL);
} else {
SDL_RenderCopy(r, mTex, &srcRect, &dstRect);
}
}
}
void eTexture::render(SDL_Renderer* const r,
const int x, const int y,
const bool flipped) const {
const int sx = mFlipTex ? mFlipTex->x() : mX;
const int sy = mFlipTex ? mFlipTex->y() : mY;
const int width = mFlipTex ? mFlipTex->width() : mWidth;
const int height = mFlipTex ? mFlipTex->height() : mHeight;
const SDL_Rect srcRect{sx, sy, width, height};
const SDL_Rect dstRect{x, y, width, height};
render(r, srcRect, dstRect, flipped);
}
void eTexture::renderRelPortion(SDL_Renderer* const r,
const int dstX,
const int dstY,
const int srcX,
const int w,
const bool flipped) const {
const int sx = mFlipTex ? mFlipTex->x() : mX;
const int sy = mFlipTex ? mFlipTex->y() : mY;
const int width = mFlipTex ? mFlipTex->width() : mWidth;
const int height = mFlipTex ? mFlipTex->height() : mHeight;
const int ww = std::min(w + srcX, width) - srcX;
SDL_Rect srcRect{sx + srcX, sy, ww, height};
SDL_Rect dstRect{dstX, dstY, ww, height};
if(srcRect.x < sx) {
const int dx = sx - srcRect.x;
srcRect.x += dx;
dstRect.x += dx;
srcRect.w -= dx;
dstRect.w -= dx;
}
if(srcRect.y < sy) {
const int dy = sy - srcRect.y;
srcRect.y += dy;
dstRect.y += dy;
srcRect.h -= dy;
dstRect.h -= dy;
}
if(srcRect.x + srcRect.w > sx + width) {
const int dw = sx + width - srcRect.x - srcRect.w;
srcRect.w += dw;
dstRect.w += dw;
}
if(srcRect.y + srcRect.h > sy + height) {
const int dh = sy + height - srcRect.y - srcRect.h;
srcRect.h += dh;
dstRect.h += dh;
}
if(srcRect.w <= 0 || srcRect.h <= 0 ||
dstRect.w <= 0 || dstRect.h <= 0) return;
if(mFlipTex) {
srcRect.x = mFlipTex->width() - srcRect.x - srcRect.w;
}
render(r, srcRect, dstRect, flipped);
}
void eTexture::setOffset(const int x, const int y) {
mOffsetX = x;
mOffsetY = y;
}
bool eTexture::isNull() const {
if(mFlipTex) return mFlipTex->isNull();
else if(mParentTex) mParentTex->isNull();
return mWidth <= 0 || mHeight <= 0;
}
void eTexture::setAlpha(const Uint8 alpha) {
if(mFlipTex) mFlipTex->setAlpha(alpha);
else if(mParentTex) mParentTex->setAlpha(alpha);
else SDL_SetTextureAlphaMod(mTex, alpha);
}
void eTexture::clearAlphaMod() {
setAlpha(255);
}
void eTexture::setColorMod(const Uint8 r, const Uint8 g, const Uint8 b) {
if(mFlipTex) mFlipTex->setColorMod(r, g, b);
else if(mParentTex) mParentTex->setColorMod(r, g, b);
else SDL_SetTextureColorMod(mTex, r, g, b);
}
void eTexture::clearColorMod() {
setColorMod(255, 255, 255);
}
void eTexture::setFlipTex(const std::shared_ptr<eTexture>& tex) {
mFlipTex = tex;
mX = mFlipTex->x();
mY = mFlipTex->y();
mWidth = mFlipTex->width();
mHeight = mFlipTex->height();
}
void eTexture::setParentTexture(const SDL_Rect& rect,
const std::shared_ptr<eTexture>& tex) {
mParentTex = tex;
mX = rect.x;
mY = rect.y;
mWidth = rect.w;
mHeight = rect.h;
}