From 2891f603417cd1b604ac8d6e98f3b21e960629a2 Mon Sep 17 00:00:00 2001 From: JugadorXEI Date: Fri, 31 May 2024 21:15:40 +0200 Subject: [PATCH] Store terrain ID instead of terrain pointer, add new helpers and accomodate for this --- src/k_terrain.c | 42 ++++++++++++++++++++++++++++++++++++++++-- src/k_terrain.h | 29 +++++++++++++++++++++++++++++ src/r_textures.c | 6 +++--- src/r_textures.h | 2 +- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/k_terrain.c b/src/k_terrain.c index b493c903c..7d2226505 100644 --- a/src/k_terrain.c +++ b/src/k_terrain.c @@ -332,6 +332,16 @@ terrain_t *K_GetDefaultTerrain(void) return K_GetTerrainByIndex(defaultTerrain); } +/*-------------------------------------------------- + size_t K_GetDefaultTerrainID(void) + + See header file for description. +--------------------------------------------------*/ +size_t K_GetDefaultTerrainID(void) +{ + return defaultTerrain; +} + /*-------------------------------------------------- terrain_t *K_GetTerrainForTextureName(const char *checkName) @@ -360,6 +370,34 @@ terrain_t *K_GetTerrainForTextureName(const char *checkName) return K_GetDefaultTerrain(); } +/*-------------------------------------------------- + size_t K_GetTerrainIDForTextureName(const char *checkName) + + See header file for description. +--------------------------------------------------*/ +size_t K_GetTerrainIDForTextureName(const char *checkName) +{ + UINT32 checkHash = quickncasehash(checkName, 8); + size_t i; + + if (numTerrainFloorDefs > 0) + { + for (i = 0; i < numTerrainFloorDefs; i++) + { + t_floor_t *f = &terrainFloorDefs[i]; + + if (checkHash == f->textureHash && !strncasecmp(checkName, f->textureName, 8)) + { + return f->terrainID; + } + } + } + + // This texture doesn't have a terrain directly applied to it, + // so we fallback to the default terrain. + return K_GetDefaultTerrainID(); +} + /*-------------------------------------------------- terrain_t *K_GetTerrainForTextureNum(INT32 textureNum) @@ -370,7 +408,7 @@ terrain_t *K_GetTerrainForTextureNum(INT32 textureNum) if (textureNum >= 0 && textureNum < numtextures) { texture_t *tex = textures[textureNum]; - return tex->terrain; + return K_GetTerrainByIndex(tex->terrainID); } // This texture doesn't have a terrain directly applied to it, @@ -2044,7 +2082,7 @@ static boolean K_TERRAINLumpParser(char *data, size_t size) INT32 tex = R_CheckTextureNumForName(f->textureName); if (tex != -1) { - textures[tex]->terrain = t; + textures[tex]->terrainID = f->terrainID; } } } diff --git a/src/k_terrain.h b/src/k_terrain.h index 314b83af6..10c862261 100644 --- a/src/k_terrain.h +++ b/src/k_terrain.h @@ -398,6 +398,19 @@ terrain_t *K_GetTerrainByName(const char *checkName); terrain_t *K_GetDefaultTerrain(void); +/*-------------------------------------------------- + size_t K_GetDefaultTerrainID(void) + + Returns the default terrain definition's ID, used + in cases where terrain is not set for a texture. + + Input Arguments:- + None + + Return:- + The default terrain definition's ID, NULL if it didn't exist. +--------------------------------------------------*/ +size_t K_GetDefaultTerrainID(void); /*-------------------------------------------------- terrain_t *K_GetTerrainForTextureName(const char *checkName); @@ -417,6 +430,22 @@ terrain_t *K_GetDefaultTerrain(void); terrain_t *K_GetTerrainForTextureName(const char *checkName); +/*-------------------------------------------------- + size_t K_GetTerrainIDForTextureName(const char *checkName) + + Returns the ID of the terrain definition applied + to the texture name inputted. + + Input Arguments:- + checkName - The texture's name. + + Return:- + The texture's terrain definition's ID if it exists, + otherwise the default terrain's ID if it exists, + otherwise NULL. +--------------------------------------------------*/ +size_t K_GetTerrainIDForTextureName(const char *checkName); + /*-------------------------------------------------- terrain_t *K_GetTerrainForTextureNum(INT32 textureNum); diff --git a/src/r_textures.c b/src/r_textures.c index 40e504610..919036e1e 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -1197,7 +1197,7 @@ Rloadflats (INT32 i, INT32 w) texture->patchcount = 1; texture->holes = false; texture->flip = 0; - texture->terrain = K_GetTerrainForTextureName(texture->name); + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); // Allocate information for the texture's patches. patch = &texture->patches[0]; @@ -1335,7 +1335,7 @@ Rloadtextures (INT32 i, INT32 w) texture->patchcount = 1; texture->holes = false; texture->flip = 0; - texture->terrain = K_GetTerrainForTextureName(texture->name); + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); // Allocate information for the texture's patches. patch = &texture->patches[0]; @@ -1877,7 +1877,7 @@ static texture_t *R_ParseTexture(boolean actuallyLoadTexture) resultTexture->width = newTextureWidth; resultTexture->height = newTextureHeight; resultTexture->type = TEXTURETYPE_COMPOSITE; - resultTexture->terrain = K_GetTerrainForTextureName(newTextureName); + resultTexture->terrainID = K_GetTerrainIDForTextureName(newTextureName); } Z_Free(texturesToken); texturesToken = M_GetToken(NULL); diff --git a/src/r_textures.h b/src/r_textures.h index b1079d81e..88ef22a8a 100644 --- a/src/r_textures.h +++ b/src/r_textures.h @@ -63,7 +63,7 @@ struct texture_t boolean holes; UINT8 flip; // 1 = flipx, 2 = flipy, 3 = both void *flat; // The texture, as a flat. - terrain_t *terrain; + size_t terrainID; // All the patches[patchcount] are drawn back to front into the cached texture. INT16 patchcount;