Programming with SDL
Tutorial 3 - Sprites
Introduction
Well now, you can manage some basic events, draw some stuff, and setup a window. Drawing a couple pixels here and there gets boring after a while - so what now?
This tutorial will cover sprites, and related SDL functions for making Sprites move around on the screen. The next few tutorials will involve the creation of a full SDL application program(actually a game). You will probably get an idea for what it is at the end of this tutorial, especially when you see the example Bitmaps included.
Cleaning up the Code
By now you've surely realized that the main program is getting quite crowded. First, it would be nice to seperate out the SDL initialization code. For the program we're developing, it would be nice to be able to pass command line arguments to this function. I'm going to create a new function InitializeSDL, and pass that function argv and argc. This function will return nonzero if there is an error.
It's also a good idea to make sure that your code will pick up on errors, and tell the user about them instead of crashing. When an SDL Error occurs, the SDL_GetError function will return a string pointer that can be printed to the screen. It takes no parameters. Using SDL_GetError allows for better debugging both in development and in release.
Loading a Bitmap
Using SDL, surfaces can be used for more than just manipulating the screen. Drawing surfaces have many uses, and one of those is for sprites(or bitmapped images). The first step is to create the BMP files. I've included many of the BMP files that will be needed for the application we are developing.
After creating the BMP files, loading them is easy. The function SDL_LoadBMP will load a bitmap based on a file parameter. If the file can't be found, SDL_LoadBMP will return NULL.
bumper = SDL_LoadBMP("bumper.bmp");
if(!bumper) return 0;
This code segment will load a bitmap and check for errors.
Clearing the Screen
So, now you have your screen surface, and have loaded a couple bitmaps. And are ready to animate. In order to prepare to draw, you probably want to write some type of background(could be a bitmap), or simply fill the screen with a fixed color. SDL doesn't have a "Clear Screen" function, but, it does provide a method of setting an entire surface to one color.
Recall, the function SDL_MapRGB can be used to determine a color value from a RGB group. The parameters to SDL_MapRGB are the format substructure of the Surface you wish to fill, and then the Red, Green, and Blue values respectively.
SDL_FillRect will fill a surface with a color. This function takes three parameters: the surface to fill, a SDL rectangle structure pointer, and an integer color. If the structure pointer passed to the function is NULL, the entire surface is filled.
Painting the Sprite
Now, the screen is clear, the bitmap is loaded. It's time to paint. SDL provides a simple method for painting sprites. SDL_BlitSurface takes the sprite to be drawn, a rectangle position definition of which portion of the sprite to draw, a surface to draw to, and a destination rectangle pointer. If the rectangle definition pointer of the area of the sprite to draw is NULL, then the entire image is drawn to the screen.
For readability, this function can be easily inserted into a draw spite or draw image function like the one bellow:
void DrawSprite( SDL_Surface *surface, int x, int y)
{
SDL_Rect destination;
destination.x = x;
destination.y = y;
SDL_BlitSurface( surface, NULL, mainScreen, &destination );
}
This function will draw the image specified by surface onto the main screen
at the specified x/y coordinates. Remember, before drawing to a surface, it
should be locked if needed.
A Note on Rectangles:
Many SDL functions use the SDL_Rect structure. This structure contains four elements: x - the horizontal position indicator, y - the vertical position indicator(starts at zero at the top of the screen and numbers down), w - the width component, and h - the height component.
Finishing Up
First, I am aware that these tutorials "skip" around a bit. Many of the smaller functions in SDL do not qualify for their own tutorial. Others, would qualify for several. I've tried to organize this mass of information as best as possible by covering smaller topics as they come up.Any functions referenced in these tutorials can be referenced using the SDL manual pages. Next time, I'll be covering Alpha blending, transparency, and go into handling surfaces in a bit more detail.
Functions Covered in this Tutorial: SDL_GetError , SDL_FillRect, SDL_MapRGB, SDL_LoadBMP, SDL_BlitSurface
Code: example3.tgz