Merge branch 'useTerrainIDinsteadOfTerrainPointer' into 'master'

Store Terrain ID instead of Terrain pointer in texture_t (resolves #93)

Closes #93

See merge request KartKrew/RingRacers!36
This commit is contained in:
Sal 2024-08-09 04:50:56 +00:00
commit 097a5c77ab
4 changed files with 73 additions and 6 deletions

View file

@ -332,6 +332,16 @@ terrain_t *K_GetDefaultTerrain(void)
return K_GetTerrainByIndex(defaultTerrain); 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) terrain_t *K_GetTerrainForTextureName(const char *checkName)
@ -360,6 +370,34 @@ terrain_t *K_GetTerrainForTextureName(const char *checkName)
return K_GetDefaultTerrain(); 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) terrain_t *K_GetTerrainForTextureNum(INT32 textureNum)
@ -370,7 +408,7 @@ terrain_t *K_GetTerrainForTextureNum(INT32 textureNum)
if (textureNum >= 0 && textureNum < numtextures) if (textureNum >= 0 && textureNum < numtextures)
{ {
texture_t *tex = textures[textureNum]; texture_t *tex = textures[textureNum];
return tex->terrain; return K_GetTerrainByIndex(tex->terrainID);
} }
// This texture doesn't have a terrain directly applied to it, // 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); INT32 tex = R_CheckTextureNumForName(f->textureName);
if (tex != -1) if (tex != -1)
{ {
textures[tex]->terrain = t; textures[tex]->terrainID = f->terrainID;
} }
} }
} }

View file

@ -398,6 +398,19 @@ terrain_t *K_GetTerrainByName(const char *checkName);
terrain_t *K_GetDefaultTerrain(void); 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); terrain_t *K_GetTerrainForTextureName(const char *checkName);
@ -417,6 +430,22 @@ terrain_t *K_GetDefaultTerrain(void);
terrain_t *K_GetTerrainForTextureName(const char *checkName); 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); terrain_t *K_GetTerrainForTextureNum(INT32 textureNum);

View file

@ -1197,7 +1197,7 @@ Rloadflats (INT32 i, INT32 w)
texture->patchcount = 1; texture->patchcount = 1;
texture->holes = false; texture->holes = false;
texture->flip = 0; texture->flip = 0;
texture->terrain = K_GetTerrainForTextureName(texture->name); texture->terrainID = K_GetTerrainIDForTextureName(texture->name);
// Allocate information for the texture's patches. // Allocate information for the texture's patches.
patch = &texture->patches[0]; patch = &texture->patches[0];
@ -1335,7 +1335,7 @@ Rloadtextures (INT32 i, INT32 w)
texture->patchcount = 1; texture->patchcount = 1;
texture->holes = false; texture->holes = false;
texture->flip = 0; texture->flip = 0;
texture->terrain = K_GetTerrainForTextureName(texture->name); texture->terrainID = K_GetTerrainIDForTextureName(texture->name);
// Allocate information for the texture's patches. // Allocate information for the texture's patches.
patch = &texture->patches[0]; patch = &texture->patches[0];
@ -1877,7 +1877,7 @@ static texture_t *R_ParseTexture(boolean actuallyLoadTexture)
resultTexture->width = newTextureWidth; resultTexture->width = newTextureWidth;
resultTexture->height = newTextureHeight; resultTexture->height = newTextureHeight;
resultTexture->type = TEXTURETYPE_COMPOSITE; resultTexture->type = TEXTURETYPE_COMPOSITE;
resultTexture->terrain = K_GetTerrainForTextureName(newTextureName); resultTexture->terrainID = K_GetTerrainIDForTextureName(newTextureName);
} }
Z_Free(texturesToken); Z_Free(texturesToken);
texturesToken = M_GetToken(NULL); texturesToken = M_GetToken(NULL);

View file

@ -63,7 +63,7 @@ struct texture_t
boolean holes; boolean holes;
UINT8 flip; // 1 = flipx, 2 = flipy, 3 = both UINT8 flip; // 1 = flipx, 2 = flipy, 3 = both
void *flat; // The texture, as a flat. 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. // All the patches[patchcount] are drawn back to front into the cached texture.
INT16 patchcount; INT16 patchcount;