Multimedia
  Home arrow Multimedia arrow Page 3 - Game Programming using SDL: Raw Graphi...
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 
 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: Raw Graphics and Event Handling
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2007-02-05

    Table of Contents:
  • Game Programming using SDL: Raw Graphics and Event Handling
  • Raw Graphics: Writing Directly onto the Display
  • Putting the Functions to Use
  • Handling the Keyboard the SDL Way

  • 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Game Programming using SDL: Raw Graphics and Event Handling - Putting the Functions to Use
    (Page 3 of 4 )

    Now that both functions have been explained, let's see how to put one of them to use, i.e. putpixel(). For this I am defining a method called putyellowpixel() that places a yellow pixel at the center of the screen. It doesn't accept any parameter nor does it return any value.

    void putyellowpixel()
    {
     
    int x, y;
      
    Uint32 yellow;

      /* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
     
    Note: If the display is palettized, you must set the palette first.
     
    */
     
    yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);

      x = screen->w / 2;
     
    y = screen->h / 2;

      /* Lock the screen for direct access to the pixels */
     
    if ( SDL_MUSTLOCK(screen) ) {
       
    if ( SDL_LockSurface(screen) < 0 ) {
         
    fprintf(stderr, "Can't lock screen: %sn", SDL_GetError());
         
    return;
       
    }
     
    }

      putpixel(screen, x, y, yellow);

      if ( SDL_MUSTLOCK(screen) ) {
       
    SDL_UnlockSurface(screen);
     
    }
     
    /* Update just the part of the display that we've changed */
     
    SDL_UpdateRect(screen, x, y, 1, 1);

      return;

    }

    To get the yellow color, the SDL_MapRGB() has to be used. The SDL_PixelFormat is the first parameter. It stores surface format information. The next three parameters correspond to the red, blue and green components of the color. The return value is the actual color corresponding to the passed color components in hexadecimal format, as follows:

    yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);

    Once the color has been retrieved, the next step is to get the required x and y coordinates, which is achieved by the following statement:

    x = screen->w / 2;
    y = screen->h / 2;

    then the screen surface is locked. If this is not done, then corruption of the SDL_Surface structure could happen, causing instability of the game as putpixel works on the address of the pixel directly. This is done by:

    SDL_MUSTLOCK(screen);

    The next step is to call the putpixel. Once putpixel has returned, then unlock the surface and update the surface. That completes placing a pixel directly onto the surface. The next section will focus on event handling with reference to the keyboard.

    More Multimedia Articles
    More By A.P.Rajshekhar


       · In this article I have discussed about event handling and handling pixel based...
     

       

    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 3 hosted by Hostway