Multimedia
  Home arrow Multimedia arrow Page 3 - 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Game Programming using SDL: Getting Started - Working with Video the SDL Way
    (Page 3 of 4 )

    When working with gaming libraries, one has to drop into system specific APIs(Win SDK on Windows and Xlib and et al on *nix) to access the video related functionalities. These functionalities include initializing the video, setting the best video mode and loading the bitmapped images among other things. But SDL encapsulates all these within the video sub-system. The functions that provide access to these are SDL_Init() and SDL_SetVideoMode.

    SDL_Init() initializes the sub-system that has been passed as parameter. To initialize video the parameter would be SDL_INIT_VIDEO. To elucidate:

      SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)

    would initialize video as well as audio.

    Once the video is initialized, the next obvious step is setting the best video mode. To achieve this end, SDL contains a method called SDL_SetVideoMode. This method sets up a video mode with specified width, height and bits per pixel i.e. depth. In short using this function one could set up the required resolution. The parameters include width, height, bpp (bits-per-pixel) and flags. The first three parameters take integer values. They represent height, width and depth of the screen respectively.

    The fourth parameter needs some consideration. The flags parameter defines the properties of the surface of the screen. They are eleven in number. The important and most commonly used are:

    a.      SDL_SWSURFACE:

    This instructs SDL to create the surface in the system memory. In other words, the rendering area is created using a software renderer. This is useful when support for software-based acceleration is on cards.

    b.      SDL_HWSURFACE:

    To create a surface in hardware memory i.e. the memory of the graphics card, use this value as the flag value. In other words, to support hardware acceleration use this value. It can be used with SDL_SWSURFACE to support both.

    c.      SDL_ANYFORMAT:

    When the passed depth value is unsupported on the target machine, then SDL emulates it with a shadow surface, i.e. to emulate required depth, SDL would use shadows. To prevent this pass SDL_ANYFORMAT as the flag value. By using SDL_ANYFORMAT, SDL can be instructed to use the video surface even if the required depth is not available.

    d.      SDL_DOUBLEBUF:

    This flag enables hardware double buffering. The double buffering works only if called with SDL_HWSURFACE. Otherwise when the flipping function is called only updating of the surface takes place.

    e.      SDL_OPENGL:

    This flag creates an OpenGL rendering context. This is useful when SDL is used in conjunction with OpenGL.

    f.       SDL_FULLSCREEN:

    By passing this as the flag value, the mode could be changed to full screen. If SDL is unable to do so, it will use the next available higher resolution. But the window will be centered on a black screen.

    g.      SDL_NOFRAME:

    To show the window without decoration (without the title bar and frame decoration), use this as the value. By setting SDL_FULLSCREEN as the flag value, this flag is automatically set.

    All the above values are the same as that of SDL_Surface. The SDL_SetVideoMode() function returns a pointer to the structure SDL_Surface. Now let's see how to use it in a program.

    #include "SDL.h"
    #include<stdio.h>

    int main(int argc,char* argv[])
      {
       
    SDL_Surface *screen;

      /*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);
       
    }
     
    }

    If you recall, most of the above code covers the same ground as I discussed earlier in this article. The parts I want to focus on are set in bold. First a point to the structure SDL_Surface is declared. When the video mode is set, this comes into the picture. Then the video is initialized using SDL_Init(). If initialization fails, then exit the application.

    As I said before, the function to set video mode returns a pointer to the initialized SDL_Surface structure. The above code sets the resolution at 640x480 at 8 bit depth. It also sets the rendering to software based, i.e. the surface is created in system memory and not in the graphics card's memory. Now that video mode has been set we can move to the next section, which will cover loading a bitmap onto the returned surface.

    More Multimedia Articles
    More By A.P.Rajshekhar


       · 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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