diff options
| author | tma <tma@edf5b092-35ff-0310-97b2-ce42778d08ea> | 2007-08-26 21:27:46 +0000 | 
|---|---|---|
| committer | tma <tma@edf5b092-35ff-0310-97b2-ce42778d08ea> | 2007-08-26 21:27:46 +0000 | 
| commit | e030ec0ba762165d6ff1c24eca0f692f863daa6e (patch) | |
| tree | b016c277887b352e1c90da840cfa98ed0c3369ab /code/renderer/tr_image.c | |
| parent | 8fff917396ba80a670c301e7fd9053cdd3574091 (diff) | |
| download | ioquake3-aero-e030ec0ba762165d6ff1c24eca0f692f863daa6e.tar.gz ioquake3-aero-e030ec0ba762165d6ff1c24eca0f692f863daa6e.zip  | |
* Rewrite of R_LoadImage to make it more generic and data driven
git-svn-id: svn://svn.icculus.org/quake3/trunk@1137 edf5b092-35ff-0310-97b2-ce42778d08ea
Diffstat (limited to 'code/renderer/tr_image.c')
| -rw-r--r-- | code/renderer/tr_image.c | 111 | 
1 files changed, 75 insertions, 36 deletions
diff --git a/code/renderer/tr_image.c b/code/renderer/tr_image.c index 609b8b2..016b6f0 100644 --- a/code/renderer/tr_image.c +++ b/code/renderer/tr_image.c @@ -4376,6 +4376,27 @@ static void LoadPNG(const char *name, byte **pic, int *width, int *height)  //=================================================================== +typedef struct +{ +	char *ext; +	void (*ImageLoader)( const char *, unsigned char **, int *, int * ); +} imageExtToLoaderMap_t; + +// Note that the ordering indicates the order of preference used +// when there are multiple images of different formats available +static imageExtToLoaderMap_t imageLoaders[ ] = +{ +	{ "tga",  LoadTGA }, +	{ "jpg",  LoadJPG }, +	{ "jpeg", LoadJPG }, +	{ "png",  LoadPNG }, +	{ "pcx",  LoadPCX32 }, +	{ "bmp",  LoadBMP } +}; + +static int numImageLoaders = sizeof( imageLoaders ) / +		sizeof( imageLoaders[ 0 ] ); +  /*  =================  R_LoadImage @@ -4384,53 +4405,71 @@ Loads any of the supported image types into a cannonical  32 bit format.  =================  */ -void R_LoadImage( const char *name, byte **pic, int *width, int *height ) { -	int		len; +void R_LoadImage( const char *name, byte **pic, int *width, int *height ) +{ +	qboolean orgNameFailed = qfalse; +	int i; +	char localName[ MAX_QPATH ]; +	const char *ext;  	*pic = NULL;  	*width = 0;  	*height = 0; -	len = strlen(name); -	if (len<5) { -		return; -	} - -	if ( !Q_stricmp( name+len-4, ".tga" ) ) { -		LoadTGA( name, pic, width, height ); +	Q_strncpyz( localName, name, MAX_QPATH ); -		// This is a hack to get around the fact that some -		// baseq3 shaders refer to tga files where the images -		// are actually jpgs -		if (!*pic) { -			// try jpg in place of tga  -			char altname[MAX_QPATH]; +	ext = COM_GetExtension( localName ); -			strcpy( altname, name ); -			len = strlen( altname ); -			altname[len-3] = 'j'; -			altname[len-2] = 'p'; -			altname[len-1] = 'g'; +	if( *ext ) +	{ +		// Look for the correct loader and use it +		for( i = 0; i < numImageLoaders; i++ ) +		{ +			if( !Q_stricmp( ext, imageLoaders[ i ].ext ) ) +			{ +				// Load +				imageLoaders[ i ].ImageLoader( localName, pic, width, height ); +				break; +			} +		} -			ri.Printf( PRINT_DEVELOPER, "WARNING: %s failed, trying %s\n", name, altname ); -			LoadJPG( altname, pic, width, height ); +		// A loader was found +		if( i < numImageLoaders ) +		{ +			if( *pic == NULL ) +			{ +				// Loader failed, most likely because the file isn't there; +				// try again without the extension +				orgNameFailed = qtrue; +				COM_StripExtension( name, localName, MAX_QPATH ); +			} +			else +			{ +				// Something loaded +				return; +			}  		}  	} -	else if ( !Q_stricmp(name+len-4, ".pcx") ) -	{ -		LoadPCX32( name, pic, width, height ); -	} -	else if ( !Q_stricmp( name+len-4, ".bmp" ) ) -	{ -		LoadBMP( name, pic, width, height ); -	} -	else if ( !Q_stricmp( name+len-4, ".jpg" ) ) -	{ -		LoadJPG( name, pic, width, height ); -	} -	else if ( !Q_stricmp( name+len-4, ".png" ) ) + +	// Try and find a suitable match using all +	// the image formats supported +	for( i = 0; i < numImageLoaders; i++ )  	{ -		LoadPNG( name, pic, width, height ); +		char *altName = va( "%s.%s", localName, imageLoaders[ i ].ext ); + +		// Load +		imageLoaders[ i ].ImageLoader( altName, pic, width, height ); + +		if( *pic ) +		{ +			if( orgNameFailed ) +			{ +				ri.Printf( PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n", +						name, altName ); +			} + +			break; +		}  	}  }  | 
