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