test/testbitmap.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1151 be9c9c8f6d53
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    13
quit (int rc)
icculus@1151
    14
{
slouken@1662
    15
    SDL_Quit ();
slouken@1662
    16
    exit (rc);
icculus@1151
    17
}
icculus@1151
    18
slouken@1662
    19
SDL_Surface *
slouken@1662
    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@1662
    26
    bitmap = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
slouken@1662
    27
    if (bitmap == NULL) {
slouken@1662
    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@1662
    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@1662
    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@1662
    73
    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
slouken@1662
    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@1662
    82
        if (strcmp (argv[argc - 1], "-bpp") == 0) {
slouken@1662
    83
            video_bpp = atoi (argv[argc]);
slouken@1662
    84
            --argc;
slouken@1662
    85
        } else if (strcmp (argv[argc], "-warp") == 0) {
slouken@1662
    86
            videoflags |= SDL_HWPALETTE;
slouken@1662
    87
        } else if (strcmp (argv[argc], "-hw") == 0) {
slouken@1662
    88
            videoflags |= SDL_HWSURFACE;
slouken@1662
    89
        } else if (strcmp (argv[argc], "-fullscreen") == 0) {
slouken@1662
    90
            videoflags |= SDL_FULLSCREEN;
slouken@1662
    91
        } else {
slouken@1662
    92
            fprintf (stderr,
slouken@1662
    93
                     "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
slouken@1662
    94
                     argv[0]);
slouken@1662
    95
            quit (1);
slouken@1662
    96
        }
slouken@1662
    97
    }
slouken@0
    98
slouken@1662
    99
    /* Set 640x480 video mode */
slouken@1662
   100
    if ((screen = SDL_SetVideoMode (640, 480, video_bpp, videoflags)) == NULL) {
slouken@1662
   101
        fprintf (stderr, "Couldn't set 640x480x%d video mode: %s\n",
slouken@1662
   102
                 video_bpp, SDL_GetError ());
slouken@1662
   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@1662
   113
        SDL_SetColors (screen, palette, 0, 256);
slouken@1662
   114
    }
slouken@691
   115
slouken@1662
   116
    /* Set the surface pixels and refresh! */
slouken@1662
   117
    if (SDL_LockSurface (screen) < 0) {
slouken@1662
   118
        fprintf (stderr, "Couldn't lock the display surface: %s\n",
slouken@1662
   119
                 SDL_GetError ());
slouken@1662
   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@1662
   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@1662
   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@1662
   139
    SDL_UnlockSurface (screen);
slouken@1662
   140
    SDL_UpdateRect (screen, 0, 0, 0, 0);
slouken@0
   141
slouken@1662
   142
    /* Load the bitmap */
slouken@1662
   143
    bitmap = LoadXBM (screen, picture_width, picture_height,
slouken@1662
   144
                      (Uint8 *) picture_bits);
slouken@1662
   145
    if (bitmap == NULL) {
slouken@1662
   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@1662
   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@1662
   163
                    SDL_BlitSurface (bitmap, NULL, screen, &dst);
slouken@1662
   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@1662
   179
    SDL_FreeSurface (bitmap);
slouken@1662
   180
    SDL_Quit ();
slouken@1662
   181
    return (0);
slouken@0
   182
}