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