Multimedia
  Home arrow Multimedia arrow Page 2 - 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 - Raw Graphics: Writing Directly onto the Display
    ( Page 2 of 4 )

    Though the SDL Graphics APIs provide pretty high level functionality, abstracting off all the low-level details, there are times when abstraction is not required. For this purpose also there are ways to do what you want. These ways don't exist as a library function but as separate functions that must be embedded into your program. The functions are freely available; for completeness I am including them here. These functions are:

    1. getpixel():

      This function is useful if pixel value must be obtained from given coordinates represented by X and Y values on the display. It works on a single pixel at a time. The first parameter is the surface from which the value has to be obtained. This is represented by a pointer to the SDL_Surface. The next two integer parameters represent the x and y coordinates from where the pixel value has to be obtained. The return value is a Uint32 representing the value of the pixel. Following is the code:

      /*
      * Return the pixel value at (x, y)
      * NOTE: The surface must be locked before calling this!
      */
      Uint32 getpixel(SDL_Surface *surface, int x, int y)
      {
        int bpp = surface->format->BytesPerPixel;
        /* Here p is the address to the pixel we want to retrieve */
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

        switch(bpp) {
          case 1:
            return *p;

          case 2:
            return *(Uint16 *)p;

          case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
              return p[0] << 16 | p[1] << 8 | p[2];
            else
              return p[0] | p[1] << 8 | p[2] << 16;

          case 4:
            return *(Uint32 *)p;

          default:
            return 0; /* shouldn't happen, but avoids warnings */
        }
      }

      The first thing to do is obtain the depth represented by BytesPerPixel. This is accomplished by the first statement:

      int bpp = surface->format->BytesPerPixel;

      The next statement is self explanatory. To get the address of the pixel, the pitch of the passed surface is multiplied by the value of the Y coordinate. The depth is multiplied by the X coordinate and the resulting values are added with pixel data to the surface represented by the pixel's member of SDL_Surface. This calculation provides the actual address of the pixel. The SDL_Surface could be thought of as a multi-dimensional array. Hence the value could be accessed in the row-major and column-major format. That is done in the second statement:

      Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

      As the value returned by BytesPerPixels ranges from 1-4 according to the bytes needed to represent the pixel, it can be used for returning the values in the corresponding format i.e. 8, 16, 24 or 32. This is achieved by the switch-case block. That covers the getpixel function.
    2. putpixel():

      This is the same as getpixel(). Apart from the parameters accepted by the getpixel() function, this function accepts one more parameter -- the address where the value has to be put. The following is the code for putpixel():

      /*
      * Set the pixel at (x, y) to the given value
      * NOTE: The surface must be locked before calling this!
      */
      void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
      {
        int bpp = surface->format->BytesPerPixel;
        /* Here p is the address to the pixel we want to set */
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

        switch(bpp) {
          case 1:
            *p = pixel;
            break;

          case 2:
            *(Uint16 *)p = pixel;
            break;

          case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
              p[0] = (pixel >> 16) & 0xff;
              p[1] = (pixel >> 8) & 0xff;
              p[2] = pixel & 0xff;
            } else {
              p[0] = pixel & 0xff;
              p[1] = (pixel >> 8) & 0xff;
              p[2] = (pixel >> 16) & 0xff;
            }
            break;

          case 4:
            *(Uint32 *)p = pixel;
            break;
        }
      }

      The way putpixel() works is almost the opposite of the way getpixel() works. With the former, the returned value is the pixel value corresponding to the coordinates whereas the latter places the pixel value according the coordinates. To accomplish this, first the BytesPerPixel of the passed SDL_Surface is extracted just as before. Then the pixel's address (or pixel value) is calculated and, according to the value, returned by BytesPerPixels, then the value is placed. Since the calculated value is the address of the pixel, the passed pixel value can be directly assigned and the display will get the new value.



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek