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