Game Development Reference
In-Depth Information
Given an SDL surface, you simply check the format's bits per pixel
surface->format->BitsPerPixel
and based on that value you create an RGB565 array of pixels that can be used by
DrawIntoTextureRGB565 .
for ( i = 0 ; i < size ; i++ ) {
unsigned char pixel = ((unsigned char *)surface->pixels)[i];
// RGB565
pixels[i] = ( (colors[pixel].r >> 3) << 11)
| ( (colors[pixel].g >> 2) << 5)
| (colors[pixel].b >> 3);
}
Each pixel consists of a Red, Green, and Blue value extracted from the surface color
palette with
SDL_Color * colors = surface->format->palette->colors;
RED: colors[pixel].r
GREEN: colors[pixel].g
BLUE: colors[pixel].b
To build an RGB565 pixel, discard the least significant bits from each color component.
colors[pixel].r >> 3 (8 - 3 = 5)
colors[pixel].g >> 2 (8 - 2 = 6)
colors[pixel].b >> 3 (8 - 3 = 5)
Then shift each component into the proper position of a 16-bit value (5+6+5 = 16, hence
RGB656).
pixels[i] = (RED << 11) | (GREEN << 5) | BLUE
Finally, you send the new array to DrawIntoTextureRGB565 along with the image width and
height. For the final piece of the puzzle, you need a way to tell whether the surface requires
zooming. This can be done at video initialization when the surface is created in the first
place. Listing 3-15 shows how to create a software surface using SDL.
Listing 3-15. Zoom Surface Initialization
// Should be zoom?
static char zoom = 0;
// Zoom scales [0,1]
static double zoomx = 1.0;
static double zoomy = 1.0;
Search WWH ::




Custom Search