24 bpp format

I’m setting up some a 24bpp double buffering code and have a few
questions.

1: Is the 24 bpp format for screen->surface always the same if I use
SDL_SWSURFACE in the SetVideoMode call?

1b: If no, how can I figure out the layout?

2: Assuming the following SetVideoMode line
screen = SDL_SetVideoMode(640x480,24,SDL_SWSURFACE);
And the GUI in use does not have (or in the case of X is not set to)
24bpp. Will screen be set to NULL or will SDL ‘do the best it can’ in
emulating the colors?

2b: Will screen->surface be formated for 24bpp or the physical color
depth.

2c: If Yes, how will SDL_UpdateRect handle the color reduction?

thanks

		-fjr

I’m setting up some a 24bpp double buffering code and have a few
questions.

First, I don’t recommend 24bpp as a video mode. It is the slowest of
all packed pixel modes because memory accesses are not word-aligned.
16bpp is generally the fastest, but if you need full 8-bit color precision,
32bpp is a better way to go if you have the memory.

That said…

1: Is the 24 bpp format for screen->surface always the same if I use
SDL_SWSURFACE in the SetVideoMode call?

No. If the video card is in 24 bpp, SDL will return the format of the
video card.

1b: If no, how can I figure out the layout?

Look at the RGB masks in the screen->surface->format.
These are the most common 24-bit formats:
Rmask, Gmask, Bmask
RGB888 = { 0x00FF0000, 0x0000FF00, 0x000000FF }
BGR888 = { 0x000000FF, 0x0000FF00, 0x00FF0000 }

2: Assuming the following SetVideoMode line
screen = SDL_SetVideoMode(640x480,24,SDL_SWSURFACE);
And the GUI in use does not have (or in the case of X is not set to)
24bpp. Will screen be set to NULL or will SDL ‘do the best it can’ in
emulating the colors?

SDL will do the best it can in emulating the colors.
There are combinations of flags that allow you to force a particular
mode, or allow SDL to pick a better mode for you. I’m on vacation,
so I don’t have the docs in fromt of me.

2b: Will screen->surface be formated for 24bpp or the physical color
depth.

24bpp

2c: If Yes, how will SDL_UpdateRect handle the color reduction?

Very carefully. :wink:
If converting to another packed pixel format, SDL will extract the RGB
components of each pixel, and repack them in the new format. As you can
imagine, this can be slow. If converting to indexed palette format, SDL
converts each pixel to a packed RGB332 format and looks this pixel up in
a colormap lookup table which matches the closest color in the real palette.
This usually doesn’t look so good, but is much better than not being able
to run at all.

SDL has optimized color conversion functions for converting from 32bpp
to 16bpp and 8bpp. If you need to run RGB888, I highly recommend 32bpp.

thanks

You’re welcome. :slight_smile:

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Ok, I changed the the requested bit depth to 32

Now, let me make sure I understand correctly.

after calling:
screen = SDL_SetVideoMode(640x480,32,SDL_SWSURFACE);
screen will be set to 640x480x32 regardless of the physical bit depth.
The format (RGBA, BGRA, etc) will depend upon the physical card.

Is this correct?

So if I have the red, green, and blue components should this work for
any video configuration?

// buffer is my back buffer (char *)
// screen is the SDL surface returned by SDL_SetVideoMode

#define BUFFER_PITCH 1920
#define BPP 3

screenOff = 0;
buffOff = 0;
rshift = screen->format->Rshift / 8;
gshift = screen->format->Gshift / 8;
bshift = screen->format->Bshift / 8;
bits = (unsigned char *)screen->pixels;

for (y = 0; y < 480; y++) {
workingScreenOff = screenOff;
workingBuffOff = buffOff;
for (x = 0; x < 640; x++) {
bits[workingScreenOff+rshift] = buffer[workingBuffOff+2];
bits[workingScreenOff+gshift] = buffer[workingBuffOff+1];
bits[workingScreenOff+bshift] = buffer[workingBuffOff];
workingScreenOff += screen->format->BytesPerPixel;
workingBuffOff += BPP;
}
screenOff += screen->pitch;
buffOff += BUFFER_PITCH;
}

Again thanks (and Sam, have a good vacation, I’m sure you earned with
with Civ:CTP)

			-fjr