test/testbitmap.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 00:56:43 +0000
branchgsoc2008_iphone
changeset 2431 7e449145b6ca
parent 1895 c121d94672cb
permissions -rw-r--r--
Added some code to set proper screensize on iPhone
     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 	int video_w, video_h;
    72 	
    73     /* Initialize SDL */
    74     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    75         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    76         return (1);
    77     }
    78 
    79     video_bpp = 0;
    80     videoflags = SDL_SWSURFACE;
    81     while (argc > 1) {
    82         --argc;
    83         if (strcmp(argv[argc - 1], "-bpp") == 0) {
    84             video_bpp = atoi(argv[argc]);
    85             --argc;
    86         } else if (strcmp(argv[argc], "-warp") == 0) {
    87             videoflags |= SDL_HWPALETTE;
    88         } else if (strcmp(argv[argc], "-hw") == 0) {
    89             videoflags |= SDL_HWSURFACE;
    90         } else if (strcmp(argv[argc], "-fullscreen") == 0) {
    91             videoflags |= SDL_FULLSCREEN;
    92         } else {
    93             fprintf(stderr,
    94                     "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
    95                     argv[0]);
    96             quit(1);
    97         }
    98     }
    99 	
   100 #ifdef __IPHONEOS__
   101 	video_w = 320;
   102 	video_h = 480;
   103 #else
   104 	video_w = 640;
   105 	video_h = 480;
   106 #endif
   107 	
   108     /* Set 640x480 video mode */
   109     if ((screen = SDL_SetVideoMode(video_w, video_h, video_bpp, videoflags)) == NULL) {
   110         fprintf(stderr, "Couldn't set %%d%d video mode: %s\n",
   111                 video_w, video_h, video_bpp, SDL_GetError());
   112         quit(2);
   113     }
   114 
   115     if (video_bpp == 8) {
   116         /* Set a gray colormap, reverse order from white to black */
   117         for (i = 0; i < 256; ++i) {
   118             palette[i].r = 255 - i;
   119             palette[i].g = 255 - i;
   120             palette[i].b = 255 - i;
   121         }
   122         SDL_SetColors(screen, palette, 0, 256);
   123     }
   124 
   125     /* Set the surface pixels and refresh! */
   126     if (SDL_LockSurface(screen) < 0) {
   127         fprintf(stderr, "Couldn't lock the display surface: %s\n",
   128                 SDL_GetError());
   129         quit(2);
   130     }
   131     buffer = (Uint8 *) screen->pixels;
   132     if (screen->format->BytesPerPixel != 2) {
   133         for (i = 0; i < screen->h; ++i) {
   134             memset(buffer, (i * 255) / screen->h, screen->pitch);
   135             buffer += screen->pitch;
   136         }
   137     } else {
   138         for (i = 0; i < screen->h; ++i) {
   139             gradient = ((i * 255) / screen->h);
   140             color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   141             buffer16 = (Uint16 *) buffer;
   142             for (k = 0; k < screen->w; k++) {
   143                 *(buffer16 + k) = color;
   144             }
   145             buffer += screen->pitch;
   146         }
   147     }
   148     SDL_UnlockSurface(screen);
   149     SDL_UpdateRect(screen, 0, 0, 0, 0);
   150 
   151     /* Load the bitmap */
   152     bitmap = LoadXBM(screen, picture_width, picture_height,
   153                      (Uint8 *) picture_bits);
   154     if (bitmap == NULL) {
   155         quit(1);
   156     }
   157 
   158     /* Wait for a keystroke */
   159     done = 0;
   160     while (!done) {
   161         /* Check for events */
   162         while (SDL_PollEvent(&event)) {
   163             switch (event.type) {
   164             case SDL_MOUSEBUTTONDOWN:
   165                 {
   166                     SDL_Rect dst;
   167 
   168                     dst.x = event.button.x - bitmap->w / 2;
   169                     dst.y = event.button.y - bitmap->h / 2;
   170                     dst.w = bitmap->w;
   171                     dst.h = bitmap->h;
   172                     SDL_BlitSurface(bitmap, NULL, screen, &dst);
   173                     SDL_UpdateRects(screen, 1, &dst);
   174                 }
   175                 break;
   176             case SDL_KEYDOWN:
   177                 /* Any key press quits the app... */
   178                 done = 1;
   179                 break;
   180             case SDL_QUIT:
   181                 done = 1;
   182                 break;
   183             default:
   184                 break;
   185             }
   186         }
   187     }
   188     SDL_FreeSurface(bitmap);
   189     SDL_Quit();
   190     return (0);
   191 }