q3map2/image.c: avoid the pyramid of if
authorThomas Debesse <dev@illwieckz.net>
Fri, 6 Jul 2018 18:28:42 +0000 (20:28 +0200)
committerThomas Debesse <dev@illwieckz.net>
Sun, 28 Oct 2018 22:21:24 +0000 (23:21 +0100)
tools/quake3/q3map2/image.c

index 1732867..4fb3f74 100644 (file)
@@ -398,83 +398,81 @@ image_t *ImageLoad( const char *filename ){
        image->name = safe_malloc( strlen( name ) + 1 );
        strcpy( image->name, name );
 
-       /* attempt to load tga */
-       StripExtension( name );
-       strcat( name, ".tga" );
-       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-       if ( size > 0 ) {
-               LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
-       }
-       else
-       {
+       do {
+               /* attempt to load tga */
+               StripExtension( name );
+               strcat( name, ".tga" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
+                       break;
+               }
+
                /* attempt to load png */
                StripExtension( name );
                strcat( name, ".png" );
                size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
                if ( size > 0 ) {
                        LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+                       break;
                }
-               else
-               {
-                       /* attempt to load jpg */
-                       StripExtension( name );
-                       strcat( name, ".jpg" );
-                       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-                       if ( size > 0 ) {
-                               if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
-                                       // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
-                                       Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
-                               }
-                               alphaHack = qtrue;
+
+               /* attempt to load jpg */
+               StripExtension( name );
+               strcat( name, ".jpg" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
+                               // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
+                               Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
                        }
-                       else
+                       alphaHack = qtrue;
+                       break;
+               }
+
+               /* attempt to load dds */
+               StripExtension( name );
+               strcat( name, ".dds" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+
+                       /* debug code */
+                       #if 1
                        {
-                               /* attempt to load dds */
-                               StripExtension( name );
-                               strcat( name, ".dds" );
-                               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-                               if ( size > 0 ) {
-                                       LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
-
-                                       /* debug code */
-                                       #if 1
-                                       {
-                                               ddsPF_t pf;
-                                               DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
-                                               Sys_Printf( "pf = %d\n", pf );
-                                               if ( image->width > 0 ) {
-                                                       StripExtension( name );
-                                                       strcat( name, "_converted.tga" );
-                                                       WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
-                                               }
-                                       }
-                                       #endif
-                               }
-                               else
-                               {
-                                       /* attempt to load ktx */
+                               ddsPF_t pf;
+                               DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
+                               Sys_Printf( "pf = %d\n", pf );
+                               if ( image->width > 0 ) {
                                        StripExtension( name );
-                                       strcat( name, ".ktx" );
-                                       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-                                       if ( size > 0 ) {
-                                               LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
-                                       }
-                                       #ifdef BUILD_CRUNCH
-                                       else
-                                       {
-                                               /* attempt to load crn */
-                                               StripExtension( name );
-                                               strcat( name, ".crn" );
-                                               size = vfsLoadFile( ( const char* ) name, ( void** ) &buffer, 0 );
-                                               if ( size > 0 ) {
-                                                       LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
-                                               }
-                                       }
-                                       #endif // BUILD_CRUNCH
+                                       strcat( name, "_converted.tga" );
+                                       WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
                                }
                        }
+                       #endif // debug
+                       break;
                }
-       }
+
+               /* attempt to load ktx */
+               StripExtension( name );
+               strcat( name, ".ktx" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
+                       break;
+               }
+
+               #ifdef BUILD_CRUNCH
+               /* attempt to load crn */
+               StripExtension( name );
+               strcat( name, ".crn" );
+               size = vfsLoadFile( ( const char* ) name, ( void** ) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+                       break;
+               }
+               #endif // BUILD_CRUNCH
+       } while (qfalse);
 
        /* free file buffer */
        free( buffer );