test/testbitmap.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1151 be9c9c8f6d53
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     1 
     2 /* Simple program:  Test bitmap blits */
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <string.h>
     7 
     8 #include "SDL.h"
     9 #include "picture.xbm"
    10 
    11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    12 static void
    13 quit (int rc)
    14 {
    15     SDL_Quit ();
    16     exit (rc);
    17 }
    18 
    19 SDL_Surface *
    20 LoadXBM (SDL_Surface * screen, int w, int h, Uint8 * bits)
    21 {
    22     SDL_Surface *bitmap;
    23     Uint8 *line;
    24 
    25     /* Allocate the bitmap */
    26     bitmap = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
    27     if (bitmap == NULL) {
    28         fprintf (stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError ());
    29         return (NULL);
    30     }
    31 
    32     /* Copy the pixels */
    33     line = (Uint8 *) bitmap->pixels;
    34     w = (w + 7) / 8;
    35     while (h--) {
    36         memcpy (line, bits, w);
    37         /* X11 Bitmap images have the bits reversed */
    38         {
    39             int i, j;
    40             Uint8 *buf, byte;
    41             for (buf = line, i = 0; i < w; ++i, ++buf) {
    42                 byte = *buf;
    43                 *buf = 0;
    44                 for (j = 7; j >= 0; --j) {
    45                     *buf |= (byte & 0x01) << j;
    46                     byte >>= 1;
    47                 }
    48             }
    49         }
    50         line += bitmap->pitch;
    51         bits += w;
    52     }
    53     return (bitmap);
    54 }
    55 
    56 int
    57 main (int argc, char *argv[])
    58 {
    59     SDL_Surface *screen;
    60     SDL_Surface *bitmap;
    61     Uint8 video_bpp;
    62     Uint32 videoflags;
    63     Uint8 *buffer;
    64     int i, k, done;
    65     SDL_Event event;
    66     Uint16 *buffer16;
    67     Uint16 color;
    68     Uint8 gradient;
    69     SDL_Color palette[256];
    70 
    71 
    72     /* Initialize SDL */
    73     if (SDL_Init (SDL_INIT_VIDEO) < 0) {
    74         fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
    75         return (1);
    76     }
    77 
    78     video_bpp = 0;
    79     videoflags = SDL_SWSURFACE;
    80     while (argc > 1) {
    81         --argc;
    82         if (strcmp (argv[argc - 1], "-bpp") == 0) {
    83             video_bpp = atoi (argv[argc]);
    84             --argc;
    85         } else if (strcmp (argv[argc], "-warp") == 0) {
    86             videoflags |= SDL_HWPALETTE;
    87         } else if (strcmp (argv[argc], "-hw") == 0) {
    88             videoflags |= SDL_HWSURFACE;
    89         } else if (strcmp (argv[argc], "-fullscreen") == 0) {
    90             videoflags |= SDL_FULLSCREEN;
    91         } else {
    92             fprintf (stderr,
    93                      "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
    94                      argv[0]);
    95             quit (1);
    96         }
    97     }
    98 
    99     /* Set 640x480 video mode */
   100     if ((screen = SDL_SetVideoMode (640, 480, video_bpp, videoflags)) == NULL) {
   101         fprintf (stderr, "Couldn't set 640x480x%d video mode: %s\n",
   102                  video_bpp, SDL_GetError ());
   103         quit (2);
   104     }
   105 
   106     if (video_bpp == 8) {
   107         /* Set a gray colormap, reverse order from white to black */
   108         for (i = 0; i < 256; ++i) {
   109             palette[i].r = 255 - i;
   110             palette[i].g = 255 - i;
   111             palette[i].b = 255 - i;
   112         }
   113         SDL_SetColors (screen, palette, 0, 256);
   114     }
   115 
   116     /* Set the surface pixels and refresh! */
   117     if (SDL_LockSurface (screen) < 0) {
   118         fprintf (stderr, "Couldn't lock the display surface: %s\n",
   119                  SDL_GetError ());
   120         quit (2);
   121     }
   122     buffer = (Uint8 *) screen->pixels;
   123     if (screen->format->BytesPerPixel != 2) {
   124         for (i = 0; i < screen->h; ++i) {
   125             memset (buffer, (i * 255) / screen->h, screen->pitch);
   126             buffer += screen->pitch;
   127         }
   128     } else {
   129         for (i = 0; i < screen->h; ++i) {
   130             gradient = ((i * 255) / screen->h);
   131             color = SDL_MapRGB (screen->format, gradient, gradient, gradient);
   132             buffer16 = (Uint16 *) buffer;
   133             for (k = 0; k < screen->w; k++) {
   134                 *(buffer16 + k) = color;
   135             }
   136             buffer += screen->pitch;
   137         }
   138     }
   139     SDL_UnlockSurface (screen);
   140     SDL_UpdateRect (screen, 0, 0, 0, 0);
   141 
   142     /* Load the bitmap */
   143     bitmap = LoadXBM (screen, picture_width, picture_height,
   144                       (Uint8 *) picture_bits);
   145     if (bitmap == NULL) {
   146         quit (1);
   147     }
   148 
   149     /* Wait for a keystroke */
   150     done = 0;
   151     while (!done) {
   152         /* Check for events */
   153         while (SDL_PollEvent (&event)) {
   154             switch (event.type) {
   155             case SDL_MOUSEBUTTONDOWN:
   156                 {
   157                     SDL_Rect dst;
   158 
   159                     dst.x = event.button.x - bitmap->w / 2;
   160                     dst.y = event.button.y - bitmap->h / 2;
   161                     dst.w = bitmap->w;
   162                     dst.h = bitmap->h;
   163                     SDL_BlitSurface (bitmap, NULL, screen, &dst);
   164                     SDL_UpdateRects (screen, 1, &dst);
   165                 }
   166                 break;
   167             case SDL_KEYDOWN:
   168                 /* Any key press quits the app... */
   169                 done = 1;
   170                 break;
   171             case SDL_QUIT:
   172                 done = 1;
   173                 break;
   174             default:
   175                 break;
   176             }
   177         }
   178     }
   179     SDL_FreeSurface (bitmap);
   180     SDL_Quit ();
   181     return (0);
   182 }