Home arrow Multimedia arrow Page 2 - Game Programming using SDL: Raw Graphics and Event Handling

Raw Graphics: Writing Directly onto the Display - Multimedia

If you have found yourself struggling with pixel-level graphics and/or user input for your games, this is the article for you. You will learn how to handle these tasks using SDL. This is the second part of a series on game programming with SDL.

TABLE OF CONTENTS:
  1. Game Programming using SDL: Raw Graphics and Event Handling
  2. Raw Graphics: Writing Directly onto the Display
  3. Putting the Functions to Use
  4. Handling the Keyboard the SDL Way
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 7
February 05, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: