Though SDL natively supports only .bmp files, on using the SDL_image extension library, we are able to load BMP, PNM, XPM, LBM, PCX, GIF, JPEG, TGA and PNG files.
SDL_Surface * IMG_Load(const char *file);
This function takes a string that holds the name of a graphics file that you want to load. If successful, it will return a pointer to an SDL_Surface that contains that newly loaded image. If there is some sort of problem, the function will return NULL.
This function looks very much like SDL_LoadBMP. The only difference is that it can load images other than those with a .bmp extension.
The example below illustrates the above explanations.
#include <sdl/sdl.h>
#include "sdl_image.h"
//images as surfaces
SDL_Surface *screen=NULL;
SDL_Surface *image=NULL;
int main(int argc, char* argv[])
{
char done;
SDL_Init(SDL_INIT_VIDEO);
//set up screen
screen=SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
//load image
image=IMG_Load("image.png");
if(image==0)
{
return(1);
}
done = 0;
SDL_Rect pos;
pos.x=0;
pos.y=0;
SDL_Event event;
while(done!=1)
{
while(SDL_PollEvent(&event))
{
if(event.type ==SDL_QUIT)
{
done=1;
}
}
//apply image to screen
SDL_BlitSurface(image, 0, screen, &pos);
//Update screen
SDL_Flip(screen);
}
SDL_Quit();
return(0);
}
The source code is available at:
http://www.mediafire.com/?spk6f4poay6j5d1
No comments:
Post a Comment