Browse Source

engine: common: mod_bmodel: apply code style fixes to match existing code, for consistency

pull/2/head
Alibek Omarov 1 year ago
parent
commit
4d4162336a
  1. 167
      engine/common/mod_bmodel.c

167
engine/common/mod_bmodel.c

@ -25,7 +25,7 @@ GNU General Public License for more details.
#include "server.h" // LUMP_ error codes #include "server.h" // LUMP_ error codes
#include "ref_common.h" #include "ref_common.h"
#define MIPTEX_CUSTOM_PALETTE_SIZE_BYTES 770 #define MIPTEX_CUSTOM_PALETTE_SIZE_BYTES ( sizeof( int16_t ) + 768 )
typedef struct wadlist_s typedef struct wadlist_s
{ {
@ -218,112 +218,95 @@ static mlumpinfo_t extlumps[EXTRA_LUMPS] =
=============================================================================== ===============================================================================
*/ */
static mip_t* Mod_GetMipTexForTexture( dbspmodel_t* bmod, int textureIndex ) static mip_t *Mod_GetMipTexForTexture( dbspmodel_t *bmod, int i )
{
if( textureIndex < 0 ||
textureIndex >= bmod->textures->nummiptex ||
bmod->textures->dataofs[textureIndex] == -1 )
{ {
if( i < 0 || i >= bmod->textures->nummiptex )
return NULL; return NULL;
}
return (mip_t*)((byte*)bmod->textures + bmod->textures->dataofs[textureIndex]); if( bmod->textures->dataofs[i] == -1 )
return NULL;
return (mip_t *)((byte *)bmod->textures + bmod->textures->dataofs[i] );
} }
// Returns index of WAD that texture was found in, or -1 if not found. // Returns index of WAD that texture was found in, or -1 if not found.
static int Mod_FindTextureInWadList( static int Mod_FindTextureInWadList( wadlist_t *list, const char *name, char *dst, size_t size )
wadlist_t* list,
const char* textureName,
char* foundPathBuffer,
size_t foundPathBufferLength )
{ {
char pathInWad[MAX_VA_STRING]; int i;
int wadIndex;
if( !list || !textureName || !(*textureName) ) if( !list || !COM_CheckString( name ))
{
return -1; return -1;
}
// check wads in reverse order // check wads in reverse order
for( wadIndex = list->count - 1; wadIndex >= 0; --wadIndex ) for( i = list->count - 1; i >= 0; i-- )
{ {
Q_snprintf( pathInWad, sizeof(pathInWad), "%s.wad/%s.mip", list->wadnames[wadIndex], textureName ); char path[MAX_VA_STRING];
if( FS_FileExists( pathInWad, false ) ) Q_snprintf( path, sizeof( path ), "%s.wad/%s.mip", list->wadnames[i], name );
{
if( foundPathBuffer && foundPathBufferLength > 0 ) if( FS_FileExists( path, false ))
{ {
Q_strncpy( foundPathBuffer, pathInWad, foundPathBufferLength ); if( dst && size > 0 )
} Q_strncpy( dst, path, size );
return wadIndex; return i;
} }
} }
return -1; return -1;
} }
static size_t Mod_CalculateMipTexSize( mip_t* mipTex, qboolean customPalette ) static fs_offset_t Mod_CalculateMipTexSize( mip_t *mt, qboolean palette )
{
if( !mipTex )
{ {
if( !mt )
return 0; return 0;
}
return sizeof(*mipTex) + return sizeof( *mt ) + (( mt->width * mt->height * 85 ) >> 6 ) +
(( mipTex->width * mipTex->height * 85) >> 6) + ( palette ? MIPTEX_CUSTOM_PALETTE_SIZE_BYTES : 0 );
(customPalette ? MIPTEX_CUSTOM_PALETTE_SIZE_BYTES : 0);
} }
static qboolean Mod_CalcMipTexUsesCustomPalette( dbspmodel_t *bmod, int textureIndex ) static qboolean Mod_CalcMipTexUsesCustomPalette( dbspmodel_t *bmod, int textureIndex )
{ {
int size = 0;
int nextTextureIndex = 0; int nextTextureIndex = 0;
mip_t* mipTex = NULL; mip_t *mipTex;
fs_offset_t size, remainingBytes;
mipTex = Mod_GetMipTexForTexture( bmod, textureIndex ); mipTex = Mod_GetMipTexForTexture( bmod, textureIndex );
if( !mipTex || mipTex->offsets[0] <= 0 ) if( !mipTex || mipTex->offsets[0] <= 0 )
{
return false; return false;
}
// Calculate the size assuming we are not using a custom palette. // Calculate the size assuming we are not using a custom palette.
size = (int)Mod_CalculateMipTexSize( mipTex, false ); size = Mod_CalculateMipTexSize( mipTex, false );
// Compute next data offset to determine allocated miptex space // Compute next data offset to determine allocated miptex space
for( nextTextureIndex = textureIndex + 1; nextTextureIndex < loadmodel->numtextures; ++nextTextureIndex ) for( nextTextureIndex = textureIndex + 1; nextTextureIndex < loadmodel->numtextures; nextTextureIndex++ )
{ {
int nextOffset = bmod->textures->dataofs[nextTextureIndex]; int nextOffset = bmod->textures->dataofs[nextTextureIndex];
if( nextOffset != -1 ) if( nextOffset != -1 )
{ {
int remainingBytes = nextOffset - (bmod->textures->dataofs[textureIndex] + size); remainingBytes = nextOffset - ( bmod->textures->dataofs[textureIndex] + size );
return remainingBytes >= MIPTEX_CUSTOM_PALETTE_SIZE_BYTES; return remainingBytes >= MIPTEX_CUSTOM_PALETTE_SIZE_BYTES;
} }
} }
// There was no other miptex after this one. // There was no other miptex after this one.
// See if there is enough space between the end and our offset. // See if there is enough space between the end and our offset.
return bmod->texdatasize - (bmod->textures->dataofs[textureIndex] + size) >= MIPTEX_CUSTOM_PALETTE_SIZE_BYTES; remainingBytes = bmod->texdatasize - ( bmod->textures->dataofs[textureIndex] + size );
return remainingBytes >= MIPTEX_CUSTOM_PALETTE_SIZE_BYTES;
} }
static qboolean Mod_NameImpliesTextureIsAnimated( texture_t *tex ) static qboolean Mod_NameImpliesTextureIsAnimated( texture_t *tex )
{ {
if( !tex ) if( !tex )
{
return false; return false;
}
if( tex->name[0] != '-' && tex->name[0] != '+' )
{
// Not an animated texture name // Not an animated texture name
if( tex->name[0] != '-' && tex->name[0] != '+' )
return false; return false;
}
// Name implies texture is animated - check second character is valid. // Name implies texture is animated - check second character is valid.
if( !( tex->name[1] >= '0' && tex->name[1] <= '9' ) && if( !( tex->name[1] >= '0' && tex->name[1] <= '9' ) &&
!( tex->name[1] >= 'a' && tex->name[1] <= 'j' )) !( tex->name[1] >= 'a' && tex->name[1] <= 'j' ))
{ {
@ -336,21 +319,21 @@ static qboolean Mod_NameImpliesTextureIsAnimated( texture_t* tex )
static void Mod_CreateDefaultTexture( texture_t **texture ) static void Mod_CreateDefaultTexture( texture_t **texture )
{ {
texture_t *tex;
// Pointer must be valid, and value pointed to must be null. // Pointer must be valid, and value pointed to must be null.
if( !texture || *texture != NULL ) if( !texture || *texture != NULL )
{
return; return;
}
*texture = Mem_Calloc( loadmodel->mempool, sizeof(texture_t) ); *texture = tex = Mem_Calloc( loadmodel->mempool, sizeof( *tex ));
Q_strncpy( (*texture)->name, REF_DEFAULT_TEXTURE, sizeof((*texture)->name) ); Q_strncpy( tex->name, REF_DEFAULT_TEXTURE, sizeof( tex->name ));
#if !XASH_DEDICATED #if !XASH_DEDICATED
if( !Host_IsDedicated( )) if( !Host_IsDedicated( ))
{ {
(*texture)->gl_texturenum = R_GetBuiltinTexture( REF_DEFAULT_TEXTURE ); tex->gl_texturenum = R_GetBuiltinTexture( REF_DEFAULT_TEXTURE );
(*texture)->width = 16; tex->width = 16;
(*texture)->height = 16; tex->height = 16;
} }
#endif // XASH_DEDICATED #endif // XASH_DEDICATED
} }
@ -908,7 +891,7 @@ static void Mod_FindModelOrigin( const char *entities, const char *modelname, ve
qboolean model_found; qboolean model_found;
qboolean origin_found; qboolean origin_found;
if( !entities || !modelname || !*modelname ) if( !entities || !COM_CheckString( modelname ))
return; return;
if( !origin || !VectorIsNull( origin )) if( !origin || !VectorIsNull( origin ))
@ -2037,19 +2020,17 @@ static void Mod_LoadTextureData( dbspmodel_t* bmod, int textureIndex )
qboolean usesCustomPalette = false; qboolean usesCustomPalette = false;
uint32_t txFlags = 0; uint32_t txFlags = 0;
if( Host_IsDedicated() )
{
// Don't load texture data on dedicated server, as there is no renderer. // Don't load texture data on dedicated server, as there is no renderer.
// FIXME: for ENGINE_IMPROVED_LINETRACE we need to load textures on server too
// but there is no facility for this yet
if( Host_IsDedicated( ))
return; return;
}
texture = loadmodel->textures[textureIndex]; texture = loadmodel->textures[textureIndex];
mipTex = Mod_GetMipTexForTexture( bmod, textureIndex ); mipTex = Mod_GetMipTexForTexture( bmod, textureIndex );
if( FBitSet( host.features, ENGINE_IMPROVED_LINETRACE ) && mipTex->name[0] == '{' ) if( FBitSet( host.features, ENGINE_IMPROVED_LINETRACE ) && mipTex->name[0] == '{' )
{
SetBits( txFlags, TF_KEEP_SOURCE ); // Paranoia2 texture alpha-tracing SetBits( txFlags, TF_KEEP_SOURCE ); // Paranoia2 texture alpha-tracing
}
usesCustomPalette = Mod_CalcMipTexUsesCustomPalette( bmod, textureIndex ); usesCustomPalette = Mod_CalcMipTexUsesCustomPalette( bmod, textureIndex );
@ -2059,9 +2040,7 @@ static void Mod_LoadTextureData( dbspmodel_t* bmod, int textureIndex )
ref.dllFuncs.R_InitSkyClouds( mipTex, texture, usesCustomPalette ); // load quake sky ref.dllFuncs.R_InitSkyClouds( mipTex, texture, usesCustomPalette ); // load quake sky
if( R_GetBuiltinTexture( REF_SOLIDSKY_TEXTURE ) && R_GetBuiltinTexture( REF_ALPHASKY_TEXTURE )) if( R_GetBuiltinTexture( REF_SOLIDSKY_TEXTURE ) && R_GetBuiltinTexture( REF_ALPHASKY_TEXTURE ))
{
SetBits( world.flags, FWORLD_SKYSPHERE ); SetBits( world.flags, FWORLD_SKYSPHERE );
}
// No texture to load in this case, so just exit. // No texture to load in this case, so just exit.
return; return;
@ -2115,7 +2094,7 @@ static void Mod_LoadTextureData( dbspmodel_t* bmod, int textureIndex )
else else
{ {
char texpath[MAX_VA_STRING]; char texpath[MAX_VA_STRING];
int wadIndex = -1; int wadIndex;
fs_offset_t srcSize = 0; fs_offset_t srcSize = 0;
byte* src = NULL; byte* src = NULL;
@ -2135,23 +2114,19 @@ static void Mod_LoadTextureData( dbspmodel_t* bmod, int textureIndex )
texture->fb_texturenum = ref.dllFuncs.GL_LoadTexture( texName, src, srcSize, TF_MAKELUMA ); texture->fb_texturenum = ref.dllFuncs.GL_LoadTexture( texName, src, srcSize, TF_MAKELUMA );
if( src ) if( src )
{
Mem_Free( src ); Mem_Free( src );
} }
} }
}
#endif // !XASH_DEDICATED #endif // !XASH_DEDICATED
} }
static void Mod_LoadTexture( dbspmodel_t *bmod, int textureIndex ) static void Mod_LoadTexture( dbspmodel_t *bmod, int textureIndex )
{ {
texture_t* texture = NULL; texture_t *texture;
mip_t* mipTex = NULL; mip_t *mipTex;
if( textureIndex < 0 || textureIndex >= loadmodel->numtextures ) if( textureIndex < 0 || textureIndex >= loadmodel->numtextures )
{
return; return;
}
mipTex = Mod_GetMipTexForTexture( bmod, textureIndex ); mipTex = Mod_GetMipTexForTexture( bmod, textureIndex );
@ -2164,11 +2139,9 @@ static void Mod_LoadTexture( dbspmodel_t* bmod, int textureIndex )
} }
if( mipTex->name[0] == '\0' ) if( mipTex->name[0] == '\0' )
{
Q_snprintf( mipTex->name, sizeof( mipTex->name ), "miptex_%i", textureIndex ); Q_snprintf( mipTex->name, sizeof( mipTex->name ), "miptex_%i", textureIndex );
}
texture = (texture_t*)Mem_Calloc( loadmodel->mempool, sizeof(texture_t) ); texture = (texture_t *)Mem_Calloc( loadmodel->mempool, sizeof( *texture ));
loadmodel->textures[textureIndex] = texture; loadmodel->textures[textureIndex] = texture;
// Ensure texture name is lowercase. // Ensure texture name is lowercase.
@ -2183,38 +2156,32 @@ static void Mod_LoadTexture( dbspmodel_t* bmod, int textureIndex )
static void Mod_LoadAllTextures( dbspmodel_t *bmod ) static void Mod_LoadAllTextures( dbspmodel_t *bmod )
{ {
for( int index = 0; index < loadmodel->numtextures; ++index ) int i;
{
Mod_LoadTexture( bmod, index ); for( i = 0; i < loadmodel->numtextures; i++ )
} Mod_LoadTexture( bmod, i );
} }
static void Mod_SequenceAnimatedTexture( int baseTextureIndex ) static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
{ {
texture_t *anims[10]; texture_t *anims[10];
texture_t *altanims[10]; texture_t *altanims[10];
texture_t* baseTexture = NULL; texture_t *baseTexture;
int max = 0; int max = 0;
int altmax = 0; int altmax = 0;
int candidateIndex = 0; int candidateIndex;
if( baseTextureIndex < 0 || baseTextureIndex >= loadmodel->numtextures ) if( baseTextureIndex < 0 || baseTextureIndex >= loadmodel->numtextures )
{
return; return;
}
baseTexture = loadmodel->textures[baseTextureIndex]; baseTexture = loadmodel->textures[baseTextureIndex];
if( !Mod_NameImpliesTextureIsAnimated( baseTexture )) if( !Mod_NameImpliesTextureIsAnimated( baseTexture ))
{
return; return;
}
if( baseTexture->anim_next )
{
// Already sequenced // Already sequenced
if( baseTexture->anim_next )
return; return;
}
// find the number of frames in the animation // find the number of frames in the animation
memset( anims, 0, sizeof( anims )); memset( anims, 0, sizeof( anims ));
@ -2238,23 +2205,19 @@ static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
} }
// Now search the rest of the textures to find all other frames. // Now search the rest of the textures to find all other frames.
for( candidateIndex = baseTextureIndex + 1; candidateIndex < loadmodel->numtextures; ++candidateIndex ) for( candidateIndex = baseTextureIndex + 1; candidateIndex < loadmodel->numtextures; candidateIndex++ )
{ {
texture_t *altTexture = loadmodel->textures[candidateIndex]; texture_t *altTexture = loadmodel->textures[candidateIndex];
if( !Mod_NameImpliesTextureIsAnimated( altTexture )) if( !Mod_NameImpliesTextureIsAnimated( altTexture ))
{
continue; continue;
}
// This texture is animated, but is it part of the same group as // This texture is animated, but is it part of the same group as
// the original texture we encountered? Check that the rest of // the original texture we encountered? Check that the rest of
// the name matches the original (both will be valid for at least // the name matches the original (both will be valid for at least
// string index 2). // string index 2).
if( Q_strcmp( altTexture->name + 2, baseTexture->name + 2 ) != 0 ) if( Q_strcmp( altTexture->name + 2, baseTexture->name + 2 ) != 0 )
{
continue; continue;
}
if( altTexture->name[1] >= '0' && altTexture->name[1] <= '9' ) if( altTexture->name[1] >= '0' && altTexture->name[1] <= '9' )
{ {
@ -2263,10 +2226,8 @@ static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
anims[frameIndex] = altTexture; anims[frameIndex] = altTexture;
if( frameIndex >= max ) if( frameIndex >= max )
{
max = frameIndex + 1; max = frameIndex + 1;
} }
}
else else
{ {
// This texture is an alternate frame. // This texture is an alternate frame.
@ -2274,14 +2235,12 @@ static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
altanims[frameIndex] = altTexture; altanims[frameIndex] = altTexture;
if( frameIndex >= altmax ) if( frameIndex >= altmax )
{
altmax = frameIndex + 1; altmax = frameIndex + 1;
} }
} }
}
// Link all standard animated frames together. // Link all standard animated frames together.
for( candidateIndex = 0; candidateIndex < max; ++candidateIndex ) for( candidateIndex = 0; candidateIndex < max; candidateIndex++ )
{ {
texture_t *tex = anims[candidateIndex]; texture_t *tex = anims[candidateIndex];
@ -2301,13 +2260,11 @@ static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
tex->anim_next = anims[( candidateIndex + 1 ) % max]; tex->anim_next = anims[( candidateIndex + 1 ) % max];
if( altmax > 0 ) if( altmax > 0 )
{
tex->alternate_anims = altanims[0]; tex->alternate_anims = altanims[0];
} }
}
// Link all alternate animated frames together. // Link all alternate animated frames together.
for( candidateIndex = 0; candidateIndex < altmax; ++candidateIndex ) for( candidateIndex = 0; candidateIndex < altmax; candidateIndex++ )
{ {
texture_t *tex = altanims[candidateIndex]; texture_t *tex = altanims[candidateIndex];
@ -2327,18 +2284,16 @@ static void Mod_SequenceAnimatedTexture( int baseTextureIndex )
tex->anim_next = altanims[( candidateIndex + 1 ) % altmax]; tex->anim_next = altanims[( candidateIndex + 1 ) % altmax];
if( max > 0 ) if( max > 0 )
{
tex->alternate_anims = anims[0]; tex->alternate_anims = anims[0];
} }
} }
}
static void Mod_SequenceAllAnimatedTextures( void ) static void Mod_SequenceAllAnimatedTextures( void )
{ {
for( int index = 0; index < loadmodel->numtextures; ++index ) int i;
{
Mod_SequenceAnimatedTexture( index ); for( i = 0; i < loadmodel->numtextures; i++ )
} Mod_SequenceAnimatedTexture( i );
} }
/* /*
@ -2348,7 +2303,7 @@ Mod_LoadTextures
*/ */
static void Mod_LoadTextures( dbspmodel_t *bmod ) static void Mod_LoadTextures( dbspmodel_t *bmod )
{ {
dmiptexlump_t* lump = NULL; dmiptexlump_t *lump;
#if !XASH_DEDICATED #if !XASH_DEDICATED
// release old sky layers first // release old sky layers first
@ -2369,7 +2324,7 @@ static void Mod_LoadTextures( dbspmodel_t* bmod )
} }
loadmodel->textures = (texture_t **)Mem_Calloc( loadmodel->mempool, lump->nummiptex * sizeof( texture_t * )); loadmodel->textures = (texture_t **)Mem_Calloc( loadmodel->mempool, lump->nummiptex * sizeof( texture_t * ));
loadmodel->numtextures = loadmodel->textures ? lump->nummiptex : 0; loadmodel->numtextures = lump->nummiptex;
Mod_LoadAllTextures( bmod ); Mod_LoadAllTextures( bmod );
Mod_SequenceAllAnimatedTextures(); Mod_SequenceAllAnimatedTextures();

Loading…
Cancel
Save