test/graywin.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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