Debian Codegraph @

prboom2/src/dsda/hud_components/free_text.c

raw
1 //
2 // Copyright(C) 2023 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Free Text HUD Component
16 //
17
18 #include "base.h"
19
20 #include "free_text.h"
21
22 typedef struct {
23 dsda_text_t component;
24 } local_component_t;
25
26 static local_component_t* local;
27
28 static char* free_text;
29
30 void dsda_UpdateFreeText(void) {
31 int i, j;
32 const char* text;
33
34 text = dsda_StringConfig(dsda_config_free_text);
35
36 if (free_text)
37 Z_Free(free_text);
38
39 free_text = Z_Malloc(strlen(text) + 1);
40
41 for (i = 0, j = 0; text[i]; ++i, ++j) {
42 if (text[i] == '\\') {
43 if (text[i + 1] == 'n') {
44 free_text[j] = '\n';
45 ++i;
46 continue;
47 }
48
49 if (text[i + 1] == 'c') {
50 int color;
51 int c10, c1;
52
53 c10 = text[i + 2];
54 if (c10 >= '0' && c10 <= '9') {
55 c10 -= '0';
56
57 c1 = text[i + 3];
58 if (c1 >= '0' && c1 <= '9') {
59 c1 -= '0';
60 c10 *= 10;
61 i += 3;
62 }
63 else {
64 c1 = 0;
65 i += 2;
66 }
67
68 color = c10 + c1;
69 }
70 else {
71 color = 0;
72 ++i;
73 }
74
75 if (color < 0 || color >= CR_HUD_LIMIT)
76 color = 0;
77
78 free_text[j] = '\x1b';
79 free_text[j + 1] = HUlib_Color(color);
80 ++j;
81
82 continue;
83 }
84 }
85
86 free_text[j] = text[i];
87 }
88
89 free_text[j] = '\0';
90 }
91
92 static void dsda_UpdateComponentText(char* str, size_t max_size) {
93 if (free_text)
94 snprintf(
95 str,
96 max_size,
97 "%s%s",
98 dsda_TextColor(dsda_tc_exhud_free_text),
99 free_text
100 );
101 else
102 str[0] = '\0';
103 }
104
105 void dsda_InitFreeTextHC(int x_offset, int y_offset, int vpt, int* args, int arg_count, void** data) {
106 *data = Z_Calloc(1, sizeof(local_component_t));
107 local = *data;
108
109 dsda_InitTextHC(&local->component, x_offset, y_offset, vpt);
110
111 dsda_UpdateFreeText();
112 }
113
114 void dsda_UpdateFreeTextHC(void* data) {
115 local = data;
116
117 dsda_UpdateComponentText(local->component.msg, sizeof(local->component.msg));
118 dsda_RefreshHudText(&local->component);
119 }
120
121 void dsda_DrawFreeTextHC(void* data) {
122 local = data;
123
124 dsda_DrawBasicText(&local->component);
125 }