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