Behaviour of ChoosePixelFormat

The wgl’s ChoosePixelFormat function uses some kind of euristic to return
the closest match to the given pixel format descriptor, whereas both, the
ARB’s ChoosePixelFormat and the glX’s ChooseVisual suceed only if they find
a pixel format that matches the given attributes. Since SDL abstract those
details, your apps may have different behaviours depending on the platform
or target machine.

For example, if you request a 32bit pixel format, when only 16bits is
available, wgl will return a 16bit pixel format, while the ARB and glX code
path will fail.

It would be nice to have a consistent behaviour in different targets, and
that can be done replacing wgl’s ChoosePixelFormat with your own function. I
can provide such replacement if there’s enough interest.–
Ignacio Casta?o
@Ignacio_Castano

The wgl’s ChoosePixelFormat function uses some kind of euristic to return
the closest match to the given pixel format descriptor, whereas both, the
ARB’s ChoosePixelFormat and the glX’s ChooseVisual suceed only if they find
a pixel format that matches the given attributes. Since SDL abstract those
details, your apps may have different behaviours depending on the platform
or target machine.

For example, if you request a 32bit pixel format, when only 16bits is
available, wgl will return a 16bit pixel format, while the ARB and glX code
path will fail.

It would be nice to have a consistent behaviour in different targets, and
that can be done replacing wgl’s ChoosePixelFormat with your own function. I
can provide such replacement if there’s enough interest.

That would be fine.

It may matter less with today’s CVS, since I fixed a bug that prevented the
ARB pixel format code from working on some drivers.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Sam Lantinga wrote:

That would be fine.

The following code enumerates the existing pixel formats and returns the
index of the first matching pixel format. Returns 0 if no matching pixel
format available.

static int MyChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR *
target ) {

PIXELFORMATDESCRIPTOR pfd;

int format_num = DescribePixelFormat( hdc, 1, sizeof(pfd), NULL );

int format_idx;
for( format_idx = 1; format_idx <= format_num; format_idx++ ) {

if( !DescribePixelFormat( hdc, format_idx, sizeof(pfd), &pfd ) ) {
// error?
continue;
}

if( (pfd.dwFlags & target->dwFlags) != target->dwFlags ) continue;

if( pfd.iPixelType != target->iPixelType ) continue;

if( pfd.cColorBits < target->cColorBits ) continue;
if( pfd.cRedBits < target->cRedBits ) continue;
if( pfd.cGreenBits < target->cGreenBits ) continue;
if( pfd.cBlueBits < target->cBlueBits ) continue;
if( pfd.cAlphaBits < target->cAlphaBits ) continue;
if( pfd.cAccumBits < target->cAccumBits ) continue;
if( pfd.cAccumRedBits < target->cAccumRedBits ) continue;
if( pfd.cAccumGreenBits < target->cAccumGreenBits ) continue;
if( pfd.cAccumBlueBits < target->cAccumBlueBits ) continue;
if( pfd.cAccumAlphaBits < target->cAccumAlphaBits ) continue;
if( pfd.cDepthBits < target->cDepthBits ) continue;
if( pfd.cStencilBits < target->cStencilBits ) continue;

return format_idx;
}

return 0;
};–
Ignacio Casta?o
@Ignacio_Castano