From 5eb07443dda81b5ef0ad51ec73edfc109fbaae21 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 21 Jun 2025 18:07:11 +0200 Subject: [PATCH] use templates for R_ClipWallSegment actual slight speedup --- src/qs22j.h | 9 +++++++ src/{r_bsp.c => r_bsp.cpp} | 53 ++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 16 deletions(-) rename src/{r_bsp.c => r_bsp.cpp} (96%) diff --git a/src/qs22j.h b/src/qs22j.h index c388b9c46..cf522b73d 100644 --- a/src/qs22j.h +++ b/src/qs22j.h @@ -16,6 +16,15 @@ #ifndef __QS22J__ #define __QS22J__ + +#ifdef __cplusplus +extern "C" { +#endif + void qs22j(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); + +#ifdef __cplusplus +} // extern "C" +#endif #endif diff --git a/src/r_bsp.c b/src/r_bsp.cpp similarity index 96% rename from src/r_bsp.c rename to src/r_bsp.cpp index b0a509b50..f0fb7cb4e 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.cpp @@ -81,15 +81,32 @@ UINT8 *solidcol; void R_AllocClipSegMemory(void) { - solidcol = Z_Realloc(solidcol, sizeof(*solidcol) * viewwidth, PU_STATIC, NULL); + solidcol = static_cast(Z_Realloc(solidcol, sizeof(*solidcol) * viewwidth, PU_STATIC, NULL)); } +namespace +{ + +enum class ClipType +{ + // Does handle solid walls, + // e.g. single sided LineDefs (middle texture) + // that entirely block the view. + kSolid, + kSolidDontRender, + + // Clips the given range of columns, but does not include it in the clip list. + // Does handle windows, e.g. LineDefs with upper and lower texture. + kPass, +}; + // CPhipps - // R_ClipWallSegment // // Replaces the old R_Clip*WallSegment functions. It draws bits of walls in those // columns which aren't solid, and updates the solidcol[] array appropriately -static void R_ClipWallSegment(int first, int last, boolean solid, boolean soliddontrender) +template +static void R_ClipWallSegment(int first, int last) { while (first < last) { @@ -97,7 +114,7 @@ static void R_ClipWallSegment(int first, int last, boolean solid, boolean solidd if (solidcol[first]) { - p = memchr(solidcol+first, 0, last-first); + p = static_cast(memchr(solidcol+first, 0, last-first)); if (!p) return; // All solid @@ -105,7 +122,7 @@ static void R_ClipWallSegment(int first, int last, boolean solid, boolean solidd } else { - p = memchr(solidcol+first, 1, last-first); + p = static_cast(memchr(solidcol+first, 1, last-first)); int to; if (!p) @@ -113,17 +130,23 @@ static void R_ClipWallSegment(int first, int last, boolean solid, boolean solidd else to = p - solidcol; - if (!soliddontrender) + if constexpr (Type != ClipType::kSolidDontRender) + { R_StoreWallRange(first, to-1); + } - if (solid) + if constexpr (Type != ClipType::kPass) + { memset(solidcol+first, 1, to-first); + } first = to; } } } +}; // namespace + // // R_ClearClipSegs // @@ -459,15 +482,15 @@ static void R_AddLine(seg_t *line) clippass: g_walloffscreen = false; - R_ClipWallSegment(x1, x2, false, false); + R_ClipWallSegment(x1, x2); if (g_walloffscreen) - R_ClipWallSegment(x1, x2, false, true); + R_ClipWallSegment(x1, x2); return; clipsolid: g_walloffscreen = false; - R_ClipWallSegment(x1, x2, true, false); + R_ClipWallSegment(x1, x2); } // @@ -592,8 +615,7 @@ void R_SortPolyObjects(subsector_t *sub) { // use free instead realloc since faster (thanks Lee ^_^) free(po_ptrs); - po_ptrs = malloc((num_po_ptrs = numpolys*2) - * sizeof(*po_ptrs)); + po_ptrs = static_cast(malloc((num_po_ptrs = numpolys*2)* sizeof(*po_ptrs))); } po = sub->polyList; @@ -610,8 +632,7 @@ void R_SortPolyObjects(subsector_t *sub) // 03/10/06: only bother if there are actually polys to sort if (numpolys >= 2) { - qs22j(po_ptrs, numpolys, sizeof(polyobj_t *), - R_PolyobjCompare); + qs22j(po_ptrs, numpolys, sizeof(polyobj_t *), R_PolyobjCompare); } } } @@ -637,8 +658,8 @@ static int R_PolysegCompare(const void *p1, const void *p2) dist2v1 = vxdist(seg2->v1); dist2v2 = vxdist(seg2->v2); - if (min(dist1v1, dist1v2) != min(dist2v1, dist2v2)) - return min(dist1v1, dist1v2) - min(dist2v1, dist2v2); + if (std::min(dist1v1, dist1v2) != std::min(dist2v1, dist2v2)) + return std::min(dist1v1, dist1v2) - std::min(dist2v1, dist2v2); { // That didn't work, so now let's try this....... fixed_t delta1, delta2, x1, y1, x2, y2; @@ -1056,7 +1077,7 @@ void R_Prep3DFloors(sector_t *sector) if (count != sector->numlights) { Z_Free(sector->lightlist); - sector->lightlist = Z_Calloc(sizeof (*sector->lightlist) * count, PU_LEVEL, NULL); + sector->lightlist = static_cast(Z_Calloc(sizeof (*sector->lightlist) * count, PU_LEVEL, NULL)); sector->numlights = count; } else