diff --git a/src/doom/r_draw.c b/src/doom/r_draw.c index 7da30861..8cecf06b 100644 --- a/src/doom/r_draw.c +++ b/src/doom/r_draw.c @@ -125,6 +125,7 @@ void R_DrawColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -144,7 +145,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; // [PN] Update frac with modulo to wrap around texture height frac = (frac + fracstep) % heightmask; @@ -160,7 +161,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -196,6 +197,7 @@ void R_DrawColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -214,8 +216,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac = (frac + fracstep) % heightmask; } } @@ -229,8 +231,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -307,13 +309,14 @@ void R_DrawFuzzColumn(void) const int *const fuzzoffsetbase = fuzzoffset; int local_fuzzpos = fuzzpos; const int fuzzalpha = fuzz_alpha; + const int screenwidth = SCREENWIDTH; // [PN] Use a for loop for clarity and potential optimizations { const int iterations = count + 1; // [PN] since do/while decrements count after use for (int i = 0; i < iterations; i++) { - const int fuzz_offset = SCREENWIDTH * fuzzoffsetbase[local_fuzzpos]; + const int fuzz_offset = screenwidth * fuzzoffsetbase[local_fuzzpos]; *dest = I_BlendDark(dest[fuzz_offset], fuzzalpha); @@ -324,14 +327,14 @@ void R_DrawFuzzColumn(void) local_fuzzpos = (realleveltime > oldleveltime) ? ID_Random() % 49 : 0; } - dest += SCREENWIDTH; + dest += screenwidth; } } // [PN] handle cutoff line if (cutoff) { - const int fuzz_offset = SCREENWIDTH * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; + const int fuzz_offset = screenwidth * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; *dest = I_BlendDark(dest[fuzz_offset], fuzzalpha); } @@ -375,13 +378,14 @@ void R_DrawFuzzColumnLow(void) const int *const fuzzoffsetbase = fuzzoffset; int local_fuzzpos = fuzzpos; const int fuzzalpha = fuzz_alpha; + const int screenwidth = SCREENWIDTH; // [PN] Use a for loop for clarity and potential optimizations { const int iterations = count + 1; for (int i = 0; i < iterations; i++) { - const int fuzz_offset = SCREENWIDTH * fuzzoffsetbase[local_fuzzpos]; + const int fuzz_offset = screenwidth * fuzzoffsetbase[local_fuzzpos]; *dest = I_BlendDark(dest[fuzz_offset], fuzzalpha); *dest2 = I_BlendDark(dest2[fuzz_offset], fuzzalpha); @@ -392,14 +396,14 @@ void R_DrawFuzzColumnLow(void) local_fuzzpos = (realleveltime > oldleveltime) ? ID_Random() % 49 : 0; } - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; } } if (cutoff) { - const int fuzz_offset = SCREENWIDTH * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; + const int fuzz_offset = screenwidth * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; *dest = I_BlendDark(dest[fuzz_offset], fuzzalpha); *dest2 = I_BlendDark(dest2[fuzz_offset], fuzzalpha); @@ -432,6 +436,7 @@ void R_DrawFuzzTLColumn(void) // [PN] Local pointers to speed up access const byte *const sourcebase = dc_source; const pixel_t *const colormap0 = dc_colormap[0]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -440,7 +445,7 @@ void R_DrawFuzzTLColumn(void) const pixel_t destrgb = colormap0[s]; *dest = I_BlendOver(*dest, destrgb, FUZZTL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -471,6 +476,7 @@ void R_DrawFuzzTLColumnLow(void) // [PN] Local pointers to speed up access const byte *const sourcebase = dc_source; const pixel_t *const colormap0 = dc_colormap[0]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -481,8 +487,8 @@ void R_DrawFuzzTLColumnLow(void) *dest = I_BlendOver(*dest, sourcecolor, FUZZTL_ALPHA); *dest2 = I_BlendOver(*dest2, sourcecolor, FUZZTL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -513,21 +519,22 @@ void R_DrawFuzzBWColumn(void) const int *const fuzzoffsetbase = fuzzoffset; int local_fuzzpos = fuzzpos; const int fuzzalpha = fuzz_alpha; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) { - const int fuzz_offset = SCREENWIDTH * fuzzoffsetbase[local_fuzzpos]; + const int fuzz_offset = screenwidth * fuzzoffsetbase[local_fuzzpos]; *dest = I_BlendDarkGrayscale(dest[fuzz_offset], fuzzalpha); local_fuzzpos = (local_fuzzpos + 1) % FUZZTABLE; - dest += SCREENWIDTH; + dest += screenwidth; } if (cutoff) { - const int fuzz_offset = SCREENWIDTH * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; + const int fuzz_offset = screenwidth * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; *dest = I_BlendDarkGrayscale(dest[fuzz_offset], fuzzalpha); } @@ -566,24 +573,25 @@ void R_DrawFuzzBWColumnLow(void) const int *const fuzzoffsetbase = fuzzoffset; int local_fuzzpos = fuzzpos; const int fuzzalpha = fuzz_alpha; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) { - const int fuzz_offset = SCREENWIDTH * fuzzoffsetbase[local_fuzzpos]; + const int fuzz_offset = screenwidth * fuzzoffsetbase[local_fuzzpos]; *dest = I_BlendDarkGrayscale(dest[fuzz_offset], fuzzalpha); *dest2 = I_BlendDarkGrayscale(dest2[fuzz_offset], fuzzalpha); local_fuzzpos = (local_fuzzpos + 1) % FUZZTABLE; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; } if (cutoff) { - const int fuzz_offset = SCREENWIDTH * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; + const int fuzz_offset = screenwidth * (fuzzoffsetbase[local_fuzzpos] - FUZZOFF) / 2; *dest = I_BlendDarkGrayscale(dest[fuzz_offset], fuzzalpha); *dest2 = I_BlendDarkGrayscale(dest2[fuzz_offset], fuzzalpha); @@ -617,6 +625,7 @@ void R_DrawTransTLFuzzColumn(void) const byte *const sourcebase = dc_source; const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -627,7 +636,7 @@ void R_DrawTransTLFuzzColumn(void) *dest = I_BlendOver(*dest, destrgb, FUZZTL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -660,6 +669,7 @@ void R_DrawTransTLFuzzColumnLow(void) const byte *const sourcebase = dc_source; const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -670,8 +680,8 @@ void R_DrawTransTLFuzzColumnLow(void) *dest = I_BlendOver(*dest, destrgb, FUZZTL_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, FUZZTL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -707,6 +717,7 @@ void R_DrawTranslatedColumn(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -716,7 +727,7 @@ void R_DrawTranslatedColumn(void) const unsigned index = (brightmap[s] ? colormap1[t] : colormap0[t]); *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -745,6 +756,7 @@ void R_DrawTranslatedColumnLow(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -756,8 +768,8 @@ void R_DrawTranslatedColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -787,6 +799,7 @@ void R_DrawTLColumn (void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -795,7 +808,7 @@ void R_DrawTLColumn (void) *dest = I_BlendOver(*dest, destrgb, TRANMAP_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -829,6 +842,7 @@ void R_DrawTLColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -838,8 +852,8 @@ void R_DrawTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, TRANMAP_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, TRANMAP_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -868,6 +882,7 @@ void R_DrawTLAddColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -877,7 +892,7 @@ void R_DrawTLAddColumn(void) *dest = I_BlendAdd(*dest, destrgb); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -910,6 +925,7 @@ void R_DrawTLAddColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -920,8 +936,8 @@ void R_DrawTLAddColumnLow(void) *dest1 = I_BlendAdd(*dest1, destrgb); *dest2 = I_BlendAdd(*dest2, destrgb); - dest1 += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest1 += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -1010,6 +1026,8 @@ void R_DrawSpan(void) const byte *const brightmap = ds_brightmap; const pixel_t *const colormap0 = ds_colormap[0]; const pixel_t *const colormap1 = ds_colormap[1]; + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; if (!gp_flip_levels) { @@ -1028,8 +1046,8 @@ void R_DrawSpan(void) const byte source = sourcebase[spot]; dest[j] = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } dest += 4; @@ -1046,8 +1064,8 @@ void R_DrawSpan(void) *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); dest++; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -1063,8 +1081,8 @@ void R_DrawSpan(void) pixel_t *dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } @@ -1092,6 +1110,8 @@ void R_DrawSpanLow(void) const byte *const brightmap = ds_brightmap; const pixel_t *const colormap0 = ds_colormap[0]; const pixel_t *const colormap1 = ds_colormap[1]; + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; if (!gp_flip_levels) { @@ -1112,8 +1132,8 @@ void R_DrawSpanLow(void) dest[1] = (brightmap[source] ? colormap1[source] : colormap0[source]); dest += 2; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } count -= 4; @@ -1131,8 +1151,8 @@ void R_DrawSpanLow(void) dest[1] = (brightmap[source] ? colormap1[source] : colormap0[source]); dest += 2; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -1152,8 +1172,8 @@ void R_DrawSpanLow(void) dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } diff --git a/src/doom/r_main.c b/src/doom/r_main.c index fa027b7c..6a1f7111 100644 --- a/src/doom/r_main.c +++ b/src/doom/r_main.c @@ -239,90 +239,55 @@ int R_PointOnSegSide (fixed_t x, fixed_t y, const seg_t *line) // [crispy] turned into a general R_PointToAngle() flavor // called with either slope_div = SlopeDivCrispy() from R_PointToAngleCrispy() // or slope_div = SlopeDiv() else +// [PN] Reformatted for readability and reduced nesting angle_t R_PointToAngleSlope ( fixed_t x, fixed_t y, int (*slope_div) (unsigned int num, unsigned int den)) -{ +{ + // [PN] Shift to local player coordinates x -= viewx; y -= viewy; - - if ( (!x) && (!y) ) - return 0; - if (x>= 0) + // [PN] If the point matches the player's position + if (!x && !y) + return 0; + + if (x >= 0) { - // x >=0 - if (y>= 0) - { - // y>= 0 - - if (x>y) - { - // octant 0 - return tantoangle[slope_div(y,x)]; - } - else - { - // octant 1 - return ANG90-1-tantoangle[slope_div(x,y)]; - } - } - else - { - // y<0 - y = -y; - - if (x>y) - { - // octant 8 - return 0-tantoangle[slope_div(y,x)]; - } - else - { - // octant 7 - return ANG270+tantoangle[slope_div(x,y)]; - } - } + if (y >= 0) + { + // [PN] First quadrant → octants 0 or 1 + return (x > y) + ? tantoangle[slope_div(y, x)] + : (ANG90 - 1 - tantoangle[slope_div(x, y)]); + } + else + { + // [PN] Fourth quadrant → octants 8 or 7 + return (x > -y) + ? (0 - tantoangle[slope_div(-y, x)]) + : (ANG270 + tantoangle[slope_div(x, -y)]); + } } else { - // x<0 - x = -x; - - if (y>= 0) - { - // y>= 0 - if (x>y) - { - // octant 3 - return ANG180-1-tantoangle[slope_div(y,x)]; - } - else - { - // octant 2 - return ANG90+ tantoangle[slope_div(x,y)]; - } - } - else - { - // y<0 - y = -y; - - if (x>y) - { - // octant 4 - return ANG180+tantoangle[slope_div(y,x)]; - } - else - { - // octant 5 - return ANG270-1-tantoangle[slope_div(x,y)]; - } - } + if (y >= 0) + { + // [PN] Second quadrant → octants 3 or 2 + return (-x > y) + ? (ANG180 - 1 - tantoangle[slope_div(y, -x)]) + : (ANG90 + tantoangle[slope_div(-x, y)]); + } + else + { + // [PN] Third quadrant → octants 4 or 5 + return (-x > -y) + ? (ANG180 + tantoangle[slope_div(-y, -x)]) + : (ANG270 - 1 - tantoangle[slope_div(-x, -y)]); + } } - return 0; } angle_t @@ -341,8 +306,8 @@ R_PointToAngleCrispy fixed_t y ) { // [crispy] fix overflows for very long distances - int64_t y_viewy = (int64_t)y - viewy; - int64_t x_viewx = (int64_t)x - viewx; + const int64_t y_viewy = (int64_t)y - viewy; + const int64_t x_viewx = (int64_t)x - viewx; // [crispy] the worst that could happen is e.g. INT_MIN-INT_MAX = 2*INT_MIN if (x_viewx < INT_MIN || x_viewx > INT_MAX || diff --git a/src/doom/r_plane.c b/src/doom/r_plane.c index a52e2c82..67adb2b2 100644 --- a/src/doom/r_plane.c +++ b/src/doom/r_plane.c @@ -133,7 +133,7 @@ R_MapPlane // angle_t angle; fixed_t distance; // fixed_t length; - unsigned index; +// unsigned index; int dx, dy; #ifdef RANGECHECK @@ -188,7 +188,7 @@ R_MapPlane ds_colormap[0] = ds_colormap[1] = fixedcolormap; else { - index = distance >> LIGHTZSHIFT; + unsigned int index = distance >> LIGHTZSHIFT; if (index >= MAXLIGHTZ ) index = MAXLIGHTZ-1; @@ -476,7 +476,6 @@ void R_DrawPlanes (void) } else // regular flat { - int light = (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); const boolean swirling = (flattranslation[pl->picnum] == -1); const int stop = pl->maxx + 1; const int lumpnum = firstflat + (swirling ? pl->picnum : flattranslation[pl->picnum]); @@ -499,7 +498,7 @@ void R_DrawPlanes (void) planeheight = abs(pl->height-viewz); // [PN] Ensure 'light' is within the range [0, LIGHTLEVELS - 1] inclusively. - light = BETWEEN(0, LIGHTLEVELS-1, light); + const int light = BETWEEN(0, LIGHTLEVELS-1, (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT)); planezlight = zlight[light]; pl->top[pl->minx-1] = pl->top[stop] = USHRT_MAX; diff --git a/src/doom/r_segs.c b/src/doom/r_segs.c index a2ec0cbe..aaab141c 100644 --- a/src/doom/r_segs.c +++ b/src/doom/r_segs.c @@ -178,20 +178,12 @@ void R_FixWiggle (sector_t *sector) } } -// +// ----------------------------------------------------------------------------- // R_RenderMaskedSegRange -// -void -R_RenderMaskedSegRange -( drawseg_t* ds, - int x1, - int x2 ) +// ----------------------------------------------------------------------------- + +void R_RenderMaskedSegRange (drawseg_t *ds, int x1, int x2) { - unsigned index; - column_t* col; - int lightnum; - int texnum; - // Calculate light table. // Use different light tables // for horizontal / vertical / diagonal. Diagonal? @@ -199,101 +191,91 @@ R_RenderMaskedSegRange curline = ds->curline; frontsector = curline->frontsector; backsector = curline->backsector; - texnum = texturetranslation[curline->sidedef->midtexture]; - - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT)+(extralight * LIGHTBRIGHT); + const int texnum = texturetranslation[curline->sidedef->midtexture]; - // [crispy] smoother fake contrast - lightnum += curline->fakecontrast; + // [crispy] smooth diminishing lighting and smoother fake contrast + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT) + + curline->fakecontrast; - if (lightnum < 0) - walllights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - walllights = scalelight[LIGHTLEVELS-1]; - else - walllights = scalelight[lightnum]; + walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; maskedtexturecol = ds->maskedtexturecol; - rw_scalestep = ds->scalestep; spryscale = ds->scale1 + (x1 - ds->x1)*rw_scalestep; mfloorclip = ds->sprbottomclip; mceilingclip = ds->sprtopclip; - + // find positioning if (curline->linedef->flags & ML_DONTPEGBOTTOM) { - dc_texturemid = frontsector->interpfloorheight > backsector->interpfloorheight - ? frontsector->interpfloorheight : backsector->interpfloorheight; - dc_texturemid = dc_texturemid + textureheight[texnum] - viewz; + dc_texturemid = frontsector->interpfloorheight > backsector->interpfloorheight + ? frontsector->interpfloorheight : backsector->interpfloorheight; + dc_texturemid = dc_texturemid + textureheight[texnum] - viewz; } else { - dc_texturemid =frontsector->interpceilingheightinterpceilingheight - ? frontsector->interpceilingheight : backsector->interpceilingheight; - dc_texturemid = dc_texturemid - viewz; + dc_texturemid =frontsector->interpceilingheight < backsector->interpceilingheight + ? frontsector->interpceilingheight : backsector->interpceilingheight; + dc_texturemid = dc_texturemid - viewz; } dc_texturemid += curline->sidedef->rowoffset; - + if (fixedcolormap) - dc_colormap[0] = dc_colormap[1] = fixedcolormap; - + dc_colormap[0] = dc_colormap[1] = fixedcolormap; + // draw the columns for (dc_x = x1 ; dc_x <= x2 ; dc_x++) { - // calculate lighting - if (maskedtexturecol[dc_x] != INT_MAX) // [JN] 32-bit integer math - { - if (!fixedcolormap) - { - index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; - - if (index >= MAXLIGHTSCALE ) - index = MAXLIGHTSCALE-1; - - // [crispy] brightmaps for mid-textures - dc_brightmap = texturebrightmap[texnum]; - dc_colormap[0] = walllights[index]; - dc_colormap[1] = vis_brightmaps ? colormaps : dc_colormap[0]; - } - - // [crispy] apply Killough's int64 sprtopscreen overflow fix - // from winmbf/Source/r_segs.c:174-191 - // killough 3/2/98: - // - // This calculation used to overflow and cause crashes in Doom: - // - // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); - // - // This code fixes it, by using double-precision intermediate - // arithmetic and by skipping the drawing of 2s normals whose - // mapping to screen coordinates is totally out of range: - - { - int64_t t = ((int64_t) centeryfrac << FRACBITS) - - (int64_t) dc_texturemid * spryscale; - - if (t + (int64_t) textureheight[texnum] * spryscale < 0 || - t > (int64_t) SCREENHEIGHT << FRACBITS*2) - { - spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator - continue; // skip if the texture is out of screen's range - } - - sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix - } - - dc_iscale = UINT_MAX / (unsigned)spryscale; - - // draw the texture - col = (column_t *)((byte *)R_GetColumnMod(texnum,maskedtexturecol[dc_x]) -3); - - R_DrawMaskedColumn (col); - maskedtexturecol[dc_x] = INT_MAX; // [JN] 32-bit integer math - } - spryscale += rw_scalestep; + // calculate lighting + if (maskedtexturecol[dc_x] != INT_MAX) // [JN] 32-bit integer math + { + if (!fixedcolormap) + { + unsigned const int index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; + + // [crispy] brightmaps for mid-textures + dc_brightmap = texturebrightmap[texnum]; + dc_colormap[0] = walllights[MIN(index, MAXLIGHTSCALE-1)]; + dc_colormap[1] = vis_brightmaps ? colormaps : dc_colormap[0]; + } + + // [crispy] apply Killough's int64 sprtopscreen overflow fix + // from winmbf/Source/r_segs.c:174-191 + // killough 3/2/98: + // + // This calculation used to overflow and cause crashes in Doom: + // + // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); + // + // This code fixes it, by using double-precision intermediate + // arithmetic and by skipping the drawing of 2s normals whose + // mapping to screen coordinates is totally out of range: + { + int64_t t = ((int64_t) centeryfrac << FRACBITS) + - (int64_t) dc_texturemid * spryscale; + + if (t + (int64_t) textureheight[texnum] * spryscale < 0 + || t > (int64_t) SCREENHEIGHT << FRACBITS * 2) + { + spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator + continue; // skip if the texture is out of screen's range + } + + sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix + } + + dc_iscale = UINT_MAX / (unsigned)spryscale; + + // draw the texture + column_t *col = (column_t *)((byte *)R_GetColumnMod(texnum,maskedtexturecol[dc_x]) -3); + + R_DrawMaskedColumn (col); + maskedtexturecol[dc_x] = INT_MAX; // [JN] 32-bit integer math + } + + spryscale += rw_scalestep; } - } // ----------------------------------------------------------------------------- @@ -523,21 +505,14 @@ fixed_t R_ScaleFromGlobalAngle (angle_t visangle) return scale; } -// +// ----------------------------------------------------------------------------- // R_StoreWallRange // A wall segment will be drawn // between start and stop pixels (inclusive). -// -void -R_StoreWallRange -( int start, - int stop) -{ - fixed_t vtop; - int lightnum; - int64_t dx, dy, dx1, dy1, dist; // [crispy] fix long wall wobble - const uint32_t len = curline->length; +// ----------------------------------------------------------------------------- +void R_StoreWallRange (int start, int stop) +{ IDRender.numsegs++; // [crispy] remove MAXDRAWSEGS Vanilla limit @@ -573,11 +548,12 @@ R_StoreWallRange // thank you very much Linguica, Andrey Budko and kb1 // http://www.doomworld.com/vb/post/1340718 // shift right to avoid possibility of int64 overflow in rw_distance calculation - dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; - dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; - dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; - dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; - dist = ((dy * dx1 - dx * dy1) / len) << 1; + const uint32_t len = curline->length; + const int64_t dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; + const int64_t dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; + const int64_t dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; + const int64_t dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; + const int64_t dist = ((dy * dx1 - dx * dy1) / len) << 1; rw_distance = (fixed_t)BETWEEN(INT_MIN, INT_MAX, dist); ds_p->x1 = rw_x = start; @@ -653,8 +629,8 @@ R_StoreWallRange markfloor = markceiling = true; if (linedef->flags & ML_DONTPEGBOTTOM) { - vtop = frontsector->interpfloorheight + - textureheight[sidedef->midtexture]; + const fixed_t vtop = frontsector->interpfloorheight + + textureheight[sidedef->midtexture]; // bottom of texture at bottom rw_midtexturemid = vtop - viewz; } @@ -772,7 +748,8 @@ R_StoreWallRange } else { - vtop = backsector->interpceilingheight + textureheight[sidedef->toptexture]; + const fixed_t vtop = backsector->interpceilingheight + + textureheight[sidedef->toptexture]; // bottom of texture rw_toptexturemid = vtop - viewz; @@ -821,10 +798,10 @@ R_StoreWallRange // OPTIMIZE: get rid of LIGHTSEGSHIFT globally if (!fixedcolormap) { - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); - - // [crispy] smoother fake contrast - lightnum += curline->fakecontrast; + // [crispy] smooth diminishing lighting and smoother fake contrast + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT) + + curline->fakecontrast; walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; } diff --git a/src/doom/r_things.c b/src/doom/r_things.c index c98fca5a..940313d1 100644 --- a/src/doom/r_things.c +++ b/src/doom/r_things.c @@ -876,30 +876,21 @@ static void R_ProjectSprite (mobj_t* thing) } } - - - -// +// ----------------------------------------------------------------------------- // R_AddSprites // During BSP traversal, this adds sprites by sector. -// -void R_AddSprites (sector_t* sec) -{ - mobj_t* thing; - int lightnum; - - lightnum = (sec->lightlevel >> LIGHTSEGSHIFT)+(extralight * LIGHTBRIGHT); +// ----------------------------------------------------------------------------- - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS-1]; - else - spritelights = scalelight[lightnum]; +void R_AddSprites (sector_t *sec) +{ + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (sec->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; // Handle all things in sector. - for (thing = sec->thinglist ; thing ; thing = thing->snext) - R_ProjectSprite (thing); + for (mobj_t *thing = sec->thinglist ; thing ; thing = thing->snext) + R_ProjectSprite (thing); } // ----------------------------------------------------------------------------- @@ -1117,44 +1108,33 @@ static void R_DrawPSprite (pspdef_t* psp) R_DrawVisSprite (vis); } - - -// +// ----------------------------------------------------------------------------- // R_DrawPlayerSprites -// +// ----------------------------------------------------------------------------- + static void R_DrawPlayerSprites (void) { - int i; - int lightnum; - pspdef_t* psp; - - // Do not draw player gun sprite if spectating + // RestlessRodent -- Do not draw player gun sprite if spectating if (crl_spectating) - return; - + return; + // get light level - lightnum = - (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) - +(extralight * LIGHTBRIGHT); - - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS-1]; - else - spritelights = scalelight[lightnum]; - + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; + // clip to screen bounds mfloorclip = screenheightarray; mceilingclip = negonearray; - + // add all active psprites - for (i=0, psp=viewplayer->psprites; - ipsprites; i < NUMPSPRITES; i++, psp++) { - if (psp->state) - R_DrawPSprite (psp); + if (psp->state) + R_DrawPSprite(psp); } } diff --git a/src/heretic/r_draw.c b/src/heretic/r_draw.c index 244ecefd..af45029b 100644 --- a/src/heretic/r_draw.c +++ b/src/heretic/r_draw.c @@ -100,6 +100,7 @@ void R_DrawColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -119,7 +120,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; // [PN] Update frac with modulo to wrap around texture height frac = (frac + fracstep) % heightmask; @@ -135,7 +136,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -171,6 +172,7 @@ void R_DrawColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -189,8 +191,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac = (frac + fracstep) % heightmask; } } @@ -204,8 +206,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -236,6 +238,7 @@ void R_DrawTLColumn (void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -244,7 +247,7 @@ void R_DrawTLColumn (void) *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -277,6 +280,7 @@ void R_DrawTLColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -286,8 +290,8 @@ void R_DrawTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, TINTTAB_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -316,6 +320,7 @@ void R_DrawTLAddColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -325,7 +330,7 @@ void R_DrawTLAddColumn(void) *dest = I_BlendAdd(*dest, destrgb); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -358,6 +363,7 @@ void R_DrawTLAddColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -368,8 +374,8 @@ void R_DrawTLAddColumnLow(void) *dest1 = I_BlendAdd(*dest1, destrgb); *dest2 = I_BlendAdd(*dest2, destrgb); - dest1 += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest1 += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -408,6 +414,7 @@ void R_DrawTranslatedColumn(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -417,7 +424,7 @@ void R_DrawTranslatedColumn(void) const unsigned index = (brightmap[s] ? colormap1[t] : colormap0[t]); *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -446,6 +453,7 @@ void R_DrawTranslatedColumnLow(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -457,8 +465,8 @@ void R_DrawTranslatedColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -490,6 +498,7 @@ void R_DrawTranslatedTLColumn(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -500,7 +509,7 @@ void R_DrawTranslatedTLColumn(void) const pixel_t destrgb = (brightmap[t] ? colormap1[t] : colormap0[t]); *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -529,6 +538,7 @@ void R_DrawTranslatedTLColumnLow(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -539,8 +549,8 @@ void R_DrawTranslatedTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, TINTTAB_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -569,6 +579,7 @@ void R_DrawExtraTLColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int heightmask = dc_texheight - 1; @@ -595,7 +606,7 @@ void R_DrawExtraTLColumn(void) const pixel_t destrgb = (brightmap[s] ? colormap1[s] : colormap0[s]); *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; if (frac >= fullmask) frac -= fullmask; @@ -610,7 +621,7 @@ void R_DrawExtraTLColumn(void) const pixel_t destrgb = (brightmap[s] ? colormap1[s] : colormap0[s]); *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -639,6 +650,7 @@ void R_DrawExtraTLColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int heightmask = dc_texheight - 1; @@ -664,8 +676,8 @@ void R_DrawExtraTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; if (frac >= fullmask) @@ -682,8 +694,8 @@ void R_DrawExtraTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -754,6 +766,9 @@ void R_DrawSpan(void) const byte *const brightmap = ds_brightmap; const pixel_t *const colormap0 = ds_colormap[0]; const pixel_t *const colormap1 = ds_colormap[1]; + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; + if (!gp_flip_levels) { @@ -772,8 +787,8 @@ void R_DrawSpan(void) const byte source = sourcebase[spot]; dest[j] = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } dest += 4; @@ -790,8 +805,8 @@ void R_DrawSpan(void) *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); dest++; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -807,8 +822,8 @@ void R_DrawSpan(void) pixel_t *dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } @@ -836,6 +851,8 @@ void R_DrawSpanLow(void) const byte *const brightmap = ds_brightmap; const pixel_t *const colormap0 = ds_colormap[0]; const pixel_t *const colormap1 = ds_colormap[1]; + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; if (!gp_flip_levels) { @@ -856,8 +873,8 @@ void R_DrawSpanLow(void) dest[1] = (brightmap[source] ? colormap1[source] : colormap0[source]); dest += 2; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } count -= 4; @@ -875,8 +892,8 @@ void R_DrawSpanLow(void) dest[1] = (brightmap[source] ? colormap1[source] : colormap0[source]); dest += 2; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -896,8 +913,8 @@ void R_DrawSpanLow(void) dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = (brightmap[source] ? colormap1[source] : colormap0[source]); - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } diff --git a/src/heretic/r_main.c b/src/heretic/r_main.c index f4f1d7e4..30740525 100644 --- a/src/heretic/r_main.c +++ b/src/heretic/r_main.c @@ -225,90 +225,55 @@ int R_PointOnSegSide (fixed_t x, fixed_t y, const seg_t *line) // [crispy] turned into a general R_PointToAngle() flavor // called with either slope_div = SlopeDivCrispy() from R_PointToAngleCrispy() // or slope_div = SlopeDiv() else +// [PN] Reformatted for readability and reduced nesting angle_t R_PointToAngleSlope ( fixed_t x, fixed_t y, int (*slope_div) (unsigned int num, unsigned int den)) -{ +{ + // [PN] Shift to local player coordinates x -= viewx; y -= viewy; - - if ( (!x) && (!y) ) - return 0; - if (x>= 0) + // [PN] If the point matches the player's position + if (!x && !y) + return 0; + + if (x >= 0) { - // x >=0 - if (y>= 0) - { - // y>= 0 - - if (x>y) - { - // octant 0 - return tantoangle[slope_div(y,x)]; - } - else - { - // octant 1 - return ANG90-1-tantoangle[slope_div(x,y)]; - } - } - else - { - // y<0 - y = -y; - - if (x>y) - { - // octant 8 - return 0-tantoangle[slope_div(y,x)]; - } - else - { - // octant 7 - return ANG270+tantoangle[slope_div(x,y)]; - } - } + if (y >= 0) + { + // [PN] First quadrant → octants 0 or 1 + return (x > y) + ? tantoangle[slope_div(y, x)] + : (ANG90 - 1 - tantoangle[slope_div(x, y)]); + } + else + { + // [PN] Fourth quadrant → octants 8 or 7 + return (x > -y) + ? (0 - tantoangle[slope_div(-y, x)]) + : (ANG270 + tantoangle[slope_div(x, -y)]); + } } else { - // x<0 - x = -x; - - if (y>= 0) - { - // y>= 0 - if (x>y) - { - // octant 3 - return ANG180-1-tantoangle[slope_div(y,x)]; - } - else - { - // octant 2 - return ANG90+ tantoangle[slope_div(x,y)]; - } - } - else - { - // y<0 - y = -y; - - if (x>y) - { - // octant 4 - return ANG180+tantoangle[slope_div(y,x)]; - } - else - { - // octant 5 - return ANG270-1-tantoangle[slope_div(x,y)]; - } - } + if (y >= 0) + { + // [PN] Second quadrant → octants 3 or 2 + return (-x > y) + ? (ANG180 - 1 - tantoangle[slope_div(y, -x)]) + : (ANG90 + tantoangle[slope_div(-x, y)]); + } + else + { + // [PN] Third quadrant → octants 4 or 5 + return (-x > -y) + ? (ANG180 + tantoangle[slope_div(-y, -x)]) + : (ANG270 - 1 - tantoangle[slope_div(-x, -y)]); + } } - return 0; } angle_t diff --git a/src/heretic/r_plane.c b/src/heretic/r_plane.c index c0c3f38f..d0cdece2 100644 --- a/src/heretic/r_plane.c +++ b/src/heretic/r_plane.c @@ -147,7 +147,7 @@ R_MapPlane // angle_t angle; fixed_t distance; // fixed_t length; - unsigned index; +// unsigned index; int dx, dy; #ifdef RANGECHECK @@ -206,7 +206,7 @@ R_MapPlane ds_colormap[0] = ds_colormap[1] = fixedcolormap; else { - index = distance >> LIGHTZSHIFT; + unsigned int index = distance >> LIGHTZSHIFT; if (index >= MAXLIGHTZ ) index = MAXLIGHTZ-1; @@ -633,7 +633,6 @@ void R_DrawPlanes (void) } else // regular flat { - int light = (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); const boolean swirling = (flattranslation[pl->picnum] == -1); const int stop = pl->maxx + 1; const int lumpnum = firstflat + (swirling ? pl->picnum : flattranslation[pl->picnum]); @@ -726,7 +725,7 @@ void R_DrawPlanes (void) planeheight = abs(pl->height-viewz); // [PN] Ensure 'light' is within the range [0, LIGHTLEVELS - 1] inclusively. - light = BETWEEN(0, LIGHTLEVELS-1, light); + const int light = BETWEEN(0, LIGHTLEVELS-1, (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT)); planezlight = zlight[light]; pl->top[pl->minx-1] = pl->top[stop] = USHRT_MAX; diff --git a/src/heretic/r_segs.c b/src/heretic/r_segs.c index b8871ad5..e5bf3bf8 100644 --- a/src/heretic/r_segs.c +++ b/src/heretic/r_segs.c @@ -175,20 +175,12 @@ void R_FixWiggle (sector_t *sector) } } -// +// ----------------------------------------------------------------------------- // R_RenderMaskedSegRange -// -void -R_RenderMaskedSegRange -( drawseg_t* ds, - int x1, - int x2 ) +// ----------------------------------------------------------------------------- + +void R_RenderMaskedSegRange (drawseg_t *ds, int x1, int x2) { - unsigned index; - column_t* col; - int lightnum; - int texnum; - // Calculate light table. // Use different light tables // for horizontal / vertical / diagonal. Diagonal? @@ -196,101 +188,91 @@ R_RenderMaskedSegRange curline = ds->curline; frontsector = curline->frontsector; backsector = curline->backsector; - texnum = texturetranslation[curline->sidedef->midtexture]; - - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); + const int texnum = texturetranslation[curline->sidedef->midtexture]; - // [crispy] smoother fake contrast - lightnum += curline->fakecontrast; + // [crispy] smooth diminishing lighting and smoother fake contrast + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT) + + curline->fakecontrast; - if (lightnum < 0) - walllights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - walllights = scalelight[LIGHTLEVELS-1]; - else - walllights = scalelight[lightnum]; + walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; maskedtexturecol = ds->maskedtexturecol; - rw_scalestep = ds->scalestep; spryscale = ds->scale1 + (x1 - ds->x1)*rw_scalestep; mfloorclip = ds->sprbottomclip; mceilingclip = ds->sprtopclip; - + // find positioning if (curline->linedef->flags & ML_DONTPEGBOTTOM) { - dc_texturemid = frontsector->interpfloorheight > backsector->interpfloorheight - ? frontsector->interpfloorheight : backsector->interpfloorheight; - dc_texturemid = dc_texturemid + textureheight[texnum] - viewz; + dc_texturemid = frontsector->interpfloorheight > backsector->interpfloorheight + ? frontsector->interpfloorheight : backsector->interpfloorheight; + dc_texturemid = dc_texturemid + textureheight[texnum] - viewz; } else { - dc_texturemid =frontsector->interpceilingheightinterpceilingheight - ? frontsector->interpceilingheight : backsector->interpceilingheight; - dc_texturemid = dc_texturemid - viewz; + dc_texturemid =frontsector->interpceilingheight < backsector->interpceilingheight + ? frontsector->interpceilingheight : backsector->interpceilingheight; + dc_texturemid = dc_texturemid - viewz; } dc_texturemid += curline->sidedef->rowoffset; - + if (fixedcolormap) - dc_colormap[0] = dc_colormap[1] = fixedcolormap; - + dc_colormap[0] = dc_colormap[1] = fixedcolormap; + // draw the columns for (dc_x = x1 ; dc_x <= x2 ; dc_x++) { - // calculate lighting - if (maskedtexturecol[dc_x] != INT_MAX) // [JN] 32-bit integer math - { - if (!fixedcolormap) - { - index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; - - if (index >= MAXLIGHTSCALE ) - index = MAXLIGHTSCALE-1; - - // [crispy] brightmaps for mid-textures - dc_brightmap = texturebrightmap[texnum]; - dc_colormap[0] = walllights[index]; - dc_colormap[1] = vis_brightmaps ? colormaps : dc_colormap[0]; - } - - // [crispy] apply Killough's int64 sprtopscreen overflow fix - // from winmbf/Source/r_segs.c:174-191 - // killough 3/2/98: - // - // This calculation used to overflow and cause crashes in Doom: - // - // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); - // - // This code fixes it, by using double-precision intermediate - // arithmetic and by skipping the drawing of 2s normals whose - // mapping to screen coordinates is totally out of range: - - { - int64_t t = ((int64_t) centeryfrac << FRACBITS) - - (int64_t) dc_texturemid * spryscale; - - if (t + (int64_t) textureheight[texnum] * spryscale < 0 || - t > (int64_t) SCREENHEIGHT << FRACBITS*2) - { - spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator - continue; // skip if the texture is out of screen's range - } - - sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix - } - - dc_iscale = UINT_MAX / (unsigned)spryscale; - - // draw the texture - col = (column_t *)((byte *)R_GetColumn(texnum,maskedtexturecol[dc_x]) -3); - - R_DrawMaskedColumn (col, -1); - maskedtexturecol[dc_x] = INT_MAX; // [JN] 32-bit integer math - } - spryscale += rw_scalestep; + // calculate lighting + if (maskedtexturecol[dc_x] != INT_MAX) // [JN] 32-bit integer math + { + if (!fixedcolormap) + { + unsigned const int index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; + + // [crispy] brightmaps for mid-textures + dc_brightmap = texturebrightmap[texnum]; + dc_colormap[0] = walllights[MIN(index, MAXLIGHTSCALE-1)]; + dc_colormap[1] = vis_brightmaps ? colormaps : dc_colormap[0]; + } + + // [crispy] apply Killough's int64 sprtopscreen overflow fix + // from winmbf/Source/r_segs.c:174-191 + // killough 3/2/98: + // + // This calculation used to overflow and cause crashes in Doom: + // + // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); + // + // This code fixes it, by using double-precision intermediate + // arithmetic and by skipping the drawing of 2s normals whose + // mapping to screen coordinates is totally out of range: + { + int64_t t = ((int64_t) centeryfrac << FRACBITS) + - (int64_t) dc_texturemid * spryscale; + + if (t + (int64_t) textureheight[texnum] * spryscale < 0 + || t > (int64_t) SCREENHEIGHT << FRACBITS * 2) + { + spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator + continue; // skip if the texture is out of screen's range + } + + sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix + } + + dc_iscale = UINT_MAX / (unsigned)spryscale; + + // draw the texture + column_t *col = (column_t *)((byte *)R_GetColumn(texnum,maskedtexturecol[dc_x]) -3); + + R_DrawMaskedColumn (col, -1); + maskedtexturecol[dc_x] = INT_MAX; // [JN] 32-bit integer math + } + + spryscale += rw_scalestep; } - } // ----------------------------------------------------------------------------- @@ -512,21 +494,14 @@ fixed_t R_ScaleFromGlobalAngle (angle_t visangle) return scale; } -// +// ----------------------------------------------------------------------------- // R_StoreWallRange // A wall segment will be drawn // between start and stop pixels (inclusive). -// -void -R_StoreWallRange -( int start, - int stop) -{ - fixed_t vtop; - int lightnum; - int64_t dx, dy, dx1, dy1, dist; // [crispy] fix long wall wobble - const uint32_t len = curline->length; +// ----------------------------------------------------------------------------- +void R_StoreWallRange (int start, int stop) +{ IDRender.numsegs++; // [JN] remove MAXDRAWSEGS Vanilla limit @@ -562,11 +537,12 @@ R_StoreWallRange // thank you very much Linguica, Andrey Budko and kb1 // http://www.doomworld.com/vb/post/1340718 // shift right to avoid possibility of int64 overflow in rw_distance calculation - dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; - dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; - dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; - dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; - dist = ((dy * dx1 - dx * dy1) / len) << 1; + const uint32_t len = curline->length; + const int64_t dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; + const int64_t dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; + const int64_t dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; + const int64_t dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; + const int64_t dist = ((dy * dx1 - dx * dy1) / len) << 1; rw_distance = (fixed_t)BETWEEN(INT_MIN, INT_MAX, dist); ds_p->x1 = rw_x = start; @@ -642,8 +618,8 @@ R_StoreWallRange markfloor = markceiling = true; if (linedef->flags & ML_DONTPEGBOTTOM) { - vtop = frontsector->interpfloorheight + - textureheight[sidedef->midtexture]; + const fixed_t vtop = frontsector->interpfloorheight + + textureheight[sidedef->midtexture]; // bottom of texture at bottom rw_midtexturemid = vtop - viewz; } @@ -762,7 +738,8 @@ R_StoreWallRange } else { - vtop = backsector->interpceilingheight + textureheight[sidedef->toptexture]; + const fixed_t vtop = backsector->interpceilingheight + + textureheight[sidedef->toptexture]; // bottom of texture rw_toptexturemid = vtop - viewz; @@ -811,10 +788,10 @@ R_StoreWallRange // OPTIMIZE: get rid of LIGHTSEGSHIFT globally if (!fixedcolormap) { - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); - - // [crispy] smoother fake contrast - lightnum += curline->fakecontrast; + // [crispy] smooth diminishing lighting and smoother fake contrast + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT) + + curline->fakecontrast; walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; } diff --git a/src/heretic/r_things.c b/src/heretic/r_things.c index 193393a5..add9e183 100644 --- a/src/heretic/r_things.c +++ b/src/heretic/r_things.c @@ -730,27 +730,21 @@ void R_ProjectSprite (mobj_t* thing) -// +// ----------------------------------------------------------------------------- // R_AddSprites // During BSP traversal, this adds sprites by sector. -// -void R_AddSprites (sector_t* sec) -{ - mobj_t* thing; - int lightnum; - - lightnum = (sec->lightlevel >> LIGHTSEGSHIFT)+(extralight * LIGHTBRIGHT); +// ----------------------------------------------------------------------------- - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS-1]; - else - spritelights = scalelight[lightnum]; +void R_AddSprites (sector_t *sec) +{ + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (sec->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; // Handle all things in sector. - for (thing = sec->thinglist ; thing ; thing = thing->snext) - R_ProjectSprite (thing); + for (mobj_t *thing = sec->thinglist ; thing ; thing = thing->snext) + R_ProjectSprite (thing); } // ----------------------------------------------------------------------------- @@ -987,44 +981,33 @@ void R_DrawPSprite (pspdef_t* psp) R_DrawVisSprite (vis); } - - -// +// ----------------------------------------------------------------------------- // R_DrawPlayerSprites -// -void R_DrawPlayerSprites (void) +// ----------------------------------------------------------------------------- + +static void R_DrawPlayerSprites (void) { - int i; - int lightnum; - pspdef_t* psp; - // RestlessRodent -- Do not draw player gun sprite if spectating if (crl_spectating) - return; - + return; + // get light level - lightnum = - (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) - +(extralight * LIGHTBRIGHT); - - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS-1]; - else - spritelights = scalelight[lightnum]; - + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; + // clip to screen bounds mfloorclip = screenheightarray; mceilingclip = negonearray; - + // add all active psprites - for (i=0, psp=viewplayer->psprites; - ipsprites; i < NUMPSPRITES; i++, psp++) { - if (psp->state) - R_DrawPSprite (psp); + if (psp->state) + R_DrawPSprite(psp); } } diff --git a/src/hexen/r_draw.c b/src/hexen/r_draw.c index 500a2088..aba56086 100644 --- a/src/hexen/r_draw.c +++ b/src/hexen/r_draw.c @@ -96,6 +96,7 @@ void R_DrawColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -115,7 +116,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; // [PN] Update frac with modulo to wrap around texture height frac = (frac + fracstep) % heightmask; @@ -131,7 +132,7 @@ void R_DrawColumn(void) const unsigned index = brightmap[s] ? colormap1[s] : colormap0[s]; *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -167,6 +168,7 @@ void R_DrawColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; int heightmask = dc_texheight - 1; const int texheightmask = dc_texheight; @@ -185,8 +187,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac = (frac + fracstep) % heightmask; } } @@ -200,8 +202,8 @@ void R_DrawColumnLow(void) *dest = index; *dest2 = index; - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -231,6 +233,7 @@ void R_DrawTLColumn (void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -239,7 +242,7 @@ void R_DrawTLColumn (void) *dest = I_BlendOver(*dest, destrgb, TRANMAP_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -269,6 +272,7 @@ void R_DrawAltTLColumn (void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -277,7 +281,7 @@ void R_DrawAltTLColumn (void) *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA_ALT); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -306,6 +310,7 @@ void R_DrawTLAddColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -315,7 +320,7 @@ void R_DrawTLAddColumn(void) *dest = I_BlendAdd(*dest, destrgb); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -348,6 +353,7 @@ void R_DrawTLAddColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -358,8 +364,8 @@ void R_DrawTLAddColumnLow(void) *dest1 = I_BlendAdd(*dest1, destrgb); *dest2 = I_BlendAdd(*dest2, destrgb); - dest1 += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest1 += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -398,6 +404,7 @@ void R_DrawTranslatedColumn(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int iterations = count + 1; for (int i = 0; i < iterations; i++) @@ -407,7 +414,7 @@ void R_DrawTranslatedColumn(void) const unsigned index = (brightmap[s] ? colormap1[t] : colormap0[t]); *dest = index; - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -439,6 +446,7 @@ void R_DrawTranslatedTLColumn(void) const byte *const translation = dc_translation; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; for (int i = 0; i <= count; i++) { @@ -449,7 +457,7 @@ void R_DrawTranslatedTLColumn(void) const pixel_t destrgb = (brightmap[t] ? colormap1[t] : colormap0[t]); *dest = I_BlendOver(*dest, destrgb, TINTTAB_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -515,6 +523,7 @@ void R_DrawExtraTLColumn(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int heightmask = dc_texheight - 1; @@ -541,7 +550,7 @@ void R_DrawExtraTLColumn(void) const pixel_t destrgb = (brightmap[s] ? colormap1[s] : colormap0[s]); *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; if (frac >= fullmask) frac -= fullmask; @@ -556,7 +565,7 @@ void R_DrawExtraTLColumn(void) const pixel_t destrgb = (brightmap[s] ? colormap1[s] : colormap0[s]); *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; + dest += screenwidth; frac += fracstep; } } @@ -589,6 +598,7 @@ void R_DrawExtraTLColumnLow(void) const byte *const brightmap = dc_brightmap; const pixel_t *const colormap0 = dc_colormap[0]; const pixel_t *const colormap1 = dc_colormap[1]; + const int screenwidth = SCREENWIDTH; const int heightmask = dc_texheight - 1; @@ -614,8 +624,8 @@ void R_DrawExtraTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; if (frac >= fullmask) @@ -632,8 +642,8 @@ void R_DrawExtraTLColumnLow(void) *dest = I_BlendOver(*dest, destrgb, EXTRATL_ALPHA); *dest2 = I_BlendOver(*dest2, destrgb, EXTRATL_ALPHA); - dest += SCREENWIDTH; - dest2 += SCREENWIDTH; + dest += screenwidth; + dest2 += screenwidth; frac += fracstep; } } @@ -684,8 +694,6 @@ fixed_t ds_xstep; fixed_t ds_ystep; byte *ds_source; // start of a 64*64 tile image -int dscount; // just for profiling - void R_DrawSpan(void) { // Calculate the span length. @@ -694,6 +702,8 @@ void R_DrawSpan(void) // [PN] Local pointers to global arrays const byte *sourcebase = ds_source; const pixel_t *colormap = (const pixel_t *)ds_colormap; // Cast to match types + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; if (!gp_flip_levels) { @@ -711,8 +721,8 @@ void R_DrawSpan(void) const int spot = xtemp | ytemp; const byte source = sourcebase[spot]; dest[j] = colormap[source]; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } dest += 4; @@ -728,8 +738,8 @@ void R_DrawSpan(void) const byte source = sourcebase[spot]; *dest++ = colormap[source]; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -745,8 +755,8 @@ void R_DrawSpan(void) pixel_t *dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = colormap[source]; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } @@ -764,6 +774,8 @@ void R_DrawSpanLow(void) // [PN] Local pointers to global arrays const byte *sourcebase = ds_source; const pixel_t *colormap = (const pixel_t *)ds_colormap; + const fixed_t xstep = ds_xstep; + const fixed_t ystep = ds_ystep; if (!gp_flip_levels) { @@ -783,8 +795,8 @@ void R_DrawSpanLow(void) dest[0] = colormap[source]; dest[1] = colormap[source]; dest += 2; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } remaining -= 4; @@ -801,8 +813,8 @@ void R_DrawSpanLow(void) *dest++ = colormap[source]; // first pixel *dest++ = colormap[source]; // second pixel - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } else @@ -824,8 +836,8 @@ void R_DrawSpanLow(void) dest = ylookup[ds_y] + columnofs[flipviewwidth[ds_x1++]]; *dest = colormap[source]; - ds_xfrac += ds_xstep; - ds_yfrac += ds_ystep; + ds_xfrac += xstep; + ds_yfrac += ystep; } } } diff --git a/src/hexen/r_main.c b/src/hexen/r_main.c index d9f6218f..be3c0910 100644 --- a/src/hexen/r_main.c +++ b/src/hexen/r_main.c @@ -191,6 +191,7 @@ int R_PointOnSegSide (fixed_t x, fixed_t y, const seg_t *line) // [crispy] turned into a general R_PointToAngle() flavor // called with either slope_div = SlopeDivCrispy() from R_PointToAngleCrispy() // or slope_div = SlopeDiv() else +// [PN] Reformatted for readability and reduced nesting /* =============================================================================== = @@ -204,49 +205,48 @@ int R_PointOnSegSide (fixed_t x, fixed_t y, const seg_t *line) angle_t R_PointToAngleSlope(fixed_t x, fixed_t y, int (*slope_div) (unsigned int num, unsigned int den)) { + // [PN] Shift to local player coordinates x -= viewx; y -= viewy; - if ((!x) && (!y)) + + // [PN] If the point matches the player's position + if (!x && !y) return 0; + if (x >= 0) - { // x >=0 + { if (y >= 0) - { // y>= 0 - if (x > y) - return tantoangle[SlopeDiv(y, x)]; // octant 0 - else - return ANG90 - 1 - tantoangle[SlopeDiv(x, y)]; // octant 1 + { + // [PN] First quadrant → octants 0 or 1 + return (x > y) + ? tantoangle[slope_div(y, x)] + : (ANG90 - 1 - tantoangle[slope_div(x, y)]); } else - { // y<0 - y = -y; - if (x > y) - return 0 - tantoangle[SlopeDiv(y, x)]; // octant 8 - else - return ANG270 + tantoangle[SlopeDiv(x, y)]; // octant 7 + { + // [PN] Fourth quadrant → octants 8 or 7 + return (x > -y) + ? (0 - tantoangle[slope_div(-y, x)]) + : (ANG270 + tantoangle[slope_div(x, -y)]); } } else - { // x<0 - x = -x; + { if (y >= 0) - { // y>= 0 - if (x > y) - return ANG180 - 1 - tantoangle[SlopeDiv(y, x)]; // octant 3 - else - return ANG90 + tantoangle[SlopeDiv(x, y)]; // octant 2 + { + // [PN] Second quadrant → octants 3 or 2 + return (-x > y) + ? (ANG180 - 1 - tantoangle[slope_div(y, -x)]) + : (ANG90 + tantoangle[slope_div(-x, y)]); } else - { // y<0 - y = -y; - if (x > y) - return ANG180 + tantoangle[SlopeDiv(y, x)]; // octant 4 - else - return ANG270 - 1 - tantoangle[SlopeDiv(x, y)]; // octant 5 + { + // [PN] Third quadrant → octants 4 or 5 + return (-x > -y) + ? (ANG180 + tantoangle[slope_div(-y, -x)]) + : (ANG270 - 1 - tantoangle[slope_div(-x, -y)]); } } - - return 0; } diff --git a/src/hexen/r_plane.c b/src/hexen/r_plane.c index cccc92c9..c8869cf2 100644 --- a/src/hexen/r_plane.c +++ b/src/hexen/r_plane.c @@ -176,7 +176,6 @@ void R_InitPlanes(void) void R_MapPlane(int y, int x1, int x2) { fixed_t distance; - unsigned index; int dx, dy; #ifdef RANGECHECK @@ -233,7 +232,7 @@ void R_MapPlane(int y, int x1, int x2) } else { - index = distance >> LIGHTZSHIFT; + unsigned int index = distance >> LIGHTZSHIFT; if (index >= MAXLIGHTZ) { index = MAXLIGHTZ - 1; @@ -681,7 +680,6 @@ void R_DrawPlanes(void) } else // regular flat { - int light = (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); // [crispy] smooth diminishing lighting const boolean swirling = (flattranslation[pl->picnum] == -1); const int stop = pl->maxx + 1; const int lumpnum = firstflat + (swirling ? pl->picnum : flattranslation[pl->picnum]); @@ -780,7 +778,7 @@ void R_DrawPlanes(void) planeheight = abs(pl->height-viewz); // [PN] Ensure 'light' is within the range [0, LIGHTLEVELS - 1] inclusively. - light = BETWEEN(0, LIGHTLEVELS-1, light); + const int light = BETWEEN(0, LIGHTLEVELS-1, (pl->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT)); planezlight = zlight[light]; pl->top[pl->minx-1] = pl->top[stop] = USHRT_MAX; diff --git a/src/hexen/r_segs.c b/src/hexen/r_segs.c index 461dd586..19071d80 100644 --- a/src/hexen/r_segs.c +++ b/src/hexen/r_segs.c @@ -159,52 +159,34 @@ void R_FixWiggle (sector_t *sector) } } -/* -================ -= -= R_RenderMaskedSegRange -= -================ -*/ - -void R_RenderMaskedSegRange(drawseg_t * ds, int x1, int x2) -{ - unsigned index; - column_t *col; - int lightnum; - int texnum; +// ----------------------------------------------------------------------------- +// R_RenderMaskedSegRange +// ----------------------------------------------------------------------------- -// -// calculate light table -// use different light tables for horizontal / vertical / diagonal -// OPTIMIZE: get rid of LIGHTSEGSHIFT globally +void R_RenderMaskedSegRange (drawseg_t *ds, int x1, int x2) +{ + // Calculate light table. + // Use different light tables + // for horizontal / vertical / diagonal. Diagonal? + // OPTIMIZE: get rid of LIGHTSEGSHIFT globally curline = ds->curline; frontsector = curline->frontsector; backsector = curline->backsector; - texnum = texturetranslation[curline->sidedef->midtexture]; - - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); // [crispy] smooth diminishing lighting - //if (curline->v1->y == curline->v2->y) - // lightnum--; - //else if (curline->v1->x == curline->v2->x) - // lightnum++; - //if (lightnum < 0) - // walllights = scalelight[0]; - if (lightnum >= LIGHTLEVELS) - walllights = scalelight[LIGHTLEVELS - 1]; - else - walllights = scalelight[lightnum]; + const int texnum = texturetranslation[curline->sidedef->midtexture]; - maskedtexturecol = ds->maskedtexturecol; + // [crispy] smooth diminishing lighting + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT); - rw_scalestep = ds->scalestep; - spryscale = ds->scale1 + (x1 - ds->x1) * rw_scalestep; + walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; + + maskedtexturecol = ds->maskedtexturecol; + rw_scalestep = ds->scalestep; + spryscale = ds->scale1 + (x1 - ds->x1)*rw_scalestep; mfloorclip = ds->sprbottomclip; mceilingclip = ds->sprtopclip; -// -// find positioning -// + // find positioning if (curline->linedef->flags & ML_DONTPEGBOTTOM) { dc_texturemid = frontsector->interpfloorheight > backsector->interpfloorheight @@ -213,76 +195,67 @@ void R_RenderMaskedSegRange(drawseg_t * ds, int x1, int x2) } else { - dc_texturemid = frontsector->interpceilingheight < backsector->interpceilingheight + dc_texturemid =frontsector->interpceilingheight < backsector->interpceilingheight ? frontsector->interpceilingheight : backsector->interpceilingheight; dc_texturemid = dc_texturemid - viewz; } dc_texturemid += curline->sidedef->rowoffset; if (fixedcolormap) - dc_colormap[0] = dc_colormap[1] = fixedcolormap; -// -// draw the columns -// - for (dc_x = x1; dc_x <= x2; dc_x++) + dc_colormap[0] = dc_colormap[1] = fixedcolormap; + + // draw the columns + for (dc_x = x1 ; dc_x <= x2 ; dc_x++) { // calculate lighting - if (maskedtexturecol[dc_x] != INT_MAX) // [crispy] 32-bit integer math + if (maskedtexturecol[dc_x] != INT_MAX) // [JN] 32-bit integer math { if (!fixedcolormap) { - index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; - if (index >= MAXLIGHTSCALE) - index = MAXLIGHTSCALE - 1; + unsigned const int index = (spryscale / vid_resolution) >> LIGHTSCALESHIFT; + // [crispy] brightmaps for mid-textures dc_brightmap = texturebrightmap[texnum]; - dc_colormap[0] = walllights[index]; - dc_colormap[1] = vis_brightmaps - && LevelUseFullBright ? colormaps : dc_colormap[0]; + dc_colormap[0] = walllights[MIN(index, MAXLIGHTSCALE-1)]; + dc_colormap[1] = vis_brightmaps ? colormaps : dc_colormap[0]; } + // [crispy] apply Killough's int64 sprtopscreen overflow fix + // from winmbf/Source/r_segs.c:174-191 + // killough 3/2/98: + // + // This calculation used to overflow and cause crashes in Doom: + // + // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); + // + // This code fixes it, by using double-precision intermediate + // arithmetic and by skipping the drawing of 2s normals whose + // mapping to screen coordinates is totally out of range: + { + int64_t t = ((int64_t) centeryfrac << FRACBITS) + - (int64_t) dc_texturemid * spryscale; - // [crispy] apply Killough's int64 sprtopscreen overflow fix - // from winmbf/Source/r_segs.c:174-191 - // killough 3/2/98: - // - // This calculation used to overflow and cause crashes in Doom: - // - // sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale); - // - // This code fixes it, by using double-precision intermediate - // arithmetic and by skipping the drawing of 2s normals whose - // mapping to screen coordinates is totally out of range: - - { - int64_t t = ((int64_t) centeryfrac << FRACBITS) - - (int64_t) dc_texturemid * spryscale; - - if (t + (int64_t) textureheight[texnum] * spryscale < 0 || - t > (int64_t) SCREENHEIGHT << FRACBITS*2) - { - spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator - continue; // skip if the texture is out of screen's range - } + if (t + (int64_t) textureheight[texnum] * spryscale < 0 + || t > (int64_t) SCREENHEIGHT << FRACBITS * 2) + { + spryscale += rw_scalestep; // [crispy] MBF had this in the for-loop iterator + continue; // skip if the texture is out of screen's range + } - sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix - } + sprtopscreen = (int64_t)(t >> FRACBITS); // [crispy] WiggleFix + } - dc_iscale = 0xffffffffu / (unsigned) spryscale; + dc_iscale = UINT_MAX / (unsigned)spryscale; - // // draw the texture - // - col = (column_t *) ((byte *) - R_GetColumn(texnum, - maskedtexturecol[dc_x]) - 3); + column_t *col = (column_t *)((byte *)R_GetColumn(texnum,maskedtexturecol[dc_x]) -3); - R_DrawMaskedColumn(col, -1); - maskedtexturecol[dc_x] = INT_MAX; + R_DrawMaskedColumn (col, -1); + maskedtexturecol[dc_x] = INT_MAX; // [JN] 32-bit integer math } + spryscale += rw_scalestep; } - } // ----------------------------------------------------------------------------- @@ -508,23 +481,14 @@ fixed_t R_ScaleFromGlobalAngle (angle_t visangle) return scale; } -/* -===================== -= -= R_StoreWallRange -= -= A wall segment will be drawn between start and stop pixels (inclusive) -= -====================== -*/ - -void R_StoreWallRange(int start, int stop) -{ - fixed_t vtop; - int lightnum; - int64_t dx, dy, dx1, dy1, dist; // [crispy] fix long wall wobble - const uint32_t len = curline->length; +// ----------------------------------------------------------------------------- +// R_StoreWallRange +// A wall segment will be drawn +// between start and stop pixels (inclusive). +// ----------------------------------------------------------------------------- +void R_StoreWallRange (int start, int stop) +{ IDRender.numsegs++; // [JN] remove MAXDRAWSEGS Vanilla limit @@ -559,11 +523,12 @@ void R_StoreWallRange(int start, int stop) // thank you very much Linguica, e6y and kb1 // http://www.doomworld.com/vb/post/1340718 // shift right to avoid possibility of int64 overflow in rw_distance calculation - dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; - dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; - dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; - dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; - dist = ((dy * dx1 - dx * dy1) / len) << 1; + const uint32_t len = curline->length; + const int64_t dx = ((int64_t)curline->v2->r_x - curline->v1->r_x) >> 1; + const int64_t dy = ((int64_t)curline->v2->r_y - curline->v1->r_y) >> 1; + const int64_t dx1 = ((int64_t)viewx - curline->v1->r_x) >> 1; + const int64_t dy1 = ((int64_t)viewy - curline->v1->r_y) >> 1; + const int64_t dist = ((dy * dx1 - dx * dy1) / len) << 1; rw_distance = (fixed_t)BETWEEN(INT_MIN, INT_MAX, dist); ds_p->x1 = rw_x = start; @@ -639,8 +604,8 @@ void R_StoreWallRange(int start, int stop) markfloor = markceiling = true; if (linedef->flags & ML_DONTPEGBOTTOM) { - vtop = frontsector->interpfloorheight + - textureheight[sidedef->midtexture]; + const fixed_t vtop = frontsector->interpfloorheight + + textureheight[sidedef->midtexture]; // bottom of texture at bottom rw_midtexturemid = vtop - viewz; } @@ -759,7 +724,8 @@ void R_StoreWallRange(int start, int stop) } else { - vtop = backsector->interpceilingheight + textureheight[sidedef->toptexture]; + const fixed_t vtop = backsector->interpceilingheight + + textureheight[sidedef->toptexture]; // bottom of texture rw_toptexturemid = vtop - viewz; @@ -808,7 +774,9 @@ void R_StoreWallRange(int start, int stop) // OPTIMIZE: get rid of LIGHTSEGSHIFT globally if (!fixedcolormap) { - lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); // [crispy] smooth diminishing lighting + // [crispy] smooth diminishing lighting + const int lightnum = (frontsector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT); walllights = scalelight[BETWEEN(0, LIGHTLEVELS-1, lightnum)]; } } diff --git a/src/hexen/r_things.c b/src/hexen/r_things.c index 749adfb5..eec11809 100644 --- a/src/hexen/r_things.c +++ b/src/hexen/r_things.c @@ -748,30 +748,21 @@ void R_ProjectSprite(mobj_t * thing) -/* -======================== -= -= R_AddSprites -= -======================== -*/ +// ----------------------------------------------------------------------------- +// R_AddSprites +// During BSP traversal, this adds sprites by sector. +// ----------------------------------------------------------------------------- -void R_AddSprites(sector_t * sec) +void R_AddSprites (sector_t *sec) { - mobj_t *thing; - int lightnum; - - lightnum = (sec->lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT); // [crispy] smooth diminishing lighting - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS - 1]; - else - spritelights = scalelight[lightnum]; - - - for (thing = sec->thinglist; thing; thing = thing->snext) - R_ProjectSprite(thing); + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (sec->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; + + // Handle all things in sector. + for (mobj_t *thing = sec->thinglist ; thing ; thing = thing->snext) + R_ProjectSprite (thing); } @@ -958,51 +949,36 @@ void R_DrawPSprite(pspdef_t * psp) R_DrawVisSprite(vis, vis->x1, vis->x2); } -/* -======================== -= -= R_DrawPlayerSprites -= -======================== -*/ +// ----------------------------------------------------------------------------- +// R_DrawPlayerSprites +// ----------------------------------------------------------------------------- -void R_DrawPlayerSprites(void) +static void R_DrawPlayerSprites (void) { - int i, lightnum; - pspdef_t *psp; - // RestlessRodent -- Do not draw player gun sprite if spectating if (crl_spectating) - return; + return; -// -// get light level -// - lightnum = - (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) + - (extralight * LIGHTBRIGHT); // [crispy] smooth diminishing lighting - if (lightnum < 0) - spritelights = scalelight[0]; - else if (lightnum >= LIGHTLEVELS) - spritelights = scalelight[LIGHTLEVELS - 1]; - else - spritelights = scalelight[lightnum]; -// -// clip to screen bounds -// + // get light level + // [crispy] smooth diminishing lighting + const int lightnum = BETWEEN(0, LIGHTLEVELS - 1, (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT) + + (extralight * LIGHTBRIGHT)); + spritelights = scalelight[lightnum]; + + // clip to screen bounds mfloorclip = screenheightarray; mceilingclip = negonearray; -// -// add all active psprites -// + // add all active psprites + int i; + pspdef_t *psp; for (i = 0, psp = viewplayer->psprites; i < NUMPSPRITES; i++, psp++) + { if (psp->state) R_DrawPSprite(psp); - + } } - // ----------------------------------------------------------------------------- // R_SortVisSprites //