Home arrow Multimedia arrow Page 4 - Game Programming using SDL: Getting Started

SDL in Real World: Loading Sprites - Multimedia

Game programmers using OpenGL have often been forced to make a choice between using a library that is platform independent but doesn't use all the available resources, or powerful but platform dependent. Simple Directmedia Layer (SML) offers a third way. This article will give you a taste of its capabilities.

TABLE OF CONTENTS:
  1. Game Programming using SDL: Getting Started
  2. SDL: The Services Provided
  3. Working with Video the SDL Way
  4. SDL in Real World: Loading Sprites
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 12
December 27, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In a game a sprite can be anything from an image to a 3-D model. To keep things simple, let's consider a bitmap as a sprite. In SDL loading a bitmap is like loading a file and reading its content using the standard C library.

To load a bitmap onto the surface the following functions come handy:

  1. SDL_LoadBMP:

This function forms the basis of loading a bitmap. It returns a pointer to the surface for the name of the bitmap given as the parameter. If the loading of the bitmap has not been successful, null is returned. For example, if the file parameter is "Tux.bmp," then the following code will load it:

  SDL_Surface *image=SDL_LoadBMP("Tux.bmp");

  1. SDL_SetColors:

The default palette would be an 8x8x4 color cube. To get better color matching we must palletize the image itself. For this the SDL_Setcolors function is quite useful. The first parameter is the surface for which the palette has to be created, second parameter is the SDL color component which decides the number of displayable colors. The third and fourth parameters set the range of colors to be used.

To palletize the loaded image, the first parameter would be the surface returned by SDL's initialization routine, the second is the color component of the image to be palletized, the third would be 0 (that would be the lower range of the colors to be used), and the maximum value of the color palette of the image would be the fourth parameter. To put it in code:

  SDL_SetColors(screen, image->format->palette->colors, 0,image->format->palette->ncolors);

where screen is the surface returned by the initialization and image is the loaded bitmap.

  1. SDL_BlitSurface:

This function performs a fast blit from the source surface to the destination surface. The parameters are source surface, source rectangle, destination surface and destination rectangle. If the source and destination rectangle are specified as null, the entire surface is copied. For example the following code copies the loaded image surface to the screen surface:

  SDL_BlitSurface(image, NULL, screen, NULL);

  1. SDL_UpdateRect:

Once the loaded image surface is copied onto the screen surface, it must be ensured that the screen display is updated accordingly. The surface to be updated is the first parameter, the rectangle of the screen to be updated is specified as the second parameter. The following fragment updates the screen according to the height and width of the loaded image:

  SDL_UpdateRect(screen, 0, 0, image->w, image->h);

 

  1. SDL_FreeSurface:

Once work is completed with the loaded image, the surface has to be freed so that the memory occupied by the surface is released. To free a surface, SDL library contains SDL_FreeSurface. The parameter is the surface to be freed. In code it would be:

  SDL_FreeSurface(image);

where image is the surface to be freed.

Now that the functions to be used have been introduced, let's see them in action. First define a function that loads a bitmap image passed to it as a parameter.

void display_bmp(char *file_name)
  {
   
SDL_Surface *image;

    /* Load the BMP file into a surface */
   
image = SDL_LoadBMP(file_name);
   
if (image == NULL) {
     
fprintf(stderr, "Couldn't load %s: %sn", file_name, SDL_GetError());
     
return;
   
}

    /*
   
* Palettized screen modes will have a default palette (a standard
   
* 8*8*4 colour cube), but if the image is palettized as well we can
   
* use that palette for a nicer colour matching
   
*/
   
if (image->format->palette && screen->format->palette) {
       
SDL_SetColors(screen, image->format->palette->colors, 0, image->format->palette->ncolors);
    
}

    /* Blit onto the screen surface */
   
if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)
     
fprintf(stderr, "BlitSurface error: %sn", SDL_GetError());

    SDL_UpdateRect(screen, 0, 0, image->w, image->h);

    /* Free the allocated BMP surface */
   
SDL_FreeSurface(image);
 
}

As you can observe the code is a compilation of all the functions I had discussed earlier. The only difference is that error handling code has been used. To use it first declare a global variable of the type SDL_Surface:

  SDL_Surface *screen=NULL;

Then call the display_bmp() as follows:

int main(int argc,char* argv[])
 
{
   
/*variable to hold the file name of the image to be loaded
   
*In real world error handling code would precede this
    */
   
char* filename="Tux.bmp";

    /*The following code does the initialization for Audio and Video*/
   
int i_error=SDL_Init(SDL_INIT_VIDEO);

    /*If initialization is unsuccessful, then quit */
   
if(i_error==-1)
     
exit(1);

    atexit(SDL_Quit);

    /*
   
* Initialize the display in a 640x480 8-bit palettized mode,
   
* requesting a software surface
   
*/
   
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
   
if ( screen == NULL )
   
{
     
fprintf(stderr, "Couldn't set 640x480x8 video mode: %sn",SDL_GetError());
     
exit(1);
   
}

    /* Now call the function to load the image and copy it to the screen surface*/
   
load_bmp(filename);

  }

That brings us to the end of this part of the discussion on SDL. In the next part I will discuss working at the pixel level and handling keyboard events as well as details of the initialization of SDL itself. Till next time...



 
 
>>> More Multimedia Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MULTIMEDIA ARTICLES

- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: