test/testpalette.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    54     {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
    54     {0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
    55 };
    55 };
    56 
    56 
    57 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    57 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    58 static void
    58 static void
    59 quit (int rc)
    59 quit(int rc)
    60 {
    60 {
    61     SDL_Quit ();
    61     SDL_Quit();
    62     exit (rc);
    62     exit(rc);
    63 }
    63 }
    64 
    64 
    65 static void
    65 static void
    66 sdlerr (char *when)
    66 sdlerr(char *when)
    67 {
    67 {
    68     fprintf (stderr, "SDL error: %s: %s\n", when, SDL_GetError ());
    68     fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
    69     quit (1);
    69     quit(1);
    70 }
    70 }
    71 
    71 
    72 /* create a background surface */
    72 /* create a background surface */
    73 static SDL_Surface *
    73 static SDL_Surface *
    74 make_bg (SDL_Surface * screen, int startcol)
    74 make_bg(SDL_Surface * screen, int startcol)
    75 {
    75 {
    76     int i;
    76     int i;
    77     SDL_Surface *bg =
    77     SDL_Surface *bg =
    78         SDL_CreateRGBSurface (SDL_SWSURFACE, screen->w, screen->h,
    78         SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
    79                               8, 0, 0, 0, 0);
    79                              8, 0, 0, 0, 0);
    80     if (!bg)
    80     if (!bg)
    81         sdlerr ("creating background surface");
    81         sdlerr("creating background surface");
    82 
    82 
    83     /* set the palette to the logical screen palette so that blits
    83     /* set the palette to the logical screen palette so that blits
    84        won't be translated */
    84        won't be translated */
    85     SDL_SetColors (bg, screen->format->palette->colors, 0, 256);
    85     SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
    86 
    86 
    87     /* Make a wavy background pattern using colours 0-63 */
    87     /* Make a wavy background pattern using colours 0-63 */
    88     if (SDL_LockSurface (bg) < 0)
    88     if (SDL_LockSurface(bg) < 0)
    89         sdlerr ("locking background");
    89         sdlerr("locking background");
    90     for (i = 0; i < SCRH; i++) {
    90     for (i = 0; i < SCRH; i++) {
    91         Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    91         Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
    92         int j, d;
    92         int j, d;
    93         d = 0;
    93         d = 0;
    94         for (j = 0; j < SCRW; j++) {
    94         for (j = 0; j < SCRW; j++) {
    95             int v = MAX (d, -2);
    95             int v = MAX(d, -2);
    96             v = MIN (v, 2);
    96             v = MIN(v, 2);
    97             if (i > 0)
    97             if (i > 0)
    98                 v += p[-bg->pitch] + 65 - startcol;
    98                 v += p[-bg->pitch] + 65 - startcol;
    99             p[j] = startcol + (v & 63);
    99             p[j] = startcol + (v & 63);
   100             d += ((rand () >> 3) % 3) - 1;
   100             d += ((rand() >> 3) % 3) - 1;
   101         }
   101         }
   102     }
   102     }
   103     SDL_UnlockSurface (bg);
   103     SDL_UnlockSurface(bg);
   104     return (bg);
   104     return (bg);
   105 }
   105 }
   106 
   106 
   107 /*
   107 /*
   108  * Return a surface flipped horisontally. Only works for 8bpp;
   108  * Return a surface flipped horisontally. Only works for 8bpp;
   109  * extension to arbitrary bitness is left as an exercise for the reader.
   109  * extension to arbitrary bitness is left as an exercise for the reader.
   110  */
   110  */
   111 static SDL_Surface *
   111 static SDL_Surface *
   112 hflip (SDL_Surface * s)
   112 hflip(SDL_Surface * s)
   113 {
   113 {
   114     int i;
   114     int i;
   115     SDL_Surface *z = SDL_CreateRGBSurface (SDL_SWSURFACE, s->w, s->h, 8,
   115     SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
   116                                            0, 0, 0, 0);
   116                                           0, 0, 0, 0);
   117     /* copy palette */
   117     /* copy palette */
   118     SDL_SetColors (z, s->format->palette->colors,
   118     SDL_SetColors(z, s->format->palette->colors,
   119                    0, s->format->palette->ncolors);
   119                   0, s->format->palette->ncolors);
   120     if (SDL_LockSurface (s) < 0 || SDL_LockSurface (z) < 0)
   120     if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
   121         sdlerr ("locking flip images");
   121         sdlerr("locking flip images");
   122 
   122 
   123     for (i = 0; i < s->h; i++) {
   123     for (i = 0; i < s->h; i++) {
   124         int j;
   124         int j;
   125         Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
   125         Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
   126         Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
   126         Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
   127         for (j = 0; j < s->w; j++)
   127         for (j = 0; j < s->w; j++)
   128             to[-j] = from[j];
   128             to[-j] = from[j];
   129     }
   129     }
   130 
   130 
   131     SDL_UnlockSurface (z);
   131     SDL_UnlockSurface(z);
   132     SDL_UnlockSurface (s);
   132     SDL_UnlockSurface(s);
   133     return z;
   133     return z;
   134 }
   134 }
   135 
   135 
   136 int
   136 int
   137 main (int argc, char **argv)
   137 main(int argc, char **argv)
   138 {
   138 {
   139     SDL_Color cmap[256];
   139     SDL_Color cmap[256];
   140     SDL_Surface *screen;
   140     SDL_Surface *screen;
   141     SDL_Surface *bg;
   141     SDL_Surface *bg;
   142     SDL_Surface *boat[2];
   142     SDL_Surface *boat[2];
   147     int boatcols, frames, i, red;
   147     int boatcols, frames, i, red;
   148     int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
   148     int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
   149     int gamma_fade = 0;
   149     int gamma_fade = 0;
   150     int gamma_ramp = 0;
   150     int gamma_ramp = 0;
   151 
   151 
   152     if (SDL_Init (SDL_INIT_VIDEO) < 0)
   152     if (SDL_Init(SDL_INIT_VIDEO) < 0)
   153         sdlerr ("initialising SDL");
   153         sdlerr("initialising SDL");
   154 
   154 
   155     while (--argc) {
   155     while (--argc) {
   156         ++argv;
   156         ++argv;
   157         if (strcmp (*argv, "-hw") == 0)
   157         if (strcmp(*argv, "-hw") == 0)
   158             vidflags |= SDL_HWSURFACE;
   158             vidflags |= SDL_HWSURFACE;
   159         else if (strcmp (*argv, "-fullscreen") == 0)
   159         else if (strcmp(*argv, "-fullscreen") == 0)
   160             vidflags |= SDL_FULLSCREEN;
   160             vidflags |= SDL_FULLSCREEN;
   161         else if (strcmp (*argv, "-nofade") == 0)
   161         else if (strcmp(*argv, "-nofade") == 0)
   162             fade_max = 1;
   162             fade_max = 1;
   163         else if (strcmp (*argv, "-gamma") == 0)
   163         else if (strcmp(*argv, "-gamma") == 0)
   164             gamma_fade = 1;
   164             gamma_fade = 1;
   165         else if (strcmp (*argv, "-gammaramp") == 0)
   165         else if (strcmp(*argv, "-gammaramp") == 0)
   166             gamma_ramp = 1;
   166             gamma_ramp = 1;
   167         else {
   167         else {
   168             fprintf (stderr,
   168             fprintf(stderr,
   169                      "usage: testpalette "
   169                     "usage: testpalette "
   170                      " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   170                     " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
   171             quit (1);
   171             quit(1);
   172         }
   172         }
   173     }
   173     }
   174 
   174 
   175     /* Ask explicitly for 8bpp and a hardware palette */
   175     /* Ask explicitly for 8bpp and a hardware palette */
   176     if ((screen =
   176     if ((screen =
   177          SDL_SetVideoMode (SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL)
   177          SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
   178     {
   178         fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   179         fprintf (stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
   179                 SCRW, SCRH, SDL_GetError());
   180                  SCRW, SCRH, SDL_GetError ());
   180         quit(1);
   181         quit (1);
       
   182     }
   181     }
   183 
   182 
   184     if (vidflags & SDL_FULLSCREEN)
   183     if (vidflags & SDL_FULLSCREEN)
   185         SDL_ShowCursor (SDL_FALSE);
   184         SDL_ShowCursor(SDL_FALSE);
   186 
   185 
   187     if ((boat[0] = SDL_LoadBMP ("sail.bmp")) == NULL)
   186     if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
   188         sdlerr ("loading sail.bmp");
   187         sdlerr("loading sail.bmp");
   189     /* We've chosen magenta (#ff00ff) as colour key for the boat */
   188     /* We've chosen magenta (#ff00ff) as colour key for the boat */
   190     SDL_SetColorKey (boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   189     SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   191                      SDL_MapRGB (boat[0]->format, 0xff, 0x00, 0xff));
   190                     SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
   192     boatcols = boat[0]->format->palette->ncolors;
   191     boatcols = boat[0]->format->palette->ncolors;
   193     boat[1] = hflip (boat[0]);
   192     boat[1] = hflip(boat[0]);
   194     SDL_SetColorKey (boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   193     SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
   195                      SDL_MapRGB (boat[1]->format, 0xff, 0x00, 0xff));
   194                     SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
   196 
   195 
   197     /*
   196     /*
   198      * First set the physical screen palette to black, so the user won't
   197      * First set the physical screen palette to black, so the user won't
   199      * see our initial drawing on the screen.
   198      * see our initial drawing on the screen.
   200      */
   199      */
   201     memset (cmap, 0, sizeof (cmap));
   200     memset(cmap, 0, sizeof(cmap));
   202     SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, 256);
   201     SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
   203 
   202 
   204     /*
   203     /*
   205      * Proper palette management is important when playing games with the
   204      * Proper palette management is important when playing games with the
   206      * colormap. We have divided the palette as follows:
   205      * colormap. We have divided the palette as follows:
   207      *
   206      *
   208      * index 0..(boatcols-1):           used for the boat
   207      * index 0..(boatcols-1):           used for the boat
   209      * index boatcols..(boatcols+63):   used for the waves
   208      * index boatcols..(boatcols+63):   used for the waves
   210      */
   209      */
   211     SDL_SetPalette (screen, SDL_LOGPAL,
   210     SDL_SetPalette(screen, SDL_LOGPAL,
   212                     boat[0]->format->palette->colors, 0, boatcols);
   211                    boat[0]->format->palette->colors, 0, boatcols);
   213     SDL_SetPalette (screen, SDL_LOGPAL, wavemap, boatcols, 64);
   212     SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
   214 
   213 
   215     /*
   214     /*
   216      * Now the logical screen palette is set, and will remain unchanged.
   215      * Now the logical screen palette is set, and will remain unchanged.
   217      * The boats already have the same palette so fast blits can be used.
   216      * The boats already have the same palette so fast blits can be used.
   218      */
   217      */
   219     memcpy (cmap, screen->format->palette->colors, 256 * sizeof (SDL_Color));
   218     memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
   220 
   219 
   221     /* save the index of the red colour for later */
   220     /* save the index of the red colour for later */
   222     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
   221     red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
   223 
   222 
   224     bg = make_bg (screen, boatcols);    /* make a nice wavy background surface */
   223     bg = make_bg(screen, boatcols);     /* make a nice wavy background surface */
   225 
   224 
   226     /* initial screen contents */
   225     /* initial screen contents */
   227     if (SDL_BlitSurface (bg, NULL, screen, NULL) < 0)
   226     if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
   228         sdlerr ("blitting background to screen");
   227         sdlerr("blitting background to screen");
   229     SDL_Flip (screen);          /* actually put the background on screen */
   228     SDL_Flip(screen);           /* actually put the background on screen */
   230 
   229 
   231     /* determine initial boat placements */
   230     /* determine initial boat placements */
   232     for (i = 0; i < NBOATS; i++) {
   231     for (i = 0; i < NBOATS; i++) {
   233         boatx[i] = (rand () % (SCRW + boat[0]->w)) - boat[0]->w;
   232         boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
   234         boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   233         boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
   235         boatdir[i] = ((rand () >> 5) & 1) * 2 - 1;
   234         boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
   236     }
   235     }
   237 
   236 
   238     start = SDL_GetTicks ();
   237     start = SDL_GetTicks();
   239     frames = 0;
   238     frames = 0;
   240     fade_dir = 1;
   239     fade_dir = 1;
   241     fade_level = 0;
   240     fade_level = 0;
   242     do {
   241     do {
   243         SDL_Event e;
   242         SDL_Event e;
   244         SDL_Rect updates[NBOATS];
   243         SDL_Rect updates[NBOATS];
   245         SDL_Rect r;
   244         SDL_Rect r;
   246         int redphase;
   245         int redphase;
   247 
   246 
   248         /* A small event loop: just exit on any key or mouse button event */
   247         /* A small event loop: just exit on any key or mouse button event */
   249         while (SDL_PollEvent (&e)) {
   248         while (SDL_PollEvent(&e)) {
   250             if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   249             if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
   251                 || e.type == SDL_MOUSEBUTTONDOWN) {
   250                 || e.type == SDL_MOUSEBUTTONDOWN) {
   252                 if (fade_dir < 0)
   251                 if (fade_dir < 0)
   253                     fade_level = 0;
   252                     fade_level = 0;
   254                 fade_dir = -1;
   253                 fade_dir = -1;
   266             /* paint over the old boat position */
   265             /* paint over the old boat position */
   267             r.x = old_x;
   266             r.x = old_x;
   268             r.y = boaty[i];
   267             r.y = boaty[i];
   269             r.w = boat[0]->w;
   268             r.w = boat[0]->w;
   270             r.h = boat[0]->h;
   269             r.h = boat[0]->h;
   271             if (SDL_BlitSurface (bg, &r, screen, &r) < 0)
   270             if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
   272                 sdlerr ("blitting background");
   271                 sdlerr("blitting background");
   273 
   272 
   274             /* construct update rectangle (bounding box of old and new pos) */
   273             /* construct update rectangle (bounding box of old and new pos) */
   275             updates[i].x = MIN (old_x, boatx[i]);
   274             updates[i].x = MIN(old_x, boatx[i]);
   276             updates[i].y = boaty[i];
   275             updates[i].y = boaty[i];
   277             updates[i].w = boat[0]->w + SPEED;
   276             updates[i].w = boat[0]->w + SPEED;
   278             updates[i].h = boat[0]->h;
   277             updates[i].h = boat[0]->h;
   279             /* clip update rectangle to screen */
   278             /* clip update rectangle to screen */
   280             if (updates[i].x < 0) {
   279             if (updates[i].x < 0) {
   287 
   286 
   288         for (i = 0; i < NBOATS; i++) {
   287         for (i = 0; i < NBOATS; i++) {
   289             /* paint boat on new position */
   288             /* paint boat on new position */
   290             r.x = boatx[i];
   289             r.x = boatx[i];
   291             r.y = boaty[i];
   290             r.y = boaty[i];
   292             if (SDL_BlitSurface (boat[(boatdir[i] + 1) / 2], NULL,
   291             if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
   293                                  screen, &r) < 0)
   292                                 screen, &r) < 0)
   294                 sdlerr ("blitting boat");
   293                 sdlerr("blitting boat");
   295         }
   294         }
   296 
   295 
   297         /* cycle wave palette */
   296         /* cycle wave palette */
   298         for (i = 0; i < 64; i++)
   297         for (i = 0; i < 64; i++)
   299             cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   298             cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
   303             fade_level += fade_dir;
   302             fade_level += fade_dir;
   304 
   303 
   305             if (gamma_fade) {
   304             if (gamma_fade) {
   306                 /* Fade linearly in gamma level (lousy) */
   305                 /* Fade linearly in gamma level (lousy) */
   307                 float level = (float) fade_level / fade_max;
   306                 float level = (float) fade_level / fade_max;
   308                 if (SDL_SetGamma (level, level, level) < 0)
   307                 if (SDL_SetGamma(level, level, level) < 0)
   309                     sdlerr ("setting gamma");
   308                     sdlerr("setting gamma");
   310 
   309 
   311             } else if (gamma_ramp) {
   310             } else if (gamma_ramp) {
   312                 /* Fade using gamma ramp (better) */
   311                 /* Fade using gamma ramp (better) */
   313                 Uint16 ramp[256];
   312                 Uint16 ramp[256];
   314                 for (i = 0; i < 256; i++)
   313                 for (i = 0; i < 256; i++)
   315                     ramp[i] = (i * fade_level / fade_max) << 8;
   314                     ramp[i] = (i * fade_level / fade_max) << 8;
   316                 if (SDL_SetGammaRamp (ramp, ramp, ramp) < 0)
   315                 if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
   317                     sdlerr ("setting gamma ramp");
   316                     sdlerr("setting gamma ramp");
   318 
   317 
   319             } else {
   318             } else {
   320                 /* Fade using direct palette manipulation (best) */
   319                 /* Fade using direct palette manipulation (best) */
   321                 memcpy (cmap, screen->format->palette->colors,
   320                 memcpy(cmap, screen->format->palette->colors,
   322                         boatcols * sizeof (SDL_Color));
   321                        boatcols * sizeof(SDL_Color));
   323                 for (i = 0; i < boatcols + 64; i++) {
   322                 for (i = 0; i < boatcols + 64; i++) {
   324                     cmap[i].r = cmap[i].r * fade_level / fade_max;
   323                     cmap[i].r = cmap[i].r * fade_level / fade_max;
   325                     cmap[i].g = cmap[i].g * fade_level / fade_max;
   324                     cmap[i].g = cmap[i].g * fade_level / fade_max;
   326                     cmap[i].b = cmap[i].b * fade_level / fade_max;
   325                     cmap[i].b = cmap[i].b * fade_level / fade_max;
   327                 }
   326                 }
   330                 fade_dir = 0;
   329                 fade_dir = 0;
   331         }
   330         }
   332 
   331 
   333         /* pulse the red colour (done after the fade, for a night effect) */
   332         /* pulse the red colour (done after the fade, for a night effect) */
   334         redphase = frames % 64;
   333         redphase = frames % 64;
   335         cmap[red].r = (int) (255 * sin (redphase * M_PI / 63));
   334         cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
   336 
   335 
   337         SDL_SetPalette (screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   336         SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
   338 
   337 
   339         /* update changed areas of the screen */
   338         /* update changed areas of the screen */
   340         SDL_UpdateRects (screen, NBOATS, updates);
   339         SDL_UpdateRects(screen, NBOATS, updates);
   341         frames++;
   340         frames++;
   342     }
   341     }
   343     while (fade_level > 0);
   342     while (fade_level > 0);
   344 
   343 
   345     printf ("%d frames, %.2f fps\n",
   344     printf("%d frames, %.2f fps\n",
   346             frames, 1000.0 * frames / (SDL_GetTicks () - start));
   345            frames, 1000.0 * frames / (SDL_GetTicks() - start));
   347 
   346 
   348     if (vidflags & SDL_FULLSCREEN)
   347     if (vidflags & SDL_FULLSCREEN)
   349         SDL_ShowCursor (SDL_TRUE);
   348         SDL_ShowCursor(SDL_TRUE);
   350     SDL_Quit ();
   349     SDL_Quit();
   351     return 0;
   350     return 0;
   352 }
   351 }