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