[SDL] LoadIMG

Hri hr_hristov at hotmail.com
Sat May 10 01:37:01 PDT 2003


Hello John, below is my function LoadIMG. Well, it is not 100% mine
(maybe just 5%).
Since I'm completely new to this news group I'm not willing to post
large binary files ;) I'll try to make a small version of the program to
see what happens then and if the problem persist I'll post it here as
you tell me.
I  use SDL_image to load .jpg images.


SDL_Surface *LoadIMG( char *FileName, ColorRGB transparentColor )
{
 SDL_Surface *temp = NULL;
 SDL_Surface *ready = NULL;

 // Load image with SDL_image call
 temp = IMG_Load( FileName );
 if( temp == NULL )
 {
  printf( "Image file load failed: %s\n",
       IMG_GetError()
     );
  exit( 2 );
 }
 // Set transparent color for this surface (image)
 if ( SDL_SetColorKey( temp, SDL_SRCCOLORKEY,
        SDL_MapRGB( temp->format,
           transparentColor.r,
           transparentColor.g,
           transparentColor.b
         )
         ) == -1 )
  printf( "Failed to set transparent color: %s\n",
       SDL_GetError()
     );

 // Set display format for fast blitting
 ready = SDL_DisplayFormat( temp );
 if( ready == NULL )
 {
  printf( "Image display format conversion failed: %s\n",
       SDL_GetError()
     );
  exit( 3 );
 }

 return ready;
}

I use two functions like the one bellow to load a new set of images. The
only difference is that the macros IMAGE0*0* are different. Such macro
is defined like
#define IMAGE0101 "./data/images/image01.jpg". For later versions I
plant to generate the strings or to look for them in a directory or to
read their names/paths from a file. What I am basically trying to do is
an image viewer with special effects and my real goal is to learn how to
use SDL to make a more productive program, that needs to load
dynamically lots of images.

void LoadImageSet01()
{
        // the following loop doesn't help either
for ( int index = 0; index < 15; index ++ )
{
if ( images[ index ] != NULL )
SDL_FreeSurface( images[ index ] );
}
// Set transparent key color to blue (255)
ColorRGB transparent;
transparent.r = 0;
transparent.g = 0;
transparent.b = 255;

// Load images
images[  0 ] = LoadIMG( IMAGE0101, transparent );
images[  1 ] = LoadIMG( IMAGE0102, transparent );
images[  2 ] = LoadIMG( IMAGE0103, transparent );
images[  3 ] = LoadIMG( IMAGE0104, transparent );
images[  4 ] = LoadIMG( IMAGE0105, transparent );
images[  5 ] = LoadIMG( IMAGE0106, transparent );
images[  6 ] = LoadIMG( IMAGE0107, transparent );
images[  7 ] = LoadIMG( IMAGE0108, transparent );
images[  8 ] = LoadIMG( IMAGE0109, transparent );
images[  9 ] = LoadIMG( IMAGE0110, transparent );
images[ 10 ] = LoadIMG( IMAGE0111, transparent );
images[ 11 ] = LoadIMG( IMAGE0112, transparent );
images[ 12 ] = LoadIMG( IMAGE0113, transparent );
images[ 13 ] = LoadIMG( IMAGE0114, transparent );
images[ 14 ] = LoadIMG( IMAGE0115, transparent );
}








More information about the SDL mailing list