Multimedia
  Home arrow Multimedia arrow Page 3 - Game Programming using SDL: Raw Graphics and Event Handling
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
MULTIMEDIA

Game Programming using SDL: Raw Graphics and Event Handling
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek