123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- #include "doomstat.h"
- #include "w_wad.h"
- #include "r_draw.h"
- #include "r_main.h"
- #include "r_sky.h"
- #include "i_system.h"
- #include "r_bsp.h"
- #include "r_things.h"
- #include "p_tick.h"
- #include "lprintf.h"
- #include "p_tick.h"
- typedef struct
- {
- short originx;
- short originy;
- short patch;
- short stepdir;
- short colormap;
- } PACKEDATTR mappatch_t;
- typedef struct
- {
- char name[8];
- char pad2[4];
- short width;
- short height;
- char pad[4];
- short patchcount;
- mappatch_t patches[1];
- } PACKEDATTR maptexture_t;
- int firstcolormaplump, lastcolormaplump;
- int firstflat, lastflat, numflats;
- int firstspritelump, lastspritelump, numspritelumps;
- int numtextures;
- texture_t **textures;
- fixed_t *textureheight;
- int *flattranslation;
- int *texturetranslation;
- const byte *R_GetTextureColumn(const rpatch_t *texpatch, int col) {
- while (col < 0)
- col += texpatch->width;
- col &= texpatch->widthmask;
-
- return texpatch->columns[col].pixels;
- }
- static void R_InitTextures (void)
- {
- const maptexture_t *mtexture;
- texture_t *texture;
- const mappatch_t *mpatch;
- texpatch_t *patch;
- int i, j;
- int maptex_lump[2] = {-1, -1};
- const int *maptex;
- const int *maptex1, *maptex2;
- char name[9];
- int names_lump;
- const char *names;
- const char *name_p;
- int *patchlookup;
- int totalwidth;
- int nummappatches;
- int offset;
- int maxoff, maxoff2;
- int numtextures1, numtextures2;
- const int *directory;
- int errors = 0;
-
- name[8] = 0;
- names = W_CacheLumpNum(names_lump = W_GetNumForName("PNAMES"));
- nummappatches = LONG(*((const int *)names));
- name_p = names+4;
- patchlookup = malloc(nummappatches*sizeof(*patchlookup));
- for (i=0 ; i<nummappatches ; i++)
- {
- strncpy (name,name_p+i*8, 8);
- patchlookup[i] = W_CheckNumForName(name);
- if (patchlookup[i] == -1)
- {
-
-
-
-
-
-
-
- patchlookup[i] = (W_CheckNumForName)(name, ns_sprites);
- if (patchlookup[i] == -1 && devparm)
-
- lprintf(LO_WARN,"\nWarning: patch %.8s, index %d does not exist",name,i);
- }
- }
- W_UnlockLumpNum(names_lump);
-
-
-
- maptex = maptex1 = W_CacheLumpNum(maptex_lump[0] = W_GetNumForName("TEXTURE1"));
- numtextures1 = LONG(*maptex);
- maxoff = W_LumpLength(maptex_lump[0]);
- directory = maptex+1;
- if (W_CheckNumForName("TEXTURE2") != -1)
- {
- maptex2 = W_CacheLumpNum(maptex_lump[1] = W_GetNumForName("TEXTURE2"));
- numtextures2 = LONG(*maptex2);
- maxoff2 = W_LumpLength(maptex_lump[1]);
- }
- else
- {
- maptex2 = NULL;
- numtextures2 = 0;
- maxoff2 = 0;
- }
- numtextures = numtextures1 + numtextures2;
-
-
- textures = Z_Malloc(numtextures*sizeof*textures, PU_STATIC, 0);
- textureheight = Z_Malloc(numtextures*sizeof*textureheight, PU_STATIC, 0);
- totalwidth = 0;
- for (i=0 ; i<numtextures ; i++, directory++)
- {
- if (i == numtextures1)
- {
-
- maptex = maptex2;
- maxoff = maxoff2;
- directory = maptex+1;
- }
- offset = LONG(*directory);
- if (offset > maxoff)
- I_Error("R_InitTextures: Bad texture directory");
- mtexture = (const maptexture_t *) ( (const byte *)maptex + offset);
- texture = textures[i] =
- Z_Malloc(sizeof(texture_t) +
- sizeof(texpatch_t)*(SHORT(mtexture->patchcount)-1),
- PU_STATIC, 0);
- texture->width = SHORT(mtexture->width);
- texture->height = SHORT(mtexture->height);
- texture->patchcount = SHORT(mtexture->patchcount);
-
-
- {
- for(j=0;j<sizeof(texture->name);j++)
- texture->name[j]=mtexture->name[j];
- }
- mpatch = mtexture->patches;
- patch = texture->patches;
- for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
- {
- patch->originx = SHORT(mpatch->originx);
- patch->originy = SHORT(mpatch->originy);
- patch->patch = patchlookup[SHORT(mpatch->patch)];
- if (patch->patch == -1)
- {
-
- lprintf(LO_ERROR,"\nR_InitTextures: Missing patch %d in texture %.8s",
- SHORT(mpatch->patch), texture->name);
- ++errors;
- }
- }
- for (j=1; j*2 <= texture->width; j<<=1)
- ;
- texture->widthmask = j-1;
- textureheight[i] = texture->height<<FRACBITS;
- totalwidth += texture->width;
- }
- free(patchlookup);
- for (i=0; i<2; i++)
- if (maptex_lump[i] != -1)
- W_UnlockLumpNum(maptex_lump[i]);
- if (errors)
- I_Error("R_InitTextures: %d errors", errors);
-
- if (devparm)
- for (i=0 ; i<numtextures ; i++)
- {
-
- R_CacheTextureCompositePatchNum(i);
- R_UnlockTextureCompositePatchNum(i);
- }
- if (errors)
- I_Error("R_InitTextures: %d errors", errors);
-
-
-
- texturetranslation =
- Z_Malloc((numtextures+1)*sizeof*texturetranslation, PU_STATIC, 0);
- for (i=0 ; i<numtextures ; i++)
- texturetranslation[i] = i;
-
- for (i = 0; i<numtextures; i++)
- textures[i]->index = -1;
- while (--i >= 0)
- {
- int texturej = W_LumpNameHash(textures[i]->name) % (unsigned) numtextures;
- textures[i]->next = textures[texturej]->index;
- textures[texturej]->index = i;
- }
- }
- static void R_InitFlats(void)
- {
- int i;
- firstflat = W_GetNumForName("F_START") + 1;
- lastflat = W_GetNumForName("F_END") - 1;
- numflats = lastflat - firstflat + 1;
-
-
-
- flattranslation =
- Z_Malloc((numflats+1)*sizeof(*flattranslation), PU_STATIC, 0);
- for (i=0 ; i<numflats ; i++)
- flattranslation[i] = i;
- }
- static void R_InitSpriteLumps(void)
- {
- firstspritelump = W_GetNumForName("S_START") + 1;
- lastspritelump = W_GetNumForName("S_END") - 1;
- numspritelumps = lastspritelump - firstspritelump + 1;
- }
- static void R_InitColormaps(void)
- {
- int i;
- firstcolormaplump = W_GetNumForName("C_START");
- lastcolormaplump = W_GetNumForName("C_END");
- numcolormaps = lastcolormaplump - firstcolormaplump;
- colormaps = Z_Malloc(sizeof(*colormaps) * numcolormaps, PU_STATIC, 0);
- colormaps[0] = (const lighttable_t *)W_CacheLumpName("COLORMAP");
- for (i=1; i<numcolormaps; i++)
- colormaps[i] = (const lighttable_t *)W_CacheLumpNum(i+firstcolormaplump);
-
- }
- int R_ColormapNumForName(const char *name)
- {
- register int i = 0;
- if (strncasecmp(name,"COLORMAP",8))
- if ((i = (W_CheckNumForName)(name, ns_colormaps)) != -1)
- i -= firstcolormaplump;
- return i;
- }
- static inline int between(int l,int u,int x)
- { return (l > x ? l : x > u ? u : x); }
- const lighttable_t* R_ColourMap(int lightlevel, fixed_t spriteyscale)
- {
- if (fixedcolormap) return fixedcolormap;
- else {
- if (curline)
- if (curline->v1->y == curline->v2->y)
- lightlevel -= 1 << LIGHTSEGSHIFT;
- else
- if (curline->v1->x == curline->v2->x)
- lightlevel += 1 << LIGHTSEGSHIFT;
- lightlevel += extralight << LIGHTSEGSHIFT;
-
- return fullcolormap + between(0,NUMCOLORMAPS-1,
- ((256-lightlevel)*2*NUMCOLORMAPS/256) - 4
- - (FixedMul(spriteyscale,pspriteiscale)/2 >> LIGHTSCALESHIFT)
- )*256;
- }
- }
- int tran_filter_pct = 66;
- #define TSC 12
- void R_InitTranMap(int progress)
- {
- int lump = W_CheckNumForName("TRANMAP");
-
- if (lump != -1)
- main_tranmap = W_CacheLumpNum(lump);
- else if (W_CheckNumForName("PLAYPAL")!=-1)
- {
- const byte *playpal = W_CacheLumpName("PLAYPAL");
- byte *my_tranmap;
- char fname[PATH_MAX+1];
- struct {
- unsigned char pct;
- unsigned char playpal[256];
- } cache;
- FILE *cachefp = fopen(strcat(strcpy(fname, I_DoomExeDir()), "/tranmap.dat"),"rb");
- main_tranmap = my_tranmap = Z_Malloc(256*256, PU_STATIC, 0);
-
- if (!cachefp ||
- fread(&cache, 1, sizeof cache, cachefp) != sizeof cache ||
- cache.pct != tran_filter_pct ||
- memcmp(cache.playpal, playpal, sizeof cache.playpal) ||
- fread(my_tranmap, 256, 256, cachefp) != 256 )
- {
- long pal[3][256], tot[256], pal_w1[3][256];
- long w1 = ((unsigned long) tran_filter_pct<<TSC)/100;
- long w2 = (1l<<TSC)-w1;
- if (progress)
- lprintf(LO_INFO, "Tranmap build [ ]\x08\x08\x08\x08\x08\x08\x08\x08\x08");
-
-
- {
- register int i = 255;
- register const unsigned char *p = playpal+255*3;
- do
- {
- register long t,d;
- pal_w1[0][i] = (pal[0][i] = t = p[0]) * w1;
- d = t*t;
- pal_w1[1][i] = (pal[1][i] = t = p[1]) * w1;
- d += t*t;
- pal_w1[2][i] = (pal[2][i] = t = p[2]) * w1;
- d += t*t;
- p -= 3;
- tot[i] = d << (TSC-1);
- }
- while (--i>=0);
- }
-
- {
- int i,j;
- byte *tp = my_tranmap;
- for (i=0;i<256;i++)
- {
- long r1 = pal[0][i] * w2;
- long g1 = pal[1][i] * w2;
- long b1 = pal[2][i] * w2;
- if (!(i & 31) && progress)
-
- lprintf(LO_INFO,".");
- for (j=0;j<256;j++,tp++)
- {
- register int color = 255;
- register long err;
- long r = pal_w1[0][j] + r1;
- long g = pal_w1[1][j] + g1;
- long b = pal_w1[2][j] + b1;
- long best = LONG_MAX;
- do
- if ((err = tot[color] - pal[0][color]*r
- - pal[1][color]*g - pal[2][color]*b) < best)
- best = err, *tp = color;
- while (--color >= 0);
- }
- }
- }
- if ((cachefp = fopen(fname,"wb")) != NULL)
- {
- cache.pct = tran_filter_pct;
- memcpy(cache.playpal, playpal, 256);
- fseek(cachefp, 0, SEEK_SET);
- fwrite(&cache, 1, sizeof cache, cachefp);
- fwrite(main_tranmap, 256, 256, cachefp);
-
- }
- }
- if (cachefp)
- fclose(cachefp);
- W_UnlockLumpName("PLAYPAL");
- }
- }
- void R_InitData(void)
- {
- lprintf(LO_INFO, "Textures ");
- R_InitTextures();
- lprintf(LO_INFO, "Flats ");
- R_InitFlats();
- lprintf(LO_INFO, "Sprites ");
- R_InitSpriteLumps();
- if (default_translucency)
- R_InitTranMap(1);
- R_InitColormaps();
- }
- int R_FlatNumForName(const char *name)
- {
- int i = (W_CheckNumForName)(name, ns_flats);
- if (i == -1)
- I_Error("R_FlatNumForName: %.8s not found", name);
- return i - firstflat;
- }
- int PUREFUNC R_CheckTextureNumForName(const char *name)
- {
- int i = NO_TEXTURE;
- if (*name != '-')
- {
- i = textures[W_LumpNameHash(name) % (unsigned) numtextures]->index;
- while (i >= 0 && strncasecmp(textures[i]->name,name,8))
- i = textures[i]->next;
- }
- return i;
- }
- static const char * germanyRemap[][2] = {
- { "ZZWOLF2", "ZZWOLF1" },
- { "ZZWOLF3", "ZZWOLF1" },
- { "ZZWOLF4", "ZZWOLF1" },
- { "ZZWOLF6", "ZZWOLF5" },
- { "ZZWOLF7", "ZZWOLF5" },
- { "ZZWOLF12", "ZZWOLF11" },
- { "ZZWOLF13", "ZZWOLF11" },
- { NULL, NULL }
- };
-
- const char * R_RemapTextureForGermany( const char * name ) {
- return name;
-
- const char ** currentTestPair = germanyRemap[0];
-
- while ( currentTestPair[0] != NULL ) {
- if ( strncmp( currentTestPair[0], name, 8 ) == 0 ) {
- return currentTestPair[1];
- }
-
- currentTestPair += 2;
- }
- return name;
- }
- int PUREFUNC R_TextureNumForName(const char *name)
- {
- const char * remappedName = R_RemapTextureForGermany( name );
-
- int i = R_CheckTextureNumForName(remappedName);
- if (i == -1)
- I_Error("R_TextureNumForName: %.8s not found", remappedName);
- return i;
- }
- int PUREFUNC R_SafeTextureNumForName(const char *name, int snum)
- {
- const char * remappedName = R_RemapTextureForGermany( name );
-
- int i = R_CheckTextureNumForName(remappedName);
- if (i == -1) {
- i = NO_TEXTURE;
- lprintf(LO_DEBUG,"bad texture '%s' in sidedef %d\n",remappedName,snum);
- }
- return i;
- }
- static inline void precache_lump(int l)
- {
- W_CacheLumpNum(l); W_UnlockLumpNum(l);
- }
- void R_PrecacheLevel(void)
- {
- register int i;
- register byte *hitlist;
- if (demoplayback)
- return;
- {
- size_t size = numflats > numsprites ? numflats : numsprites;
- hitlist = malloc((size_t)numtextures > size ? numtextures : size);
- }
-
- memset(hitlist, 0, numflats);
- for (i = numsectors; --i >= 0; )
- hitlist[sectors[i].floorpic] = hitlist[sectors[i].ceilingpic] = 1;
- for (i = numflats; --i >= 0; )
- if (hitlist[i])
- precache_lump(firstflat + i);
-
- memset(hitlist, 0, numtextures);
- for (i = numsides; --i >= 0;)
- hitlist[sides[i].bottomtexture] =
- hitlist[sides[i].toptexture] =
- hitlist[sides[i].midtexture] = 1;
-
-
-
-
-
-
- hitlist[skytexture] = 1;
- for (i = numtextures; --i >= 0; )
- if (hitlist[i])
- {
- texture_t *texture = textures[i];
- int j = texture->patchcount;
- while (--j >= 0)
- precache_lump(texture->patches[j].patch);
- }
-
- memset(hitlist, 0, numsprites);
- {
- thinker_t *th = NULL;
- while ((th = P_NextThinker(th,th_all)) != NULL)
- if (th->function == P_MobjThinker)
- hitlist[((mobj_t *)th)->sprite] = 1;
- }
- for (i=numsprites; --i >= 0;)
- if (hitlist[i])
- {
- int j = sprites[i].numframes;
- while (--j >= 0)
- {
- short *sflump = sprites[i].spriteframes[j].lump;
- int k = 7;
- do
- precache_lump(firstspritelump + sflump[k]);
- while (--k >= 0);
- }
- }
- free(hitlist);
- }
- void R_SetPatchNum(patchnum_t *patchnum, const char *name)
- {
- const rpatch_t *patch = R_CachePatchName(name);
- patchnum->width = patch->width;
- patchnum->height = patch->height;
- patchnum->leftoffset = patch->leftoffset;
- patchnum->topoffset = patch->topoffset;
- patchnum->lumpnum = W_GetNumForName(name);
- R_UnlockPatchName(name);
- }
|