From 598278f9f54f296bf415bc8cf4435ca7b90b052d Mon Sep 17 00:00:00 2001 From: black Date: Fri, 23 Nov 2007 00:26:10 +0000 Subject: [PATCH] Add cl_dyntexture.h and .c (don't need to be added to the makefile or vc project yet). They expose a simple interface to manage dynamic textures. It supports getting the current texture handle for a dynamic texture path, linking a dynamic texture path to a texture handle and unlinking a dynamic texture path. This is pretty transparent and it could be used for arbitrary dynamic textures (even ones that aren't updated but only linked dynamically into the game world - alias texture names). This works for both skinframes and cachepics. Still contains lots of todos. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7713 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_dyntexture.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ cl_dyntexture.h | 18 +++++++++++ 2 files changed, 101 insertions(+) create mode 100644 cl_dyntexture.c create mode 100644 cl_dyntexture.h diff --git a/cl_dyntexture.c b/cl_dyntexture.c new file mode 100644 index 00000000..f2b62451 --- /dev/null +++ b/cl_dyntexture.c @@ -0,0 +1,83 @@ +// Andreas Kirsch 07 + +#include "quakedef.h" +#include "cl_dyntexture.h" + +typedef struct dyntexture_s { + // everything after DYNAMIC_TEXTURE_PATH_PREFIX + char name[ MAX_QPATH + 32 ]; + // texture pointer (points to r_texture_white at first) + rtexture_t *texture; +} dyntexture_t; + +static dyntexture_t dyntextures[ MAX_DYNAMIC_TEXTURE_COUNT ]; +static unsigned dyntexturecount; + +#define DEFAULT_DYNTEXTURE r_texture_grey128 + +static dyntexture_t * _CL_FindDynTexture( const char *name ) { + unsigned i; + dyntexture_t *dyntexture = NULL; + // some sanity checks - and make sure its actually a dynamic texture path + if( !name || strncmp( name, DYNAMIC_TEXTURE_PATH_PREFIX, sizeof( DYNAMIC_TEXTURE_PATH_PREFIX ) - 1 ) != 0 ) { + return NULL; + } + + for( i = 0 ; i < dyntexturecount ; i++ ) { + dyntexture = &dyntextures[ i ]; + if( dyntexture->name && strcmp( dyntexture->name, name ) == 0 ) { + return dyntexture; + } + } + + if( dyntexturecount == MAX_DYNAMIC_TEXTURE_COUNT ) { + // TODO: warn or expand the array, etc. + return NULL; + } + dyntexture = &dyntextures[ dyntexturecount++ ]; + strlcpy( dyntexture->name, name, sizeof( dyntexture->name ) ); + dyntexture->texture = DEFAULT_DYNTEXTURE; + return dyntexture; +} + +rtexture_t * CL_GetDynTexture( const char *name ) { + dyntexture_t *dyntexture = _CL_FindDynTexture( name ); + if( dyntexture ) { + return dyntexture->texture; + } else { + return NULL; + } +} + +void CL_LinkDynTexture( const char *name, rtexture_t *texture ) { + dyntexture_t *dyntexture; + cachepic_t *cachepic; + skinframe_t *skinframe; + + dyntexture = _CL_FindDynTexture( name ); + // TODO: assert dyntexture != NULL! + if( dyntexture->texture != texture ) { + cachepic = Draw_CachePic( name, false ); + skinframe = R_SkinFrame_Find( name, 0, 0, 0, 0, false ); + // this is kind of hacky + // TODO: assert cachepic and skinframe should be valid pointers... + + // TODO: assert cachepic->tex = dyntexture->texture + cachepic->tex = texture; + // update cachepic's size, too + cachepic->width = R_TextureWidth( texture ); + cachepic->height = R_TextureHeight( texture ); + // TODO: assert skinframe->base = dyntexture->texture + skinframe->base = texture; + // simply reset the compare* attributes of skinframe + skinframe->comparecrc = 0; + skinframe->comparewidth = skinframe->compareheight = 0; + + dyntexture->texture = texture; + } +} + +void CL_UnlinkDynTexture( const char *name ) { + CL_LinkDynTexture( name, DEFAULT_DYNTEXTURE ); +} + diff --git a/cl_dyntexture.h b/cl_dyntexture.h new file mode 100644 index 00000000..c513ab12 --- /dev/null +++ b/cl_dyntexture.h @@ -0,0 +1,18 @@ +// Andreas 'Black' Kirsch 07 +#ifndef CL_DYNTEXTURE_H +#define CL_DYNTEXTURE_H + +#define DYNAMIC_TEXTURE_PATH_PREFIX "_dynamic/" +#define MAX_DYNAMIC_TEXTURE_COUNT 64 + +// return a valid texture handle for a dynamic texture (might be filler texture if it hasnt been initialized yet) +// textureflags will be ignored though for now [11/22/2007 Black] +rtexture_t * CL_GetDynTexture( const char *name ); + +// link a texture handle as dynamic texture and update texture handles in the renderer and draw_* accordingly +void CL_LinkDynTexture( const char *name, rtexture_t *texture ); + +// unlink a texture handle from its name +void CL_UnlinkDynTexture( const char *name ); + +#endif \ No newline at end of file -- 2.39.2