Debian Codegraph @

grtext/grtext.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/grtext/grtext.cpp $
21 * $Revision: 38 $
22 * $Date: 11/16/99 3:18p $
23 * $Author: Samir $
24 *
25 *
26 *
27 * $Log: /DescentIII/Main/grtext/grtext.cpp $
28 *
29 * 38 11/16/99 3:18p Samir
30 * added new data to font file and kept compatibility with D3 fonts:
31 * tracking value.
32 *
33 * 37 10/21/99 9:27p Jeff
34 * B.A. Macintosh code merge
35 *
36 * 36 5/02/99 7:16p Samir
37 * fixed text line width problem with newline chars.
38 *
39 * 35 5/02/99 1:50a Samir
40 * fixed nasty bug where intrastring text colors weren't getting set due
41 * to a change in how we do color for text now.
42 *
43 * 34 4/26/99 6:56p Kevin
44 * doh! forgot the 0 termination
45 *
46 * 33 4/26/99 6:55p Kevin
47 * added some more naughty words
48 *
49 * 32 4/24/99 8:41p Samir
50 * fixed clipped text scaling probs.
51 *
52 * 31 4/17/99 6:16p Samir
53 * added kerning and 4444 alphaed font support.
54 *
55 * 30 4/13/99 4:39p Samir
56 * simplified color scheme.
57 *
58 * 29 4/09/99 3:16p Kevin
59 * Fixed a stupid memory leak
60 *
61 * 28 4/02/99 3:49p Kevin
62 * Added profanity filter code
63 *
64 * 27 3/02/99 6:26p Samir
65 * added font template width and height functions.
66 *
67 * 26 1/20/99 3:47a Jeff
68 * added function to get clipping parameters
69 *
70 * 25 11/03/98 7:04p Samir
71 * made Grtext_spacing global so word wrapper could access it.
72 *
73 * 24 11/01/98 1:58a Jeff
74 * converted the vsprintf calls to use the Pvsprintf, which is a safe
75 * vsprintf, no buffer overflows allowed
76 *
77 * 23 10/22/98 2:41p Samir
78 * made grtext_Puts public.
79 *
80 * 22 10/19/98 10:42p Jason
81 * made Int3s into breaks
82 *
83 * 21 9/24/98 2:56p Samir
84 * added ability to format strings inside string.
85 *
86 * 20 9/21/98 2:58p Samir
87 * may have fixed GetTextHeight function.
88 *
89 * 19 9/18/98 11:29a Samir
90 * use strchr instead of strtok.
91 *
92 * 18 9/08/98 5:42p Samir
93 * fixed memory leak
94 *
95 * 17 9/08/98 10:27a Samir
96 * added function to get text height.
97 *
98 * 16 9/04/98 3:22p Samir
99 * newlines handled a bit better.
100 *
101 * 15 8/24/98 3:12p Samir
102 * fixed text clipping
103 *
104 * 14 6/23/98 5:05p Samir
105 * streamlined strlen calls
106 *
107 * 13 5/15/98 5:35p Samir
108 * fixed instring text color formatting.
109 *
110 * 12 5/05/98 6:29p Samir
111 * spaces don't render, just width of it counts.
112 *
113 * 11 4/27/98 3:46p Samir
114 * scaling fonts.
115 *
116 * 10 4/18/98 2:08a Samir
117 * extended text buffer.
118 *
119 * 9 2/13/98 6:37p Samir
120 * Fixed tabs.
121 *
122 * 8 2/03/98 12:20p Samir
123 * Be sure to set alphatype when Grtext_SetFlags operation occurs.
124 *
125 * 7 2/03/98 12:13p Samir
126 * Font shadowing support added.
127 *
128 * 6 1/30/98 2:15p Samir
129 * Allow for text saturation.
130 *
131 * 5 1/23/98 6:53p Samir
132 * Added grtext_PutChar.
133 *
134 * 4 1/08/98 12:16p Samir
135 * GetLineWidth now takes a const char *
136 *
137 * 3 12/30/97 5:20p Samir
138 * Some stuff.
139 *
140 * 2 12/29/97 5:49p Samir
141 * Added centering text capability.
142 *
143 * 1 12/29/97 3:24p Samir
144 * Initial revision
145 *
146 * $NoKeywords: $
147 */
148
149 #include <cstdarg>
150 #include <cstdio>
151 #include <cstring>
152 #include <cctype>
153
154 #include "grtextlib.h"
155 #include "pserror.h"
156 #include "renderer.h"
157
158 #define CLIP_LEFT Grtext_left
159 #define CLIP_TOP Grtext_top
160 #define CLIP_RIGHT Grtext_right
161 #define CLIP_BOTTOM Grtext_bottom
162
163 // Text opcodes
164 #define GRTEXTOP_PUTS 0x1 // 2 shorts, x and y
165 #define GRTEXTOP_SETCOLOR 0x2 // 1 ddgr_color
166 #define GRTEXTOP_FANCYCOLOR 0x3 // 4 ddgr_colors
167 #define GRTEXTOP_SETFONT 0x4 // 1 font handle (int)
168 #define GRTEXTOP_SETALPHA 0x5 // 1 uint8_t
169 #define GRTEXTOP_PUTCHAR 0x6 // (2 shorts) 1 uint8_t
170 #define GRTEXTOP_SETFLAGS 0x7 // (flags) = 1 int.
171 #define GRTEXTOP_SCALE 0x8 // 1 float (scale) scale current font.
172
173 // Text execution buffer
174 int Grtext_spacing = 1;
175 static char Grtext_buffer[GRTEXT_BUFLEN];
176 static int Grtext_ptr = 0;
177 static int Grtext_font = 0;
178 static uint8_t Grtext_alpha = 255;
179 static int8_t Grtext_alphatype = ATF_TEXTURE + ATF_CONSTANT;
180 static bool Grtext_shadow = false;
181 static int Grtext_line_spacing = 1;
182 static int Grtext_tabspace = 0;
183 static int Grtext_left, Grtext_top, Grtext_right, Grtext_bottom;
184 static ddgr_color Grtext_colors[2];
185 static float Grtext_scale = 1.0f;
186 static ddgr_color Grtext_color = GR_BLACK;
187
188 // draws a string
189 static void grtext_RenderString(int x, int y, char *str);
190 static void grtext_DrawTextLine(int x, int y, char *str);
191 static void grtext_DrawTextLineClip(int x, int y, char *str);
192
193 #define XORVAL 205
194 #define MAX_BAD_WORD_LEN 10
195 typedef uint8_t badword[MAX_BAD_WORD_LEN];
196 // These all need to be lower case!
197 static badword bad_words[] = {
198 // These are just XOR'd with XORVAL
199 {0xab, 0xb8, 0xae, 0xa6, 0x00}, // = fuck
200 {0xbe, 0xa5, 0xa4, 0xb9, 0x00}, // = shit
201 {0xae, 0xb8, 0xa3, 0xb9, 0x00}, // = cunt
202 {0xba, 0xa5, 0xa2, 0xbf, 0xa8, 0x00}, // = whore
203 {0xac, 0xbe, 0xbe, 0xa5, 0xa2, 0xa1, 0xa8, 0x00}, // = asshole
204 {0xa9, 0xac, 0xa0, 0xa3, 0x00}, // = damn
205 {0xbd, 0xb8, 0xbe, 0xbe, 0xb4, 0x00}, // pussy
206 {0xaf, 0xa4, 0xb9, 0xae, 0xa5, 0x00}, // bitch
207 {0xa9, 0xac, 0xa0, 0xa3, 0x00}, // damn
208 {0xbd, 0xa4, 0xbe, 0xbe, 0x00} // piss
209 };
210
211 #define NUM_BAD_WORDS (sizeof(bad_words) / sizeof(badword))
212
213 static const char subst_chars[] = "#!&@&#%*";
214
215 #define NUM_SUBST_CHARS (sizeof(subst_chars) - 1)
216
217 static bool grtext_FilterProfanity = false;
218
219 void grtext_SetProfanityFilter(bool enabled) { grtext_FilterProfanity = enabled; }
220 // Right now this just decrypts the bad words
221 void grtext_Init() {
222 for (auto & bad_word : bad_words) {
223 int pos = 0;
224
225 while (bad_word[pos]) {
226 bad_word[pos] = bad_word[pos] ^ XORVAL;
227 pos++;
228 }
229 }
230 }
231
•232 // macros to get character dimensions
233 static inline int GRCHAR_WIDTH(int font, int ch) { return (int)(grfont_GetCharWidth(font, ch) * Grtext_scale); }
234 static inline int GRCHAR_HEIGHT(int font) { return (int)(grfont_GetHeight(font) * Grtext_scale); }
235 static inline int GRCHAR_SPACING(int font, int ch1, int ch2) { return (int)(grfont_GetKernedSpacing(font, ch1, ch2)); }
236 static inline int GRCHAR_HEIGHT_TEMP(const tFontTemplate *ft) { return (int)ft->ch_height; }
237 static inline int GRCHAR_WIDTH_TEMP(const tFontTemplate *ft, int ch) {
238 if (ch > ft->max_ascii && ft->uppercase) {
239 ch = toupper(ch);
240 } else if (ch < ft->min_ascii || ch > ft->max_ascii) {
241 return 0;
242 }
243
244 return (int)((ft->proportional) ? ft->ch_widths[ch - ft->min_ascii] : ft->ch_maxwidth);
245 }
•246 static inline int GRCHAR_SPACING_TEMP(const tFontTemplate *ft, int ch1, int ch2) {
247 if (ch1 > ft->max_ascii && ft->uppercase) {
248 ch1 = toupper(ch1);
249 }
250 if (ch2 > ft->max_ascii && ft->uppercase) {
251 ch2 = toupper(ch2);
252 }
253
254 return (int)(grfont_GetKernedSpacingTemp(ft, ch1, ch2));
255 }
256
257 // clears text buffer. doesn't render.
258 void grtext_Reset() {
259 Grtext_buffer[0] = 0;
260 Grtext_ptr = 0;
261 Grtext_alpha = 255;
262 Grtext_scale = 1.0f;
263 Grtext_spacing = 1;
264 grtext_SetFlags(0);
265 grtext_SetColor(GR_BLACK);
266 }
267
268 // sets text clipping parameters
269 void grtext_SetParameters(int left, int top, int right, int bottom, int tabspace) {
270 Grtext_left = left;
271 Grtext_top = top;
272 Grtext_right = right;
273 Grtext_bottom = bottom;
274 Grtext_tabspace = tabspace;
275 }
276
277 // gets text clipping parameters
278 void grtext_GetParameters(int *left, int *top, int *right, int *bottom, int *tabspace) {
279 if (left)
280 *left = Grtext_left;
281 if (top)
282 *top = Grtext_top;
283 if (right)
284 *right = Grtext_right;
285 if (bottom)
286 *bottom = Grtext_bottom;
287 if (tabspace)
288 *tabspace = Grtext_tabspace;
289 }
290
291 // sets the color for text
292 void grtext_SetColor(ddgr_color col) {
293 struct {
294 char op;
295 ddgr_color col;
296 } cmd;
297
298 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
299
300 cmd.op = GRTEXTOP_SETCOLOR;
301 cmd.col = col;
302
303 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
304 Grtext_ptr += sizeof(cmd);
305 Grtext_color = col;
306 }
307
308 // returns color set by grtext_SetColor (does not count intra-string color changes)
309 ddgr_color grtext_GetColor() { return Grtext_color; }
310
311 // sets font scale (1.0 = normal, 0.5 = 1/2, 2.0 = twice as large.
312 void grtext_SetFontScale(float scale) {
313 struct {
314 char op;
315 float scale;
316 } cmd;
317
318 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
319
320 cmd.op = GRTEXTOP_SCALE;
321 cmd.scale = scale;
322
323 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
324 Grtext_ptr += sizeof(cmd);
325
326 Grtext_scale = scale;
327 }
328
329 // sets fancy color for text
330 void grtext_SetFancyColor(ddgr_color col1, ddgr_color col2, ddgr_color col3, ddgr_color col4) {
331 struct {
332 char op;
333 ddgr_color col;
334 } cmd;
335
336 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
337
338 cmd.op = GRTEXTOP_FANCYCOLOR;
339 cmd.col = col1;
340
341 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
342 Grtext_ptr += sizeof(cmd);
343 }
344
345 // sets the alpha value for text
346 void grtext_SetAlpha(uint8_t alpha) {
347 struct {
348 char op;
349 uint8_t alpha;
350 } cmd;
351
352 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
353 cmd.op = GRTEXTOP_SETALPHA;
354 cmd.alpha = alpha;
355 Grtext_alpha = alpha;
356
357 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
358 Grtext_ptr += sizeof(cmd);
359 }
360
361 // gets font alpha
362 uint8_t grtext_GetAlpha() { return Grtext_alpha; }
363
364 // toggles text saturation
365 void grtext_SetFlags(int flags) {
366 struct {
367 char op;
368 int flags;
369 } cmd;
370
371 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
372
373 cmd.op = GRTEXTOP_SETFLAGS;
374 cmd.flags = flags;
375
376 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
377 Grtext_ptr += sizeof(cmd);
378 }
379
380 // sets the font for text
381 void grtext_SetFont(int font_handle) {
382 struct {
383 char op;
384 int handle;
385 } cmd;
386
387 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
388 cmd.op = GRTEXTOP_SETFONT;
389 cmd.handle = font_handle;
390 Grtext_font = cmd.handle;
391 Grtext_spacing = 1 + grfont_GetTracking(Grtext_font);
392
393 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
394 Grtext_ptr += sizeof(cmd);
395 }
396
397 // puts a formatted string in the text buffer
398 void grtext_Printf(int x, int y, const char *fmt, ...) {
399 std::va_list arglist;
400 int len;
401 char buf[512];
402
403 va_start(arglist, fmt);
404 len = std::vsnprintf(buf, 512, fmt, arglist);
405 va_end(arglist);
406 if (len < 0)
407 return;
408
409 grtext_Puts(x, y, buf);
410 }
411
412 // gets the current font
413 int grtext_GetFont() { return Grtext_font; }
414
415 // puts a centered string in the text buffer.
416 void grtext_CenteredPrintf(int xoff, int y, const char *fmt, ...) {
417 int new_x;
418 std::va_list arglist;
419 int len;
420 char buf[512];
421
422 va_start(arglist, fmt);
423 len = std::vsnprintf(buf, 512, fmt, arglist);
424 va_end(arglist);
425 if (len < 0)
426 return;
427
428 new_x = Grtext_left + (Grtext_right - Grtext_left) / 2 - grtext_GetTextLineWidth(buf) / 2;
429
430 grtext_Puts(new_x + xoff, y, buf);
431 }
432
433 #define GR_STR_LEN 128
434 // draws a string
435 void grtext_Puts(int x, int y, const char *str) {
436 struct {
437 char op;
438 int16_t x, y;
439 } cmd;
440
441 ASSERT((Grtext_ptr + sizeof(cmd) + strlen(str) + 1) < GRTEXT_BUFLEN);
442
443 cmd.op = GRTEXTOP_PUTS;
444 cmd.x = (int16_t)x;
445 cmd.y = (int16_t)y;
446
447 ASSERT((Grtext_ptr + sizeof(cmd) + strlen(str) + 1) < GRTEXT_BUFLEN);
448
449 cmd.op = GRTEXTOP_PUTS;
450 cmd.x = (int16_t)x;
451 cmd.y = (int16_t)y;
452 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
453 Grtext_ptr += sizeof(cmd);
454 strcpy(&Grtext_buffer[Grtext_ptr], str);
455
456 if (grtext_FilterProfanity) {
457 // DAJ changed to local to reduce memory thrashing
458 // DAJ char *lowerstr = (char *)mem_malloc(strlen(str)+1);
459 char lowerstr[GR_STR_LEN];
460
461 int slen = strlen(str);
462 if (slen >= GR_STR_LEN)
463 slen = GR_STR_LEN - 1;
464
465 for (int a = 0; a < slen; a++)
466 lowerstr[a] = tolower(str[a]);
467
468 lowerstr[slen] = '\0';
469
470 for (auto & bad_word : bad_words) {
471 char *p = strstr(lowerstr, (char *)bad_word);
472 while (p) {
473 int len = strlen((char *)bad_word);
474 char *realp = (char *)((int)(p - lowerstr) + &Grtext_buffer[Grtext_ptr]);
475 for (int a = 0; a < len; a++) {
476 ASSERT(p);
477 *realp = subst_chars[(*realp) % NUM_SUBST_CHARS];
478 *p = *realp;
479 realp++;
480 }
481 p = strstr(lowerstr, (char *)bad_word);
482 };
483 }
484 // DAJ mem_free(lowerstr);
485 }
486
487 Grtext_ptr += (strlen(str) + 1);
488 }
489
490 // puts a character down
491 void grtext_PutChar(int x, int y, int ch) {
492 struct {
493 char op;
494 int16_t x, y;
495 uint8_t ch;
496 } cmd;
497
498 ASSERT((Grtext_ptr + sizeof(cmd)) < GRTEXT_BUFLEN);
499
500 cmd.op = GRTEXTOP_PUTCHAR;
501 cmd.x = (int16_t)x;
502 cmd.y = (int16_t)y;
503 cmd.ch = (uint8_t)ch;
504 memcpy(&Grtext_buffer[Grtext_ptr], &cmd, sizeof(cmd));
505 Grtext_ptr += sizeof(cmd);
506 }
507
508 // renders all text in buffer and clears buffer. USUALLY CALL THIS TO RENDER TEXT
509 void grtext_Flush() {
510 grtext_Render();
511 grtext_Reset();
512 }
513
514 // renders all text but DOESN'T flush buffer
515 void grtext_Render() {
516 // setup rendering of text.
517 rend_SetTextureType(TT_LINEAR);
518 rend_SetOverlayType(OT_NONE);
519 rend_SetFiltering(0);
520 rend_SetLighting(LS_FLAT_GOURAUD);
521 rend_SetAlphaType(ATF_TEXTURE + ATF_CONSTANT);
522 rend_SetColorModel(CM_MONO);
523 rend_SetZBufferState(0);
524 rend_SetAlphaValue(Grtext_alpha);
525
526 // render text.
527 int pos = 0;
528
529 while (pos < Grtext_ptr) {
530 switch (Grtext_buffer[pos]) {
531 case GRTEXTOP_SETCOLOR: {
532 struct {
533 char op;
534 ddgr_color col;
535 } cmd;
536
537 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
538 pos += sizeof(cmd);
539 rend_SetFlatColor(cmd.col);
540 // rend_SetCharacterParameters(cmd.col, cmd.col, cmd.col, cmd.col);
541 Grtext_colors[0] = cmd.col;
542 // Grtext_colors[1] = cmd.col[1];
543 // Grtext_colors[2] = cmd.col[2];
544 // Grtext_colors[3] = cmd.col[3];
545 } break;
546
547 case GRTEXTOP_SETFLAGS: {
548 struct {
549 char op;
550 int flags;
551 } cmd;
552
553 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
554 pos += sizeof(cmd);
555
556 if (cmd.flags & GRTEXTFLAG_SATURATE)
557 Grtext_alphatype = AT_SATURATE_TEXTURE;
558 else
559 Grtext_alphatype = ATF_CONSTANT + ATF_TEXTURE;
560 rend_SetAlphaType(Grtext_alphatype);
561
562 if (cmd.flags & GRTEXTFLAG_SHADOW)
563 Grtext_shadow = true;
564 else
565 Grtext_shadow = false;
566 } break;
567
568 case GRTEXTOP_FANCYCOLOR: {
569 struct {
570 char op;
571 // ddgr_color col[4];
572 ddgr_color col;
573 } cmd;
574
575 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
576 pos += sizeof(cmd);
577 rend_SetFlatColor(cmd.col);
578 // rend_SetCharacterParameters(cmd.col[0], cmd.col[1], cmd.col[2], cmd.col[3]);
579 Grtext_colors[0] = cmd.col;
580 // Grtext_colors[1] = cmd.col[1];
581 // Grtext_colors[2] = cmd.col[2];
582 // Grtext_colors[3] = cmd.col[3];
583 } break;
584
585 case GRTEXTOP_SETALPHA: {
586 struct {
587 char op;
588 uint8_t alpha;
589 } cmd;
590
591 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
592 pos += sizeof(cmd);
593 rend_SetAlphaValue(cmd.alpha);
594 } break;
595
596 case GRTEXTOP_SETFONT: {
597 struct {
598 char op;
599 int handle;
600 } cmd;
601
602 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
603 pos += sizeof(cmd);
604 Grtext_font = cmd.handle;
605 } break;
606
607 case GRTEXTOP_PUTS: {
608 struct {
609 char op;
610 int16_t x, y;
611 } cmd;
612
613 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
614 pos += sizeof(cmd);
615 if (Grtext_shadow) {
616 // rend_SetCharacterParameters(0,0,0,0);
617 rend_SetFlatColor(0);
618 grtext_RenderString(cmd.x + 1, cmd.y + 1, &Grtext_buffer[pos]);
619 rend_SetFlatColor(Grtext_colors[0]);
620 // rend_SetCharacterParameters(Grtext_colors[0],Grtext_colors[1],Grtext_colors[2],Grtext_colors[3]);
621 }
622 grtext_RenderString(cmd.x, cmd.y, &Grtext_buffer[pos]);
623 pos += (strlen(&Grtext_buffer[pos]) + 1);
624 } break;
625
626 case GRTEXTOP_PUTCHAR: {
627 tCharBlt cbi;
628 struct {
629 char op;
630 int16_t x, y;
631 uint8_t ch;
632 } cmd;
633
634 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
635 pos += sizeof(cmd);
636 cbi.ch = cmd.ch;
637 cbi.clipped = false; // if =1, use sx,sy,sw,sh
638 cbi.x = cmd.x;
639 cbi.y = cmd.y;
640 if (Grtext_shadow) {
641 // rend_SetCharacterParameters(0,0,0,0);
642 rend_SetFlatColor(0);
643 cbi.x += 1;
644 cbi.y += 1;
645 grfont_BltChar(Grtext_font, &cbi);
646 rend_SetFlatColor(Grtext_colors[0]);
647 // rend_SetCharacterParameters(Grtext_colors[0],Grtext_colors[1],Grtext_colors[2],Grtext_colors[3]);
648 cbi.x -= 1;
649 cbi.y -= 1;
650 }
651 grfont_BltChar(Grtext_font, &cbi);
652 } break;
653
654 case GRTEXTOP_SCALE: {
655 struct {
656 char op;
657 float scale;
658 } cmd;
659
660 memcpy(&cmd, &Grtext_buffer[pos], sizeof(cmd));
661 pos += sizeof(cmd);
662 Grtext_scale = cmd.scale;
663 } break;
664
665 default:
666 Int3(); // Truly bad.
667 }
668 }
669
670 // restore original state
671 rend_SetFiltering(1);
672 rend_SetZBufferState(1);
673 }
674
675 // draws a string
676 void grtext_RenderString(int x, int y, char *str) {
677 int cur_x = x, cur_y = y, gx, gy;
678 char *line = str, *save_pos;
679
680 do {
681 // Look for newline, and if found, put in a 0 to terminate the line
682 save_pos = strchr(line, '\n');
683 if (save_pos) // found an newline
684 *save_pos = 0; // terminate the line
685
686 int line_width;
687 int clipped;
688
689 line_width = grtext_GetTextLineWidth(line);
690
691 gy = cur_y;
692 gx = cur_x; // Clip function needs global coords.
693
694 clipped = 0;
•695 if ((CLIP_TOP > (gy + GRCHAR_HEIGHT(Grtext_font))) || (CLIP_BOTTOM < gy))
696 clipped = 2;
697 else if ((CLIP_LEFT > (gx + line_width)) || (CLIP_RIGHT < gx))
698 clipped = 2;
699
700 if (clipped != 2) {
701 if (CLIP_LEFT > gx || CLIP_RIGHT < (gx + line_width))
702 clipped = 1;
•703 if (CLIP_TOP > gy || CLIP_BOTTOM < (gy + GRCHAR_HEIGHT(Grtext_font)))
704 clipped = 1;
705
706 if (clipped == 0)
707 grtext_DrawTextLine(gx, gy, line);
708 else if (clipped == 1)
709 grtext_DrawTextLineClip(gx, gy, line);
710 }
•711 cur_y += (Grtext_line_spacing + GRCHAR_HEIGHT(Grtext_font));
712 cur_x = CLIP_LEFT;
713
714 // replace the newline, if there was one
715 if (save_pos)
716 *save_pos++ = '\n';
717
718 line = save_pos;
719
720 } while (line);
721 }
722
723 int grtext_GetTextHeightTemplate(tFontTemplate *ft, const char *str) {
724 int cur_h = 0;
725 const char *spos = str;
726
727 do {
•728 cur_h += (Grtext_line_spacing + GRCHAR_HEIGHT_TEMP(ft));
729
730 spos = strchr(spos, '\n');
731 if (spos)
732 spos++;
733 } while (spos);
734
735 return cur_h;
736 }
737
738 int grtext_GetTextHeight(const char *str) {
739 int cur_h = 0;
740 const char *spos = str;
741
742 do {
•743 cur_h += (Grtext_line_spacing + GRCHAR_HEIGHT(Grtext_font));
744
745 spos = strchr(spos, '\n');
746 if (spos)
747 spos++;
748 } while (spos);
749
750 return cur_h;
751 }
752
753 int grtext_GetTextLineWidth(const char *str) {
754 int line_width = 0, max_width = 0;
755 int rgb_define_mode = 0;
756 int strsize = strlen(str);
757 int line_idx = 0;
758
759 for (int i = 0; i < strsize; i++) {
760 int width;
761 char ch = str[i], ch2 = str[i + 1];
762
763 // note that if we hit the GR_COLOR_CHAR then the next three values should
764 // not count when defining the width of the line.
765 if (rgb_define_mode == 3)
766 rgb_define_mode = 0;
767 else if (ch == GR_COLOR_CHAR)
768 rgb_define_mode = 1;
769 if (!rgb_define_mode) {
770 if (ch == '\t') { // tab char
•771 int space_width;
772 space_width = (GRCHAR_WIDTH(Grtext_font, ' ') + Grtext_spacing) * Grtext_tabspace;
773 line_width = (line_width + space_width) / space_width * space_width;
774 } else if (ch == GRTEXT_FORMAT_CHAR) {
775 if ((i + 1) >= strsize)
776 break;
777 line_width = ((int)str[i + 1] * GRTEXT_FORMAT_SCALAR);
778 i++;
779 } else if (ch == '\n') {
780 if (line_width > max_width) {
781 max_width = line_width;
782 }
783 line_width = 0;
•784 } else {
785 width = GRCHAR_WIDTH(Grtext_font, ch);
786 line_width += (width + Grtext_spacing + GRCHAR_SPACING(Grtext_font, ch, ch2));
787 }
788 } else
789 rgb_define_mode++;
790 }
791
792 if (line_width > max_width) {
793 max_width = line_width;
794 }
795 return (max_width) ? max_width - Grtext_spacing : 0;
796 }
797
798 // returns width of text using a font template
799 int grtext_GetTextLineWidthTemplate(const tFontTemplate *ft, const char *str) {
800 int max_width = 0, i, line_width = 0, strsize = strlen(str);
801 int rgb_define_mode = 0;
802
803 for (i = 0; i < strsize; i++) {
804 int width;
805 char ch = str[i], ch2 = str[i + 1];
806
807 // note that if we hit the GR_COLOR_CHAR then the next three values should
808 // not count when defining the width of the line.
809 if (rgb_define_mode == 3)
810 rgb_define_mode = 0;
811 else if (ch == GR_COLOR_CHAR)
812 rgb_define_mode = 1;
813 if (!rgb_define_mode) {
814 if (ch == '\t') { // tab char
•815 int space_width;
816 space_width = (GRCHAR_WIDTH_TEMP(ft, ' ') + Grtext_spacing) * Grtext_tabspace;
817 line_width = (line_width + space_width) / space_width * space_width;
818 } else if (ch == GRTEXT_FORMAT_CHAR) {
819 if ((i + 1) >= strsize)
820 break;
821 line_width = ((int)str[i + 1] * GRTEXT_FORMAT_SCALAR);
822 i++;
823 } else if (ch == '\n') {
824 if (line_width > max_width) {
825 max_width = line_width;
826 }
827 line_width = 0;
•828 } else {
829 width = GRCHAR_WIDTH_TEMP(ft, ch);
830 line_width += (width + Grtext_spacing + GRCHAR_SPACING_TEMP(ft, ch, ch2));
831 }
832 } else
833 rgb_define_mode++;
834 }
835
836 if (max_width < line_width) {
837 max_width = line_width;
838 }
839 return (max_width) ? max_width - Grtext_spacing : 0;
840 }
841
842 /* These internal routines just draw a line of text. depending on the function
843 we clip or don't clip
844 */
845 void grtext_DrawTextLineClip(int x, int y, char *str) {
846 int ch_y, ch_x, ch_w, ch_h; // what part of the character to draw
847 int i, cur_x, draw_x, draw_y; // where to draw character section
848 int h;
849 tCharBlt cbi;
850 int strsize = strlen(str);
851
852 /* by clipping, we should first determine what our vertical clipping is. then
853 go through each character in the line and determine what is totally clipped,
854 partially clipped and by how much, and not clipped at all and draw accordingly
•855 */
856 h = GRCHAR_HEIGHT(Grtext_font);
857 ch_y = 0;
858 ch_h = h;
859 draw_y = y;
860 // determine each character bitmap y and height to blt.
861 if (CLIP_TOP >= y) {
862 ch_y = CLIP_TOP - y;
863 draw_y = CLIP_TOP;
•864 }
865 if (CLIP_BOTTOM < (y + GRCHAR_HEIGHT(Grtext_font))) {
866 ch_h = CLIP_BOTTOM - y;
867 }
868 ch_h = ch_h - ch_y; // do this to clip both top and bottom
869
870 cur_x = x;
871 for (i = 0; i < strsize; i++) {
872 uint8_t ch, ch2;
873 int w;
874 ch = (uint8_t)str[i];
875 ch2 = (uint8_t)str[i + 1];
•876
877 w = GRCHAR_WIDTH(Grtext_font, ch);
878
879 if (ch == GR_COLOR_CHAR) {
880 ddgr_color col;
881 if ((i + 3) >= strsize)
882 break; // This shouldn't happen! bad string!
883 col = GR_RGB(str[i + 1], str[i + 2], str[i + 3]);
884 rend_SetFlatColor(col);
885 // rend_SetCharacterParameters(col, col, col, col);
886 i += 3;
887 } else if (ch == '\t') { // tab char
•888 int space_width;
889 space_width = (GRCHAR_WIDTH(Grtext_font, ' ') + Grtext_spacing) * Grtext_tabspace;
890 cur_x = (cur_x + space_width) / space_width * space_width;
891 } else if (((cur_x + w) < CLIP_LEFT) || (cur_x > CLIP_RIGHT)) {
892 cur_x += (Grtext_spacing + w);
893 } else if (ch == GRTEXT_FORMAT_CHAR) {
894 if ((i + 1) >= strsize)
895 break; // This shouldn't happen! bad string!
896 cur_x = x + ((int)str[i + 1] * GRTEXT_FORMAT_SCALAR);
897 i++;
•898 } else if (ch == ' ') {
899 cur_x += (GRCHAR_WIDTH(Grtext_font, ' ') + Grtext_spacing + GRCHAR_SPACING(Grtext_font, ch, ch2));
900 } else {
901 ch_x = 0;
902 ch_w = w;
903 draw_x = cur_x;
904 if (CLIP_LEFT > cur_x) {
905 ch_x = CLIP_LEFT - cur_x;
906 draw_x = CLIP_LEFT;
907 }
908 if (CLIP_RIGHT < (cur_x + w)) {
909 ch_w = CLIP_RIGHT - cur_x;
910 }
911 ch_w = ch_w - ch_x;
912
913 if (ch_x == 0 && ch_w == w && ch_y == 0 && ch_h == h) {
914 cbi.ch = ch;
915 cbi.clipped = false; // if =1, use sx,sy,sw,sh
916 cbi.x = draw_x;
917 cbi.y = draw_y;
918 cbi.dsw = cbi.dsh = Grtext_scale;
919 } else {
920 cbi.ch = ch;
921 cbi.clipped = true; // if =1, use sx,sy,sw,sh
922 cbi.x = draw_x;
923 cbi.y = draw_y;
924 cbi.dsw = cbi.dsh = Grtext_scale; // this should be useless since ch_w and ch_h are scaled values
925 cbi.sx = ch_x;
926 cbi.sy = ch_y;
927 cbi.sw = ch_w;
928 cbi.sh = ch_h;
929 }
•930 cur_x = grfont_BltChar(Grtext_font, &cbi);
931 cur_x += (Grtext_spacing + GRCHAR_SPACING(Grtext_font, ch, ch2));
932 }
933 }
934 }
935
936 void grtext_DrawTextLine(int x, int y, char *str) {
937 int i, cur_x;
938 tCharBlt cbi;
939 int strsize = strlen(str);
940
941 /* by clipping, we should first determine what our vertical clipping is. then
942 go through each character in the line and determine what is totally clipped,
943 partially clipped and by how much, and not clipped at all and draw accordingly
944
945 perform string drawing without viewport clipping.
946 supports bitmap fonts or color(alpha) mapped fonts.
947 */
948 cur_x = x;
949 for (i = 0; i < strsize; i++) {
950 uint8_t ch, ch2;
951 int w;
952 ch = (uint8_t)str[i];
•953 ch2 = (uint8_t)str[i + 1];
954 w = GRCHAR_WIDTH(Grtext_font, ch);
955
956 if (ch == GR_COLOR_CHAR) {
957 ddgr_color col;
958 if ((i + 3) >= strsize)
959 break; // This shouldn't happen! bad string!
960 col = GR_RGB(str[i + 1], str[i + 2], str[i + 3]);
961 rend_SetFlatColor(col);
962 // rend_SetCharacterParameters(col, col, col, col);
963 i += 3;
964 } else if (ch == '\t') { // tab char
•965 int space_width;
966 space_width = (GRCHAR_WIDTH(Grtext_font, ' ') + Grtext_spacing) * Grtext_tabspace;
967 cur_x = (cur_x + space_width) / space_width * space_width;
968 } else if (ch == GRTEXT_FORMAT_CHAR) {
969 if ((i + 1) >= strsize)
970 break; // This shouldn't happen! bad string!
971 cur_x = x + ((int)str[i + 1] * GRTEXT_FORMAT_SCALAR);
972 i++;
•973 } else if (ch == ' ') {
974 cur_x += (GRCHAR_WIDTH(Grtext_font, ' ') + Grtext_spacing);
975 } else {
976 cbi.ch = ch;
977 cbi.clipped = false; // if =1, use sx,sy,sw,sh
978 cbi.x = cur_x;
979 cbi.y = y;
980 cbi.dsw = cbi.dsh = Grtext_scale;
•981 cur_x = grfont_BltChar(Grtext_font, &cbi);
982 cur_x += (Grtext_spacing + GRCHAR_SPACING(Grtext_font, ch, ch2));
983 }
984 }
985 }