-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathText.cpp
375 lines (286 loc) · 12.4 KB
/
Text.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
#include "Text.h"
#include "Game.h"
#include "TextureManager.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <string>
#include <map>
static std::vector<std::string> extractTextSegments(std::string text) {
size_t delimitingIndex;
std::string delimitingText;
std::vector<std::string> textSegments;
if ((delimitingIndex = text.find("Dying Trees")) != std::string::npos) {
delimitingText = "Dying Trees";
}
else if ((delimitingIndex = text.find("The Bucket")) != std::string::npos) {
delimitingText = "The Bucket";
}
else if ((delimitingIndex = text.find("Tree Stump")) != std::string::npos) {
delimitingText = "Tree Stump";
}
else if ((delimitingIndex = text.find("Stalker Pups")) != std::string::npos) {
delimitingText = "Stalker Pups";
}
else if ((delimitingIndex = text.find("Stalker")) != std::string::npos) {
delimitingText = "Stalker";
}
else if ((delimitingIndex = text.find("Brute")) != std::string::npos) {
delimitingText = "Brute";
}
else if ((delimitingIndex = text.find("Night Gale")) != std::string::npos) {
delimitingText = "Night Gale";
}
else {
delimitingText = "";
delimitingIndex = 0;
}
if (delimitingIndex != 0) {
textSegments.push_back(std::string(text, 0, delimitingIndex));
textSegments.push_back(delimitingText);
}
size_t start = delimitingIndex + delimitingText.size();
size_t next = start;
for (; ;) {
if ((next = text.find('\n', start)) == std::string::npos) {
textSegments.push_back(std::string(text, start, text.size() - start));
break;
}
else {
textSegments.push_back(std::string(text, start, next - start));
textSegments.push_back("\\n");
start = next + 1;
}
}
return textSegments;
}
TextManager::~TextManager() {
for (auto& text : mTextCache) {
SDL_DestroyTexture(text.second);
}
mTextCache.clear();
}
void TextManager::init() {
SDL_Color dinoRed = { 184, 37, 53 };
SDL_Color snowWhite = { 255, 255, 255 };
addToTextCache_AllAtOnce("DINO SAUR", "Assets/fonts/Marshland_Beauty.otf", 72, dinoRed);
addToTextCache_AllAtOnce("press [SPACE] or tap to start", "Assets/fonts/ALBA____.TTF", 36, snowWhite);
addToTextCache_AllAtOnce("a 2D remake of the classic Chrome dinosaur game", "Assets/fonts/ALBA____.TTF", 36, snowWhite);
addToTextCache_AllAtOnce("by wldfngrs; https://github.com/wldfngrs", "Assets/fonts/ALBA____.TTF", 36, snowWhite);
addToTextCache_AllAtOnce("press [SPACE] or tap to run again", "Assets/fonts/ALBA____.TTF", 36, snowWhite);
addToTextCache_AllAtOnce("HIGH SCORE: ", "Assets/fonts/junegull.ttf", 48, snowWhite);
addToTextCache_AllAtOnce("CURRENT SCORE: ", "Assets/fonts/junegull.ttf", 48, snowWhite);
addToTextCache_CharByChar("0123456789", "Assets/fonts/junegull.ttf", 48, snowWhite);
addToTextCache_CharByChar(" ", "Assets/fonts/junegull.ttf", 48, snowWhite);
addToTextCache_AllAtOnce("Congratulations! You beat the game!", "Assets/fonts/ALBA____.TTF", 48, snowWhite);
addToTextCache_AllAtOnce("It's no big deal, and certainly no difficult task, yet...", "Assets/fonts/ALBA____.TTF", 48, snowWhite);
addToTextCache_AllAtOnce("I appreciate you for playing this long", "Assets/fonts/ALBA____.TTF", 48, snowWhite);
addToTextCache_AllAtOnce("I had fun working on this. I hope you had fun playing as well", "Assets/fonts/ALBA____.TTF", 48, snowWhite);
addToTextCache_AllAtOnce("Have a great day, anon!", "Assets/fonts/ALBA____.TTF", 48, snowWhite);
addToTextCache_AllAtOnce("Sh*t! caught in the ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Dying Trees", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("!", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Wriggle out for your next run?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Danmit! You've kicked ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("The Bucket", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("Did I mention you have nine (ahem) lives?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Oof! crashed right into a ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Tree Stump", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("Watch your path, Rex!", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Hehe, the one-eyed ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Stalker", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("...", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Look away next time, okay?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Yikes, ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Stalker Pups", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("Nothing to see or say here, Rex, run again?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Ouch, crushed by the merciless ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Brute", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce("Oh well, you're not THAT hurt, go again?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Whoaa, you let the ", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Night Gale", "Assets/fonts/adrip1.ttf", 72, dinoRed);
addToTextCache_AllAtOnce(" sweep you away?", "Assets/fonts/adrip1.ttf", 72, snowWhite);
addToTextCache_AllAtOnce("Hold on tight next run!", "Assets/fonts/adrip1.ttf", 72, snowWhite);
mInitDone = true;
}
static int calculateXPosition(std::vector<std::string> textSegments, size_t indexIntoTextSegments, xPosition xpos, size_t letterWidth, renderEffect effect) {
int x = 0;
int textWidth = 0;
int index = static_cast<int>(indexIntoTextSegments);
if (effect == TYPEWRITER) {
for (size_t i = 0; i < textSegments[index].size(); i++) {
std::string letter;
letter += textSegments[index][i];
if (letter == "i" || letter == "!" || letter == "," || letter == "." || letter == "l" || letter == "I" || letter == "'") {
textWidth += static_cast<int>(letterWidth / 2);
}
else {
textWidth += static_cast<int>(letterWidth);
}
}
}
else {
for (size_t i = index; i < textSegments.size(); i++) {
if (textSegments[i] == "\\n") {
break;
}
textWidth += static_cast<int>(textSegments[i].size() * letterWidth);
}
}
switch (xpos) {
case CENTERED: {
x = static_cast<int>(Game::mSCREEN_WIDTH / 2 - textWidth / 2);
break;
}
case RIGHT: {
x = static_cast<int>(Game::mSCREEN_WIDTH - textWidth);
break;
}
case LEFT: {
x = 0;
}
}
return x;
}
void TextManager::typeWriterDrawAndRender(std::string lineOfText, int x, int y, size_t letterWidth, size_t letterHeight) {
if (mInitDone == false) return;
for (size_t i = 1; i < lineOfText.size(); i++) {
std::string letter;
letter += lineOfText[i];
if (letter == "i" || letter == "!" || letter == "," || letter == "." || letter == "l" || letter == "I" || letter == "'") {
TextureManager::drawText(mTextCache[letter], x, y, letterWidth / 2, letterHeight);
x += static_cast<int>(letterWidth / 2);
}
else {
TextureManager::drawText(mTextCache[letter], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth);
}
SDL_RenderPresent(Game::mGameRenderer);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
void TextManager::drawText_Static(std::string text, xPosition xpos, int y, size_t letterWidth, size_t letterHeight, renderEffect effect) {
if (mInitDone == false) return;
std::vector<std::string> textSegments = extractTextSegments(text);
int x = calculateXPosition(textSegments, 0, xpos, letterWidth, effect);
if (effect == TYPEWRITER) {
typeWriterDrawAndRender(textSegments[0], x, y, letterWidth, letterHeight);
}
else {
TextureManager::drawText(mTextCache[textSegments[0]], x, y, textSegments[0].size() * letterWidth, letterHeight);
}
for (size_t i = 1; i < textSegments.size(); i++) {
if (textSegments[i] == "\\n") {
y += static_cast<int>(letterHeight + 2);
x = calculateXPosition(textSegments, ++i, xpos, letterWidth, effect);
}
else {
x += static_cast<int>(textSegments[i - 1].size() * letterWidth);
}
if (effect == TYPEWRITER) {
typeWriterDrawAndRender(textSegments[i], x, y, letterWidth, letterHeight);
}
else {
TextureManager::drawText(mTextCache[textSegments[i]], x, y, textSegments[i].size() * letterWidth, letterHeight);
}
}
}
void TextManager::drawText_Static(std::string text, int x, int y, size_t letterWidth, size_t letterHeight, renderEffect effect) {
if (mInitDone == false) return;
std::vector<std::string> textSegments = extractTextSegments(text);
size_t textWidth = textSegments[0].size() * letterWidth;
if (effect == TYPEWRITER) {
typeWriterDrawAndRender(textSegments[0], x, y, letterWidth, letterHeight);
}
else {
TextureManager::drawText(mTextCache[textSegments[0]], x, y, textWidth, letterHeight);
}
for (size_t i = 1; i < textSegments.size(); i++) {
if (textSegments[i] == "\\n") {
y += static_cast<int>(letterHeight + 2);
textWidth = textSegments[++i].size() * letterWidth;
}
else {
x += static_cast<int>(textWidth);
textWidth = textSegments[i].size() * letterWidth;
}
if (effect == TYPEWRITER) {
typeWriterDrawAndRender(textSegments[i], x, y, letterWidth, letterHeight);
}
else {
TextureManager::drawText(mTextCache[textSegments[i]], x, y, textWidth, letterHeight);
}
}
}
void TextManager::drawText_NonStatic(std::string text, int x, int y, size_t letterWidth, size_t letterHeight) {
if (mInitDone == false) return;
size_t spaces = 7 - text.size();
for (size_t i = 0; i < spaces; i++) {
TextureManager::drawText(mTextCache[" "], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth + 1);
}
for (size_t i = 0; i < text.size(); i++) {
std::string num;
num += text[i];
TextureManager::drawText(mTextCache[num], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth + 1);
}
}
void TextManager::drawText_NonStatic(std::string text, xPosition xpos, int y, size_t letterWidth, size_t letterHeight) {
if (mInitDone == false) return;
size_t spaces = 7 - text.size();
std::vector<std::string> textSegments = extractTextSegments(text);
int x = calculateXPosition(textSegments, 0, xpos, letterWidth, INSTANT);
for (size_t i = 0; i < spaces; i++) {
TextureManager::drawText(mTextCache[" "], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth + 1);
}
for (size_t i = 0; i < text.size(); i++) {
std::string num;
num += text[i];
TextureManager::drawText(mTextCache[num], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth + 1);
}
}
void TextManager::drawText_Static_NonStatic(std::string text1, std::string text2, xPosition xpos, int y, size_t letterWidth, size_t letterHeight) {
if (mInitDone == false) return;
std::vector<std::string> textSegments = extractTextSegments(text1 + text2);
int x = calculateXPosition(textSegments, 0, xpos, letterWidth, INSTANT);
size_t w = text1.size() * letterWidth;
TextureManager::drawText(mTextCache[text1], x, y, w, letterHeight);
x += static_cast<int>(w + 1);
w = text2.size() * letterWidth;
for (size_t i = 0; i < text2.size(); i++) {
std::string txt;
txt += text2[i];
TextureManager::drawText(mTextCache[txt], x, y, letterWidth, letterHeight);
x += static_cast<int>(letterWidth + 1);
}
}
void TextManager::addToTextCache_AllAtOnce(std::string text, const char* fontPath, const int fontIndex, SDL_Color color) {
TTF_Font* font = TTF_OpenFont(fontPath, fontIndex);
if (font == nullptr) {
std::cerr << "[Error] TextManager::init(): '" << fontPath << "' TTF_OpenFont() failed!\nDetails: " << TTF_GetError() << "\n\n";
return;
}
mTextCache[text] = TextureManager::loadTextTexture(text, font, color);
TTF_CloseFont(font);
}
void TextManager::addToTextCache_CharByChar(std::string text, const char* fontPath, const int fontIndex, SDL_Color color) {
TTF_Font* font = TTF_OpenFont(fontPath, fontIndex);
if (font == nullptr) {
std::cerr << "[Error] TextManager::init(): '" << fontPath << "' TTF_OpenFont() failed!\nDetails: " << TTF_GetError() << "\n\n";
return;
}
for (size_t i = 0; i < text.size(); i++) {
std::string txt;
txt += text[i];
if (mTextCache.find(txt) == mTextCache.end()) {
mTextCache[txt] = TextureManager::loadTextTexture(txt, font, color);
}
}
TTF_CloseFont(font);
}