From c09305de629ff83ba0b139b906c66e0b0b352762 Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Sat, 23 Sep 2023 20:43:59 -0300 Subject: [PATCH] Minor refactoring --- src/hardware/hw_cache.c | 4 ++-- src/hardware/hw_md2.c | 4 ++-- src/r_defs.h | 2 +- src/r_patch.c | 38 +++++++++++++++++++++++++++++--------- src/r_patch.h | 4 +++- src/r_picformats.c | 2 +- src/r_things.c | 14 +++++++------- src/w_wad.c | 6 ++++-- 8 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 3a24d2dd1..bc3532862 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -1107,8 +1107,8 @@ patch_t *HWR_GetCachedGLPatchPwad(UINT16 wadnum, UINT16 lumpnum) lumpcache_t *lumpcache = wadfiles[wadnum]->patchcache; if (!lumpcache[lumpnum]) { - void *ptr = Z_Calloc(sizeof(patch_t), PU_PATCH, &lumpcache[lumpnum]); - Patch_Create(NULL, ptr); + void *ptr = Patch_Create(0, 0); + Z_SetUser(ptr, &lumpcache[lumpnum]); Patch_AllocateHardwarePatch(ptr); } return (patch_t *)(lumpcache[lumpnum]); diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index 02d7d1693..ed4ed5da3 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -377,7 +377,7 @@ static void md2_loadTexture(md2_t *model) Z_Free(grPatch->mipmap->data); } else - model->grpatch = patch = Patch_Create(NULL, NULL); + model->grpatch = patch = Patch_Create(0, 0); if (!patch->hardware) Patch_AllocateHardwarePatch(patch); @@ -442,7 +442,7 @@ static void md2_loadBlendTexture(md2_t *model) Z_Free(grPatch->mipmap->data); } else - model->blendgrpatch = patch = Patch_Create(NULL, NULL); + model->blendgrpatch = patch = Patch_Create(0, 0); if (!patch->hardware) Patch_AllocateHardwarePatch(patch); diff --git a/src/r_defs.h b/src/r_defs.h index 5bdcaff1a..25dc1891b 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -811,7 +811,7 @@ typedef struct { INT16 width, height; INT16 leftoffset, topoffset; - INT32 width_po2, width_mask; + INT32 width_mask; UINT8 *pixels; column_t *columns; diff --git a/src/r_patch.c b/src/r_patch.c index 1998cc319..5ed5c52ab 100644 --- a/src/r_patch.c +++ b/src/r_patch.c @@ -21,12 +21,19 @@ // // Creates a patch. -// Assumes a PU_PATCH zone memory tag and no user, but can always be set later // -patch_t *Patch_Create(softwarepatch_t *source, void *dest) +patch_t *Patch_Create(INT16 width, INT16 height) { - patch_t *patch = (dest == NULL) ? Z_Calloc(sizeof(patch_t), PU_PATCH, NULL) : (patch_t *)(dest); + patch_t *patch = Z_Calloc(sizeof(patch_t), PU_PATCH, NULL); + patch->width = width; + patch->height = height; + return patch; +} + +patch_t *Patch_CreateFromDoomPatch(softwarepatch_t *source) +{ + patch_t *patch = Patch_Create(0, 0); if (!source) return patch; @@ -40,16 +47,16 @@ patch_t *Patch_Create(softwarepatch_t *source, void *dest) Patch_CalcDataSizes(source, &total_pixels, &total_posts); - patch->width_po2 = 1; - while (patch->width_po2 < patch->width) - patch->width_po2 <<= 1; - patch->width_mask = patch->width_po2 - 1; + int width_po2 = 1; + while (width_po2 < patch->width) + width_po2 <<= 1; + patch->width_mask = width_po2 - 1; - patch->columns = Z_Calloc(sizeof(column_t) * patch->width_po2, PU_PATCH_DATA, NULL); + patch->columns = Z_Calloc(sizeof(column_t) * patch->width, PU_PATCH_DATA, NULL); patch->posts = Z_Calloc(sizeof(post_t) * total_posts, PU_PATCH_DATA, NULL); patch->pixels = Z_Calloc(sizeof(UINT8) * total_pixels, PU_PATCH_DATA, NULL); - Patch_MakeColumns(source, patch->width_po2, patch->width, patch->pixels, patch->columns, patch->posts, false); + Patch_MakeColumns(source, patch->width, patch->width, patch->pixels, patch->columns, patch->posts, false); return patch; } @@ -116,6 +123,19 @@ void Patch_MakeColumns(softwarepatch_t *source, size_t num_columns, INT16 width, } } +column_t *Patch_GetColumn(patch_t *patch, unsigned column) +{ + if (column >= (unsigned)patch->width) + { + if (patch->width_mask + 1 == patch->width) + column &= patch->width_mask; + else + column %= patch->width; + } + + return &patch->columns[column]; +} + // // Frees a patch from memory. // diff --git a/src/r_patch.h b/src/r_patch.h index 9b703ef34..b31afa099 100644 --- a/src/r_patch.h +++ b/src/r_patch.h @@ -18,7 +18,9 @@ #include "doomdef.h" // Patch functions -patch_t *Patch_Create(softwarepatch_t *source, void *dest); +patch_t *Patch_Create(INT16 width, INT16 height); +patch_t *Patch_CreateFromDoomPatch(softwarepatch_t *source); +column_t *Patch_GetColumn(patch_t *patch, unsigned column); void Patch_CalcDataSizes(softwarepatch_t *source, size_t *total_pixels, size_t *total_posts); void Patch_MakeColumns(softwarepatch_t *source, size_t num_columns, INT16 width, UINT8 *pixels, column_t *columns, post_t *posts, boolean flip); void Patch_Free(patch_t *patch); diff --git a/src/r_picformats.c b/src/r_picformats.c index d4623e69a..a19f984d7 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -245,7 +245,7 @@ void *Picture_PatchConvert( { if (outsize != NULL) *outsize = sizeof(patch_t); - return Patch_Create(picture, NULL); + return Patch_CreateFromDoomPatch(picture); } INT32 outbpp = Picture_FormatBPP(outformat); diff --git a/src/r_things.c b/src/r_things.c index ba34f5343..e87fccddd 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -943,7 +943,7 @@ static void R_DrawVisSprite(vissprite_t *vis) sprtopscreen = (centeryfrac - FixedMul(dc_texturemid, spryscale)); dc_iscale = (0xffffffffu / (unsigned)spryscale); - column = &patch->columns[texturecolumn]; + column = Patch_GetColumn(patch, texturecolumn); localcolfunc (column); } @@ -957,8 +957,8 @@ static void R_DrawVisSprite(vissprite_t *vis) // Vertically sheared sprite for (dc_x = vis->x1; dc_x <= vis->x2; dc_x++, frac += vis->xiscale, dc_texturemid -= vis->shear.tan) { - texturecolumn = (frac>>FRACBITS) & patch->width_mask; - column = &patch->columns[texturecolumn]; + texturecolumn = frac>>FRACBITS; + column = Patch_GetColumn(patch, texturecolumn); sprtopscreen = (centeryfrac - FixedMul(dc_texturemid, spryscale)); localcolfunc (column); } @@ -972,8 +972,8 @@ static void R_DrawVisSprite(vissprite_t *vis) // Non-paper drawing loop for (dc_x = vis->x1; dc_x <= vis->x2; dc_x++, frac += vis->xiscale, sprtopscreen += vis->shear.tan) { - texturecolumn = (frac>>FRACBITS) & patch->width_mask; - column = &patch->columns[texturecolumn]; + texturecolumn = frac>>FRACBITS; + column = Patch_GetColumn(patch, texturecolumn); localcolfunc (column); } } @@ -1031,8 +1031,8 @@ static void R_DrawPrecipitationVisSprite(vissprite_t *vis) for (dc_x = vis->x1; dc_x <= vis->x2; dc_x++, frac += vis->xiscale) { - texturecolumn = (frac>>FRACBITS) & patch->width_mask; - column = &patch->columns[texturecolumn]; + texturecolumn = frac>>FRACBITS; + column = Patch_GetColumn(patch, texturecolumn); R_DrawMaskedColumn(column); } diff --git a/src/w_wad.c b/src/w_wad.c index 70e68a292..4a38a6e66 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -2064,9 +2064,11 @@ void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag) } #endif - dest = Z_Calloc(sizeof(patch_t), tag, &lumpcache[lump]); - Patch_Create(ptr, dest); + dest = Patch_CreateFromDoomPatch(ptr); Z_Free(ptr); + + Z_ChangeTag(dest, tag); + Z_SetUser(dest, &lumpcache[lump]); } else Z_ChangeTag(lumpcache[lump], tag);