Debian Codegraph @

prboom2/src/r_data.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2002 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Preparation of data for rendering,
31 * generation of lookups, caching, retrieval by name.
32 *
33 *-----------------------------------------------------------------------------*/
34
35 #include "doomstat.h"
36 #include "w_wad.h"
37 #include "r_draw.h"
38 #include "r_main.h"
39 #include "r_sky.h"
40 #include "i_system.h"
41 #include "r_bsp.h"
42 #include "r_things.h"
43 #include "p_tick.h"
44 #include "lprintf.h" // jff 08/03/98 - declaration of lprintf
45 #include "p_tick.h"
46
47 #include "dsda/args.h"
48 #include "dsda/configuration.h"
49 #include "dsda/map_format.h"
50 #include "dsda/utility.h"
51
52 //
53 // Graphics.
54 // DOOM graphics for walls and sprites
55 // is stored in vertical runs of opaque pixels (posts).
56 // A column is composed of zero or more posts,
57 // a patch or sprite is composed of zero or more columns.
58 //
59
60 //
61 // Texture definition.
62 // Each texture is composed of one or more patches,
63 // with patches being lumps stored in the WAD.
64 // The lumps are referenced by number, and patched
65 // into the rectangular texture space using origin
66 // and possibly other attributes.
67 //
68
69 typedef struct
70 {
71 short originx;
72 short originy;
73 short patch;
74 short stepdir; // unused in Doom but might be used in Phase 2 Boom
75 short colormap; // unused in Doom but might be used in Phase 2 Boom
76 } PACKEDATTR mappatch_t;
77
78
79 typedef struct
80 {
81 char name[8];
82 char pad2[4]; // unused
83 short width;
84 short height;
85 char pad[4]; // unused in Doom but might be used in Boom Phase 2
86 short patchcount;
87 mappatch_t patches[1];
88 } PACKEDATTR maptexture_t;
89
90 // A maptexturedef_t describes a rectangular texture, which is composed
91 // of one or more mappatch_t structures that arrange graphic patches.
92
93 // killough 4/17/98: make firstcolormaplump,lastcolormaplump external
94 int firstcolormaplump, lastcolormaplump; // killough 4/17/98
95
96 int firstflat, lastflat, numflats;
97 int firstspritelump, lastspritelump, numspritelumps;
98 int numtextures;
99 texture_t **textures; // proff - 04/05/2000 removed static for OpenGL
100 fixed_t *textureheight; //needed for texture pegging (and TFE fix - killough)
101 int *flattranslation; // for global animation
102 int *texturetranslation;
103
104 //
105 // R_GetTextureColumn
106 //
107
108 const byte *R_GetTextureColumn(const rpatch_t *texpatch, int col) {
109 const int width = texpatch->width;
110 const unsigned int mask = texpatch->widthmask;
111
112 while (col < 0)
113 col += width;
114
115 if (mask + 1 == width)
116 col &= mask;
117 else
118 col %= width;
119
120 return texpatch->columns[col].pixels;
121 }
122
123 //
124 // R_InitTextures
125 // Initializes the texture list
126 // with the textures from the world map.
127 //
128
129 static dboolean R_IsPNGLump(int lump_num)
130 {
131 return W_LumpLength(lump_num) >= 8 &&
132 !memcmp(W_LumpByNum(lump_num), "\211PNG\r\n\032\n", 8);
133 }
134
135 static int R_FilterValidPatch(int lump_num, const char *name)
136 {
137 if (lump_num != LUMP_NOT_FOUND)
138 {
139 if (R_IsPNGLump(lump_num))
140 {
141 lprintf(LO_WARN, "Warning: patch %s is in an unsupported format (PNG)\n", name);
142 lump_num = W_CheckNumForName2("TNT1A0", ns_sprites);
143 }
144 }
145
146 return lump_num;
147 }
148
149 static void R_InitTextures (void)
150 {
151 const maptexture_t *mtexture;
152 texture_t *texture;
153 const mappatch_t *mpatch;
154 texpatch_t *patch;
155 int i, j;
156 int maptex_lump[2] = {-1, -1};
157 const int *maptex;
158 const int *maptex1, *maptex2;
159 char name[9];
160 int names_lump; // cph - new wad lump handling
161 const char *names; // cph -
162 const char *name_p;// const*'s
163 int *patchlookup;
164 int nummappatches;
165 int offset;
166 int maxoff, maxoff2;
167 int numtextures1, numtextures2;
168 const int *directory;
169 int errors = 0;
170
171 // Load the patch names from pnames.lmp.
172 name[8] = 0;
173 names = W_LumpByNum(names_lump = W_GetNumForName("PNAMES"));
174 nummappatches = LittleLong(*((const int *)names));
175 name_p = names+4;
176 patchlookup = Z_Malloc(nummappatches*sizeof(*patchlookup)); // killough
177
178 for (i=0 ; i<nummappatches ; i++)
179 {
180 strncpy (name,name_p+i*8, 8);
181 patchlookup[i] = W_CheckNumForName(name);
182 if (patchlookup[i] == LUMP_NOT_FOUND)
183 {
184 // killough 4/17/98:
185 // Some wads use sprites as wall patches, so repeat check and
186 // look for sprites this time, but only if there were no wall
187 // patches found. This is the same as allowing for both, except
188 // that wall patches always win over sprites, even when they
189 // appear first in a wad. This is a kludgy solution to the wad
190 // lump namespace problem.
191
192 patchlookup[i] = W_CheckNumForName2(name, ns_sprites);
193 }
194
195 patchlookup[i] = R_FilterValidPatch(patchlookup[i], name);
196 }
197
198 // Load the map texture definitions from textures.lmp.
199 // The data is contained in one or two lumps,
200 // TEXTURE1 for shareware, plus TEXTURE2 for commercial.
201
202 maptex = maptex1 = W_LumpByNum(maptex_lump[0] = W_GetNumForName("TEXTURE1"));
203 numtextures1 = LittleLong(*maptex);
204 maxoff = W_LumpLength(maptex_lump[0]);
205 directory = maptex+1;
206
207 if (W_LumpNameExists("TEXTURE2"))
208 {
209 maptex2 = W_LumpByNum(maptex_lump[1] = W_GetNumForName("TEXTURE2"));
210 numtextures2 = LittleLong(*maptex2);
211 maxoff2 = W_LumpLength(maptex_lump[1]);
212 }
213 else
214 {
215 maptex2 = NULL;
216 numtextures2 = 0;
217 maxoff2 = 0;
218 }
219 numtextures = numtextures1 + numtextures2;
220
221 // killough 4/9/98: make column offsets 32-bit;
222 // clean up malloc-ing to use sizeof
223
224 textures = Z_Malloc(numtextures*sizeof*textures);
225 textureheight = Z_Malloc(numtextures*sizeof*textureheight);
226
227 for (i=0 ; i<numtextures ; i++, directory++)
228 {
229 if (i == numtextures1)
230 {
231 // Start looking in second texture file.
232 maptex = maptex2;
233 maxoff = maxoff2;
234 directory = maptex+1;
235 }
236
237 offset = LittleLong(*directory);
238
239 if (offset > maxoff)
240 I_Error("R_InitTextures: Bad texture directory");
241
242 mtexture = (const maptexture_t *) ( (const byte *)maptex + offset);
243
244 texture = textures[i] =
245 Z_Malloc(sizeof(texture_t) + sizeof(texpatch_t)*(LittleShort(mtexture->patchcount)-1));
246
247 texture->width = LittleShort(mtexture->width);
248 texture->height = LittleShort(mtexture->height);
249 texture->patchcount = LittleShort(mtexture->patchcount);
250
251 /* Mattias Engdeg�rd emailed me of the following explenation of
252 * why memcpy doesnt work on some systems:
253 * "I suppose it is the mad unaligned allocation
254 * going on (and which gcc in some way manages to cope with
255 * through the __attribute__ ((packed))), and which it forgets
256 * when optimizing memcpy (to a single word move) since it appears
257 * to be aligned. Technically a gcc bug, but I can't blame it when
258 * it's stressed with that amount of
259 * non-standard nonsense."
260 * So in short the unaligned struct confuses gcc's optimizer so
261 * i took the memcpy out alltogether to avoid future problems-Jess
262 */
263 /* The above was #ifndef SPARC, but i got a mail from
264 * Putera Joseph F NPRI <[email protected]> containing:
265 * I had to use the memcpy function on a sparc machine. The
266 * other one would give me a core dump.
267 * cph - I find it hard to believe that sparc memcpy is broken,
268 * but I don't believe the pointers to memcpy have to be aligned
269 * either. Use fast memcpy on other machines anyway.
270 */
271 /*
272 proff - I took this out, because Oli Kraus ([email protected]) told
273 me the memcpy produced a buserror. Since this function isn't time-
274 critical I'm using the for loop now.
275 */
276 /*
277 #ifndef GCC
278 memcpy(texture->name, mtexture->name, sizeof(texture->name));
279 #else
280 */
281 {
282 size_t j;
283 for(j=0;j<sizeof(texture->name);j++)
284 texture->name[j]=mtexture->name[j];
285 }
286 /* #endif */
287
288 mpatch = mtexture->patches;
289 patch = texture->patches;
290
291 for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
292 {
293 patch->originx = LittleShort(mpatch->originx);
294 patch->originy = LittleShort(mpatch->originy);
295 patch->patch = patchlookup[LittleShort(mpatch->patch)];
296 if (patch->patch == -1)
297 {
298 //jff 8/3/98 use logical output routine
299 lprintf(LO_ERROR,"\nR_InitTextures: Missing patch %d in texture %.8s",
300 LittleShort(mpatch->patch), texture->name); // killough 4/17/98
301 ++errors;
302 }
303 }
304
305 for (j=1; j*2 <= texture->width; j<<=1)
306 ;
307 texture->widthmask = j-1;
308 textureheight[i] = texture->height<<FRACBITS;
309 }
310
311 Z_Free(patchlookup); // killough
312
313 if (errors)
314 {
315 const lumpinfo_t* info;
316
317 info = W_GetLumpInfoByNum(names_lump);
318
319 I_Error("Texture errors: %d!\n%s seems to be incompatible with %s.\nAre you using the right IWAD?",
320 errors, dsda_BaseName(info->wadfile->name), doomverstr);
321 }
322
323 // Create translation table for global animation.
324 // killough 4/9/98: make column offsets 32-bit;
325 // clean up malloc-ing to use sizeof
326
327 texturetranslation = Z_Malloc((numtextures+1)*sizeof*texturetranslation);
328
329 for (i=0 ; i<numtextures ; i++)
330 texturetranslation[i] = i;
331
332 // killough 1/31/98: Initialize texture hash table
333 for (i = 0; i<numtextures; i++)
334 textures[i]->index = -1;
335 while (--i >= 0)
336 {
337 int j = W_LumpNameHash(textures[i]->name) % (unsigned) numtextures;
338 textures[i]->next = textures[j]->index; // Prepend to chain
339 textures[j]->index = i;
340 }
341 }
342
343 //
344 // R_InitFlats
345 //
346 static void R_InitFlats(void)
347 {
348 int i;
349
350 firstflat = W_GetNumForName("F_START") + 1;
351 lastflat = W_GetNumForName("F_END") - 1;
352 numflats = lastflat - firstflat + 1;
353
354 // Create translation table for global animation.
355 // killough 4/9/98: make column offsets 32-bit;
356 // clean up malloc-ing to use sizeof
357
358 flattranslation = Z_Malloc((numflats+1)*sizeof(*flattranslation));
359
360 for (i=0 ; i<numflats ; i++)
361 flattranslation[i] = i;
362 }
363
364 //
365 // R_InitSpriteLumps
366 // Finds the width and hoffset of all sprites in the wad,
367 // so the sprite does not need to be cached completely
368 // just for having the header info ready during rendering.
369 //
370 static void R_InitSpriteLumps(void)
371 {
372 firstspritelump = W_GetNumForName("S_START") + 1;
373 lastspritelump = W_GetNumForName("S_END") - 1;
374 numspritelumps = lastspritelump - firstspritelump + 1;
375 }
376
377 //
378 // R_InitColormaps
379 //
380 // killough 3/20/98: rewritten to allow dynamic colormaps
381 // and to remove unnecessary 256-byte alignment
382 //
383 // killough 4/4/98: Add support for C_START/C_END markers
384 //
385
386 static void R_InitColormaps(void)
387 {
388 int i;
389 extern const byte* colormap_lump;
390 // MAP_FORMAT_TODO: not sure about this
391 if (hexen)
392 {
393 firstcolormaplump = -1;
394 lastcolormaplump = -1;
395 numcolormaps = 1;
396 }
397 else
398 {
399 firstcolormaplump = W_GetNumForName("C_START");
400 lastcolormaplump = W_GetNumForName("C_END");
401 numcolormaps = lastcolormaplump - firstcolormaplump;
402 }
403 colormaps = Z_Malloc(sizeof(*colormaps) * numcolormaps);
404 colormap_lump = W_LumpByName("COLORMAP");
405 colormaps[0] = (const lighttable_t *)colormap_lump;
406 for (i=1; i<numcolormaps; i++)
407 colormaps[i] = (const lighttable_t *)W_LumpByNum(i+firstcolormaplump);
408 // cph - always lock
409 }
410
411 // killough 4/4/98: get colormap number from name
412 // killough 4/11/98: changed to return -1 for illegal names
413 // killough 4/17/98: changed to use ns_colormaps tag
414
415 int R_ColormapNumForName(const char *name)
416 {
417 register int i = 0;
418 if (strncasecmp(name,"COLORMAP",8)) // COLORMAP predefined to return 0
419 if ((i = W_CheckNumForName2(name, ns_colormaps)) != LUMP_NOT_FOUND)
420 i -= firstcolormaplump;
421 return i;
422 }
423
424 //
425 // R_InitData
426 // Locates all the lumps
427 // that will be used by all views
428 // Must be called after W_Init.
429 //
430
431 void R_InitData(void)
432 {
433 lprintf(LO_DEBUG, "Textures ");
434 R_InitTextures();
435 lprintf(LO_DEBUG, "Flats ");
436 R_InitFlats();
437 lprintf(LO_DEBUG, "Sprites ");
438 R_InitSpriteLumps();
439 R_InitColormaps(); // killough 3/20/98
440 }
441
442 //
443 // R_FlatNumForName
444 // Retrieval, get a flat number for a flat name.
445 //
446 // killough 4/17/98: changed to use ns_flats namespace
447 //
448
449 int R_FlatNumForName(const char *name) // killough -- const added
450 {
451 int i = W_CheckNumForName2(name, ns_flats);
452 if (i == LUMP_NOT_FOUND)
453 {
454 // e6y
455 // Ability to play wads with wrong flat names
456 // Unknown flats will be replaced with "NO TEXTURE" preset from dsda-doom.wad
457 lprintf(LO_DEBUG, "R_FlatNumForName: %.8s not found\n", name);
458 i = W_CheckNumForName2("-N0_TEX-", ns_flats);
459 if (i == LUMP_NOT_FOUND)
460 {
461 I_Error("R_FlatNumForName: -N0_TEX- not found");
462 }
463 }
464 return i - firstflat;
465 }
466
467 //
468 // R_CheckTextureNumForName
469 // Check whether texture is available.
470 // Filter out NoTexture indicator.
471 //
472 // Rewritten by Lee Killough to use hash table for fast lookup. Considerably
473 // reduces the time needed to start new levels. See w_wad.c for comments on
474 // the hashing algorithm, which is also used for lump searches.
475 //
476 // killough 1/21/98, 1/31/98
477 //
478
479 int PUREFUNC R_CheckTextureNumForName(const char *name)
480 {
481 int i = NO_TEXTURE;
482 if (*name != '-') // "NoTexture" marker.
483 {
484 i = textures[W_LumpNameHash(name) % (unsigned) numtextures]->index;
485 while (i >= 0 && strncasecmp(textures[i]->name,name,8))
486 i = textures[i]->next;
487 }
488 return i;
489 }
490
491 //
492 // R_TextureNumForName
493 // Calls R_CheckTextureNumForName,
494 // aborts with error message.
495 //
496
497 int PUREFUNC R_TextureNumForName(const char *name) // const added -- killough
498 {
499 int i = R_CheckTextureNumForName(name);
500 if (i == -1)
501 {
502 int lump;
503 const lumpinfo_t* info;
504
505 lump = W_GetNumForName("TEXTURE1");
506 info = W_GetLumpInfoByNum(lump);
507
508 I_Error("Texture not found: %.8s!\n%s seems to be incompatible with %s.\nAre you using the right IWAD?",
509 name, dsda_BaseName(info->wadfile->name), doomverstr);
510 }
511 return i;
512 }
513
514 //
515 // R_SafeTextureNumForName
516 // Calls R_CheckTextureNumForName, and changes any error to NO_TEXTURE
517 int PUREFUNC R_SafeTextureNumForName(const char *name, int snum)
518 {
519 int i = R_CheckTextureNumForName(name);
520 if (i == -1) {
521 i = NO_TEXTURE; // e6y - return "no texture"
522 lprintf(LO_DEBUG,"bad texture '%.8s' in sidedef %d\n",name,snum);
523 }
524 return i;
525 }
526
527 //
528 // R_PrecacheLevel
529 // Preloads all relevant graphics for the level.
530 //
531 // Totally rewritten by Lee Killough to use less memory,
532 // to avoid using alloca(), and to improve performance.
533 // cph - new wad lump handling, calls cache functions but acquires no locks
534
535 static inline void precache_lump(int l)
536 {
537 W_LumpByNum(l);
538 }
539
540 void R_PrecacheLevel(void)
541 {
542 register int i;
543 register byte *hitlist;
544
545 if (timingdemo)
546 return;
547
548 {
549 int size = numflats > num_sprites ? numflats : num_sprites;
550 hitlist = Z_Malloc(numtextures > size ? numtextures : size);
551 }
552
553 // Precache flats.
554
555 memset(hitlist, 0, numflats);
556
557 for (i = numsectors; --i >= 0; )
558 hitlist[sectors[i].floorpic] = hitlist[sectors[i].ceilingpic] = 1;
559
560 for (i = numflats; --i >= 0; )
561 if (hitlist[i])
562 precache_lump(firstflat + i);
563
564 // Precache textures.
565
566 memset(hitlist, 0, numtextures);
567
568 for (i = numsides; --i >= 0;)
569 hitlist[sides[i].bottomtexture] =
570 hitlist[sides[i].toptexture] =
571 hitlist[sides[i].midtexture] = 1;
572
573 // Sky texture is always present.
574 // Note that F_SKY1 is the name used to
575 // indicate a sky floor/ceiling as a flat,
576 // while the sky texture is stored like
577 // a wall texture, with an episode dependend
578 // name.
579
580 if (map_format.doublesky)
581 {
582 hitlist[Sky1Texture] = 1;
583 hitlist[Sky2Texture] = 1;
584 }
585 else
586 {
587 hitlist[skytexture] = 1;
588 }
589
590 for (i = numtextures; --i >= 0; )
591 if (hitlist[i])
592 {
593 texture_t *texture = textures[i];
594 int j = texture->patchcount;
595 while (--j >= 0)
596 precache_lump(texture->patches[j].patch);
597 }
598
599 // Precache sprites.
600 memset(hitlist, 0, num_sprites);
601
602 {
603 thinker_t *th = NULL;
604 while ((th = P_NextThinker(th,th_all)) != NULL)
605 if (th->function == P_MobjThinker)
606 hitlist[((mobj_t *)th)->sprite] = 1;
607 }
608
609 for (i=num_sprites; --i >= 0;)
610 if (hitlist[i])
611 {
612 int j = sprites[i].numframes;
613 while (--j >= 0)
614 {
615 short *sflump = sprites[i].spriteframes[j].lump;
616 int k = 7;
617 do
618 precache_lump(firstspritelump + sflump[k]);
619 while (--k >= 0);
620 }
621 }
622 Z_Free(hitlist);
623 }
624
625 // Proff - Added for OpenGL
626 void R_SetPatchNum(patchnum_t *patchnum, const char *name)
627 {
628 const rpatch_t *patch = R_PatchByName(name);
629 patchnum->width = patch->width;
630 patchnum->height = patch->height;
631 patchnum->leftoffset = patch->leftoffset;
632 patchnum->topoffset = patch->topoffset;
633 patchnum->lumpnum = W_GetNumForName(name);
634 }
635
636 void R_SetSpriteByNum(patchnum_t *patchnum, int lump)
637 {
638 const rpatch_t *patch = R_PatchByNum(lump);
639 patchnum->width = patch->width;
640 patchnum->height = patch->height;
641 patchnum->leftoffset = patch->leftoffset;
642 patchnum->topoffset = patch->topoffset;
643 patchnum->lumpnum = lump;
644 }
645
646 int R_SetSpriteByIndex(patchnum_t *patchnum, spritenum_t item)
647 {
648 int result = false;
649 if (item < num_sprites)
650 {
651 int lump = firstspritelump + sprites[item].spriteframes->lump[0];
652 R_SetSpriteByNum(patchnum, lump);
653 result = true;
654 }
655 return result;
656 }
657
658 int R_NumPatchForSpriteIndex(spritenum_t item)
659 {
660 if (item < 0 || item >= num_sprites)
661 {
662 return -1;
663 }
664
665 return firstspritelump + sprites[item].spriteframes->lump[0];
666 }
667
668 int R_SetSpriteByName(patchnum_t *patchnum, const char *name)
669 {
670 int result = false;
671 patchnum->lumpnum = W_CheckNumForName2(name, ns_sprites);
672 if (patchnum->lumpnum != LUMP_NOT_FOUND)
673 {
674 R_SetSpriteByNum(patchnum, patchnum->lumpnum);
675 result = true;
676 }
677 return result;
678 }
679
680 int R_SetPatchByName(patchnum_t *patchnum, const char *name)
681 {
682 int result = false;
683 int lump = W_CheckNumForName(name);
684 if (lump != LUMP_NOT_FOUND)
685 {
686 R_SetPatchNum(patchnum, name);
687 result = true;
688 }
689 return result;
690 }
691
692 // e6y: Added for "GRNROCK" mostly
693 void R_SetFloorNum(patchnum_t *patchnum, const char *name)
694 {
695 patchnum->width = 64;
696 patchnum->height = 64;
697 patchnum->leftoffset = 0;
698 patchnum->topoffset = 0;
699 patchnum->lumpnum = R_FlatNumForName(name);
700 }