test/testbitmap.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 3094 c0ce3380742d
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program:  Test bitmap blits */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 
    19 #include "SDL.h"
    20 #include "picture.xbm"
    21 
    22 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    23 static void
    24 quit(int rc)
    25 {
    26     SDL_Quit();
    27     exit(rc);
    28 }
    29 
    30 SDL_Surface *
    31 LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
    32 {
    33     SDL_Surface *bitmap;
    34     Uint8 *line;
    35 
    36     /* Allocate the bitmap */
    37     bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
    38     if (bitmap == NULL) {
    39         fprintf(stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError());
    40         return (NULL);
    41     }
    42 
    43     /* Copy the pixels */
    44     line = (Uint8 *) bitmap->pixels;
    45     w = (w + 7) / 8;
    46     while (h--) {
    47         memcpy(line, bits, w);
    48         /* X11 Bitmap images have the bits reversed */
    49         {
    50             int i, j;
    51             Uint8 *buf, byte;
    52             for (buf = line, i = 0; i < w; ++i, ++buf) {
    53                 byte = *buf;
    54                 *buf = 0;
    55                 for (j = 7; j >= 0; --j) {
    56                     *buf |= (byte & 0x01) << j;
    57                     byte >>= 1;
    58                 }
    59             }
    60         }
    61         line += bitmap->pitch;
    62         bits += w;
    63     }
    64     return (bitmap);
    65 }
    66 
    67 int
    68 main(int argc, char *argv[])
    69 {
    70     SDL_Surface *screen;
    71     SDL_Surface *bitmap;
    72     Uint8 video_bpp;
    73     Uint32 videoflags;
    74     Uint8 *buffer;
    75     int i, k, done;
    76     SDL_Event event;
    77     Uint16 *buffer16;
    78     Uint16 color;
    79     Uint8 gradient;
    80     SDL_Color palette[256];
    81 
    82     int video_w, video_h;
    83 
    84     /* Initialize SDL */
    85     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    86         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    87         return (1);
    88     }
    89 
    90     video_bpp = 0;
    91     videoflags = SDL_SWSURFACE;
    92     while (argc > 1) {
    93         --argc;
    94         if (strcmp(argv[argc - 1], "-bpp") == 0) {
    95             video_bpp = atoi(argv[argc]);
    96             --argc;
    97         } else if (strcmp(argv[argc], "-warp") == 0) {
    98             videoflags |= SDL_HWPALETTE;
    99         } else if (strcmp(argv[argc], "-hw") == 0) {
   100             videoflags |= SDL_HWSURFACE;
   101         } else if (strcmp(argv[argc], "-fullscreen") == 0) {
   102             videoflags |= SDL_FULLSCREEN;
   103         } else {
   104             fprintf(stderr,
   105                     "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   106                     argv[0]);
   107             quit(1);
   108         }
   109     }
   110 
   111 #ifdef __IPHONEOS__
   112     video_w = 320;
   113     video_h = 480;
   114 #else
   115     video_w = 640;
   116     video_h = 480;
   117 #endif
   118 
   119     /* Set 640x480 video mode */
   120     if ((screen =
   121          SDL_SetVideoMode(video_w, video_h, video_bpp, videoflags)) == NULL) {
   122         fprintf(stderr, "Couldn't set %dx%d %d video mode: %s\n", video_w,
   123                 video_h, video_bpp, SDL_GetError());
   124         quit(2);
   125     }
   126 
   127     if (video_bpp == 8) {
   128         /* Set a gray colormap, reverse order from white to black */
   129         for (i = 0; i < 256; ++i) {
   130             palette[i].r = 255 - i;
   131             palette[i].g = 255 - i;
   132             palette[i].b = 255 - i;
   133         }
   134         SDL_SetColors(screen, palette, 0, 256);
   135     }
   136 
   137     /* Set the surface pixels and refresh! */
   138     if (SDL_LockSurface(screen) < 0) {
   139         fprintf(stderr, "Couldn't lock the display surface: %s\n",
   140                 SDL_GetError());
   141         quit(2);
   142     }
   143     buffer = (Uint8 *) screen->pixels;
   144     if (screen->format->BytesPerPixel != 2) {
   145         for (i = 0; i < screen->h; ++i) {
   146             memset(buffer, (i * 255) / screen->h, screen->pitch);
   147             buffer += screen->pitch;
   148         }
   149     } else {
   150         for (i = 0; i < screen->h; ++i) {
   151             gradient = ((i * 255) / screen->h);
   152             color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   153             buffer16 = (Uint16 *) buffer;
   154             for (k = 0; k < screen->w; k++) {
   155                 *(buffer16 + k) = color;
   156             }
   157             buffer += screen->pitch;
   158         }
   159     }
   160     SDL_UnlockSurface(screen);
   161     SDL_UpdateRect(screen, 0, 0, 0, 0);
   162 
   163     /* Load the bitmap */
   164     bitmap = LoadXBM(screen, picture_width, picture_height,
   165                      (Uint8 *) picture_bits);
   166     if (bitmap == NULL) {
   167         quit(1);
   168     }
   169 
   170     /* Wait for a keystroke */
   171     done = 0;
   172     while (!done) {
   173         /* Check for events */
   174         while (SDL_PollEvent(&event)) {
   175             switch (event.type) {
   176             case SDL_MOUSEBUTTONDOWN:
   177                 {
   178                     SDL_Rect dst;
   179 
   180                     dst.x = event.button.x - bitmap->w / 2;
   181                     dst.y = event.button.y - bitmap->h / 2;
   182                     dst.w = bitmap->w;
   183                     dst.h = bitmap->h;
   184                     SDL_BlitSurface(bitmap, NULL, screen, &dst);
   185                     SDL_UpdateRects(screen, 1, &dst);
   186                 }
   187                 break;
   188             case SDL_KEYDOWN:
   189                 /* Any key press quits the app... */
   190                 done = 1;
   191                 break;
   192             case SDL_QUIT:
   193                 done = 1;
   194                 break;
   195             default:
   196                 break;
   197             }
   198         }
   199     }
   200     SDL_FreeSurface(bitmap);
   201     SDL_Quit();
   202     return (0);
   203 }