Video | Events | Audio | CD-ROM | Threads |
Introduction | Function List | Function Reference | Examples |
Initializing the video display
Initializing the best 640x480 video mode
Loading and displaying a BMP file
Fading a palette
SDL_Surface *screen;
/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit */
atexit(SDL_Quit);
/* Initialize the display in a 640x480 8-bit palettized mode */
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
/* Have a preference for 8-bit, but accept any depth */
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_ANYFORMAT);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
printf("Set 640x480 at %d bits-per-pixel mode\n",
screen->format->BitsPerPixel);
SDL_Surface *image;
SDL_Rect dest;
int ncolors, i;
SDL_Color *colors;
/* Load the BMP file into a surface */
image = SDL_LoadBMP("sample.bmp");
if ( image == NULL ) {
fprintf(stderr, "Couldn't load sample.bmp: %s\n",
SDL_GetError());
return;
}
/* Set the display colors -- SDL_SetColors() only does something on
palettized displays, but it doesn't hurt anything on HiColor or
TrueColor displays.
If the display colors have already been set, this step can be
skipped, and the library will automatically map the image to
the current display colors.
*/
if ( image->format->palette ) {
ncolors = image->format->palette->ncolors;
colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
memcpy(colors, image->format->palette->colors, ncolors);
} else {
int r, g, b;
/* Allocate 256 color palette */
ncolors = 256;
colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
/* Set a 3,3,2 color cube */
for ( r=0; r<8; ++r ) {
for ( g=0; g<8; ++g ) {
for ( b=0; b<4; ++b ) {
i = ((r<<5)|(g<<2)|b);
colors[i].r = r<<5;
colors[i].g = g<<5;
colors[i].b = b<<6;
}
}
}
/* Note: A better way of allocating the palette might be
to calculate the frequency of colors in the image
and create a palette based on that information.
*/
}
/* Set colormap, try for all the colors, but don't worry about it */
SDL_SetColors(screen, colors, 0, ncolors);
free(colors);
/* Blit onto the screen surface
*/
dest.x = 0;
dest.y = 0;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_UpdateRects(screen, 1, &dest);
/* Free the allocated BMP surface */
SDL_FreeSurface(image);
return;
/* Note: This fading process is really slow when the display cannot
allocate all of the requested colors, i.e. warp_colors isn't
set in SDL_SetColors()
*/
SDL_Palette *palette;
SDL_Color *orig_colors;
SDL_Color *fade_colors;
int ncolors, i;
/* Get the current colormap */
palette = screen->format->palette;
if ( palette == NULL ) { /* The display is not palettized */
return;
}
ncolors = palette->ncolors;
orig_colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
fade_colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
memcpy(orig_colors, palette->colors, ncolors*sizeof(SDL_Color));
/* Copy the original colors to the faded colors */
memcpy(fade_colors, orig_colors, ncolors*sizeof(SDL_Color));
/* Fade the colormap to black and back
Note: A more sophisticated fading routine would detect the speed
of the palette change and base the number of fading steps
on that to provide a consistent fade time on all displays.
*/
for ( i=32-1; i>=0; --i ) { /* 32 step fade out */
int c;
for ( c=0; c<ncolors; ++c ) {
fade_colors[c].r = ((int)orig_colors[c].r*i)/32;
fade_colors[c].g = ((int)orig_colors[c].g*i)/32;
fade_colors[c].b = ((int)orig_colors[c].b*i)/32;
}
SDL_SetColors(screen, fade_colors, 0, ncolors);
}
for ( i=0; i<32; ++i ) { /* 32 step fade in */
int c;
for ( c=0; c<ncolors; ++c ) {
fade_colors[c].r = ((int)orig_colors[c].r*i)/32;
fade_colors[c].g = ((int)orig_colors[c].g*i)/32;
fade_colors[c].b = ((int)orig_colors[c].b*i)/32;
}
SDL_SetColors(screen, fade_colors, 0, ncolors);
}
free(orig_colors);
free(fade_colors);
return;