r1137 - in trunk: . code/qcommon code/renderer

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Sun Aug 26 17:27:46 EDT 2007


Author: tma
Date: 2007-08-26 17:27:46 -0400 (Sun, 26 Aug 2007)
New Revision: 1137

Modified:
   trunk/README
   trunk/code/qcommon/files.c
   trunk/code/qcommon/q_shared.c
   trunk/code/qcommon/q_shared.h
   trunk/code/renderer/tr_image.c
   trunk/code/renderer/tr_shader.c
Log:
* Rewrite of R_LoadImage to make it more generic and data driven


Modified: trunk/README
===================================================================
--- trunk/README	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/README	2007-08-26 21:27:46 UTC (rev 1137)
@@ -314,10 +314,25 @@
 
 PNG support
   ioquake3 supports the use of PNG (Portable Network Graphic) images as
-  textures. It should be noted that the use of such images in a maps will
+  textures. It should be noted that the use of such images in a map will
   result in missing placeholder textures where the map is used with the id
   Quake 3 client or earlier versions of ioquake3.
 
+  Recent versions of GtkRadiant and q3map2 support PNG images without
+  modification. However GtkRadiant is not aware that PNG textures are supported
+  by ioquake3. To change this behaviour open the file 'q3.game' in the 'games'
+  directory of the GtkRadiant base directory with an editor and change the
+  line:
+
+    texturetypes="tga jpg"
+
+  to
+
+    texturetypes="tga jpg png"
+
+  Restart GtkRadiant and PNG textures are now available.
+
+
 ------------------------------------------------------------- Contributing -----
 
 Please send all patches to bugzilla (https://bugzilla.icculus.org), or join the

Modified: trunk/code/qcommon/files.c
===================================================================
--- trunk/code/qcommon/files.c	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/code/qcommon/files.c	2007-08-26 21:27:46 UTC (rev 1137)
@@ -1207,7 +1207,6 @@
 		}		
 	}
 	
-	Com_DPrintf ("Can't find %s\n", filename);
 #ifdef FS_MISSING
 	if (missingFiles) {
 		fprintf(missingFiles, "%s\n", filename);

Modified: trunk/code/qcommon/q_shared.c
===================================================================
--- trunk/code/qcommon/q_shared.c	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/code/qcommon/q_shared.c	2007-08-26 21:27:46 UTC (rev 1137)
@@ -55,6 +55,28 @@
 
 /*
 ============
+COM_GetExtension
+============
+*/
+const char *COM_GetExtension( const char *name ) {
+	int length, i;
+
+	length = strlen(name)-1;
+	i = length;
+
+	while (name[i] != '.')
+	{
+		i--;
+		if (name[i] == '/' || i == 0)
+			return ""; // no extension
+	}
+
+	return &name[i+1];
+}
+
+
+/*
+============
 COM_StripExtension
 ============
 */

Modified: trunk/code/qcommon/q_shared.h
===================================================================
--- trunk/code/qcommon/q_shared.h	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/code/qcommon/q_shared.h	2007-08-26 21:27:46 UTC (rev 1137)
@@ -599,6 +599,7 @@
 float Com_Clamp( float min, float max, float value );
 
 char	*COM_SkipPath( char *pathname );
+const char	*COM_GetExtension( const char *name );
 void	COM_StripExtension(const char *in, char *out, int destsize);
 void	COM_DefaultExtension( char *path, int maxSize, const char *extension );
 

Modified: trunk/code/renderer/tr_image.c
===================================================================
--- trunk/code/renderer/tr_image.c	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/code/renderer/tr_image.c	2007-08-26 21:27:46 UTC (rev 1137)
@@ -4376,6 +4376,27 @@
 
 //===================================================================
 
+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,54 +4405,72 @@
 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;
+	Q_strncpyz( localName, name, MAX_QPATH );
+
+	ext = COM_GetExtension( localName );
+
+	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;
+			}
+		}
+
+		// 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;
+			}
+		}
 	}
 
-	if ( !Q_stricmp( name+len-4, ".tga" ) ) {
-		LoadTGA( name, pic, width, height );
+	// Try and find a suitable match using all
+	// the image formats supported
+	for( i = 0; i < numImageLoaders; i++ )
+	{
+		char *altName = va( "%s.%s", localName, imageLoaders[ i ].ext );
 
-		// 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];
+		// Load
+		imageLoaders[ i ].ImageLoader( altName, pic, width, height );
 
-			strcpy( altname, name );
-			len = strlen( altname );
-			altname[len-3] = 'j';
-			altname[len-2] = 'p';
-			altname[len-1] = 'g';
+		if( *pic )
+		{
+			if( orgNameFailed )
+			{
+				ri.Printf( PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n",
+						name, altName );
+			}
 
-			ri.Printf( PRINT_DEVELOPER, "WARNING: %s failed, trying %s\n", name, altname );
-			LoadJPG( altname, pic, width, height );
+			break;
 		}
 	}
-	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" ) )
-	{
-		LoadPNG( name, pic, width, height );
-	}
 }
 
 

Modified: trunk/code/renderer/tr_shader.c
===================================================================
--- trunk/code/renderer/tr_shader.c	2007-08-24 13:54:23 UTC (rev 1136)
+++ trunk/code/renderer/tr_shader.c	2007-08-26 21:27:46 UTC (rev 1137)
@@ -2416,7 +2416,6 @@
 */
 shader_t *R_FindShader( const char *name, int lightmapIndex, qboolean mipRawImage ) {
 	char		strippedName[MAX_QPATH];
-	char		fileName[MAX_QPATH];
 	int			i, hash;
 	char		*shaderText;
 	image_t		*image;
@@ -2494,13 +2493,11 @@
 
 	//
 	// if not defined in the in-memory shader descriptions,
-	// look for a single TGA, BMP, or PCX
+	// look for a single supported image file
 	//
-	Q_strncpyz( fileName, name, sizeof( fileName ) );
-	COM_DefaultExtension( fileName, sizeof( fileName ), ".tga" );
-	image = R_FindImageFile( fileName, mipRawImage, mipRawImage, mipRawImage ? GL_REPEAT : GL_CLAMP );
+	image = R_FindImageFile( name, mipRawImage, mipRawImage, mipRawImage ? GL_REPEAT : GL_CLAMP );
 	if ( !image ) {
-		ri.Printf( PRINT_DEVELOPER, "Couldn't find image for shader %s\n", name );
+		ri.Printf( PRINT_DEVELOPER, "Couldn't find image file for shader %s\n", name );
 		shader.defaultShader = qtrue;
 		return FinishShader();
 	}




More information about the quake3-commits mailing list