Multimedia
  Home arrow Multimedia arrow Page 4 - Game Programming using SDL: Getting St...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MULTIMEDIA

Game Programming using SDL: Getting Started
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-12-27

    Table of Contents:
  • Game Programming using SDL: Getting Started
  • SDL: The Services Provided
  • Working with Video the SDL Way
  • SDL in Real World: Loading Sprites

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Guide to Virtual Infrastructure Implementation by VMware

    Game Programming using SDL: Getting Started - SDL in Real World: Loading Sprites
    (Page 4 of 4 )

    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...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · SDL greatly simplifies game development. In this article I have discussed the basics...
       · Thank you, I have been looking for game programming tutarials. I don't want to use...
     

       

    MULTIMEDIA ARTICLES

    - 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




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway