9 extern void CheckAbortStartup(void);
11 int firstflat, lastflat, numflats;
12 int firstpatch, lastpatch, numpatches;
13 int firstspritelump, lastspritelump, numspritelumps;
17 int *texturewidthmask;
18 fixed_t *textureheight; // needed for texture pegging
19 int *texturecompositesize;
20 short **texturecolumnlump;
21 unsigned short **texturecolumnofs;
22 byte **texturecomposite;
24 int *flattranslation; // for global animation
25 int *texturetranslation; // for global animation
27 fixed_t *spritewidth; // needed for pre rendering
28 fixed_t *spriteoffset;
29 fixed_t *spritetopoffset;
31 lighttable_t *colormaps;
35 ==============================================================================
39 when a texture is first needed, it counts the number of composite columns
40 required in the texture and allocates space for a column directory and any
41 new columns. The directory will simply point inside other patches if there
42 is only one patch in a given column, but any columns with multiple patches
43 will have new column_ts generated.
45 ==============================================================================
53 = Clip and draw a column from a patch into a cached post
58 void R_DrawColumnInCache (column_t *patch, byte *cache, int originy, int cacheheight)
63 dest = (byte *)cache + 3;
65 while (patch->topdelta != 0xff)
67 source = (byte *)patch + 3;
68 count = patch->length;
69 position = originy + patch->topdelta;
75 if (position + count > cacheheight)
76 count = cacheheight - position;
78 memcpy (cache + position, source, count);
80 patch = (column_t *)( (byte *)patch + patch->length+ 4);
93 void R_GenerateComposite (int texnum)
103 unsigned short *colofs;
105 texture = textures[texnum];
106 block = Z_Malloc (texturecompositesize[texnum], PU_STATIC,
107 &texturecomposite[texnum]);
108 collump = texturecolumnlump[texnum];
109 colofs = texturecolumnofs[texnum];
112 // composite the columns together
114 patch = texture->patches;
116 for (i=0 , patch = texture->patches; i<texture->patchcount ; i++, patch++)
118 realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
120 x2 = x1 + SHORT(realpatch->width);
126 if (x2 > texture->width)
132 continue; // column does not have multiple patches
133 patchcol = (column_t *)((byte *)realpatch +
134 LONG(realpatch->columnofs[x-x1]));
135 R_DrawColumnInCache (patchcol, block + colofs[x], patch->originy,
141 // now that the texture has been built, it is purgable
142 Z_ChangeTag (block, PU_CACHE);
154 void R_GenerateLookup (int texnum)
157 byte *patchcount; // [texture->width]
163 unsigned short *colofs;
165 texture = textures[texnum];
167 texturecomposite[texnum] = 0; // composited not created yet
168 texturecompositesize[texnum] = 0;
169 collump = texturecolumnlump[texnum];
170 colofs = texturecolumnofs[texnum];
173 // count the number of columns that are covered by more than one patch
174 // fill in the lump / offset, so columns with only a single patch are
177 patchcount = (byte *)alloca (texture->width);
178 memset (patchcount, 0, texture->width);
179 patch = texture->patches;
181 for (i=0 , patch = texture->patches; i<texture->patchcount ; i++, patch++)
183 realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
185 x2 = x1 + SHORT(realpatch->width);
190 if (x2 > texture->width)
195 collump[x] = patch->patch;
196 colofs[x] = LONG(realpatch->columnofs[x-x1])+3;
200 for (x=0 ; x<texture->width ; x++)
204 printf ("R_GenerateLookup: column without a patch (%s)\n", texture->name);
207 // I_Error ("R_GenerateLookup: column without a patch");
208 if (patchcount[x] > 1)
210 collump[x] = -1; // use the cached block
211 colofs[x] = texturecompositesize[texnum];
212 if (texturecompositesize[texnum] > 0x10000-texture->height)
213 I_Error ("R_GenerateLookup: texture %i is >64k",texnum);
214 texturecompositesize[texnum] += texture->height;
228 byte *R_GetColumn (int tex, int col)
232 col &= texturewidthmask[tex];
233 lump = texturecolumnlump[tex][col];
234 ofs = texturecolumnofs[tex][col];
236 return (byte *)W_CacheLumpNum(lump,PU_CACHE)+ofs;
237 if (!texturecomposite[tex])
238 R_GenerateComposite (tex);
239 return texturecomposite[tex] + ofs;
248 = Initializes the texture list with the textures from the world map
253 void R_InitTextures (void)
255 maptexture_t *mtexture;
260 int *maptex, *maptex2, *maptex1;
261 char name[9], *names, *name_p;
265 int offset, maxoff, maxoff2;
266 int numtextures1, numtextures2;
270 // load the patch names from pnames.lmp
273 names = W_CacheLumpName ("PNAMES", PU_STATIC);
274 nummappatches = LONG ( *((int *)names) );
276 patchlookup = alloca (nummappatches*sizeof(*patchlookup));
277 for (i=0 ; i<nummappatches ; i++)
279 strncpy (name,name_p+i*8, 8);
280 patchlookup[i] = W_CheckNumForName (name);
285 // load the map texture definitions from textures.lmp
287 maptex = maptex1 = W_CacheLumpName ("TEXTURE1", PU_STATIC);
288 numtextures1 = LONG(*maptex);
289 maxoff = W_LumpLength (W_GetNumForName ("TEXTURE1"));
290 directory = maptex+1;
292 if (W_CheckNumForName ("TEXTURE2") != -1)
294 maptex2 = W_CacheLumpName ("TEXTURE2", PU_STATIC);
295 numtextures2 = LONG(*maptex2);
296 maxoff2 = W_LumpLength (W_GetNumForName ("TEXTURE2"));
304 numtextures = numtextures1 + numtextures2;
307 // Init the startup thermometer at this point...
311 spramount = W_GetNumForName("S_END") - W_GetNumForName("S_START") + 1;
312 InitThermo(spramount + numtextures + 6);
315 textures = Z_Malloc (numtextures*sizeof(*textures), PU_STATIC, 0);
316 texturecolumnlump = Z_Malloc (numtextures*sizeof(*texturecolumnlump), PU_STATIC, 0);
317 texturecolumnofs = Z_Malloc (numtextures*sizeof(*texturecolumnofs), PU_STATIC, 0);
318 texturecomposite = Z_Malloc (numtextures*sizeof(*texturecomposite), PU_STATIC, 0);
319 texturecompositesize = Z_Malloc (numtextures*sizeof(*texturecompositesize), PU_STATIC, 0);
320 texturewidthmask = Z_Malloc (numtextures*sizeof(*texturewidthmask), PU_STATIC, 0);
321 textureheight = Z_Malloc (numtextures*sizeof(*textureheight), PU_STATIC, 0);
325 for (i=0 ; i<numtextures ; i++, directory++)
333 if (i == numtextures1)
334 { // start looking in second texture file
337 directory = maptex+1;
340 offset = LONG(*directory);
342 I_Error ("R_InitTextures: bad texture directory");
343 mtexture = (maptexture_t *) ( (byte *)maptex + offset);
344 texture = textures[i] = Z_Malloc (sizeof(texture_t)
345 + sizeof(texpatch_t)*(SHORT(mtexture->patchcount)-1), PU_STATIC,
347 texture->width = SHORT(mtexture->width);
348 texture->height = SHORT(mtexture->height);
349 texture->patchcount = SHORT(mtexture->patchcount);
350 memcpy (texture->name, mtexture->name, sizeof(texture->name));
351 mpatch = &mtexture->patches[0];
352 patch = &texture->patches[0];
353 for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
355 patch->originx = SHORT(mpatch->originx);
356 patch->originy = SHORT(mpatch->originy);
357 patch->patch = patchlookup[SHORT(mpatch->patch)];
358 if (patch->patch == -1)
360 "R_InitTextures: Missing patch in texture %s",texture->name);
362 texturecolumnlump[i] = Z_Malloc (texture->width*2, PU_STATIC,0);
363 texturecolumnofs[i] = Z_Malloc (texture->width*2, PU_STATIC,0);
365 while (j*2 <= texture->width)
367 texturewidthmask[i] = j-1;
368 textureheight[i] = texture->height<<FRACBITS;
370 totalwidth += texture->width;
378 // precalculate whatever possible
380 for(i = 0; i < numtextures; i++)
387 // translation table for global animation
389 texturetranslation = Z_Malloc ((numtextures+1)*4, PU_STATIC, 0);
390 for (i=0 ; i<numtextures ; i++)
391 texturetranslation[i] = i;
403 void R_InitFlats (void)
407 firstflat = W_GetNumForName ("F_START") + 1;
408 lastflat = W_GetNumForName ("F_END") - 1;
409 numflats = lastflat - firstflat + 1;
411 // translation table for global animation
412 flattranslation = Z_Malloc ((numflats+1)*4, PU_STATIC, 0);
413 for (i=0 ; i<numflats ; i++)
414 flattranslation[i] = i;
423 = Finds the width and hoffset of all sprites in the wad, so the sprite doesn't
424 = need to be cached just for the header during rendering
428 void R_InitSpriteLumps (void)
433 firstspritelump = W_GetNumForName ("S_START") + 1;
434 lastspritelump = W_GetNumForName ("S_END") - 1;
435 numspritelumps = lastspritelump - firstspritelump + 1;
436 spritewidth = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
437 spriteoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
438 spritetopoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
440 for (i=0 ; i< numspritelumps ; i++)
448 patch = W_CacheLumpNum (firstspritelump+i, PU_CACHE);
449 spritewidth[i] = SHORT(patch->width)<<FRACBITS;
450 spriteoffset[i] = SHORT(patch->leftoffset)<<FRACBITS;
451 spritetopoffset[i] = SHORT(patch->topoffset)<<FRACBITS;
464 void R_InitColormaps (void)
468 // load in the light tables
469 // 256 byte align tables
471 lump = W_GetNumForName("COLORMAP");
472 length = W_LumpLength (lump) + 255;
473 colormaps = Z_Malloc (length, PU_STATIC, 0);
474 colormaps = (byte *)( ((intptr_t)colormaps + 255)&~0xff);
475 W_ReadLump (lump,colormaps);
484 = Locates all the lumps that will be used by all views
485 = Must be called after W_Init
489 void R_InitData (void)
491 tprintf("\nR_InitTextures ",0);
494 tprintf("R_InitFlats\n",0);
498 tprintf("R_InitSpriteLumps ",0);
499 R_InitSpriteLumps ();
506 //=============================================================================
516 int R_FlatNumForName (char *name)
521 i = W_CheckNumForName (name);
525 memcpy (namet, name,8);
526 I_Error ("R_FlatNumForName: %s not found",namet);
528 return i - firstflat;
535 = R_CheckTextureNumForName
540 int R_CheckTextureNumForName (char *name)
544 if (name[0] == '-') // no texture marker
547 for (i=0 ; i<numtextures ; i++)
548 if (!strncasecmp (textures[i]->name, name, 8) )
558 = R_TextureNumForName
563 int R_TextureNumForName (char *name)
568 i = R_CheckTextureNumForName (name);
570 I_Error ("R_TextureNumForName: %s not found",name);
581 = Preloads all relevent graphics for the level
585 int flatmemory, texturememory, spritememory;
587 void R_PrecacheLevel (void)
590 char *texturepresent;
603 flatpresent = alloca(numflats);
604 memset (flatpresent,0,numflats);
605 for (i=0 ; i<numsectors ; i++)
607 flatpresent[sectors[i].floorpic] = 1;
608 flatpresent[sectors[i].ceilingpic] = 1;
612 for (i=0 ; i<numflats ; i++)
615 lump = firstflat + i;
616 flatmemory += lumpinfo[lump].size;
617 W_CacheLumpNum(lump, PU_CACHE);
623 texturepresent = alloca(numtextures);
624 memset (texturepresent,0, numtextures);
626 for (i=0 ; i<numsides ; i++)
628 texturepresent[sides[i].toptexture] = 1;
629 texturepresent[sides[i].midtexture] = 1;
630 texturepresent[sides[i].bottomtexture] = 1;
633 texturepresent[skytexture] = 1;
636 for (i=0 ; i<numtextures ; i++)
638 if (!texturepresent[i])
640 texture = textures[i];
641 for (j=0 ; j<texture->patchcount ; j++)
643 lump = texture->patches[j].patch;
644 texturememory += lumpinfo[lump].size;
645 W_CacheLumpNum(lump , PU_CACHE);
652 spritepresent = alloca(numsprites);
653 memset (spritepresent,0, numsprites);
655 for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
657 if (th->function == P_MobjThinker)
658 spritepresent[((mobj_t *)th)->sprite] = 1;
662 for (i=0 ; i<numsprites ; i++)
664 if (!spritepresent[i])
666 for (j=0 ; j<sprites[i].numframes ; j++)
668 sf = &sprites[i].spriteframes[j];
669 for (k=0 ; k<8 ; k++)
671 lump = firstspritelump + sf->lump[k];
672 spritememory += lumpinfo[lump].size;
673 W_CacheLumpNum(lump , PU_CACHE);