From 9baae4cecbe10712b6594fd749c92a82dba8f158 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 | 46 +++++++++++++++++++++++++++++++++++++++++++++- src/k_terrain.h | 29 +++++++++++++++++++++++++++++ src/r_textures.c | 3 +++ src/r_textures.h | 1 + 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/k_terrain.c b/src/k_terrain.c index 94f943805..ae858226d 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 K_GetTerrainForTextureName(tex->name); + return K_GetTerrainByIndex(tex->terrainID); } // This texture doesn't have a terrain directly applied to it, @@ -1828,6 +1866,12 @@ static boolean K_TERRAINLumpParser(UINT8 *data, size_t size) { f->terrainID = K_GetTerrainHeapIndex(t); CONS_Printf("Texture '%s' set to Terrain '%s'\n", f->textureName, tkn); + + INT32 tex = R_CheckTextureNumForName(f->textureName); + if (tex != -1) + { + textures[tex]->terrainID = f->terrainID; + } } } else diff --git a/src/k_terrain.h b/src/k_terrain.h index c9abba2ce..51307b003 100644 --- a/src/k_terrain.h +++ b/src/k_terrain.h @@ -388,6 +388,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); @@ -407,6 +420,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 35d79e872..40dbf291c 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -853,6 +853,7 @@ Rloadflats (INT32 i, INT32 w) texture->patchcount = 1; texture->holes = false; texture->flip = 0; + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); // Allocate information for the texture's patches. patch = &texture->patches[0]; @@ -955,6 +956,7 @@ Rloadtextures (INT32 i, INT32 w) texture->patchcount = 1; texture->holes = false; texture->flip = 0; + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); // Allocate information for the texture's patches. patch = &texture->patches[0]; @@ -1475,6 +1477,7 @@ static texture_t *R_ParseTexture(boolean actuallyLoadTexture) resultTexture->width = newTextureWidth; resultTexture->height = newTextureHeight; resultTexture->type = TEXTURETYPE_COMPOSITE; + 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 1de51f666..8e24d276c 100644 --- a/src/r_textures.h +++ b/src/r_textures.h @@ -62,6 +62,7 @@ typedef struct boolean holes; UINT8 flip; // 1 = flipx, 2 = flipy, 3 = both void *flat; // The texture, as a flat. + size_t terrainID; // All the patches[patchcount] are drawn back to front into the cached texture. INT16 patchcount;