Installation‎ > ‎

SDL_image Installation

SDL_image is an image file loading library.
It loads images as SDL surfaces, and supports the following formats: BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, XCF, XPM, XV

As of SDL_image 1.2.5, JPEG, PNG, and TIFF image loading libraries are dynamically loaded, so if you don't need to load those formats, you don't need to include those shared libraries. libpng depends on libz, and libtiff depends on both libz and libjpeg.

You may visit the official SDL website and download the latest version.    However , it is suggested to follow the procedures and download the file from here.    So you may avoid some unnecessary troubles.

Install SDL_image into your Library :
1. Download this zip file and unzip it into your library folder , such as .\My_Library 1296k Download

2. Download zip file here , and unzip it into your lib folder : .\My_Library\SDL-1.2.14\lib 304k Download

3. Modify the Property Sheet of your project :
Default : Microsoft.Cpp.Win32.user  or Your new add "Property Sheet For SDL"

a) Right Click on one Property Sheet and select "Properties"
b) Select VC++ Directories -> Include Directories -> New Add a Include Directory for .\My_Library\SDL_image-1.2.5
c) Since
.\My_Library\SDL-1.2.14\lib already included in the Library Directory , so here does not need to do it
d) Select Linker -> Input -> Additional Dependencies -> New Add SDL_image.lib

e) Click on Save for Property Sheet
f) Copy SDL_image.dll from
.\My_Library\SDL-1.2.14\lib to your Project Directory

Create a Head File into this Project

#ifndef TOOLS_H
#define TOOLS_H

#include "SDL.h"
#include "SDL_image.h"

// This function load a image file to a surface
// Set bCKey with colorkey (R,G,B) to clear a color on the image
// Set alpha value for transparency 0(No transparent) ~ 255(Ivisible)
inline SDL_Surface *ImgLoader(char *file,bool bCKey, int r , int g , int b , int alpha)
    SDL_Surface *pic;
    pic = IMG_Load(file); // From SDL_image.h , load the image to pic
    // Log error message into stderr.txt if happened
    if(pic==NULL) fprintf(stderr,"Missing image %s : %s\n",file,IMG_GetError());
    if( bCKey ) {
        // Sets the color key (transparent pixel) in a blittable surface and RLE acceleration.
    if(alpha) SDL_SetAlpha(pic, SDL_SRCALPHA|SDL_RLEACCEL , 255 - alpha);
    // This function takes a surface and copies it to a new surface of the
    // pixel format and colors of the video framebuffer, suitable for fast
    // blitting onto the display surface.
    pic = SDL_DisplayFormat(pic);
    return (pic);


Additional Program Code that make life eazier
// Load a normal picture into a surface
inline SDL_Surface *ImgLoader(char *file)      
                { return ImgLoader(file,1,0,0,0,0) ; }
// Load a pic & set the transparent color to (255,255,255) , no alpha
inline SDL_Surface *ImgLoader(char *file,bool bCKey)
                { return ImgLoader(file,1,255,255,255,0) ; }

5. Create a C++ File into this Project

#include "SDL.h"
#include "SDL_image.h"

#include "tools.h"

int main(int argc, char *argv[])

int pidx = 0 ;
SDL_Surface *screen ;

enum walk { justice1 , justice2 , justice3 , justice4
    , justice5 , justice6 , justice_No  };

SDL_Surface *anim[justice_No];

SDL_Rect animRect ;
animRect.x = 160 ;
animRect.y = 160 ;


if( SDL_Init(SDL_INIT_VIDEO) < 0 ) exit(1);

SDL_WM_SetCaption("SDL Window", NULL);

screen = SDL_SetVideoMode( 400 , 300 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);

// Use the new add image loader function to load GIF , JPG , PNG image file.
// Here shows two equivalent ways to load a image file that has a white
// background (RGB=255,255,255)
anim[0] = ImgLoader("./anim/justice1.gif",1,255,255,255,0);
anim[1] = ImgLoader("./anim/justice2.gif",1,255,255,255,0);
anim[2] = ImgLoader("./anim/justice3.gif",1,255,255,255,0);
anim[3] = ImgLoader("./anim/justice4.gif",1);
anim[4] = ImgLoader("./anim/justice5.gif",1);
anim[5] = ImgLoader("./anim/justice6.gif",1);

for(int tick=0 ; tick<1000 ; tick++ ) {
    SDL_FillRect(screen , NULL , 0x221122);
    SDL_BlitSurface( anim[pidx] , NULL , screen , &animRect );
    SDL_Delay(20); // Delay 20 msec
    if(pidx >= justice_No) pidx = 0;

}; // for(int tick=0 ; tick<1000 ; tick++ ) { END

return 0;

6. Create a Folder "anim" in the same directory of executable file , then copy these images into it


7. Copy "SDL.dll" and "SDL_image.dll" into the same directory of executable file , then build and run this project

You will get this result if everything is right.
This little girl will keep walking until time up(20sec in here)

Copyright © 2011 bdragon All rights reserved.
Bdragon Ho,
Jan 23, 2011, 8:51 PM
Bdragon Ho,
Jan 23, 2011, 9:35 PM