-
Notifications
You must be signed in to change notification settings - Fork 3
/
m_misc.pas
498 lines (438 loc) · 11.2 KB
/
m_misc.pas
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
//------------------------------------------------------------------------------
//
// DoomXS - A basic Windows source port of Doom
// based on original Linux Doom as published by "id Software"
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// Site: https://sourceforge.net/projects/doomxs/
//------------------------------------------------------------------------------
unit m_misc;
interface
function M_WriteFile(const name: string; source: pointer; len: integer): boolean;
function M_ReadFile(const name: string; var buffer: Pointer): integer;
procedure M_ScreenShot;
procedure M_LoadDefaults;
procedure M_SaveDefaults;
implementation
uses
d_delphi,
doomdef,
d_englsh,
d_main,
d_player,
g_game,
hu_stuff,
m_menu,
m_argv,
i_system,
i_video,
s_sound,
v_video,
w_wad,
z_memory;
function M_WriteFile(const name: string; source: pointer; len: integer): boolean;
var
handle: file;
count: integer;
begin
assign(handle, name);
{$I-}
rewrite(handle, 1);
{$I+}
if IOResult <> 0 then
begin
Result := False;
Exit;
end;
BlockWrite(handle, source^, len, count);
close(handle);
Result := count > 0;
end;
function M_ReadFile(const name: string; var buffer: Pointer): integer;
var
handle: file;
count: integer;
begin
assign(handle, name);
{$I-}
reset(handle, 1);
{$I+}
if IOResult <> 0 then
I_Error('M_ReadFile(): Could not read file %s', [name]);
Result := FileSize(handle);
buffer := Z_Malloc(Result, PU_STATIC, nil);
BlockRead(handle, buffer^, Result, count);
close(handle);
if count < Result then
I_Error('M_ReadFile(): Could not read file %s', [name]);
end;
type
ttype_t = (tString, tInteger);
default_t = record
name: string;
location: pointer;
defaultsvalue: string;
defaultivalue: integer;
_type: ttype_t;
end;
const
NUMDEFAULTS = 39;
defaults: array[0..NUMDEFAULTS - 1] of default_t = (
(name: 'mouse_sensitivity';
location: @mouseSensitivity;
defaultivalue: 5;
_type: tInteger),
(name: 'sfx_volume';
location: @snd_SfxVolume;
defaultivalue: 8;
_type: tInteger),
(name: 'music_volume';
location: @snd_MusicVolume;
defaultivalue: 8;
_type: tInteger),
(name: 'show_messages';
location: @showMessages;
defaultivalue: 1;
_type: tInteger),
(name: 'key_right';
location: @key_right;
defaultivalue: KEY_RIGHTARROW;
_type: tInteger),
(name: 'key_left';
location: @key_left;
defaultivalue: KEY_LEFTARROW;
_type: tInteger),
(name: 'key_up';
location: @key_up;
defaultivalue: KEY_UPARROW;
_type: tInteger),
(name: 'key_down';
location: @key_down;
defaultivalue: KEY_DOWNARROW;
_type: tInteger),
(name: 'key_strafeleft';
location: @key_strafeleft;
defaultivalue: Ord(',');
_type: tInteger),
(name: 'key_straferight';
location: @key_straferight;
defaultivalue: Ord('.');
_type: tInteger),
(name: 'key_fire';
location: @key_fire;
defaultivalue: KEY_RCTRL;
_type: tInteger),
(name: 'key_use';
location: @key_use;
defaultivalue: Ord(' ');
_type: tInteger),
(name: 'key_strafe';
location: @key_strafe;
defaultivalue: KEY_RALT;
_type: tInteger),
(name: 'key_speed';
location: @key_speed;
defaultivalue: KEY_RSHIFT;
_type: tInteger),
(name: 'autorun_mode';
location: @autorun_mode;
defaultivalue: 0;
_type: tInteger),
(name: 'use_mouse';
location: @usemouse;
defaultivalue: 1;
_type: tInteger),
(name: 'mouseb_fire';
location: @mousebfire;
defaultivalue: 0;
_type: tInteger),
(name: 'mouseb_strafe';
location: @mousebstrafe;
defaultivalue: 1;
_type: tInteger),
(name: 'mouseb_forward';
location: @mousebforward;
defaultivalue: 2;
_type: tInteger),
(name: 'use_joystick';
location: @usejoystick;
defaultivalue: 0;
_type: tInteger),
(name: 'joyb_fire';
location: @joybfire;
defaultivalue: 0;
_type: tInteger),
(name: 'joyb_strafe';
location: @joybstrafe;
defaultivalue: 1;
_type: tInteger),
(name: 'joyb_use';
location: @joybuse;
defaultivalue: 3;
_type: tInteger),
(name: 'joyb_speed';
location: @joybspeed;
defaultivalue: 2;
_type: tInteger),
(name: 'screenblocks';
location: @screenblocks;
defaultivalue: 9;
_type: tInteger),
(name: 'detaillevel';
location: @detailLevel;
defaultivalue: 0;
_type: tInteger),
(name: 'smoothdisplay';
location: @smoothdisplay;
defaultivalue: 1;
_type: tInteger),
(name: 'snd_channels';
location: @numChannels;
defaultivalue: 32;
_type: tInteger),
(name: 'usegamma';
location: @usegamma;
defaultivalue: 0;
_type: tInteger),
(name: 'chatmacro0';
location: @chat_macros[0];
defaultsvalue: HUSTR_CHATMACRO0;
_type: tString),
(name: 'chatmacro1';
location: @chat_macros[1];
defaultsvalue: HUSTR_CHATMACRO1;
_type: tString),
(name: 'chatmacro2';
location: @chat_macros[2];
defaultsvalue: HUSTR_CHATMACRO2;
_type: tString),
(name: 'chatmacro3';
location: @chat_macros[3];
defaultsvalue: HUSTR_CHATMACRO3;
_type: tString),
(name: 'chatmacro4';
location: @chat_macros[4];
defaultsvalue: HUSTR_CHATMACRO4;
_type: tString),
(name: 'chatmacro5';
location: @chat_macros[5];
defaultsvalue: HUSTR_CHATMACRO5;
_type: tString),
(name: 'chatmacro6';
location: @chat_macros[6];
defaultsvalue: HUSTR_CHATMACRO6;
_type: tString),
(name: 'chatmacro7';
location: @chat_macros[7];
defaultsvalue: HUSTR_CHATMACRO7;
_type: tString),
(name: 'chatmacro8';
location: @chat_macros[8];
defaultsvalue: HUSTR_CHATMACRO8;
_type: tString),
(name: 'chatmacro9';
location: @chat_macros[9];
defaultsvalue: HUSTR_CHATMACRO9;
_type: tString)
);
var
defaultfile: string;
procedure M_SaveDefaults;
var
i: integer;
s: TStrList;
begin
s := TStrList.Create;
try
for i := 0 to NUMDEFAULTS - 1 do
if defaults[i]._type = tInteger then
s.Add(defaults[i].name + '=' + itoa(PInteger(defaults[i].location)^))
else if defaults[i]._type = tString then
s.Add(defaults[i].name + '=' + PString(defaults[i].location)^);
s.SaveToFile(defaultfile);
finally
s.Free;
end;
end;
procedure M_LoadDefaults;
var
i: integer;
j: integer;
idx: integer;
s: TStrList;
n, v: string;
begin
// set everything to base values
for i := 0 to NUMDEFAULTS - 1 do
if defaults[i]._type = tInteger then
PInteger(defaults[i].location)^ := defaults[i].defaultivalue
else if defaults[i]._type = tString then
PString(defaults[i].location)^ := defaults[i].defaultsvalue;
// check for a custom default file
i := M_CheckParm ('-config');
if (i > 0) and (i < myargc - 1) then
begin
defaultfile := myargv[i + 1];
printf(' default file: %s' + #13#10, [defaultfile]);
end
else
defaultfile := basedefault;
s := TStrList.Create;
try
// read the file in, overriding any set defaults
if fexists(defaultfile) then
s.LoadFromFile(defaultfile);
for i := 0 to s.Count - 1 do
begin
idx := -1;
n := s.Names(i);
for j := 0 to NUMDEFAULTS - 1 do
if defaults[j].name = n then
begin
idx := j;
Break;
end;
if idx > -1 then
begin
v := s.Values(i);
if defaults[idx]._type = tInteger then
begin
if v <> '' then
PInteger(defaults[idx].location)^ := atoi(v)
end
else if defaults[idx]._type = tString then
PString(defaults[idx].location)^ := v;
end;
end;
finally
s.Free;
end;
end;
type
pcx_t = record
manufacturer: byte;
version: byte;
encoding: byte;
bits_per_pixel: byte;
xmin: word;
ymin: word;
xmax: word;
ymax: word;
hres: word;
vres: word;
palette: array[0..47] of byte;
reserved: byte;
color_planes: byte;
bytes_per_line: word;
palette_type: word;
filler: array[0..57] of byte;
end;
Ppcx_t = ^pcx_t;
// WritePCXfile
procedure WritePCXfile(const filename: string; data: PByteArray;
width, height: integer; palette: PByteArray);
var
i: integer;
pcx: Ppcx_t;
pack: PByteArray;
i_d,
i_p: integer;
begin
pcx := Z_Malloc(width * height * 2 + 1000, PU_STATIC, nil);
pcx.manufacturer := $0a; // PCX id
pcx.version := 5; // 256 color
pcx.encoding := 1; // uncompressed
pcx.bits_per_pixel := 8; // 256 color
pcx.xmin := 0;
pcx.ymin := 0;
pcx.xmax := WORD(width - 1);
pcx.ymax := WORD(height - 1);
pcx.hres := 1;
pcx.vres := 1;
ZeroMemory(@pcx.palette, SizeOf(pcx.palette));
pcx.color_planes := 1; // chunky image
pcx.bytes_per_line := WORD(width);
pcx.palette_type := 2; // not a grey scale
ZeroMemory(@pcx.filler, SizeOf(pcx.filler));
// pack the image
pack := PByteArray(pcx);
i_d := 0;
i_p := SizeOf(pcx_t);
for i := 0 to width * height - 1 do
begin
if data[i_d] and $c0 <> $c0 then
begin
pack[i_p] := data[i_d];
inc(i_p);
inc(i_d);
end
else
begin
pack[i_p] := $c1;
inc(i_p);
pack[i_p] := data[i_d];
inc(i_p);
inc(i_d);
end;
end;
// write the palette
pack[i_p] := $0c; // palette ID byte
inc(i_p);
i_d := 0;
for i := 1 to 768 do
begin
pack[i_p] := palette[i_d];
inc(i_p);
inc(i_d);
end;
// write output file
M_WriteFile(filename, pcx, i_p);
Z_Free(pcx);
end;
// M_ScreenShot
procedure M_ScreenShot;
var
i: integer;
linear: PByteArray;
lbmname: string;
begin
// munge planar buffer to linear
linear := screens[2];
I_ReadScreen(linear);
// find a file name to save it to
lbmname := 'DOOM000.pcx';
i := 0;
while i < 1000 do
begin
lbmname[5] := Chr((i div 100) + Ord('0'));
lbmname[6] := Chr(((i mod 100) div 10) + Ord('0'));
lbmname[7] := Chr((i mod 10) + Ord('0'));
if not fexists(lbmname) then
Break; // file doesn't exist
inc(i);
end;
if i = 1000 then
I_Error('M_ScreenShot(): Couldn''t create a PCX');
// save the pcx file
WritePCXfile(lbmname, linear, SCREENWIDTH, SCREENHEIGHT,
W_CacheLumpName('PLAYPAL', PU_CACHE));
players[consoleplayer].msg := 'screen shot';
end;
end.