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