Reduce code duplication

This commit is contained in:
Jaime Ita Passos 2021-08-26 18:46:26 -03:00
parent 6631f90fa8
commit 869b12c48f
8 changed files with 107 additions and 355 deletions

View file

@ -804,57 +804,18 @@ GLMapTexture_t *HWR_GetTexture(INT32 tex)
static void HWR_CacheFlat(GLMipmap_t *grMipmap, lumpnum_t flatlumpnum)
{
size_t size, pflatsize;
size_t size = W_LumpLength(flatlumpnum);
UINT16 pflatsize = R_GetFlatSize(size);
// setup the texture info
grMipmap->format = GL_TEXFMT_P_8;
grMipmap->flags = TF_WRAPXY|TF_CHROMAKEYED;
size = W_LumpLength(flatlumpnum);
switch (size)
{
case 4194304: // 2048x2048 lump
pflatsize = 2048;
break;
case 1048576: // 1024x1024 lump
pflatsize = 1024;
break;
case 262144:// 512x512 lump
pflatsize = 512;
break;
case 65536: // 256x256 lump
pflatsize = 256;
break;
case 16384: // 128x128 lump
pflatsize = 128;
break;
case 1024: // 32x32 lump
pflatsize = 32;
break;
case 256: // 16x16 lump
pflatsize = 16;
break;
case 64: // 8x8 lump
pflatsize = 8;
break;
case 16: // 4x4 lump
pflatsize = 4;
break;
case 4: // 2x2 lump
pflatsize = 2;
break;
default: // 64x64 lump
pflatsize = 64;
break;
}
grMipmap->width = (UINT16)pflatsize;
grMipmap->height = (UINT16)pflatsize;
grMipmap->width = pflatsize;
grMipmap->height = pflatsize;
// the flat raw data needn't be converted with palettized textures
W_ReadLump(flatlumpnum, Z_Malloc(W_LumpLength(flatlumpnum),
PU_HWRCACHE, &grMipmap->data));
W_ReadLump(flatlumpnum, Z_Malloc(size, PU_HWRCACHE, &grMipmap->data));
}
static void HWR_CacheTextureAsFlat(GLMipmap_t *grMipmap, INT32 texturenum)

View file

@ -575,58 +575,10 @@ void HWR_DrawPic(INT32 x, INT32 y, lumpnum_t lumpnum)
// --------------------------------------------------------------------------
void HWR_DrawFlatFill (INT32 x, INT32 y, INT32 w, INT32 h, lumpnum_t flatlumpnum)
{
FOutVector v[4];
double dflatsize;
INT32 flatflag;
FOutVector v[4];
const size_t len = W_LumpLength(flatlumpnum);
switch (len)
{
case 4194304: // 2048x2048 lump
dflatsize = 2048.0f;
flatflag = 2047;
break;
case 1048576: // 1024x1024 lump
dflatsize = 1024.0f;
flatflag = 1023;
break;
case 262144:// 512x512 lump
dflatsize = 512.0f;
flatflag = 511;
break;
case 65536: // 256x256 lump
dflatsize = 256.0f;
flatflag = 255;
break;
case 16384: // 128x128 lump
dflatsize = 128.0f;
flatflag = 127;
break;
case 1024: // 32x32 lump
dflatsize = 32.0f;
flatflag = 31;
break;
case 256: // 16x16 lump
dflatsize = 16.0f;
flatflag = 15;
break;
case 64: // 8x8 lump
dflatsize = 8.0f;
flatflag = 7;
break;
case 16: // 4x4 lump
dflatsize = 4.0f;
flatflag = 3;
break;
case 4: // 2x2 lump
dflatsize = 2.0f;
flatflag = 1;
break;
default: // 64x64 lump
dflatsize = 64.0f;
flatflag = 63;
break;
}
UINT16 flatflag = R_GetFlatSize(len) - 1;
double dflatsize = (double)(flatflag + 1);
// 3--2
// | /|
@ -640,7 +592,6 @@ void HWR_DrawFlatFill (INT32 x, INT32 y, INT32 w, INT32 h, lumpnum_t flatlumpnum
v[0].z = v[1].z = v[2].z = v[3].z = 1.0f;
// flat is 64x64 lod and texture offsets are [0.0, 1.0]
v[0].s = v[3].s = (float)((x & flatflag)/dflatsize);
v[2].s = v[1].s = (float)(v[0].s + w/dflatsize);
v[0].t = v[1].t = (float)((y & flatflag)/dflatsize);

View file

@ -358,30 +358,39 @@ static FUINT HWR_CalcSlopeLight(FUINT lightnum, angle_t dir, fixed_t delta)
// -----------------+
static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, FBITFIELD PolyFlags, INT32 lightlevel, levelflat_t *levelflat, sector_t *FOFsector, UINT8 alpha, extracolormap_t *planecolormap)
{
polyvertex_t * pv;
float height; //constant y for all points on the convex flat polygon
FOutVector *v3d;
INT32 nrPlaneVerts; //verts original define of convex flat polygon
INT32 i;
float flatxref,flatyref;
float fflatwidth = 64.0f, fflatheight = 64.0f;
INT32 flatflag = 63;
boolean texflat = false;
float scrollx = 0.0f, scrolly = 0.0f, anglef = 0.0f;
angle_t angle = 0;
FSurfaceInfo Surf;
float tempxsow, tempytow;
FSurfaceInfo Surf;
FOutVector *v3d;
polyvertex_t *pv;
pslope_t *slope = NULL;
INT32 shader = SHADER_DEFAULT;
size_t nrPlaneVerts;
INT32 i;
float height; // constant y for all points on the convex flat polygon
float flatxref, flatyref, anglef = 0.0f;
float fflatwidth = 64.0f, fflatheight = 64.0f;
UINT16 flatflag = 63;
boolean texflat = false;
float tempxsow, tempytow;
float scrollx = 0.0f, scrolly = 0.0f;
angle_t angle = 0;
static FOutVector *planeVerts = NULL;
static UINT16 numAllocedPlaneVerts = 0;
INT32 shader = SHADER_DEFAULT;
// no convex poly were generated for this subsector
if (!xsub->planepoly)
return;
pv = xsub->planepoly->pts;
nrPlaneVerts = xsub->planepoly->numpts;
if (nrPlaneVerts < 3) // not even a triangle?
return;
// Get the slope pointer to simplify future code
if (FOFsector)
{
@ -404,12 +413,6 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool
height = FIXED_TO_FLOAT(fixedheight);
pv = xsub->planepoly->pts;
nrPlaneVerts = xsub->planepoly->numpts;
if (nrPlaneVerts < 3) //not even a triangle ?
return;
// Allocate plane-vertex buffer if we need to
if (!planeVerts || nrPlaneVerts > numAllocedPlaneVerts)
{
@ -424,43 +427,8 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool
if (levelflat->type == LEVELFLAT_FLAT)
{
size_t len = W_LumpLength(levelflat->u.flat.lumpnum);
switch (len)
{
case 4194304: // 2048x2048 lump
fflatwidth = fflatheight = 2048.0f;
break;
case 1048576: // 1024x1024 lump
fflatwidth = fflatheight = 1024.0f;
break;
case 262144:// 512x512 lump
fflatwidth = fflatheight = 512.0f;
break;
case 65536: // 256x256 lump
fflatwidth = fflatheight = 256.0f;
break;
case 16384: // 128x128 lump
fflatwidth = fflatheight = 128.0f;
break;
case 1024: // 32x32 lump
fflatwidth = fflatheight = 32.0f;
break;
case 256: // 16x16 lump
fflatwidth = fflatheight = 16.0f;
break;
case 64: // 8x8 lump
fflatwidth = fflatheight = 8.0f;
break;
case 16: // 4x4 lump
fflatwidth = fflatheight = 4.0f;
break;
case 4: // 2x2 lump
fflatwidth = fflatheight = 2.0f;
break;
default: // 64x64 lump
fflatwidth = fflatheight = 64.0f;
break;
}
flatflag = ((INT32)fflatwidth)-1;
flatflag = R_GetFlatSize(len) - 1;
fflatwidth = fflatheight = (float)(flatflag + 1);
}
else
{
@ -560,7 +528,7 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool
}\
}
for (i = 0, v3d = planeVerts; i < nrPlaneVerts; i++,v3d++,pv++)
for (i = 0, v3d = planeVerts; i < (INT32)nrPlaneVerts; i++,v3d++,pv++)
SETUP3DVERT(v3d, pv->x, pv->y);
if (slope)
@ -2721,13 +2689,13 @@ static void HWR_RenderPolyObjectPlane(polyobj_t *polysector, boolean isceiling,
float height = FIXED_TO_FLOAT(fixedheight); // constant y for all points on the convex flat polygon
float flatxref, flatyref;
float fflatwidth = 64.0f, fflatheight = 64.0f;
INT32 flatflag = 63;
UINT16 flatflag = 63;
boolean texflat = false;
float scrollx = 0.0f, scrolly = 0.0f;
float tempxsow, tempytow, anglef = 0.0f;
angle_t angle = 0;
fixed_t tempxs, tempyt;
static FOutVector *planeVerts = NULL;
static UINT16 numAllocedPlaneVerts = 0;
@ -2754,43 +2722,8 @@ static void HWR_RenderPolyObjectPlane(polyobj_t *polysector, boolean isceiling,
if (levelflat->type == LEVELFLAT_FLAT)
{
size_t len = W_LumpLength(levelflat->u.flat.lumpnum);
switch (len)
{
case 4194304: // 2048x2048 lump
fflatwidth = fflatheight = 2048.0f;
break;
case 1048576: // 1024x1024 lump
fflatwidth = fflatheight = 1024.0f;
break;
case 262144:// 512x512 lump
fflatwidth = fflatheight = 512.0f;
break;
case 65536: // 256x256 lump
fflatwidth = fflatheight = 256.0f;
break;
case 16384: // 128x128 lump
fflatwidth = fflatheight = 128.0f;
break;
case 1024: // 32x32 lump
fflatwidth = fflatheight = 32.0f;
break;
case 256: // 16x16 lump
fflatwidth = fflatheight = 16.0f;
break;
case 64: // 8x8 lump
fflatwidth = fflatheight = 8.0f;
break;
case 16: // 4x4 lump
fflatwidth = fflatheight = 4.0f;
break;
case 4: // 2x2 lump
fflatwidth = fflatheight = 2.0f;
break;
default: // 64x64 lump
fflatwidth = fflatheight = 64.0f;
break;
}
flatflag = ((INT32)fflatwidth)-1;
flatflag = R_GetFlatSize(len) - 1;
fflatwidth = fflatheight = (float)(flatflag + 1);
}
else
{
@ -2853,20 +2786,13 @@ static void HWR_RenderPolyObjectPlane(polyobj_t *polysector, boolean isceiling,
if (angle) // Only needs to be done if there's an altered angle
{
angle = (InvAngle(angle))>>ANGLETOFINESHIFT;
tempxsow = flatxref;
tempytow = flatyref;
// This needs to be done so that it scrolls in a different direction after rotation like software
/*tempxs = FLOAT_TO_FIXED(scrollx);
tempyt = FLOAT_TO_FIXED(scrolly);
scrollx = (FIXED_TO_FLOAT(FixedMul(tempxs, FINECOSINE(angle)) - FixedMul(tempyt, FINESINE(angle))));
scrolly = (FIXED_TO_FLOAT(FixedMul(tempxs, FINESINE(angle)) + FixedMul(tempyt, FINECOSINE(angle))));*/
anglef = ANG2RAD(InvAngle(angle));
// This needs to be done so everything aligns after rotation
// It would be done so that rotation is done, THEN the translation, but I couldn't get it to rotate AND scroll like software does
tempxs = FLOAT_TO_FIXED(flatxref);
tempyt = FLOAT_TO_FIXED(flatyref);
flatxref = (FIXED_TO_FLOAT(FixedMul(tempxs, FINECOSINE(angle)) - FixedMul(tempyt, FINESINE(angle))));
flatyref = (FIXED_TO_FLOAT(FixedMul(tempxs, FINESINE(angle)) + FixedMul(tempyt, FINECOSINE(angle))));
flatxref = (tempxsow * cos(anglef)) - (tempytow * sin(anglef));
flatyref = (tempxsow * sin(anglef)) + (tempytow * cos(anglef));
}
for (i = 0; i < (INT32)nrPlaneVerts; i++,v3d++)
@ -2887,10 +2813,11 @@ static void HWR_RenderPolyObjectPlane(polyobj_t *polysector, boolean isceiling,
// Need to rotate before translate
if (angle) // Only needs to be done if there's an altered angle
{
tempxs = FLOAT_TO_FIXED(v3d->s);
tempyt = FLOAT_TO_FIXED(v3d->t);
v3d->s = (FIXED_TO_FLOAT(FixedMul(tempxs, FINECOSINE(angle)) - FixedMul(tempyt, FINESINE(angle))));
v3d->t = (FIXED_TO_FLOAT(FixedMul(tempxs, FINESINE(angle)) + FixedMul(tempyt, FINECOSINE(angle))));
tempxsow = v3d->s;
tempytow = v3d->t;
v3d->s = (tempxsow * cos(anglef)) - (tempytow * sin(anglef));
v3d->t = (tempxsow * sin(anglef)) + (tempytow * cos(anglef));
}
v3d->x = FIXED_TO_FLOAT(polysector->vertices[i]->x);

View file

@ -870,7 +870,6 @@ void R_DrawSinglePlane(visplane_t *pl)
INT32 x;
INT32 stop, angle;
ffloor_t *rover;
INT32 type;
INT32 spanfunctype = BASEDRAWFUNC;
if (!(pl->minx <= pl->maxx))
@ -883,8 +882,8 @@ void R_DrawSinglePlane(visplane_t *pl)
return;
}
levelflat = &levelflats[pl->picnum];
planeripple.active = false;
spanfunc = spanfuncs[BASEDRAWFUNC];
if (pl->polyobj)
{
@ -995,29 +994,25 @@ void R_DrawSinglePlane(visplane_t *pl)
light = (pl->lightlevel >> LIGHTSEGSHIFT);
}
currentplane = pl;
ds_powersoftwo = false;
levelflat = &levelflats[pl->picnum];
/* :james: */
type = levelflat->type;
switch (type)
switch (levelflat->type)
{
case LEVELFLAT_NONE:
return;
case LEVELFLAT_FLAT:
ds_source = (UINT8 *)R_GetFlat(levelflat->u.flat.lumpnum);
R_CheckFlatLength(W_LumpLength(levelflat->u.flat.lumpnum));
ds_powersoftwo = true; // Raw flats always have dimensions that are powers-of-two numbers.
R_SetFlatVars(W_LumpLength(levelflat->u.flat.lumpnum));
ds_powersoftwo = true;
break;
default:
ds_source = (UINT8 *)R_GetLevelFlat(levelflat);
if (!ds_source)
return;
// Check if this texture or patch has power-of-two dimensions.
if (R_CheckPowersOfTwo())
else if (R_CheckPowersOfTwo())
{
R_CheckFlatLength(ds_flatwidth * ds_flatheight);
R_SetFlatVars(ds_flatwidth * ds_flatheight);
ds_powersoftwo = true;
}
}
@ -1091,7 +1086,7 @@ void R_DrawSinglePlane(visplane_t *pl)
planezlight = zlight[light];
}
// Use the correct span drawer depending on the powers-of-twoness
// Set the span drawer
if (!ds_powersoftwo)
{
if (spanfuncs_npo2[spanfunctype])
@ -1108,6 +1103,7 @@ void R_DrawSinglePlane(visplane_t *pl)
pl->bottom[pl->maxx+1] = 0x0000;
pl->bottom[pl->minx-1] = 0x0000;
currentplane = pl;
stop = pl->maxx + 1;
if (pl->slope)

View file

@ -416,7 +416,7 @@ static void R_RasterizeFloorSplat(floorsplat_t *pSplat, vector2_t *verts, visspr
if (R_CheckPowersOfTwo())
{
R_CheckFlatLength(ds_flatwidth * ds_flatheight);
R_SetFlatVars(ds_flatwidth * ds_flatheight);
ds_powersoftwo = true;
}

View file

@ -619,9 +619,7 @@ void *R_GetLevelFlat(levelflat_t *levelflat)
}
//
// R_CheckPowersOfTwo
//
// Checks if the flat's dimensions are powers of two.
// Checks if the current flat's dimensions are powers of two
//
boolean R_CheckPowersOfTwo(void)
{
@ -637,67 +635,67 @@ boolean R_CheckPowersOfTwo(void)
}
//
// R_CheckFlatLength
// Returns the flat size corresponding to the length of a lump
//
// Determine the flat's dimensions from its lump length.
//
void R_CheckFlatLength(size_t length)
UINT16 R_GetFlatSize(size_t length)
{
INT32 size, bits;
switch (length)
{
case 4194304: // 2048x2048 lump
size = 2048;
bits = 11;
break;
return 2048;
case 1048576: // 1024x1024 lump
size = 1024;
bits = 10;
break;
return 1024;
case 262144:// 512x512 lump
size = 512;
bits = 9;
break;
return 512;
case 65536: // 256x256 lump
size = 256;
bits = 8;
break;
return 256;
case 16384: // 128x128 lump
size = 128;
bits = 7;
break;
return 128;
case 1024: // 32x32 lump
size = 32;
bits = 5;
break;
return 32;
case 256: // 16x16 lump
size = 16;
bits = 4;
break;
return 16;
case 64: // 8x8 lump
size = 8;
bits = 3;
break;
return 8;
case 16: // 4x4 lump
size = 4;
bits = 2;
break;
return 4;
case 4: // 2x2 lump
size = 2;
bits = 1;
break;
return 2;
default: // 64x64 lump
size = 64;
bits = 6;
break;
return 64;
}
}
//
// Determines a flat's width bits from its size
//
UINT8 R_GetFlatBits(INT32 size)
{
switch (size)
{
case 2048: return 11;
case 1024: return 10;
case 512: return 9;
case 256: return 8;
case 128: return 7;
case 32: return 5;
case 16: return 4;
case 8: return 3;
case 4: return 2;
case 2: return 1;
default: return 6; // 64x64
}
}
void R_SetFlatVars(size_t length)
{
UINT16 size = R_GetFlatSize(length);
UINT8 bits = R_GetFlatBits(size);
nflatshiftup = 16 - bits;
nflatxshift = 16 + nflatshiftup;
nflatyshift = nflatxshift - bits;
nflatmask = (size - 1) * size;
ds_flatwidth = ds_flatheight = size;
}
//
@ -746,7 +744,7 @@ Rloadflats (INT32 i, INT32 w)
UINT16 wadnum = (UINT16)w;
lumpnum_t lumpnum = texstart + j;
size_t lumplength;
size_t flatsize = 0;
size_t flatsize;
if (wadfiles[w]->type == RET_PK3)
{
@ -756,43 +754,7 @@ Rloadflats (INT32 i, INT32 w)
flatlump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE);
lumplength = W_LumpLengthPwad(wadnum, lumpnum);
switch (lumplength)
{
case 4194304: // 2048x2048 lump
flatsize = 2048;
break;
case 1048576: // 1024x1024 lump
flatsize = 1024;
break;
case 262144:// 512x512 lump
flatsize = 512;
break;
case 65536: // 256x256 lump
flatsize = 256;
break;
case 16384: // 128x128 lump
flatsize = 128;
break;
case 1024: // 32x32 lump
flatsize = 32;
break;
case 256: // 16x16 lump
flatsize = 16;
break;
case 64: // 8x8 lump
flatsize = 8;
break;
case 16: // 4x4 lump
flatsize = 4;
break;
case 4: // 2x2 lump
flatsize = 2;
break;
default: // 64x64 lump
flatsize = 64;
break;
}
flatsize = R_GetFlatSize(lumplength);
//CONS_Printf("\n\"%s\" is a flat, dimensions %d x %d",W_CheckNameForNumPwad((UINT16)w,texstart+j),flatsize,flatsize);
texture = textures[i] = Z_Calloc(sizeof(texture_t) + sizeof(texpatch_t), PU_STATIC, NULL);

View file

@ -91,7 +91,9 @@ UINT8 *R_GetColumn(fixed_t tex, INT32 col);
void *R_GetFlat(lumpnum_t flatnum);
boolean R_CheckPowersOfTwo(void);
void R_CheckFlatLength(size_t length);
UINT16 R_GetFlatSize(size_t length);
UINT8 R_GetFlatBits(INT32 size);
void R_SetFlatVars(size_t length);
// Returns the texture number for the texture name.
INT32 R_TextureNumForName(const char *name);

View file

@ -1705,7 +1705,7 @@ void V_DrawFlatFill(INT32 x, INT32 y, INT32 w, INT32 h, lumpnum_t flatnum)
fixed_t dx, dy, xfrac, yfrac;
const UINT8 *src, *deststop;
UINT8 *flat, *dest;
size_t size, lflatsize, flatshift;
size_t lflatsize, flatshift;
#ifdef HWRENDER
if (rendermode == render_opengl)
@ -1715,55 +1715,8 @@ void V_DrawFlatFill(INT32 x, INT32 y, INT32 w, INT32 h, lumpnum_t flatnum)
}
#endif
size = W_LumpLength(flatnum);
switch (size)
{
case 4194304: // 2048x2048 lump
lflatsize = 2048;
flatshift = 11;
break;
case 1048576: // 1024x1024 lump
lflatsize = 1024;
flatshift = 10;
break;
case 262144:// 512x512 lump
lflatsize = 512;
flatshift = 9;
break;
case 65536: // 256x256 lump
lflatsize = 256;
flatshift = 8;
break;
case 16384: // 128x128 lump
lflatsize = 128;
flatshift = 7;
break;
case 1024: // 32x32 lump
lflatsize = 32;
flatshift = 5;
break;
case 256: // 16x16 lump
lflatsize = 16;
flatshift = 4;
break;
case 64: // 8x8 lump
lflatsize = 8;
flatshift = 3;
break;
case 16: // 4x4 lump
lflatsize = 4;
flatshift = 2;
break;
case 4: // 2x2 lump
lflatsize = 2;
flatshift = 1;
break;
default: // 64x64 lump
lflatsize = 64;
flatshift = 6;
break;
}
lflatsize = R_GetFlatSize(W_LumpLength(flatnum));
flatshift = R_GetFlatBits(lflatsize);
flat = W_CacheLumpNum(flatnum, PU_CACHE);