Debian Codegraph @

prboom2/src/dsda/endoom.c

raw
1 //
2 // Copyright(C) 2022 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 Endoom
16 //
17
18 #ifdef _WIN32
19 #define WIN32_LEAN_AND_MEAN
20 #include "windows.h"
21 #endif
22
23 #include "doomdef.h"
24 #include "doomtype.h"
25 #include "lprintf.h"
26 #include "w_wad.h"
27
28 #include "dsda/configuration.h"
29
30 #include "endoom.h"
31
32 static const char* cp437_to_utf8[256] = {
33 " ",
34 "\xe2\x98\xba",
35 "\xe2\x98\xbb",
36 "\xe2\x99\xa5",
37 "\xe2\x99\xa6",
38 "\xe2\x99\xa3",
39 "\xe2\x99\xa0",
40 "\xe2\x80\xa2",
41 "\xe2\x97\x98",
42 "\xe2\x97\x8b",
43 "\xe2\x97\x99",
44 "\xe2\x99\x82",
45 "\xe2\x99\x80",
46 "\xe2\x99\xaa",
47 "\xe2\x99\xab",
48 "\xe2\x98\xbc",
49
50 "\xe2\x96\xba",
51 "\xe2\x97\x84",
52 "\xe2\x86\x95",
53 "\xe2\x80\xbc",
54 "\xc2\xb6",
55 "\xc2\xa7",
56 "\xe2\x96\xac",
57 "\xe2\x86\xa8",
58 "\xe2\x86\x91",
59 "\xe2\x86\x93",
60 "\xe2\x86\x92",
61 "\xe2\x86\x90",
62 "\xe2\x88\x9f",
63 "\xe2\x86\x94",
64 "\xe2\x96\xb2",
65 "\xe2\x96\xbc",
66
67 " ",
68 "!",
69 "\"",
70 "#",
71 "$",
72 "%",
73 "&",
74 "'",
75 "(",
76 ")",
77 "*",
78 "+",
79 ",",
80 "-",
81 ".",
82 "/",
83
84 "0",
85 "1",
86 "2",
87 "3",
88 "4",
89 "5",
90 "6",
91 "7",
92 "8",
93 "9",
94 ":",
95 ";",
96 "<",
97 "=",
98 ">",
99 "?",
100
101 "@",
102 "A",
103 "B",
104 "C",
105 "D",
106 "E",
107 "F",
108 "G",
109 "H",
110 "I",
111 "J",
112 "K",
113 "L",
114 "M",
115 "N",
116 "O",
117
118 "P",
119 "Q",
120 "R",
121 "S",
122 "T",
123 "U",
124 "V",
125 "W",
126 "X",
127 "Y",
128 "Z",
129 "[",
130 "\\",
131 "]",
132 "^",
133 "_",
134
135 "`",
136 "a",
137 "b",
138 "c",
139 "d",
140 "e",
141 "f",
142 "g",
143 "h",
144 "i",
145 "j",
146 "k",
147 "l",
148 "m",
149 "n",
150 "o",
151
152 "p",
153 "q",
154 "r",
155 "s",
156 "t",
157 "u",
158 "v",
159 "w",
160 "x",
161 "y",
162 "z",
163 "{",
164 "|",
165 "}",
166 "~",
167 "\xe2\x8c\x82",
168
169 "\xc3\x87",
170 "\xc3\xbc",
171 "\xc3\xa9",
172 "\xc3\xa2",
173 "\xc3\xa4",
174 "\xc3\xa0",
175 "\xc3\xa5",
176 "\xc3\xa7",
177 "\xc3\xaa",
178 "\xc3\xab",
179 "\xc3\xa8",
180 "\xc3\xaf",
181 "\xc3\xae",
182 "\xc3\xac",
183 "\xc3\x84",
184 "\xc3\x85",
185
186 "\xc3\x89",
187 "\xc3\xa6",
188 "\xc3\x86",
189 "\xc3\xb4",
190 "\xc3\xb6",
191 "\xc3\xb2",
192 "\xc3\xbb",
193 "\xc3\xb9",
194 "\xc3\xbf",
195 "\xc3\x96",
196 "\xc3\x9c ",
197 "\xc2\xa2",
198 "\xc2\xa3",
199 "\xc2\xa5",
200 "\xe2\x82\xa7",
201 "\xc6\x92",
202
203 "\xc3\xa1",
204 "\xc3\xad",
205 "\xc3\xb3",
206 "\xc3\xba",
207 "\xc3\xb1",
208 "\xc3\x91",
209 "\xc2\xaa",
210 "\xc2\xba",
211 "\xc2\xbf",
212 "\xe2\x8c\x90",
213 "\xc2\xac",
214 "\xc2\xbd",
215 "\xc2\xbc",
216 "\xc2\xa1",
217 "\xc2\xab",
218 "\xc2\xbb",
219
220 "\xe2\x96\x91",
221 "\xe2\x96\x92",
222 "\xe2\x96\x93",
223 "\xe2\x94\x82",
224 "\xe2\x94\xa4",
225 "\xe2\x95\xa1",
226 "\xe2\x95\xa2",
227 "\xe2\x95\x96",
228 "\xe2\x95\x95",
229 "\xe2\x95\xa3",
230 "\xe2\x95\x91",
231 "\xe2\x95\x97",
232 "\xe2\x95\x9d",
233 "\xe2\x95\x9c",
234 "\xe2\x95\x9b",
235 "\xe2\x94\x90",
236
237 "\xe2\x94\x94",
238 "\xe2\x94\xb4",
239 "\xe2\x94\xac",
240 "\xe2\x94\x9c",
241 "\xe2\x94\x80",
242 "\xe2\x94\xbc",
243 "\xe2\x95\x9e",
244 "\xe2\x95\x9f",
245 "\xe2\x95\x9a",
246 "\xe2\x95\x94",
247 "\xe2\x95\xa9",
248 "\xe2\x95\xa6",
249 "\xe2\x95\xa0",
250 "\xe2\x95\x90",
251 "\xe2\x95\xac",
252 "\xe2\x95\xa7",
253
254 "\xe2\x95\xa8",
255 "\xe2\x95\xa4",
256 "\xe2\x95\xa5",
257 "\xe2\x95\x99",
258 "\xe2\x95\x98",
259 "\xe2\x95\x92",
260 "\xe2\x95\x93",
261 "\xe2\x95\xab",
262 "\xe2\x95\xaa",
263 "\xe2\x94\x98",
264 "\xe2\x94\x8c",
265 "\xe2\x96\x88",
266 "\xe2\x96\x84",
267 "\xe2\x96\x8c",
268 "\xe2\x96\x90",
269 "\xe2\x96\x80",
270
271 "\xce\xb1",
272 "\xc3\x9f",
273 "\xce\x93",
274 "\xcf\x80",
275 "\xce\xa3",
276 "\xcf\x83",
277 "\xc2\xb5",
278 "\xcf\x84",
279 "\xce\xa6",
280 "\xce\x98",
281 "\xce\xa9",
282 "\xce\xb4",
283 "\xe2\x88\x9e",
284 "\xcf\x86",
285 "\xce\xb5",
286 "\xe2\x88\xa9",
287
288 "\xe2\x89\xa1",
289 "\xc2\xb1",
290 "\xe2\x89\xa5",
291 "\xe2\x89\xa4",
292 "\xe2\x8c\xa0",
293 "\xe2\x8c\xa1",
294 "\xc3\xb7",
295 "\xe2\x89\x88",
296 "\xc2\xb0",
297 "\xe2\x88\x99",
298 "\xc2\xb7",
299 "\xe2\x88\x9a",
300 "\xe2\x81\xbf",
301 "\xc2\xb2",
302 "\xe2\x96\xa0",
303 "\xc2\xa0",
304 };
305
306 typedef enum {
307 format_null,
308 format_cp437,
309 format_utf8,
310 } output_format_t;
311
312 static byte* endoom;
313 static output_format_t output_format;
314
315 #ifdef _WIN32
316 static HANDLE hConsole;
317 static DWORD OldMode;
318 static dboolean restore_mode = false;
319
320 static void EnableVTMode(void) {
321 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
322 if (hConsole == INVALID_HANDLE_VALUE)
323 return;
324
325 if (!GetConsoleMode(hConsole, &OldMode))
326 return;
327
328 if (!SetConsoleMode(hConsole, ENABLE_PROCESSED_OUTPUT |
329 ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
330 return;
331 }
332
333 restore_mode = true;
334 }
335
336 static void RestoreOldMode(void) {
337 if (!restore_mode)
338 return;
339
340 SetConsoleMode(hConsole, OldMode);
341 }
342 #endif
343
344 void dsda_CacheEndoom(void) {
345 int lump;
346
347 output_format = dsda_IntConfig(dsda_config_ansi_endoom);
348
349 if (!output_format)
350 return;
351
352 if (hexen)
353 return;
354
355 if (heretic)
356 lump = W_CheckNumForName("ENDTEXT");
357 else {
358 lump = W_CheckNumForName("ENDBOOM");
359 if (lump == LUMP_NOT_FOUND)
360 lump = W_CheckNumForName("ENDOOM");
361 }
362
363 if (lump == LUMP_NOT_FOUND || W_LumpLength(lump) != 4000)
364 return;
365
366 endoom = Z_Malloc(4000);
367 memcpy(endoom, W_LumpByNum(lump), 4000);
368 }
369
370 void dsda_DumpEndoom(void) {
371 if (endoom) {
372 int i;
373 const char* color_lookup[] = {
374 "0", "4", "2", "6", "1", "5", "3", "7",
375 "0;1", "4;1", "2;1", "6;1", "1;1", "5;1", "3;1", "7;1",
376 };
377
378 #ifdef _WIN32
379 EnableVTMode();
380 #endif
381
382 for (i = 0; i < 2000; ++i) {
383 byte character;
384 byte data;
385 const char *foreground;
386 const char *background;
387 const char *blink;
388
389 character = endoom[i * 2];
390 data = endoom[i * 2 + 1];
391 foreground = color_lookup[data & 0x0f];
392 background = color_lookup[(data >> 4) & 0x07];
393 blink = ((data >> 7) & 0x01) ? "5" : "25";
394
395 if (!character)
396 character = ' ';
397
398 if (output_format == format_utf8)
399 lprintf(LO_INFO, "\033[3%sm\033[4%sm\033[%sm%s\033[0m",
400 foreground, background, blink, cp437_to_utf8[character]);
401 else
402 lprintf(LO_INFO, "\033[3%sm\033[4%sm\033[%sm%c\033[0m",
403 foreground, background, blink, character);
404
405 if ((i + 1) % 80 == 0)
406 lprintf(LO_INFO, "\n");
407 }
408
409 lprintf(LO_INFO, "\n");
410
411 Z_Free(endoom);
412 endoom = NULL;
413
414 #ifdef _WIN32
415 RestoreOldMode();
416 #endif
417 }
418 }