test/testbitmap.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Oct 2009 20:44:04 +0000
changeset 3342 3da232c5980a
parent 3094 c0ce3380742d
child 5535 96594ac5fd1a
permissions -rw-r--r--
Split acinclude.m4 into its constituent parts for easy upgrading
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];
slouken@691
    70
slouken@2765
    71
    int video_w, video_h;
slouken@0
    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
    }
slouken@0
    99
slouken@2765
   100
#ifdef __IPHONEOS__
slouken@2765
   101
    video_w = 320;
slouken@2765
   102
    video_h = 480;
slouken@2765
   103
#else
slouken@2765
   104
    video_w = 640;
slouken@2765
   105
    video_h = 480;
slouken@2765
   106
#endif
slouken@2765
   107
slouken@1895
   108
    /* Set 640x480 video mode */
slouken@2765
   109
    if ((screen =
slouken@2765
   110
         SDL_SetVideoMode(video_w, video_h, video_bpp, videoflags)) == NULL) {
slouken@3094
   111
        fprintf(stderr, "Couldn't set %dx%d %d video mode: %s\n", video_w,
slouken@2765
   112
                video_h, video_bpp, SDL_GetError());
slouken@1895
   113
        quit(2);
slouken@1895
   114
    }
slouken@0
   115
slouken@1895
   116
    if (video_bpp == 8) {
slouken@1895
   117
        /* Set a gray colormap, reverse order from white to black */
slouken@1895
   118
        for (i = 0; i < 256; ++i) {
slouken@1895
   119
            palette[i].r = 255 - i;
slouken@1895
   120
            palette[i].g = 255 - i;
slouken@1895
   121
            palette[i].b = 255 - i;
slouken@1895
   122
        }
slouken@1895
   123
        SDL_SetColors(screen, palette, 0, 256);
slouken@1895
   124
    }
slouken@691
   125
slouken@1895
   126
    /* Set the surface pixels and refresh! */
slouken@1895
   127
    if (SDL_LockSurface(screen) < 0) {
slouken@1895
   128
        fprintf(stderr, "Couldn't lock the display surface: %s\n",
slouken@1895
   129
                SDL_GetError());
slouken@1895
   130
        quit(2);
slouken@1895
   131
    }
slouken@1895
   132
    buffer = (Uint8 *) screen->pixels;
slouken@1895
   133
    if (screen->format->BytesPerPixel != 2) {
slouken@1895
   134
        for (i = 0; i < screen->h; ++i) {
slouken@1895
   135
            memset(buffer, (i * 255) / screen->h, screen->pitch);
slouken@1895
   136
            buffer += screen->pitch;
slouken@691
   137
        }
slouken@1895
   138
    } else {
slouken@1895
   139
        for (i = 0; i < screen->h; ++i) {
slouken@1895
   140
            gradient = ((i * 255) / screen->h);
slouken@1895
   141
            color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
slouken@1895
   142
            buffer16 = (Uint16 *) buffer;
slouken@1895
   143
            for (k = 0; k < screen->w; k++) {
slouken@1895
   144
                *(buffer16 + k) = color;
slouken@1895
   145
            }
slouken@1895
   146
            buffer += screen->pitch;
slouken@691
   147
        }
slouken@1895
   148
    }
slouken@1895
   149
    SDL_UnlockSurface(screen);
slouken@1895
   150
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   151
slouken@1895
   152
    /* Load the bitmap */
slouken@1895
   153
    bitmap = LoadXBM(screen, picture_width, picture_height,
slouken@1895
   154
                     (Uint8 *) picture_bits);
slouken@1895
   155
    if (bitmap == NULL) {
slouken@1895
   156
        quit(1);
slouken@1895
   157
    }
slouken@0
   158
slouken@1895
   159
    /* Wait for a keystroke */
slouken@1895
   160
    done = 0;
slouken@1895
   161
    while (!done) {
slouken@1895
   162
        /* Check for events */
slouken@1895
   163
        while (SDL_PollEvent(&event)) {
slouken@1895
   164
            switch (event.type) {
slouken@1895
   165
            case SDL_MOUSEBUTTONDOWN:
slouken@1895
   166
                {
slouken@1895
   167
                    SDL_Rect dst;
slouken@0
   168
slouken@1895
   169
                    dst.x = event.button.x - bitmap->w / 2;
slouken@1895
   170
                    dst.y = event.button.y - bitmap->h / 2;
slouken@1895
   171
                    dst.w = bitmap->w;
slouken@1895
   172
                    dst.h = bitmap->h;
slouken@1895
   173
                    SDL_BlitSurface(bitmap, NULL, screen, &dst);
slouken@1895
   174
                    SDL_UpdateRects(screen, 1, &dst);
slouken@1895
   175
                }
slouken@1895
   176
                break;
slouken@1895
   177
            case SDL_KEYDOWN:
slouken@1895
   178
                /* Any key press quits the app... */
slouken@1895
   179
                done = 1;
slouken@1895
   180
                break;
slouken@1895
   181
            case SDL_QUIT:
slouken@1895
   182
                done = 1;
slouken@1895
   183
                break;
slouken@1895
   184
            default:
slouken@1895
   185
                break;
slouken@1895
   186
            }
slouken@1895
   187
        }
slouken@1895
   188
    }
slouken@1895
   189
    SDL_FreeSurface(bitmap);
slouken@1895
   190
    SDL_Quit();
slouken@1895
   191
    return (0);
slouken@0
   192
}