Friday, July 8, 2011

Loading an image on the SDL screen

The knowledge of the following are required to some extent for loading an image on the screen.

SDL_Surface

It is a structure which acts as the stock and trade of SDL’s video subsystem. It abstracts a rectangular block of pixels. There is only one SDL_Surface that represents the actual display area. All of the other surfaces are buffers for storing pixel (such as images to be displayed) data that will eventually be shown on the main display.

SDL_Rect

The SDL video subsystem, being 2D in nature, deals primarily with copying rectangular blocks of pixels from one

SDL_Surface to another. This has been the way of dealing with raster displays since their initiation. It is The Way Things are done.

Naturally, every API or library for dealing with 2D graphics needs to have a structure that can contain a rectangular area. The term “rectangular area” has a special meaning when you are dealing with raster displays. In this case, you are talking about rectangles whose sides consist of horizontal and vertical segments. In SDL, this structure is SDL_Rect, and it looks like this:

typedef struct{

Sint16 x, y;

Uint16 w, h;

} SDL_Rect;

Since the rectangle is aligned so that each of the edges are parallel to either the top or left edge of the screen, you need only two points to define it. Within the SDL_Rect structure, these points are defined as (x,y) and (x+w,y+h). The other two points are (x+w,y) and (x,y+h).

The examples of what SDL_Rect can and cannot represent as far as rectangles are concerned:







SDL_LoadBMP

If you have bitmapped images to be loaded for the game . SDL has supplied a function just for this occasion called SDL_LoadBMP. It is somewhat limited in its use because it will only load .bmp files. For the ability to load other types of images, such as JPGs, you can use SDL_image. But, for now, just stick to .bmp files.

Here is the prototype for SDL_LoadBMP.

SDL_Surface * = SDL_LoadBMP(const char *file);

Such as

bitmap=SDL_LoadBMP("bitmap.bmp");

This function takes a single parameter named file, which is a string that specifies which file you want to load. The value returned by this function is a pointer to a new surface that contains the loaded bitmap file. If this function fails, the return value is NULL.

SDL_BlitSurface

One of the method of creating graphics with SDL is by blitting. The word “blit” comes from the words “Block Transfer”—in the past it was typically abbreviated BLT.To make BLT pronounceable “i” was inserted to make the word “blit” .

In order to blit, you need two surfaces (one source and one destination) and two rectangles (one each for the source and destination). Once you’ve got this information, you can call SDL_BlitSurface.

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

This function takes four parameters—a pointer to the source surface (src), a pointer to the source rectangle (srcrect), a pointer to the destination surface (dst), and a pointer to the destination rectangle (dstrect). srcrect and dstrect do not necessarily have to point to rectangles that are the same size (in other words, the same width and height), and these parameters can be NULL to indicate that the entire surface is to be used either as the source or the destination.

The return value of SDL_BlitSurface is an int. If the return value is 0, then everything is cool. If the return value is -1, there is an error; if it is –2, one of the surfaces is in video memory, which was lost, and you need to restore it.

SDL_Flip

It completely redraw the entire screen. The images thus loaded or any changes made will not be observed until the screen is flipped.

int SDL_Flip(SDL_Surface *screen);



Now you are ready to load an image ( but now just bitmap image ).


#include

//images as surfaces

SDL_Surface *screen=0;

SDL_Surface *bitmap=0;

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

bitmap=SDL_LoadBMP("bitmap.bmp");

if(bitmap==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(bitmap, 0, screen, &pos);

//Update screen

SDL_Flip(screen);

}


SDL_Quit();

return(0);

}


With the codes above you'll end up with sdl screen with a bitmap image loaded on it.


The source code is available at:

http://www.mediafire.com/?kxymynsdgtlzx3m

No comments:

Post a Comment