📄 r_data.c
字号:
texture_t* texture; mappatch_t* mpatch; texpatch_t* patch; char* pnames; int i; int j; int* maptex; int* maptex2; int* maptex1; char name[9]; char* name_p; int* patchlookup; int nummappatches; int offset; int maxoff; int maxoff2; int numtextures1; int numtextures2; int* directory; // free previous memory before numtextures change if (numtextures>0) for (i=0; i<numtextures; i++) { if (textures[i]) Z_Free (textures[i]); if (texturecache[i]) Z_Free (texturecache[i]); } // Load the patch names from pnames.lmp. name[8] = 0; pnames = W_CacheLumpName ("PNAMES", PU_STATIC); nummappatches = LONG ( *((int *)pnames) ); name_p = pnames+4; patchlookup = alloca (nummappatches*sizeof(*patchlookup)); for (i=0 ; i<nummappatches ; i++) { strncpy (name,name_p+i*8, 8); patchlookup[i] = W_CheckNumForName (name); } Z_Free (pnames); // Load the map texture definitions from textures.lmp. // The data is contained in one or two lumps, // TEXTURE1 for shareware, plus TEXTURE2 for commercial. maptex = maptex1 = W_CacheLumpName ("TEXTURE1", PU_STATIC); numtextures1 = LONG(*maptex); maxoff = W_LumpLength (W_GetNumForName ("TEXTURE1")); directory = maptex+1; if (W_CheckNumForName ("TEXTURE2") != -1) { maptex2 = W_CacheLumpName ("TEXTURE2", PU_STATIC); numtextures2 = LONG(*maptex2); maxoff2 = W_LumpLength (W_GetNumForName ("TEXTURE2")); } else { maptex2 = NULL; numtextures2 = 0; maxoff2 = 0; } numtextures = numtextures1 + numtextures2; //faB : there is actually 5 buffers allocated in one for convenience if (textures) Z_Free (textures); textures = Z_Malloc (numtextures*4*5, PU_STATIC, 0); texturecolumnofs = (void*)((int*)textures + numtextures); texturecache = (void*)((int*)textures + numtextures*2); texturewidthmask = (void*)((int*)textures + numtextures*3); textureheight = (void*)((int*)textures + numtextures*4); for (i=0 ; i<numtextures ; i++, directory++) { //only during game startup //if (!(i&63)) // CONS_Printf ("."); if (i == numtextures1) { // Start looking in second texture file. maptex = maptex2; maxoff = maxoff2; directory = maptex+1; } // offset to the current texture in TEXTURESn lump offset = LONG(*directory); if (offset > maxoff) I_Error ("R_LoadTextures: bad texture directory"); // maptexture describes texture name, size, and // used patches in z order from bottom to top mtexture = (maptexture_t *) ( (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); memcpy (texture->name, mtexture->name, sizeof(texture->name)); mpatch = &mtexture->patches[0]; patch = &texture->patches[0]; 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) { I_Error ("R_InitTextures: Missing patch in texture %s", texture->name); } } j = 1; while (j*2 <= texture->width) j<<=1; texturewidthmask[i] = j-1; textureheight[i] = texture->height<<FRACBITS; } Z_Free (maptex1); if (maptex2) Z_Free (maptex2); //added:01-04-98: this takes 90% of texture loading time.. // Precalculate whatever possible. for (i=0 ; i<numtextures ; i++) texturecache[i] = NULL; // Create translation table for global animation. if (texturetranslation) Z_Free (texturetranslation); texturetranslation = Z_Malloc ((numtextures+1)*4, PU_STATIC, 0); for (i=0 ; i<numtextures ; i++) texturetranslation[i] = i;}int R_CheckNumForNameList(char *name, lumplist_t* list, int listsize){ int i; int lump; for(i = listsize - 1; i > -1; i--) { lump = W_CheckNumForNamePwad(name, list[i].wadfile, list[i].firstlump); if((lump & 0xffff) > (list[i].firstlump + list[i].numlumps) || lump == -1) continue; else return lump; } return -1;}lumplist_t* colormaplumps;int numcolormaplumps;void R_InitExtraColormaps(){ int startnum; int endnum; int cfile; int clump; numcolormaplumps = 0; colormaplumps = NULL; cfile = clump = 0; for(;cfile < numwadfiles;cfile ++, clump = 0) { startnum = W_CheckNumForNamePwad("C_START", cfile, clump); if(startnum == -1) continue; endnum = W_CheckNumForNamePwad("C_END", cfile, clump); if(endnum == -1) I_Error("R_InitColormaps: C_START without C_END\n"); if((startnum >> 16) != (endnum >> 16)) I_Error("R_InitColormaps: C_START and C_END in different wad files!\n"); colormaplumps = (lumplist_t *)realloc(colormaplumps, sizeof(lumplist_t) * (numcolormaplumps + 1)); colormaplumps[numcolormaplumps].wadfile = startnum >> 16; colormaplumps[numcolormaplumps].firstlump = (startnum&0xFFFF) + 1; colormaplumps[numcolormaplumps].numlumps = endnum - (startnum + 1); numcolormaplumps++; }}lumplist_t* flats;int numflatlists;extern int numwadfiles;void R_InitFlats (){ int startnum; int endnum; int cfile; int clump; numflatlists = 0; flats = NULL; cfile = clump = 0;#ifdef OLDWATER //added:18-02-98: WATER! flatnum of the first waterflat firstwaterflat = W_GetNumForName ("WATER0");#endif for(;cfile < numwadfiles;cfile ++, clump = 0) { startnum = W_CheckNumForNamePwad("F_START", cfile, clump); if(startnum == -1) { clump = 0; startnum = W_CheckNumForNamePwad("FF_START", cfile, clump); if(startnum == -1) //If STILL -1, search the whole file! { flats = (lumplist_t *)realloc(flats, sizeof(lumplist_t) * (numflatlists + 1)); flats[numflatlists].wadfile = cfile; flats[numflatlists].firstlump = 0; flats[numflatlists].numlumps = 0xffff; //Search the entire file! numflatlists ++; continue; } } endnum = W_CheckNumForNamePwad("F_END", cfile, clump); if(endnum == -1) endnum = W_CheckNumForNamePwad("FF_END", cfile, clump); if(endnum == -1 || (startnum &0xFFFF) > (endnum & 0xFFFF)) { flats = (lumplist_t *)realloc(flats, sizeof(lumplist_t) * (numflatlists + 1)); flats[numflatlists].wadfile = cfile; flats[numflatlists].firstlump = 0; flats[numflatlists].numlumps = 0xffff; //Search the entire file! numflatlists ++; continue; } flats = (lumplist_t *)realloc(flats, sizeof(lumplist_t) * (numflatlists + 1)); flats[numflatlists].wadfile = startnum >> 16; flats[numflatlists].firstlump = (startnum&0xFFFF) + 1; flats[numflatlists].numlumps = endnum - (startnum + 1); numflatlists++; continue; } if(!numflatlists) I_Error("R_InitFlats: No flats found!\n");}int R_GetFlatNumForName(char *name){ // BP: don't work with gothic2.wad //return R_CheckNumForNameList(name, flats, numflatlists); int lump = W_CheckNumForName(name); if(lump == -1) I_Error("R_GetFlatNumForName: Could not find flat %.8s\n", name); return lump;}//// R_InitSpriteLumps// Finds the width and hoffset of all sprites in the wad,// so the sprite does not need to be cached completely// just for having the header info ready during rendering.////// allocate sprite lookup tables//void R_InitSpriteLumps (void){ // the original Doom used to set numspritelumps from S_END-S_START+1 //Fab:FIXME: find a better solution for adding new sprites dynamically numspritelumps = 0; spritewidth = Z_Malloc (MAXSPRITELUMPS*4, PU_STATIC, 0); spriteoffset = Z_Malloc (MAXSPRITELUMPS*4, PU_STATIC, 0); spritetopoffset = Z_Malloc (MAXSPRITELUMPS*4, PU_STATIC, 0); spriteheight = Z_Malloc (MAXSPRITELUMPS*4, PU_STATIC, 0);}void R_InitExtraColormaps();//// R_InitColormaps//void R_InitColormaps (void){ int lump; // Load in the light tables, // now 64k aligned for smokie... lump = W_GetNumForName("COLORMAP"); colormaps = Z_MallocAlign (W_LumpLength (lump), PU_STATIC, 0, 16); W_ReadLump (lump,colormaps); //SoM: 3/30/2000: Init Boom colormaps. { num_extra_colormaps = 0; memset(extra_colormaps, 0, sizeof(extra_colormaps)); R_InitExtraColormaps(); }}int foundcolormaps[MAXCOLORMAPS];//SoM: Clears out extra colormaps between levels.void R_ClearColormaps(){ int i; num_extra_colormaps = 0; for(i = 0; i < MAXCOLORMAPS; i++) { foundcolormaps[i] = -1;// colormapparams[i] = 0; } memset(extra_colormaps, 0, sizeof(extra_colormaps));}int R_ColormapNumForName(char *name){ int lump, i; if(num_extra_colormaps == MAXCOLORMAPS) I_Error("R_ColormapNumForName: Too many colormaps!\n"); lump = R_CheckNumForNameList(name, colormaplumps, numcolormaplumps); if(lump == -1) I_Error("R_ColormapNumForName: Cannot find colormap lump %s\n", name); for(i = 0; i < num_extra_colormaps; i++) if(lump == foundcolormaps[i]) return i; foundcolormaps[num_extra_colormaps] = lump; // aligned on 8 bit for asm code extra_colormaps[num_extra_colormaps].colormap = Z_MallocAlign (W_LumpLength (lump), PU_LEVEL, 0, 8); W_ReadLump (lump,extra_colormaps[num_extra_colormaps].colormap); // SoM: Added, we set all params of the colormap to normal because there // is no real way to tell how GL should handle a colormap lump anyway.. extra_colormaps[num_extra_colormaps].maskcolor = 0xffff; extra_colormaps[num_extra_colormaps].fadecolor = 0x0; extra_colormaps[num_extra_colormaps].maskamt = 0x0; extra_colormaps[num_extra_colormaps].fadestart = 0; extra_colormaps[num_extra_colormaps].fadeend = 33; extra_colormaps[num_extra_colormaps].fog = 0; num_extra_colormaps++; return num_extra_colormaps - 1;}// SoM://// R_CreateColormap// This is a more GL friendly way of doing colormaps: Specify colormap// data in a special linedef's texture areas and use that to generate// custom colormaps at runtime. NOTE: For GL mode, we only need to color// data and not the colormap data. double deltas[256][3], map[256][3];unsigned char NearestColor(unsigned char r, unsigned char g, unsigned char b);int RoundUp(double number);int R_CreateColormap(char *p1, char *p2, char *p3){ double cmaskr, cmaskg, cmaskb, cdestr, cdestg, cdestb; double r, g, b; double cbrightness; double maskamt = 0, othermask = 0; int i, p; char *colormap_p; unsigned int cr, cg, cb; unsigned int maskcolor, fadecolor; unsigned int fadestart = 0, fadeend = 33, fadedist = 33; int fog = 0; #define HEX2INT(x) (x >= '0' && x <= '9' ? x - '0' : x >= 'a' && x <= 'f' ? x - 'a' + 10 : x >= 'A' && x <= 'F' ? x - 'A' + 10 : 0) if(p1[0] == '#') { cr = cmaskr = ((HEX2INT(p1[1]) * 16) + HEX2INT(p1[2])); cg = cmaskg = ((HEX2INT(p1[3]) * 16) + HEX2INT(p1[4])); cb = cmaskb = ((HEX2INT(p1[5]) * 16) + HEX2INT(p1[6])); // Create a rough approximation of the color (a 16 bit color) maskcolor = ((cb) >> 3) + (((cg) >> 2) << 5) + (((cr) >> 3) << 11); if(p1[7] >= 'a' && p1[7] <= 'z') maskamt = (p1[7] - 'a'); else if(p1[7] >= 'A' && p1[7] <= 'Z') maskamt = (p1[7] - 'A'); maskamt /= (double)24; othermask = 1 - maskamt; maskamt /= 0xff; cmaskr *= maskamt; cmaskg *= maskamt; cmaskb *= maskamt; } else { cmaskr = 0xff; cmaskg = 0xff; cmaskb = 0xff; maskamt = 0; maskcolor = ((0xff) >> 3) + (((0xff) >> 2) << 5) + (((0xff) >> 3) << 11);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -