From dae87fe802669eeabca81d0d08d41a0f07b52ab9 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 13 Oct 2025 19:19:33 +0200 Subject: [PATCH] use std::vector for our image buffers this is just to have it cleaner and less code bloat the resizing loop will almost never trigger, 1meg is enough to even rotate gigantamax eevee without needing to grow --- src/Sourcefile | 2 +- src/{r_patch.c => r_patch.cpp} | 52 +++++++++++-------- src/r_patch.h | 8 +++ ...{r_patchrotation.c => r_patchrotation.cpp} | 52 ++++++------------- 4 files changed, 57 insertions(+), 57 deletions(-) rename src/{r_patch.c => r_patch.cpp} (93%) rename src/{r_patchrotation.c => r_patchrotation.cpp} (83%) diff --git a/src/Sourcefile b/src/Sourcefile index 9474b797d..352081bc5 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -53,7 +53,7 @@ r_data.c r_draw.c r_fps.cpp r_main.cpp -r_patch.c +r_patch.cpp r_patchrotation.c r_plane.cpp r_segs.cpp diff --git a/src/r_patch.c b/src/r_patch.cpp similarity index 93% rename from src/r_patch.c rename to src/r_patch.cpp index cac31bff0..09aca078a 100644 --- a/src/r_patch.c +++ b/src/r_patch.cpp @@ -19,6 +19,7 @@ #include "r_skins.h" #include "z_zone.h" #include "w_wad.h" +#include #ifdef HWRENDER #include "hardware/hw_glob.h" @@ -37,7 +38,7 @@ static void R_ParseSpriteInfoFrame(spriteinfo_t *info) UINT8 frameFrame = 0xFF; INT16 frameXPivot = 0; INT16 frameYPivot = 0; - rotaxis_t frameRotAxis = 0; + rotaxis_t frameRotAxis = ROTAXIS_X; // Sprite identifier sprinfoToken = M_GetToken(NULL); @@ -158,14 +159,14 @@ static void R_ParseSpriteInfo(boolean spr2) I_Error("Error parsing SPRTINFO lump: Unknown sprite name \"%s\"", newSpriteName); if (!memcmp(newSpriteName,sprnames[i],4)) { - sprnum = i; + sprnum = static_cast(i); break; } } } // allocate a spriteinfo - info = Z_Calloc(sizeof(spriteinfo_t), PU_STATIC, NULL); + info = static_cast(Z_Calloc(sizeof(spriteinfo_t), PU_STATIC, NULL)); info->available = true; // Left Curly Brace @@ -329,7 +330,7 @@ void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps) patch_t *Patch_Create(softwarepatch_t *source, size_t srcsize, void *dest) { - patch_t *patch = (dest == NULL) ? Z_Calloc(sizeof(patch_t), PU_PATCH, NULL) : (patch_t *)(dest); + patch_t *patch = static_cast((dest == NULL) ? Z_Calloc(sizeof(patch_t), PU_PATCH, NULL) : dest); if (source) { @@ -341,7 +342,7 @@ patch_t *Patch_Create(softwarepatch_t *source, size_t srcsize, void *dest) patch->height = SHORT(source->height); patch->leftoffset = SHORT(source->leftoffset); patch->topoffset = SHORT(source->topoffset); - patch->columnofs = Z_Calloc(size, PU_PATCH_DATA, NULL); + patch->columnofs = static_cast(Z_Calloc(size, PU_PATCH_DATA, NULL)); for (col = 0; col < source->width; col++) { @@ -357,7 +358,7 @@ patch_t *Patch_Create(softwarepatch_t *source, size_t srcsize, void *dest) if (colsize <= 0) I_Error("Patch_Create: no column data!"); - patch->columns = Z_Calloc(colsize, PU_PATCH_DATA, NULL); + patch->columns = static_cast(Z_Calloc(colsize, PU_PATCH_DATA, NULL)); M_Memcpy(patch->columns, ((UINT8 *)source + LONG(source->columnofs[0])), colsize); } @@ -429,8 +430,8 @@ void *Patch_AllocateHardwarePatch(patch_t *patch) { if (!patch->hardware) { - GLPatch_t *glPatch = Z_Calloc(sizeof(GLPatch_t), PU_HWRPATCHINFO, &patch->hardware); - glPatch->mipmap = Z_Calloc(sizeof(GLMipmap_t), PU_HWRPATCHINFO, &glPatch->mipmap); + GLPatch_t *glPatch = static_cast(Z_Calloc(sizeof(GLPatch_t), PU_HWRPATCHINFO, &patch->hardware)); + glPatch->mipmap = static_cast(Z_Calloc(sizeof(GLMipmap_t), PU_HWRPATCHINFO, &glPatch->mipmap)); } return (void *)(patch->hardware); } @@ -448,7 +449,8 @@ void *Patch_CreateGL(patch_t *patch) } #endif // HWRENDER -static UINT8 *imgbuf = NULL; +// 1 MB should be enough for most cases +static std::vector imgbuf(1024 * 1024); // // R_MaskedFlatToPatch @@ -473,8 +475,12 @@ void *R_PixelsToPatch(UINT8 *raw, INT16 width, INT16 height, INT16 leftoffset, I size_t maxoutsize = maxcolumnsize * width + (8 + 4 * width); // so, a 512x512 flat should maximally need 393,760 (384.53 KiB) bytes. // quite a bit smaller than 64 megabytes, and much less annoying to the windows debug allocator! - imgbuf = Z_Malloc(maxoutsize, PU_STATIC, NULL); - imgptr = imgbuf; + while (imgbuf.size() < maxoutsize) + { + imgbuf.resize(imgbuf.size() * 2); + } + + imgptr = imgbuf.data(); // Write image size and offset WRITEINT16(imgptr, width); @@ -494,7 +500,7 @@ void *R_PixelsToPatch(UINT8 *raw, INT16 width, INT16 height, INT16 leftoffset, I startofspan = NULL; // Write column pointer - WRITEINT32(colpointers, imgptr - imgbuf); + WRITEINT32(colpointers, imgptr - imgbuf.data()); // Write pixels for (y = 0; y < height; y++) @@ -563,9 +569,9 @@ void *R_PixelsToPatch(UINT8 *raw, INT16 width, INT16 height, INT16 leftoffset, I WRITEUINT8(imgptr, 0xFF); } - size = imgptr-imgbuf; - img = Z_Malloc(size, PU_STATIC, NULL); - memcpy(img, imgbuf, size); + size = imgptr-imgbuf.data(); + img = static_cast(Z_Malloc(size, PU_STATIC, NULL)); + memcpy(img, imgbuf.data(), size); if (destsize != NULL) *destsize = size; @@ -603,8 +609,12 @@ void *R_MaskedFlatToPatch(UINT16 *raw, INT16 width, INT16 height, INT16 leftoffs size_t maxoutsize = maxcolumnsize * width + (8 + 4 * width); // so, a 512x512 flat should maximally need 393,760 (384.53 KiB) bytes. // quite a bit smaller than 64 megabytes, and much less annoying to the windows debug allocator! - imgbuf = Z_Malloc(maxoutsize, PU_STATIC, NULL); - imgptr = imgbuf; + while (imgbuf.size() < maxoutsize) + { + imgbuf.resize(imgbuf.size() * 2); + } + + imgptr = imgbuf.data(); // Write image size and offset WRITEINT16(imgptr, width); @@ -624,7 +634,7 @@ void *R_MaskedFlatToPatch(UINT16 *raw, INT16 width, INT16 height, INT16 leftoffs startofspan = NULL; // Write column pointer - WRITEINT32(colpointers, imgptr - imgbuf); + WRITEINT32(colpointers, imgptr - imgbuf.data()); // Write pixels for (y = 0; y < height; y++) @@ -694,9 +704,9 @@ void *R_MaskedFlatToPatch(UINT16 *raw, INT16 width, INT16 height, INT16 leftoffs WRITEUINT8(imgptr, 0xFF); } - size = imgptr-imgbuf; - img = Z_Malloc(size, PU_STATIC, NULL); - memcpy(img, imgbuf, size); + size = imgptr-imgbuf.data(); + img = static_cast(Z_Malloc(size, PU_STATIC, NULL)); + memcpy(img, imgbuf.data(), size); if (destsize != NULL) *destsize = size; diff --git a/src/r_patch.h b/src/r_patch.h index 4d44b5416..266e4a563 100644 --- a/src/r_patch.h +++ b/src/r_patch.h @@ -14,6 +14,10 @@ #ifndef __R_PATCH__ #define __R_PATCH__ +#ifdef __cplusplus +extern "C" { +#endif + #include "r_defs.h" // Structs @@ -63,4 +67,8 @@ void *R_PixelsToPatch(UINT8 *raw, INT16 width, INT16 height, INT16 leftoffset, I UINT16 R_GetPatchPixel(patch_t *patch, INT32 x, INT32 y, boolean flip); void R_PatchToPixels(patch_t *patch, UINT8 *dst); // Important: dst must have enough space for patch->width*patch->height pixels! +#ifdef __cplusplus +} // extern "C" +#endif + #endif // __R_PATCH__ diff --git a/src/r_patchrotation.c b/src/r_patchrotation.cpp similarity index 83% rename from src/r_patchrotation.c rename to src/r_patchrotation.cpp index 862febf2f..ebf8960e6 100644 --- a/src/r_patchrotation.c +++ b/src/r_patchrotation.cpp @@ -15,6 +15,7 @@ #include "r_patchrotation.h" #include "z_zone.h" #include "w_wad.h" +#include #ifdef ROTSPRITE // @@ -89,14 +90,14 @@ patch_t *Patch_GetRotatedSprite(spriteframe_t *sprite, size_t frame, size_t spri RotatedPatch_DoRotation(rotsprite, patch, rotationangle, xpivot, ypivot, flip); } - return rotsprite->patches[idx]; + return static_cast(rotsprite->patches[idx]); } rotsprite_t *RotatedPatch_Create(INT32 numangles) { - rotsprite_t *rotsprite = Z_Calloc(sizeof(rotsprite_t), PU_STATIC, NULL); + rotsprite_t *rotsprite = static_cast(Z_Calloc(sizeof(rotsprite_t), PU_STATIC, NULL)); rotsprite->angles = numangles; - rotsprite->patches = Z_Calloc(rotsprite->angles * 2 * sizeof(void *), PU_STATIC, NULL); + rotsprite->patches = static_cast(Z_Calloc(rotsprite->angles * 2 * sizeof(void *), PU_STATIC, NULL)); return rotsprite; } @@ -118,30 +119,8 @@ static void RotatedPatch_CalculateDimensions( h1 = FixedInt(FixedCeil(h1 + (FRACUNIT/2))); h2 = FixedInt(FixedCeil(h2 + (FRACUNIT/2))); - *newwidth = max(width, max(w1, w2)); - *newheight = max(height, max(h1, h2)); -} - -static void RotatedPatch_AllocImgBuf(UINT16 **buf, size_t size, size_t *capacity) -{ - if (!*buf) - { - *buf = Z_Malloc(*capacity, PU_STATIC, NULL); - } - - size_t allocsize = size * sizeof(UINT16); - - // make sure our buffer always fits with what we need - if (allocsize > *capacity) - { - while (*capacity < allocsize) - *capacity *= 2; - - Z_Free(*buf); - *buf = Z_Malloc(*capacity, PU_STATIC, NULL); - } - - //CONS_Printf("RotatedPatch_AllocBuf: Allocated %.2f KB\n", *capacity / 1024.0); + *newwidth = std::max(width, std::max(w1, w2)); + *newheight = std::max(height, std::max(h1, h2)); } void RotatedPatch_DoRotation(rotsprite_t *rotsprite, patch_t *patch, INT32 angle, INT32 xpivot, INT32 ypivot, boolean flip) @@ -149,10 +128,10 @@ void RotatedPatch_DoRotation(rotsprite_t *rotsprite, patch_t *patch, INT32 angle UINT32 i; patch_t *rotated; - static UINT16 *rawdst = NULL, *rawconv = NULL; + static std::vector rawdst(1024 * 1024), rawconv(1024 * 1024); // 1 MB should be enough for most cases + UINT16 *rawout; size_t size; - static size_t dst_capacity = (1024 * 1024), conv_capacity = (1024 * 1024); // 1 MB should be enough for most cases INT32 bflip = (flip != 0x00); INT32 width = patch->width; @@ -206,7 +185,10 @@ void RotatedPatch_DoRotation(rotsprite_t *rotsprite, patch_t *patch, INT32 angle if (!size) size = (width * height); - RotatedPatch_AllocImgBuf(&rawdst, size, &dst_capacity); + while (rawdst.size() < size) + { + rawdst.resize(rawdst.size() * 2); + } for (i = 0; i < size; i++) rawdst[i] = 0xFF00; @@ -250,11 +232,11 @@ void RotatedPatch_DoRotation(rotsprite_t *rotsprite, patch_t *patch, INT32 angle size = (width * height); - RotatedPatch_AllocImgBuf(&rawconv, size, &conv_capacity); - memset(rawconv, 0, conv_capacity * sizeof(UINT16)); + rawconv.resize(size); + memset(rawconv.data(), 0, size * sizeof(UINT16)); src = &rawdst[(miny * newwidth) + minx]; - dest = rawconv; + dest = rawconv.data(); dy = height; while (dy--) @@ -267,14 +249,14 @@ void RotatedPatch_DoRotation(rotsprite_t *rotsprite, patch_t *patch, INT32 angle ox -= minx; oy -= miny; - rawout = rawconv; + rawout = rawconv.data(); } else { width = newwidth; height = newheight; - rawout = rawdst; + rawout = rawdst.data(); } // make patch