GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — UZDoom/src/m_misc.cpp at trunk · UZDoom/UZDoom

Expand file tree

/

m_misc.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
816 lines (710 loc) · 16.8 KB
/

m_misc.cpp

Copy path

File metadata and controls

816 lines (710 loc) · 16.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
** m_misc.cpp
**
** Default Config File. Screenshots.
**
**---------------------------------------------------------------------------
**
** Copyright 1993-1996 id Software
** Copyright 1999-2016 Marisa Heit
** Copyright 2002-2016 Christoph Oelckers
** Copyright 2017-2025 GZDoom Maintainers and Contributors
** Copyright 2025-2026 UZDoom Maintainers and Contributors
**
** SPDX-License-Identifier: GPL-3.0-or-later
**
**---------------------------------------------------------------------------
**
*/
#include <cctype>
#include <cstdint>
#include <sys/stat.h>
#include <sys/types.h>
#include <sstream>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <valarray>
#include "r_defs.h"
#include "version.h"
#if defined(_WIN32)
#include <io.h>
#else
#endif
#include "m_swap.h"
#include "m_argv.h"
#include "filesystem.h"
#include "c_cvars.h"
#include "c_dispatch.h"
#include "c_bind.h"
#include "i_video.h"
#include "v_video.h"
#include "i_system.h"
// Data.
#include "m_misc.h"
#include "m_png.h"
#include "cmdlib.h"
#include "g_game.h"
#include "gi.h"
#include "gameconfigfile.h"
#include "gstrings.h"
#include "vm.h"
FGameConfigFile *GameConfig;
CVAR(Bool, screenshot_quiet, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
CVAR(String, screenshot_type, "png", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
CVAR(String, screenshot_dir, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
EXTERN_CVAR(Bool, longsavemessages);
FARG(shotdir, "Configuration", "Sets an alternate directory for saving screenshots.", "path",
"Specifies an alternate directory to use for screenshots. If this is not specified, " GAMENAME
" stores them in the directory indicated by the screenshot_dir CVAR.");
static size_t ParseCommandLine (const char *args, int *argc, char **argv);
//---------------------------------------------------------------------------
//
// PROC M_FindResponseFile
//
//---------------------------------------------------------------------------
// Change this if you want to use a different token to open comments inside a response file.
static const char *const COMMENT_TOKEN = "#";
static const uint16_t UF_STRIP_WHITESPACE = 1;
static const uint16_t UF_SKIP_EMPTY = 2;
static const uint16_t UF_ALLOW_TRAILING = 4;
static std::string Uncomment(const char* from, const uint16_t uncomment_flags = 0)
{
std::stringstream outstream;
{
std::stringstream instream(from);
std::string line;
const bool strip_whitespace = (uncomment_flags & UF_STRIP_WHITESPACE) != 0;
const bool skip_empty = (uncomment_flags & UF_SKIP_EMPTY) != 0;
const bool allow_trailing = (uncomment_flags & UF_ALLOW_TRAILING) != 0;
while (std::getline(instream, line)) {
// skip comments
auto cidx = line.find(COMMENT_TOKEN); // comment token position
if (cidx == 0) {
// starts with token
continue;
}
// comment must be first non-whitespace token
if (!allow_trailing && cidx != std::string::npos) {
size_t start = 0;
while (start < cidx && std::isspace(static_cast<unsigned char>(line[start]))) start++;
if (start != cidx) cidx = std::string::npos;
}
// case 1: token is in the middle, strip until cidx
// case 2: token not present, cidx == npos, aka max size value, so a noop
// (faster than adding an IF branch)
const std::string_view substring = std::string_view(line).substr(0, cidx);
// strip whitespace around line
size_t start = 0;
size_t end = substring.size();
// skip actually empty lines
if (skip_empty && end == 0) {
continue;
}
if (strip_whitespace) {
// find leading
while (start < end && std::isspace(static_cast<unsigned char>(substring[start]))) start++;
// trim trailing
while (end > start && std::isspace(static_cast<unsigned char>(substring[end - 1]))) end--;
}
const std::string_view trimmed = substring.substr(start, end - start);
// skip functionally empty lines
if (skip_empty && trimmed.size() == 0) {
continue;
}
// spit good value
outstream << trimmed << ' ';
}
}
return outstream.str();
}
void M_FindResponseFile (void)
{
const int limit = 100; // avoid infinite recursion
int added_stuff = 0;
int i = 1;
while (i < Args->NumArgs())
{
if (Args->GetArg(i)[0] != '@')
{
i++;
}
else
{
char **argv;
FileSys::FileData file;
std::string uncommented;
int argc = 0;
size_t argsize = 0;
int index;
// Any more response files after the limit will be removed from the
// command line.
if (added_stuff < limit)
{
// READ THE RESPONSE FILE INTO MEMORY
FileReader fr;
if (!fr.OpenFile(Args->GetArg(i) + 1))
{ // [RH] Make this a warning, not an error.
Printf ("No such response file (%s)!\n", Args->GetArg(i) + 1);
}
else
{
Printf ("Found response file %s!\n", Args->GetArg(i) + 1);
file = fr.ReadPadded(1);
uncommented = Uncomment(file.string(), UF_SKIP_EMPTY | UF_STRIP_WHITESPACE);
argsize = ParseCommandLine (uncommented.c_str(), &argc, nullptr);
}
}
else
{
Printf ("Ignored response file %s.\n", Args->GetArg(i) + 1);
}
if (argc != 0)
{
argv = (char **)M_Malloc (argc*sizeof(char *) + argsize);
argv[0] = (char *)argv + argc*sizeof(char *);
ParseCommandLine (uncommented.c_str(), nullptr, argv);
// Create a new argument vector
FArgs *newargs = new FArgs;
// Copy parameters before response file.
for (index = 0; index < i; ++index)
newargs->AppendRawArg(Args->GetArg(index));
// Copy parameters from response file.
for (index = 0; index < argc; ++index)
newargs->AppendRawArg(argv[index]);
// Copy parameters after response file.
for (index = i + 1; index < Args->NumArgs(); ++index)
newargs->AppendRawArg(Args->GetArg(index));
// Use the new argument vector as the global Args object.
delete Args;
Args = newargs;
if (++added_stuff == limit)
{
Printf("Response file limit of %d hit.\n", limit);
}
}
else
{
// Remove the response file from the Args object
Args->RemoveArg(i);
}
}
}
if (added_stuff > 0)
{
// DISPLAY ARGS
Printf ("Added %d response file%s, now have %d command-line args:\n",
added_stuff, added_stuff > 1 ? "s" : "", Args->NumArgs ());
for (int k = 1; k < Args->NumArgs (); k++)
Printf ("%s\n", Args->GetArg (k));
}
}
// ParseCommandLine
//
// This is just like the version in c_dispatch.cpp, except it does not
// do cvar expansion.
static size_t ParseCommandLine (const char *args, int *argc, char **argv)
{
int count;
char* buffstart;
char *buffplace;
count = 0;
buffstart = NULL;
if (argv != NULL)
{
buffstart = argv[0];
}
buffplace = buffstart;
for (;;)
{
while (*args <= ' ' && *args)
{ // skip white space
args++;
}
if (*args == 0)
{
break;
}
else if (*args == '\"')
{ // read quoted string
char stuff;
if (argv != NULL)
{
argv[count] = buffplace;
}
count++;
args++;
do
{
stuff = *args++;
if (stuff == '\\' && *args == '\"')
{
stuff = '\"', args++;
}
else if (stuff == '\"')
{
stuff = 0;
}
else if (stuff == 0)
{
args--;
}
if (argv != NULL)
{
*buffplace = stuff;
}
buffplace++;
} while (stuff);
}
else
{ // read unquoted string
const char *start = args++, *end;
while (*args && *args > ' ' && *args != '\"')
args++;
end = args;
if (argv != NULL)
{
argv[count] = buffplace;
while (start < end)
*buffplace++ = *start++;
*buffplace++ = 0;
}
else
{
buffplace += end - start + 1;
}
count++;
}
}
if (argc != NULL)
{
*argc = count;
}
return (buffplace - buffstart);
}
//
// M_SaveDefaults
//
bool M_SaveDefaults (const char *filename)
{
FString oldpath;
bool success;
if (GameConfig == nullptr) return true;
if (filename != nullptr)
{
oldpath = GameConfig->GetPathName();
GameConfig->ChangePathName (filename);
}
GameConfig->ArchiveGlobalData ();
if (gameinfo.ConfigName.IsNotEmpty())
{
GameConfig->ArchiveGameData (gameinfo.ConfigName.GetChars());
}
success = GameConfig->WriteConfigFile ();
if (filename != nullptr)
{
GameConfig->ChangePathName (filename);
}
return success;
}
void M_SaveDefaultsFinal ()
{
if (GameConfig == nullptr) return;
while (!M_SaveDefaults (nullptr) && I_WriteIniFailed (GameConfig->GetPathName()))
{
/* Loop until the config saves or I_WriteIniFailed() returns false */
}
delete GameConfig;
GameConfig = nullptr;
}
UNSAFE_CCMD (writeini)
{
FString fileName = {};
if (argv.argc() > 1)
{
FString file = argv[1];
C_SanitizeFileName(file);
file += ".ini";
FString path = M_GetConfigPath(true);
auto i = path.LastIndexOf("/") + 1;
path.Remove(i, path.Len() - i);
path.AppendFormat("%s", file.GetChars());
fileName = path;
}
if (!M_SaveDefaults (fileName.IsEmpty() ? nullptr : fileName.GetChars()))
{
Printf ("Writing config failed: %s\n", strerror(errno));
}
else
{
Printf ("Config saved.\n");
}
}
void M_OpenConfigDir()
{
M_SaveDefaults(nullptr);
I_OpenShellFolder(ExtractFilePath(GameConfig->GetPathName()).GetChars());
}
CCMD(openconfig)
{
M_OpenConfigDir();
}
//
// M_LoadDefaults
//
void M_LoadDefaults ()
{
GameConfig = new FGameConfigFile;
GameConfig->DoGlobalSetup ();
}
//
// SCREEN SHOTS
//
struct pcx_t
{
int8_t manufacturer;
int8_t version;
int8_t encoding;
int8_t bits_per_pixel;
uint16_t xmin;
uint16_t ymin;
uint16_t xmax;
uint16_t ymax;
uint16_t hdpi;
uint16_t vdpi;
uint8_t palette[48];
int8_t reserved;
int8_t color_planes;
uint16_t bytes_per_line;
uint16_t palette_type;
int8_t filler[58];
};
inline void putc(unsigned char chr, FileWriter *file)
{
file->Write(&chr, 1);
}
//
// WritePCXfile
//
void WritePCXfile (FileWriter *file, const uint8_t *buffer, const PalEntry *palette,
ESSType color_type, int width, int height, int pitch)
{
TArray<uint8_t> temprow_storage(width * 3, true);
uint8_t *temprow = &temprow_storage[0];
const uint8_t *data;
int x, y;
int runlen;
int bytes_per_row_minus_one;
uint8_t color;
pcx_t pcx;
pcx.manufacturer = 10; // PCX id
pcx.version = 5; // 256 (or more) colors
pcx.encoding = 1;
pcx.bits_per_pixel = 8; // 256 (or more) colors
pcx.xmin = 0;
pcx.ymin = 0;
pcx.xmax = LittleShort((unsigned short)(width-1));
pcx.ymax = LittleShort((unsigned short)(height-1));
pcx.hdpi = LittleShort((unsigned short)75);
pcx.vdpi = LittleShort((unsigned short)75);
memset (pcx.palette, 0, sizeof(pcx.palette));
pcx.reserved = 0;
pcx.color_planes = (color_type == SS_PAL) ? 1 : 3; // chunky image
pcx.bytes_per_line = width + (width & 1);
pcx.palette_type = 1; // not a grey scale
memset (pcx.filler, 0, sizeof(pcx.filler));
file->Write(&pcx, 128);
bytes_per_row_minus_one = ((color_type == SS_PAL) ? width : width * 3) - 1;
// pack the image
for (y = height; y > 0; y--)
{
switch (color_type)
{
case SS_PAL:
data = buffer;
break;
case SS_RGB:
// Unpack RGB into separate planes.
for (int i = 0; i < width; ++i)
{
temprow[i ] = buffer[i*3];
temprow[i + width ] = buffer[i*3 + 1];
temprow[i + width * 2] = buffer[i*3 + 2];
}
data = temprow;
break;
case SS_BGRA:
// Unpack RGB into separate planes, discarding A.
for (int i = 0; i < width; ++i)
{
temprow[i ] = buffer[i*4 + 2];
temprow[i + width ] = buffer[i*4 + 1];
temprow[i + width * 2] = buffer[i*4];
}
data = temprow;
break;
default:
// Should never happen.
return;
}
buffer += pitch;
color = *data++;
runlen = 1;
for (x = bytes_per_row_minus_one; x > 0; x--)
{
if (*data == color)
{
runlen++;
}
else
{
if (runlen > 1 || color >= 0xc0)
{
while (runlen > 63)
{
putc (0xff, file);
putc (color, file);
runlen -= 63;
}
if (runlen > 0)
{
putc (0xc0 + runlen, file);
}
}
if (runlen > 0)
{
putc (color, file);
}
runlen = 1;
color = *data;
}
data++;
}
if (runlen > 1 || color >= 0xc0)
{
while (runlen > 63)
{
putc (0xff, file);
putc (color, file);
runlen -= 63;
}
if (runlen > 0)
{
putc (0xc0 + runlen, file);
}
}
if (runlen > 0)
{
putc (color, file);
}
if (width & 1)
putc (0, file);
}
// write the palette
if (color_type == SS_PAL)
{
putc (12, file); // palette ID byte
for (x = 0; x < 256; x++, palette++)
{
putc (palette->r, file);
putc (palette->g, file);
putc (palette->b, file);
}
}
}
//
// WritePNGfile
//
void WritePNGfile (FileWriter *file, const uint8_t *buffer, const PalEntry *palette,
ESSType color_type, int width, int height, int pitch, float gamma)
{
char software[100];
mysnprintf(software, countof(software), GAMENAME " %s", GetVersionString());
if (!M_CreatePNG (file, buffer, palette, color_type, width, height, pitch, gamma) ||
!M_AppendPNGText (file, "Software", software) ||
!M_FinishPNG (file))
{
Printf ("%s\n", GStrings.GetString("TXT_SCREENSHOTERR"));
}
}
//
// M_ScreenShot
//
static bool FindFreeName (FString &fullname, const char *extension)
{
FString lbmname;
int i;
for (i = 0; i <= 9999; i++)
{
const char *gamename = gameinfo.ConfigName.GetChars();
time_t now;
tm *tm;
time(&now);
tm = localtime(&now);
if (tm == NULL)
{
lbmname.Format ("%sScreenshot_%s_%04d.%s", fullname.GetChars(), gamename, i, extension);
}
else if (i == 0)
{
lbmname.Format ("%sScreenshot_%s_%04d%02d%02d_%02d%02d%02d.%s", fullname.GetChars(), gamename,
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
extension);
}
else
{
lbmname.Format ("%sScreenshot_%s_%04d%02d%02d_%02d%02d%02d_%02d.%s", fullname.GetChars(), gamename,
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
i, extension);
}
if (!FileExists (lbmname.GetChars()))
{
fullname = lbmname;
return true; // file doesn't exist
}
}
return false;
}
void M_ScreenShot (const char *filename)
{
FileWriter *file;
FString autoname;
bool writepcx = (stricmp (screenshot_type, "pcx") == 0); // PNG is the default
// find a file name to save it to
if (filename == NULL || filename[0] == '\0')
{
size_t dirlen;
autoname = Args->CheckValue(FArg_shotdir);
if (autoname.IsEmpty())
{
autoname = screenshot_dir;
}
dirlen = autoname.Len();
if (dirlen == 0)
{
autoname = M_GetScreenshotsPath();
dirlen = autoname.Len();
}
if (dirlen > 0)
{
if (autoname[dirlen-1] != '/' && autoname[dirlen-1] != '\\')
{
autoname += '/';
}
}
autoname = NicePath(autoname.GetChars());
CreatePath(autoname.GetChars());
if (!FindFreeName (autoname, writepcx ? "pcx" : "png"))
{
Printf ("M_ScreenShot: Delete some screenshots\n");
return;
}
}
else
{
autoname = filename;
DefaultExtension (autoname, writepcx ? ".pcx" : ".png");
}
// save the screenshot
int pitch;
ESSType color_type;
float gamma;
auto buffer = screen->GetScreenshotBuffer(pitch, color_type, gamma);
if (buffer.Size() > 0)
{
file = FileWriter::Open(autoname.GetChars());
if (file == NULL)
{
Printf ("Could not open %s\n", autoname.GetChars());
return;
}
if (writepcx)
{
WritePCXfile(file, buffer.Data(), nullptr, color_type,
screen->GetWidth(), screen->GetHeight(), pitch);
}
else
{
WritePNGfile(file, buffer.Data(), nullptr, color_type,
screen->GetWidth(), screen->GetHeight(), pitch, gamma);
}
delete file;
if (!screenshot_quiet)
{
ptrdiff_t slash = -1;
if (!longsavemessages) slash = autoname.LastIndexOfAny(":/\\");
Printf ("Captured %s\n", autoname.GetChars()+slash+1);
}
}
else
{
if (!screenshot_quiet)
{
Printf ("Could not create screenshot.\n");
}
}
}
UNSAFE_CCMD (screenshot)
{
if (argv.argc() == 1)
G_ScreenShot (NULL);
else
G_ScreenShot (argv[1]);
}
CCMD(openscreenshots)
{
size_t dirlen;
FString autoname;
autoname = Args->CheckValue(FArg_shotdir);
if (autoname.IsEmpty())
{
autoname = screenshot_dir;
}
dirlen = autoname.Len();
if (dirlen == 0)
{
autoname = M_GetScreenshotsPath();
dirlen = autoname.Len();
}
if (dirlen > 0)
{
if (autoname[dirlen-1] != '/' && autoname[dirlen-1] != '\\')
{
autoname += '/';
}
}
autoname = NicePath(autoname.GetChars());
CreatePath(autoname.GetChars());
I_OpenShellFolder(autoname.GetChars());
}
static int SaveConfig()
{
return M_SaveDefaults(nullptr);
}
DEFINE_ACTION_FUNCTION_NATIVE(_CVar, SaveConfig, SaveConfig)
{
PARAM_PROLOGUE;
ACTION_RETURN_INT(M_SaveDefaults(nullptr));
}
void M_OpenWadDir()
{
size_t dirlen;
FString autoname = M_GetAppDataPath(true);
dirlen = autoname.Len();
if (dirlen > 0)
{
if (autoname[dirlen-1] != '/' && autoname[dirlen-1] != '\\')
{
autoname += '/';
}
}
autoname = NicePath(autoname.GetChars());
CreatePath(autoname.GetChars());
I_OpenShellFolder(autoname.GetChars());
}
CCMD(openwads)
{
M_OpenWadDir();
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.