Listing 2. Drawing Individual Pixels on the Screen

#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
Uint16 CreateHicolorPixel(SDL_PixelFormat *fmt,
            Uint8 red, Uint8
            green, Uint8 blue)
{
    Uint16 value;
     /* This series of bit shifts uses the
   information from the SDL_Format
   structure to correctly compose a 16-bit
   pixel value from 8-bit red,
   green, and blue data.
     */
     value = ((red >> fmt->Rloss) << fmt->Rshift) +
        ((green >> fmt->Gloss) << fmt->Gshift) +
        ((blue >> fmt->Bloss) << fmt->Bshift);
     return value;
}
int main()
{
  SDL_Surface *screen;
  Uint16 *raw_pixels;
  int x,y;
     /* Initialization code goes here. It should
   create a 256x256, 16-bit display and save
   the surface in the screen pointer. See
   the previous example. */
     /* "Lock" the screen surface so we can draw to
   it directly. */
     SDL_LockSurface(screen);
     /* Get a pointer to the video surface's i
   memory. */
     raw_pixels = (Uint16 *)screen->pixels;
     /* We can now safely write to the video
   surface. We'll draw a nice gradient pattern
   by varying our red and blue components along
   the X and Y axes. */
     for (x = 0; x < 256; x++) {
   for (y = 0; y < 256; y++) {
            Uint16 pixel_color;
            int offset;
            pixel_color =
            CreateHicolorPixel(screen->format,
                                       x,0,y);
            /* Calculate the memory offset of the
               pixel we wish to change. */
       offset = (screen->pitch/2 * y + x);
            raw_pixels[offset] = pixel_color;
        };
    };
    /* We're finished drawing, so unlock the
       surface. */
    SDL:_UnlockSurface(screen);
    /* Inform SDL that the screen has been changed.
       This is necessary because SDL's screen
       surface is not always the real frame buffer;
       it is sometimes emulated behind the scenes.
    */
    SDL_UpdateRect(screen,0,0,0,0);
    /* Pause for a few seconds as the viewer gasps
       in awe. */
    SDL_Delay(3000);
    return 0;
}