test/graywin.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 1895 c121d94672cb
child 4884 27ab20a36eba
permissions -rw-r--r--
Added testresample.c
slouken@0
     1
slouken@0
     2
/* Simple program:  Fill a colormap with gray and stripe it down the screen */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
#include <time.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
slouken@1895
    11
#ifdef TEST_VGA16               /* Define this if you want to test VGA 16-color video modes */
slouken@0
    12
#define NUM_COLORS	16
slouken@0
    13
#else
slouken@0
    14
#define NUM_COLORS	256
slouken@0
    15
#endif
slouken@0
    16
slouken@0
    17
/* Draw a randomly sized and colored box centered about (X,Y) */
slouken@1895
    18
void
slouken@1895
    19
DrawBox(SDL_Surface * screen, int X, int Y, int width, int height)
slouken@0
    20
{
slouken@1895
    21
    static unsigned int seeded = 0;
slouken@1895
    22
    SDL_Rect area;
slouken@1895
    23
    Uint32 color;
slouken@1895
    24
    Uint32 randc;
slouken@0
    25
slouken@1895
    26
    /* Seed the random number generator */
slouken@1895
    27
    if (seeded == 0) {
slouken@1895
    28
        srand(time(NULL));
slouken@1895
    29
        seeded = 1;
slouken@1895
    30
    }
slouken@0
    31
slouken@1895
    32
    /* Get the bounds of the rectangle */
slouken@1895
    33
    area.w = (rand() % width);
slouken@1895
    34
    area.h = (rand() % height);
slouken@1895
    35
    area.x = X - (area.w / 2);
slouken@1895
    36
    area.y = Y - (area.h / 2);
slouken@1895
    37
    randc = (rand() % NUM_COLORS);
slouken@691
    38
slouken@1895
    39
    if (screen->format->BytesPerPixel == 1) {
slouken@1895
    40
        color = randc;
slouken@1895
    41
    } else {
slouken@1895
    42
        color = SDL_MapRGB(screen->format, randc, randc, randc);
slouken@1895
    43
    }
slouken@1895
    44
slouken@1895
    45
    /* Do it! */
slouken@1895
    46
    SDL_FillRect(screen, &area, color);
slouken@1895
    47
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1895
    48
        SDL_Flip(screen);
slouken@1895
    49
    } else {
slouken@1895
    50
        SDL_UpdateRects(screen, 1, &area);
slouken@1895
    51
    }
slouken@1895
    52
}
slouken@1895
    53
slouken@1895
    54
void
slouken@1895
    55
DrawBackground(SDL_Surface * screen)
slouken@1895
    56
{
slouken@1895
    57
    int i, j, k;
slouken@1895
    58
    Uint8 *buffer;
slouken@1895
    59
    Uint16 *buffer16;
slouken@1895
    60
    Uint16 color;
slouken@1895
    61
    Uint8 gradient;
slouken@1895
    62
slouken@1895
    63
    /* Set the surface pixels and refresh! */
slouken@1895
    64
    /* Use two loops in case the surface is double-buffered (both sides) */
slouken@1895
    65
slouken@1895
    66
    for (j = 0; j < 2; ++j) {
slouken@1895
    67
        if (SDL_LockSurface(screen) < 0) {
slouken@1895
    68
            fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@1895
    69
                    SDL_GetError());
slouken@1895
    70
            return;
slouken@691
    71
        }
slouken@1895
    72
        buffer = (Uint8 *) screen->pixels;
slouken@1895
    73
slouken@1895
    74
        if (screen->format->BytesPerPixel != 2) {
slouken@1895
    75
            for (i = 0; i < screen->h; ++i) {
slouken@1895
    76
                memset(buffer, (i * (NUM_COLORS - 1)) / screen->h,
slouken@1895
    77
                       screen->w * screen->format->BytesPerPixel);
slouken@1895
    78
                buffer += screen->pitch;
slouken@1895
    79
            }
slouken@1895
    80
        } else {
slouken@1895
    81
            for (i = 0; i < screen->h; ++i) {
slouken@1895
    82
                gradient = ((i * (NUM_COLORS - 1)) / screen->h);
slouken@1895
    83
                color =
slouken@1895
    84
                    SDL_MapRGB(screen->format, gradient, gradient, gradient);
slouken@1895
    85
                buffer16 = (Uint16 *) buffer;
slouken@1895
    86
                for (k = 0; k < screen->w; k++) {
slouken@1895
    87
                    *(buffer16 + k) = color;
slouken@1895
    88
                }
slouken@1895
    89
                buffer += screen->pitch;
slouken@1895
    90
            }
slouken@691
    91
        }
slouken@0
    92
slouken@1895
    93
        SDL_UnlockSurface(screen);
slouken@1895
    94
        if (screen->flags & SDL_DOUBLEBUF) {
slouken@1895
    95
            SDL_Flip(screen);
slouken@1895
    96
        } else {
slouken@1895
    97
            SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1895
    98
            break;
slouken@1895
    99
        }
slouken@1895
   100
    }
slouken@0
   101
}
slouken@0
   102
slouken@1895
   103
SDL_Surface *
slouken@1895
   104
CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
slouken@691
   105
{
slouken@1895
   106
    SDL_Surface *screen;
slouken@1895
   107
    int i;
slouken@1895
   108
    SDL_Color palette[NUM_COLORS];
slouken@691
   109
slouken@1895
   110
    /* Set the video mode */
slouken@1895
   111
    screen = SDL_SetVideoMode(w, h, bpp, flags);
slouken@1895
   112
    if (screen == NULL) {
slouken@1895
   113
        fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
slouken@1895
   114
        return (NULL);
slouken@1895
   115
    }
slouken@1895
   116
    fprintf(stderr, "Screen is in %s mode\n",
slouken@1895
   117
            (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@691
   118
slouken@1895
   119
    if (bpp == 8) {
slouken@1895
   120
        /* Set a gray colormap, reverse order from white to black */
slouken@1895
   121
        for (i = 0; i < NUM_COLORS; ++i) {
slouken@1895
   122
            palette[i].r = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
slouken@1895
   123
            palette[i].g = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
slouken@1895
   124
            palette[i].b = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
slouken@1895
   125
        }
slouken@1895
   126
        SDL_SetColors(screen, palette, 0, NUM_COLORS);
slouken@1895
   127
    }
slouken@691
   128
slouken@1895
   129
    return (screen);
slouken@691
   130
}
slouken@691
   131
slouken@1895
   132
int
slouken@1895
   133
main(int argc, char *argv[])
slouken@0
   134
{
slouken@1895
   135
    SDL_Surface *screen;
slouken@1895
   136
    Uint32 videoflags;
slouken@1895
   137
    int done;
slouken@1895
   138
    SDL_Event event;
slouken@1895
   139
    int width, height, bpp;
slouken@0
   140
slouken@1895
   141
    /* Initialize SDL */
slouken@1895
   142
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   143
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   144
        exit(1);
slouken@1895
   145
    }
slouken@0
   146
slouken@1895
   147
    /* See if we try to get a hardware colormap */
slouken@1895
   148
    width = 640;
slouken@1895
   149
    height = 480;
slouken@1895
   150
    bpp = 8;
slouken@1895
   151
    videoflags = SDL_SWSURFACE;
slouken@1895
   152
    while (argc > 1) {
slouken@1895
   153
        --argc;
slouken@1895
   154
        if (argv[argc - 1] && (strcmp(argv[argc - 1], "-width") == 0)) {
slouken@1895
   155
            width = atoi(argv[argc]);
slouken@1895
   156
            --argc;
slouken@1895
   157
        } else if (argv[argc - 1]
slouken@1895
   158
                   && (strcmp(argv[argc - 1], "-height") == 0)) {
slouken@1895
   159
            height = atoi(argv[argc]);
slouken@1895
   160
            --argc;
slouken@1895
   161
        } else if (argv[argc - 1] && (strcmp(argv[argc - 1], "-bpp") == 0)) {
slouken@1895
   162
            bpp = atoi(argv[argc]);
slouken@1895
   163
            --argc;
slouken@1895
   164
        } else if (argv[argc] && (strcmp(argv[argc], "-hw") == 0)) {
slouken@1895
   165
            videoflags |= SDL_HWSURFACE;
slouken@1895
   166
        } else if (argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0)) {
slouken@1895
   167
            videoflags |= SDL_HWPALETTE;
slouken@1895
   168
        } else if (argv[argc] && (strcmp(argv[argc], "-flip") == 0)) {
slouken@1895
   169
            videoflags |= SDL_DOUBLEBUF;
slouken@1895
   170
        } else if (argv[argc] && (strcmp(argv[argc], "-noframe") == 0)) {
slouken@1895
   171
            videoflags |= SDL_NOFRAME;
slouken@1895
   172
        } else if (argv[argc] && (strcmp(argv[argc], "-resize") == 0)) {
slouken@1895
   173
            videoflags |= SDL_RESIZABLE;
slouken@1895
   174
        } else if (argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0)) {
slouken@1895
   175
            videoflags |= SDL_FULLSCREEN;
slouken@1895
   176
        } else {
slouken@1895
   177
            fprintf(stderr,
slouken@1895
   178
                    "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
slouken@1895
   179
                    argv[0]);
slouken@1895
   180
            exit(1);
slouken@1895
   181
        }
slouken@1895
   182
    }
slouken@0
   183
slouken@1895
   184
    /* Set a video mode */
slouken@1895
   185
    screen = CreateScreen(width, height, bpp, videoflags);
slouken@1895
   186
    if (screen == NULL) {
slouken@1895
   187
        exit(2);
slouken@1895
   188
    }
slouken@1895
   189
slouken@1895
   190
    DrawBackground(screen);
slouken@1895
   191
slouken@1895
   192
    /* Wait for a keystroke */
slouken@1895
   193
    done = 0;
slouken@1895
   194
    while (!done && SDL_WaitEvent(&event)) {
slouken@1895
   195
        switch (event.type) {
slouken@1895
   196
        case SDL_MOUSEBUTTONDOWN:
slouken@1895
   197
            DrawBox(screen, event.button.x, event.button.y, width, height);
slouken@1895
   198
            break;
slouken@1895
   199
        case SDL_KEYDOWN:
slouken@1895
   200
            /* Ignore ALT-TAB for windows */
slouken@1895
   201
            if ((event.key.keysym.sym == SDLK_LALT) ||
slouken@1895
   202
                (event.key.keysym.sym == SDLK_TAB)) {
slouken@1895
   203
                break;
slouken@1895
   204
            }
slouken@1895
   205
            /* Center the mouse on <SPACE> */
slouken@1895
   206
            if (event.key.keysym.sym == SDLK_SPACE) {
slouken@1895
   207
                SDL_WarpMouse(width / 2, height / 2);
slouken@1895
   208
                break;
slouken@1895
   209
            }
slouken@1895
   210
            /* Toggle fullscreen mode on <RETURN> */
slouken@1895
   211
            if (event.key.keysym.sym == SDLK_RETURN) {
slouken@1895
   212
                videoflags ^= SDL_FULLSCREEN;
slouken@1895
   213
                screen = CreateScreen(screen->w, screen->h,
slouken@1895
   214
                                      screen->format->BitsPerPixel,
slouken@1895
   215
                                      videoflags);
slouken@1895
   216
                if (screen == NULL) {
slouken@1895
   217
                    fprintf(stderr, "Couldn't toggle fullscreen mode\n");
slouken@1895
   218
                    done = 1;
slouken@1895
   219
                }
slouken@1895
   220
                DrawBackground(screen);
slouken@1895
   221
                break;
slouken@1895
   222
            }
slouken@1895
   223
            /* Any other key quits the application... */
slouken@1895
   224
        case SDL_QUIT:
slouken@1895
   225
            done = 1;
slouken@1895
   226
            break;
slouken@1895
   227
        case SDL_VIDEOEXPOSE:
slouken@1895
   228
            DrawBackground(screen);
slouken@1895
   229
            break;
slouken@1895
   230
        case SDL_VIDEORESIZE:
slouken@1895
   231
            printf("Screen resized to %dx%d\n", event.resize.w,
slouken@1895
   232
                   event.resize.h);
slouken@1895
   233
            screen =
slouken@1895
   234
                CreateScreen(event.resize.w, event.resize.h,
slouken@1895
   235
                             screen->format->BitsPerPixel, videoflags);
slouken@1895
   236
            if (screen == NULL) {
slouken@1895
   237
                fprintf(stderr, "Couldn't resize video mode\n");
slouken@1895
   238
                done = 1;
slouken@1895
   239
            }
slouken@1895
   240
            DrawBackground(screen);
slouken@1895
   241
            break;
slouken@1895
   242
        default:
slouken@1895
   243
            break;
slouken@1895
   244
        }
slouken@1895
   245
    }
slouken@1895
   246
    SDL_Quit();
slouken@1895
   247
    return (0);
slouken@0
   248
}