More refactoring

This commit is contained in:
Lactozilla 2024-01-16 15:17:58 -03:00
parent e73f33a677
commit 62db9f17d2
4 changed files with 81 additions and 113 deletions

View file

@ -778,9 +778,6 @@ Rloadflats (INT32 i, INT32 w)
UINT16 j, numlumps = 0; UINT16 j, numlumps = 0;
UINT16 texstart = 0, texend = 0; UINT16 texstart = 0, texend = 0;
UINT32 *list = NULL; UINT32 *list = NULL;
#ifndef NO_PNG_LUMPS
UINT8 header[PNG_HEADER_SIZE];
#endif
// Get every lump inside the Flats/ folder // Get every lump inside the Flats/ folder
if (W_FileHasFolders(wadfiles[w])) if (W_FileHasFolders(wadfiles[w]))
@ -802,7 +799,7 @@ Rloadflats (INT32 i, INT32 w)
for (j = 0; j < numlumps; j++) for (j = 0; j < numlumps; j++)
{ {
UINT16 wadnum = list ? WADFILENUM(list[j]) : (UINT16)w; UINT16 wadnum = list ? WADFILENUM(list[j]) : (UINT16)w;
lumpnum_t lumpnum = list ? LUMPNUM(list[j]) : (texstart + j); UINT16 lumpnum = list ? LUMPNUM(list[j]) : (texstart + j);
size_t lumplength = W_LumpLengthPwad(wadnum, lumpnum); size_t lumplength = W_LumpLengthPwad(wadnum, lumpnum);
size_t flatsize = R_GetFlatSize(lumplength); size_t flatsize = R_GetFlatSize(lumplength);
@ -810,6 +807,8 @@ Rloadflats (INT32 i, INT32 w)
INT16 width = flatsize, height = flatsize; INT16 width = flatsize, height = flatsize;
#ifndef NO_PNG_LUMPS #ifndef NO_PNG_LUMPS
UINT8 header[PNG_HEADER_SIZE];
W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof header, 0); W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof header, 0);
if (Picture_IsLumpPNG(header, lumplength)) if (Picture_IsLumpPNG(header, lumplength))
@ -885,44 +884,15 @@ Rloadtextures (INT32 i, INT32 w)
for (j = 0; j < numlumps; j++) for (j = 0; j < numlumps; j++)
{ {
UINT16 wadnum = list ? WADFILENUM(list[j]) : (UINT16)w; UINT16 wadnum = list ? WADFILENUM(list[j]) : (UINT16)w;
lumpnum_t lumpnum = list ? LUMPNUM(list[j]) : (texstart + j); UINT16 lumpnum = list ? LUMPNUM(list[j]) : (texstart + j);
softwarepatch_t patchlump;
#ifndef NO_PNG_LUMPS
size_t lumplength;
#endif
W_ReadLumpHeaderPwad(wadnum, lumpnum, &patchlump, PNG_HEADER_SIZE, 0);
#ifndef NO_PNG_LUMPS
lumplength = W_LumpLengthPwad(wadnum, lumpnum);
#endif
INT16 width = 0, height = 0; INT16 width = 0, height = 0;
#ifndef NO_PNG_LUMPS if (!W_ReadPatchHeaderPwad(wadnum, lumpnum, &width, &height, NULL, NULL))
if (Picture_IsLumpPNG((UINT8 *)&patchlump, lumplength))
{
INT32 texw, texh;
UINT8 *png = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE);
if (Picture_PNGDimensions(png, &texw, &texh, NULL, NULL, lumplength))
{
width = (INT16)width;
height = (INT16)height;
}
else
{ {
width = 1; width = 1;
height = 1; height = 1;
} }
Z_Free(png);
}
else
#endif
{
width = SHORT(patchlump.width);
height = SHORT(patchlump.height);
}
// printf("\"%s\" (wad: %u, lump: %u) is a single patch, dimensions %d x %d\n",W_CheckNameForNumPwad(wadnum,lumpnum),wadnum,lumpnum,width,height); // printf("\"%s\" (wad: %u, lump: %u) is a single patch, dimensions %d x %d\n",W_CheckNameForNumPwad(wadnum,lumpnum),wadnum,lumpnum,width,height);

View file

@ -256,7 +256,6 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16
UINT8 frame; UINT8 frame;
UINT8 rotation; UINT8 rotation;
lumpinfo_t *lumpinfo; lumpinfo_t *lumpinfo;
softwarepatch_t patch;
UINT16 numadded = 0; UINT16 numadded = 0;
memset(sprtemp,0xFF, sizeof (sprtemp)); memset(sprtemp,0xFF, sizeof (sprtemp));
@ -284,11 +283,8 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16
{ {
if (memcmp(lumpinfo[l].name,sprname,4)==0) if (memcmp(lumpinfo[l].name,sprname,4)==0)
{ {
INT32 width, height; INT16 width, height;
INT16 topoffset, leftoffset; INT16 topoffset, leftoffset;
#ifndef NO_PNG_LUMPS
boolean isPNG = false;
#endif
frame = R_Char2Frame(lumpinfo[l].name[4]); frame = R_Char2Frame(lumpinfo[l].name[4]);
rotation = R_Char2Rotation(lumpinfo[l].name[5]); rotation = R_Char2Rotation(lumpinfo[l].name[5]);
@ -303,38 +299,12 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16
if (W_LumpLengthPwad(wadnum,l)<=8) if (W_LumpLengthPwad(wadnum,l)<=8)
continue; continue;
// Get the patch's dimensions only
if (!W_ReadPatchHeaderPwad(wadnum, l, &width, &height, &topoffset, &leftoffset))
continue;
// store sprite info in lookup tables // store sprite info in lookup tables
//FIXME : numspritelumps do not duplicate sprite replacements //FIXME : numspritelumps do not duplicate sprite replacements
#ifndef NO_PNG_LUMPS
{
UINT8 *png = W_CacheLumpNumPwad(wadnum, l, PU_STATIC);
size_t len = W_LumpLengthPwad(wadnum, l);
if (Picture_IsLumpPNG(png, len))
{
if (!Picture_PNGDimensions(png, &width, &height, &topoffset, &leftoffset, len))
{
Z_Free(png);
continue;
}
isPNG = true;
}
Z_Free(png);
}
if (!isPNG)
#endif
{
W_ReadLumpHeaderPwad(wadnum, l, &patch, sizeof(INT16) * 4, 0);
width = (INT32)(SHORT(patch.width));
height = (INT32)(SHORT(patch.height));
topoffset = (INT16)(SHORT(patch.topoffset));
leftoffset = (INT16)(SHORT(patch.leftoffset));
}
spritecachedinfo[numspritelumps].width = width<<FRACBITS; spritecachedinfo[numspritelumps].width = width<<FRACBITS;
spritecachedinfo[numspritelumps].offset = leftoffset<<FRACBITS; spritecachedinfo[numspritelumps].offset = leftoffset<<FRACBITS;
spritecachedinfo[numspritelumps].topoffset = topoffset<<FRACBITS; spritecachedinfo[numspritelumps].topoffset = topoffset<<FRACBITS;

View file

@ -1836,10 +1836,6 @@ size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, si
bytesread = fread(dest, 1, size, handle); bytesread = fread(dest, 1, size, handle);
if (wadfiles[wad]->type == RET_FOLDER) if (wadfiles[wad]->type == RET_FOLDER)
fclose(handle); fclose(handle);
#ifdef NO_PNG_LUMPS
if (Picture_IsLumpPNG((UINT8 *)dest, bytesread))
Picture_ThrowPNGError(l->fullname, wadfiles[wad]->filename);
#endif
return bytesread; return bytesread;
case CM_LZF: // Is it LZF compressed? Used by ZWADs. case CM_LZF: // Is it LZF compressed? Used by ZWADs.
{ {
@ -1875,10 +1871,6 @@ size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, si
M_Memcpy(dest, decData + offset, size); M_Memcpy(dest, decData + offset, size);
Z_Free(rawData); Z_Free(rawData);
Z_Free(decData); Z_Free(decData);
#ifdef NO_PNG_LUMPS
if (Picture_IsLumpPNG((UINT8 *)dest, size))
Picture_ThrowPNGError(l->fullname, wadfiles[wad]->filename);
#endif
return size; return size;
#else #else
//I_Error("ZWAD files not supported on this platform."); //I_Error("ZWAD files not supported on this platform.");
@ -1938,10 +1930,6 @@ size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, si
Z_Free(rawData); Z_Free(rawData);
Z_Free(decData); Z_Free(decData);
#ifdef NO_PNG_LUMPS
if (Picture_IsLumpPNG((UINT8 *)dest, size))
Picture_ThrowPNGError(l->fullname, wadfiles[wad]->filename);
#endif
return size; return size;
} }
#endif #endif
@ -2097,18 +2085,10 @@ void *W_CacheLumpName(const char *name, INT32 tag)
// CACHING OF GRAPHIC PATCH RESOURCES // CACHING OF GRAPHIC PATCH RESOURCES
// ========================================================================== // ==========================================================================
// Graphic 'patches' are loaded, and if necessary, converted into the format
// the most useful for the current rendermode. For software renderer, the
// graphic patches are kept as is. For the hardware renderer, graphic patches
// are 'unpacked', and are kept into the cache in that unpacked format, and
// the heap memory cache then acts as a 'level 2' cache just after the
// graphics card memory.
// //
// Cache a patch into heap memory, convert the patch format as necessary // Cache a patch into heap memory, convert the patch format as necessary
// //
static void *W_GetPatchPwad(UINT16 wad, UINT16 lump, INT32 tag)
void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag)
{ {
lumpcache_t *lumpcache = NULL; lumpcache_t *lumpcache = NULL;
@ -2126,15 +2106,19 @@ void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag)
W_ReadLumpHeaderPwad(wad, lump, lumpdata, 0, 0); W_ReadLumpHeaderPwad(wad, lump, lumpdata, 0, 0);
ptr = lumpdata; ptr = lumpdata;
#ifndef NO_PNG_LUMPS
if (Picture_IsLumpPNG((UINT8 *)lumpdata, len)) if (Picture_IsLumpPNG((UINT8 *)lumpdata, len))
{ {
#ifndef NO_PNG_LUMPS
ptr = Picture_PNGConvert((UINT8 *)lumpdata, PICFMT_PATCH, NULL, NULL, NULL, NULL, len, &len, 0); ptr = Picture_PNGConvert((UINT8 *)lumpdata, PICFMT_PATCH, NULL, NULL, NULL, NULL, len, &len, 0);
Z_ChangeTag(ptr, tag); Z_ChangeTag(ptr, tag);
Z_SetUser(ptr, &lumpcache[lump]); Z_SetUser(ptr, &lumpcache[lump]);
Z_Free(lumpdata);
return lumpcache[lump]; return lumpcache[lump];
} #else
Picture_ThrowPNGError(W_CheckNameForNumPwad(wad, lump), wadfiles[wad]->filename);
return NULL;
#endif #endif
}
dest = Patch_CreateFromDoomPatch(ptr); dest = Patch_CreateFromDoomPatch(ptr);
Z_Free(ptr); Z_Free(ptr);
@ -2148,30 +2132,19 @@ void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag)
return lumpcache[lump]; return lumpcache[lump];
} }
void *W_CacheSoftwarePatchNum(lumpnum_t lumpnum, INT32 tag)
{
return W_CacheSoftwarePatchNumPwad(WADFILENUM(lumpnum),LUMPNUM(lumpnum),tag);
}
void *W_CachePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag) void *W_CachePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag)
{ {
patch_t *patch;
if (!TestValidLump(wad, lump)) if (!TestValidLump(wad, lump))
return NULL; return NULL;
patch = W_CacheSoftwarePatchNumPwad(wad, lump, tag); patch_t *patch = W_GetPatchPwad(wad, lump, tag);
#ifdef HWRENDER
// Software-only compile cache the data without conversion
if (rendermode == render_soft || rendermode == render_none)
#endif
return (void *)patch;
#ifdef HWRENDER #ifdef HWRENDER
if (rendermode == render_opengl)
Patch_CreateGL(patch); Patch_CreateGL(patch);
return (void *)patch;
#endif #endif
return (void *)patch;
} }
void *W_CachePatchNum(lumpnum_t lumpnum, INT32 tag) void *W_CachePatchNum(lumpnum_t lumpnum, INT32 tag)
@ -2187,6 +2160,63 @@ void *W_GetCachedPatchNumPwad(UINT16 wad, UINT16 lump)
return wadfiles[wad]->patchcache[lump]; return wadfiles[wad]->patchcache[lump];
} }
boolean W_ReadPatchHeaderPwad(UINT16 wadnum, UINT16 lumpnum, INT16 *width, INT16 *height, INT16 *topoffset, INT16 *leftoffset)
{
UINT8 header[PNG_HEADER_SIZE];
if (!TestValidLump(wadnum, lumpnum))
return false;
W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof header, 0);
size_t len = W_LumpLengthPwad(wadnum, lumpnum);
if (Picture_IsLumpPNG(header, len))
{
#ifndef NO_PNG_LUMPS
UINT8 *png = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE);
INT32 pwidth = 0, pheight = 0;
if (!Picture_PNGDimensions(png, &pwidth, &pheight, topoffset, leftoffset, len))
{
Z_Free(png);
return false;
}
*width = (INT16)pwidth;
*height = (INT16)pheight;
Z_Free(png);
return true;
#else
Picture_ThrowPNGError(W_CheckNameForNumPwad(wadnum, lumpnum), wadfiles[wadnum]->filename);
return false;
#endif
}
softwarepatch_t patch;
if (!W_ReadLumpHeaderPwad(wadnum, lumpnum, &patch, sizeof(INT16) * 4, 0))
return false;
*width = SHORT(patch.width);
*height = SHORT(patch.height);
if (topoffset)
*topoffset = SHORT(patch.topoffset);
if (leftoffset)
*leftoffset = SHORT(patch.leftoffset);
return true;
}
boolean W_ReadPatchHeader(lumpnum_t lumpnum, INT16 *width, INT16 *height, INT16 *topoffset, INT16 *leftoffset)
{
return W_ReadPatchHeaderPwad(WADFILENUM(lumpnum), LUMPNUM(lumpnum), width, height, topoffset, leftoffset);
}
void W_UnlockCachedPatch(void *patch) void W_UnlockCachedPatch(void *patch)
{ {
if (!patch) if (!patch)

View file

@ -225,10 +225,8 @@ void *W_CachePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag);
void *W_CachePatchNum(lumpnum_t lumpnum, INT32 tag); void *W_CachePatchNum(lumpnum_t lumpnum, INT32 tag);
void *W_GetCachedPatchNumPwad(UINT16 wad, UINT16 lump); void *W_GetCachedPatchNumPwad(UINT16 wad, UINT16 lump);
// Returns a Software patch. boolean W_ReadPatchHeaderPwad(UINT16 wadnum, UINT16 lumpnum, INT16 *width, INT16 *height, INT16 *topoffset, INT16 *leftoffset);
// Performs any necessary conversions from PNG images. boolean W_ReadPatchHeader(lumpnum_t lumpnum, INT16 *width, INT16 *height, INT16 *topoffset, INT16 *leftoffset);
void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag);
void *W_CacheSoftwarePatchNum(lumpnum_t lumpnum, INT32 tag);
void W_UnlockCachedPatch(void *patch); void W_UnlockCachedPatch(void *patch);