-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextures.c
454 lines (393 loc) · 13.6 KB
/
textures.c
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
/*
Texture display by Rapha‰l Quinet <[email protected]>,
Trevor Phillips <[email protected]>,
and Christian Johannes Schladetsch <[email protected]>
You are allowed to use any parts of this code in another program, as
long as you give credits to the authors in the documentation and in
the program itself. Read the file README.1ST for more information.
This program comes with absolutely no warranty.
TEXTURES.C - Textures in 256 colors.
Note from CJS:
DisplayPic() could be further speeded up by avoiding reading
the same column numerous times. However, this approach involves
exhorbitant memory usage for certain pictures.
*/
/* the includes */
#include "deu.h"
/*
display a floor or ceiling texture at coords x0, y0 and not beyond x1, y1
*/
void DisplayFloorTexture( int x0, int y0, int x1, int y1, char *texname)
{
MDirPtr dir; /* main directory pointer to the texture entry */
unsigned char huge *pixels; /* array of pixels that hold the image */
dir = FindMasterDir( MasterDir, texname);
if (dir == NULL)
{
SetColor( DARKGRAY);
DrawScreenLine( x0, y0, x1, y1);
DrawScreenLine( x0, y1, x1, y0);
return;
}
BasicWadSeek( dir->wadfile, dir->dir.start);
pixels = GetFarMemory( 4100 * sizeof( char));
BasicWadRead( dir->wadfile, &(pixels[ 4]), 4096L);
if (GfxMode < -1)
{
/* Probably a bug in the VESA driver... */
/* It requires "size-1" instead of "size"! */
((unsigned int huge *)pixels)[ 0] = 63;
((unsigned int huge *)pixels)[ 1] = 63;
}
else
{
((unsigned int huge *)pixels)[ 0] = 64;
((unsigned int huge *)pixels)[ 1] = 64;
}
putimage( x0, y0, pixels, COPY_PUT);
FreeFarMemory( pixels);
}
/*
display a picture "picname" at coords x0, y0 and not beyond x1, y1
*/
void DisplayPic( int x0, int y0, int x1, int y1, char *picname)
{
MDirPtr dir;
int xsize, ysize, xofs, yofs;
int x, y;
unsigned char huge *lpColumnData;
unsigned char huge *lpColumn;
long huge *lpNeededOffsets;
int nColumns, nCurrentColumn;
long lCurrentOffset;
int fColumnInMemory;
int i, n;
unsigned char bRowStart, bColored;
if (bioskey( 1) != 0)
return; /* speedup */
dir = FindMasterDir( MasterDir, picname);
if (dir == NULL)
{
SetColor( DARKGRAY);
DrawScreenLine( x0, y0, x1, y1);
DrawScreenLine( x0, y1, x1, y0);
return;
}
BasicWadSeek( dir->wadfile, dir->dir.start);
BasicWadRead( dir->wadfile, &xsize, 2L);
BasicWadRead( dir->wadfile, &ysize, 2L);
BasicWadRead( dir->wadfile, &xofs, 2L);
BasicWadRead( dir->wadfile, &yofs, 2L);
/* ignore the picture offsets */
xofs = 0;
yofs = 0;
#define TEX_COLUMNBUFFERSIZE (60L * 1024L)
#define TEX_COLUMNSIZE 512L
nColumns = xsize;
/* Note from CJS:
I tried to use far memory originally, but kept getting out-of-mem errors
that is really strange - I assume that the wad dir et al uses all
the far mem, and there is only near memory available. NEVER seen
this situation before..... I'll keep them huge pointers anyway,
in case something changes later
*/
lpColumnData = GetMemory( TEX_COLUMNBUFFERSIZE);
lpNeededOffsets = GetMemory( nColumns * 4L);
BasicWadRead( dir->wadfile, lpNeededOffsets, nColumns * 4L);
/* read first column data, and subsequent column data */
BasicWadSeek( dir->wadfile, dir->dir.start + lpNeededOffsets[ 0]);
BasicWadRead( dir->wadfile, lpColumnData, TEX_COLUMNBUFFERSIZE);
for (nCurrentColumn = 0; nCurrentColumn < nColumns; nCurrentColumn++)
{
lCurrentOffset = lpNeededOffsets[ nCurrentColumn];
fColumnInMemory = lCurrentOffset >= lpNeededOffsets[ 0] && lCurrentOffset < (long)(lpNeededOffsets[ 0] + TEX_COLUMNBUFFERSIZE - TEX_COLUMNSIZE);
if (fColumnInMemory)
{
lpColumn = &lpColumnData[ lCurrentOffset - lpNeededOffsets[ 0]];
}
else
{
lpColumn = GetFarMemory( TEX_COLUMNSIZE);
BasicWadSeek( dir->wadfile, dir->dir.start + lCurrentOffset);
BasicWadRead( dir->wadfile, lpColumn, TEX_COLUMNSIZE);
}
/* we now have the needed column data, one way or another, so write it */
n = 1;
bRowStart = lpColumn[ 0];
while (bRowStart != 255 && n < TEX_COLUMNSIZE)
{
bColored = lpColumn[ n];
n += 2; /* skip over 'null' pixel in data */
for (i = 0; i < bColored; i++)
{
x = x0 + xofs + nCurrentColumn;
y = y0 + yofs + bRowStart + i;
if (x >= x0 && y >= y0 && x <= x1 && y <= y1)
putpixel( x, y, lpColumn[ i + n]);
}
n += bColored + 1; /* skip over written pixels, and the 'null' one */
bRowStart = lpColumn[ n++];
}
if (bRowStart != 255)
ProgError( "BUG: bRowStart != 255.");
if (!fColumnInMemory)
FreeFarMemory( lpColumn);
}
FreeMemory( lpColumnData);
FreeMemory( lpNeededOffsets);
}
/*
display a wall texture ("texture1" or "texture2" object) at coords x0, y0
*/
void DisplayWallTexture( int x0, int y0, int x1, int y1, char *texname)
{
MDirPtr dir; /* main directory pointer to the TEXTURE* entries */
MDirPtr pdir; /* main directory pointer to the PNAMES entry */
long *offsets; /* array of offsets to texture names */
int n; /* general counter */
int xsize, ysize; /* size of the texture */
int xofs, yofs; /* offset in texture space for the wall patch */
int fields; /* number of wall patches used to build this texture */
int pnameind; /* patch name index in PNAMES table */
int junk; /* holds useless data */
long numtex; /* number of texture names in TEXTURE* list */
long texofs; /* offset in the wad file to the texture data */
char tname[9]; /* texture name */
char picname[9]; /* wall patch name */
if (bioskey( 1) != 0)
return; /* speedup */
/* offset for texture we want. */
texofs = 0;
/* search for texname in texture1 names */
dir = FindMasterDir( MasterDir, "TEXTURE1");
BasicWadSeek( dir->wadfile, dir->dir.start);
BasicWadRead( dir->wadfile, &numtex, 4);
/* read in the offsets for texture1 names and info. */
offsets = GetMemory( numtex * sizeof( long));
for (n = 0; n < numtex; n++)
BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
for (n = 0; n < numtex && !texofs; n++)
{
BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
BasicWadRead( dir->wadfile, &tname, 8);
if (!strnicmp(tname, texname, 8))
texofs = dir->dir.start + offsets[ n];
}
FreeMemory( offsets);
if (Registered && texofs == 0)
{
/* search for texname in texture2 names */
dir = FindMasterDir( MasterDir, "TEXTURE2");
BasicWadSeek( dir->wadfile, dir->dir.start);
BasicWadRead( dir->wadfile, &numtex, 4);
/* read in the offsets for texture2 names */
offsets = GetMemory( numtex * sizeof( long));
for (n = 0; n < numtex; n++)
BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
for (n = 0; n < numtex && !texofs; n++)
{
BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
BasicWadRead( dir->wadfile, &tname, 8);
if (!strnicmp( tname, texname, 8))
texofs = dir->dir.start + offsets[ n];
}
FreeMemory( offsets);
}
/* clear the box where the texture size will be drawn - see below */
SetColor( LIGHTGRAY);
DrawScreenBox( x0 - 171, y0 + 40, x0 - 110, y0 + 50);
/* texture name not found */
if (texofs == 0)
return;
/* read the info for this texture */
BasicWadSeek( dir->wadfile, texofs + 12L);
BasicWadRead( dir->wadfile, &xsize, 2L);
BasicWadRead( dir->wadfile, &ysize, 2L);
BasicWadSeek( dir->wadfile, texofs + 20L);
BasicWadRead( dir->wadfile, &fields, 2L);
/* display the texture size - yes, you can laugh at the way I did it... */
SetColor( BLACK);
DrawScreenText( x0 - 171, y0 + 40, "%dx%d", xsize, ysize);
if (bioskey( 1) != 0)
return; /* speedup */
if (x1 - x0 > xsize)
x1 = x0 + xsize;
if (y1 - y0 > ysize)
y1 = y0 + ysize;
/* not really necessary, except when xofs or yofs < 0 */
setviewport( x0, y0, x1, y1, TRUE);
/* display the texture */
for (n = 0; n < fields; n++)
{
BasicWadSeek( dir->wadfile, texofs + 22L + n * 10L);
BasicWadRead( dir->wadfile, &xofs, 2L);
BasicWadRead( dir->wadfile, &yofs, 2L);
BasicWadRead( dir->wadfile, &pnameind, 2L);
BasicWadRead( dir->wadfile, &junk, 2L); /* Junk should = 1. */
BasicWadRead( dir->wadfile, &junk, 2L); /* Junk should = 0. */
/* OK, now look up the pic's name in the PNAMES entry. */
pdir = FindMasterDir( MasterDir, "PNAMES");
BasicWadSeek( pdir->wadfile, pdir->dir.start + 4L + pnameind * 8L);
BasicWadRead( pdir->wadfile, &picname, 8L);
picname[ 8] = '\0';
/* coords changed because of the "setviewport" */
DisplayPic( xofs, yofs, x1 - x0, y1 - y0, strupr( picname));
}
/* restore the normal viewport */
setviewport( 0, 0, ScrMaxX, ScrMaxY, TRUE);
}
/*
Function to get the size of a wall texture
*/
void GetWallTextureSize( int *xsize_r, int *ysize_r, char *texname)
{
MDirPtr dir; /* pointer in main directory to texname */
long *offsets; /* array of offsets to texture names */
int n; /* general counter */
long numtex; /* number of texture names in TEXTURE* list */
long texofs; /* offset in doom.wad for the texture data */
char tname[9]; /* texture name */
/* offset for texture we want. */
texofs = 0;
/* search for texname in texture1 names */
dir = FindMasterDir( MasterDir, "TEXTURE1");
BasicWadSeek( dir->wadfile, dir->dir.start);
BasicWadRead( dir->wadfile, &numtex, 4);
/* read in the offsets for texture1 names and info. */
offsets = GetMemory( numtex * sizeof( long));
for (n = 0; n < numtex; n++)
BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
for (n = 0; n < numtex && !texofs; n++)
{
BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
BasicWadRead( dir->wadfile, &tname, 8);
if (!strnicmp(tname, texname, 8))
texofs = dir->dir.start + offsets[ n];
}
FreeMemory( offsets);
if (Registered && texofs == 0)
{
/* search for texname in texture2 names */
dir = FindMasterDir( MasterDir, "TEXTURE2");
BasicWadSeek( dir->wadfile, dir->dir.start);
BasicWadRead( dir->wadfile, &numtex, 4);
/* read in the offsets for texture2 names */
offsets = GetMemory( numtex * sizeof( long));
for (n = 0; n < numtex; n++)
BasicWadRead( dir->wadfile, &(offsets[ n]), 4L);
for (n = 0; n < numtex && !texofs; n++)
{
BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
BasicWadRead( dir->wadfile, &tname, 8);
if (!strnicmp( tname, texname, 8))
texofs = dir->dir.start + offsets[ n];
}
FreeMemory( offsets);
}
if (texofs != 0)
{
/* read the info for this texture */
BasicWadSeek( dir->wadfile, texofs + 12L);
BasicWadRead( dir->wadfile, xsize_r, 2L);
BasicWadRead( dir->wadfile, ysize_r, 2L);
}
else
{
/* texture data not found */
*xsize_r = -1;
*ysize_r = -1;
}
}
/*
choose a floor or ceiling texture
*/
void ChooseFloorTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
{
if (UseMouse)
HideMousePointer();
SwitchToVGA256();
/* if we only have a 320x200x256 VGA driver, we must change x0 and y0. Yuck! */
if (GfxMode > -2)
{
x0 = -1;
y0 = -1;
}
InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 5, name, 64, 64, DisplayFloorTexture);
SwitchToVGA16();
if (UseMouse)
ShowMousePointer();
}
/*
choose a wall texture
*/
void ChooseWallTexture( int x0, int y0, char *prompt, int listsize, char **list, char *name)
{
if (UseMouse)
HideMousePointer();
SwitchToVGA256();
/* if we only have a 320x200x256 VGA driver, we must change x0 and y0. Yuck! */
if (GfxMode > -2)
{
x0 = 0;
y0 = -1;
}
InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayWallTexture);
SwitchToVGA16();
if (UseMouse)
ShowMousePointer();
}
/*
function used by qsort to sort the sprite names
*/
int SortSprites( const void *a, const void *b)
{
return strcmp( *((char **)a), *((char **)b));
}
/*
choose a "sprite"
*/
void ChooseSprite( int x0, int y0, char *prompt, char *sname)
{
MDirPtr dir;
int n, listsize;
char **list;
char name[ 9];
/* count the names */
dir = FindMasterDir( MasterDir, "S_START");
dir = dir->next;
for (n = 0; dir && strcmp(dir->dir.name, "S_END"); n++)
dir = dir->next;
listsize = n;
/* get the actual names from master dir. */
dir = FindMasterDir( MasterDir, "S_START");
dir = dir->next;
list = GetMemory( listsize * sizeof( char *));
for (n = 0; n < listsize; n++)
{
list[ n] = GetMemory( 9 * sizeof( char));
strncpy( list[ n], dir->dir.name, 8);
list[ n][ 8] = '\0';
dir = dir->next;
}
qsort( list, listsize, sizeof( char *), SortSprites);
if (sname != NULL)
strncpy( name, sname, 8);
else
strcpy( name, list[ 0]);
if (UseMouse)
HideMousePointer();
SwitchToVGA256();
/* if we only have a 320x200x256 VGA driver, we must change x0 and y0. Yuck! */
if (GfxMode > -2)
{
x0 = 0;
y0 = -1;
}
InputNameFromListWithFunc( x0, y0, prompt, listsize, list, 11, name, 256, 128, DisplayPic);
SwitchToVGA16();
if (UseMouse)
ShowMousePointer();
for (n = 0; n < listsize; n++)
FreeMemory( list[ n]);
FreeMemory( list);
}