---------------Part of
Code------------------------------------------------------------------------
-------------
dmS.dmPelsWidth = 800; dmS.dmPelsHeight = 600; dmS.dmBitsPerPel = 16;
dmS.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
ChangeDisplaySettings (&dmS, CDS_FULLSCREEN);
//no problem
hWnd = CreateWindowEx (WS_EX_APPWINDOW, "OpenGL", "OpenGL",
WS_POPUP|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, 800, 600, 0,
0, 0, 0); //no problem
hDC = GetDC (hWnd);
// no problem
static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0,
0, 0, 0 };
int pf = ChoosePixelFormat (hDC, &pfd);
//PROBLEM! returns Zero
SetPixelFormat(hDC, pf, &pfd);
hRC = wglCreateContext (hDC);
wglMakeCurrent (hDC, hRC);
ShowWindow(hWnd,SW_SHOW);
----------------------------------------------------------------------------
----------------------------------
NOTE:
1- some members like : "m_hdc" and "m_ready" need to be changed for
YOUR project
2- try different values with SetContext(16, 16); ...
BOOL CGLContext::SetContext(BYTE iPixelType, BYTE cDepthBits)
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
iPixelType,
0,0,0,0,0,0,
0,0,0,0,0,0,0,
cDepthBits,
0,0,
PFD_MAIN_PLANE,
0,
0,0,0
};
int pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
if(pixelFormat <= 0)
{
MessageBox(NULL, "ChoosePixelFormat failed!", "CWGLContext", 1);
return false;
}
m_ready = SetPixelFormat(m_hdc, pixelFormat, &pfd);
if(!m_ready)
{
MessageBox(NULL, "SetPixelFormat failed!", "CWGLContext", 1);
return false;
}
DescribePixelFormat(m_hdc, pixelFormat, sizeof(pfd), &pfd);
if(pfd.dwFlags & PFD_NEED_PALETTE)
this->SetPalette();
m_glrc = wglCreateContext(m_hdc);
if(m_glrc == 0)
{
MessageBox(NULL, "wglCreateContext failed!", "CWGLContext", 1);
m_ready=false;
return false;
}
m_ready = wglMakeCurrent(m_hdc, m_glrc);
if(!m_ready)
{
MessageBox(NULL, "wglMakeCurrent failed!", "CWGLContext", MB_OK);
m_ready=false;
return false;
}
return m_ready;
}
BOOL CGLContext::SetPalette()
{
PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
LOGPALETTE *pPal; // Pointer to memory for logical palette
int PixelFormat; // Pixel format index
int paletteSize; // Number of entries in palette
BYTE RedMask; // Range for each color entry (7,7,and 3)
BYTE GreenMask;
BYTE BlueMask;
// Get the pixel format index and retrieve the pixel format description
PixelFormat = GetPixelFormat(m_hdc);
DescribePixelFormat(m_hdc, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR),
&pfd);
// Get the number of entries in palette. 256 colors for 8 bits
paletteSize = 1 << pfd.cColorBits;
// Allocate for the logical palette
pPal = (LOGPALETTE*)
malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
// Fill the logical palette header information
pPal->palVersion = 0x300; //support Windows3.0
pPal->palNumEntries = paletteSize; //number of colors entries
// Set the 1st entries of logical palette with the current system
palette
(void) GetSystemPaletteEntries(m_hdc, 0, paletteSize,
&pPal->palPalEntry[0]);
//Set the RGB mask
RedMask = (1 << pfd.cRedBits) - 1;
GreenMask = (1 << pfd.cGreenBits) - 1;
BlueMask = (1 << pfd.cBlueBits) - 1;
//Set all entries of the logical palette
for (int i=0; i<paletteSize; ++i)
{
pPal->palPalEntry[i].peRed =
(((i >> pfd.cRedShift) & RedMask) * 255) / RedMask;
pPal->palPalEntry[i].peGreen =
(((i >> pfd.cGreenShift) & GreenMask) * 255) / GreenMask;
pPal->palPalEntry[i].peBlue =
(((i >> pfd.cBlueShift) & BlueMask) * 255) / BlueMask;
pPal->palPalEntry[i].peFlags = 0;
}
//Create the palette
m_Palette = CreatePalette(pPal);
SelectPalette(m_hdc, m_Palette, FALSE);
RealizePalette(m_hdc);
//Free the memory allocated for the logical palette
free(pPal);
return true;
}
> int pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
e.g.:
Windows Resolution: 1024 x 768 x 8 bit if this resolution is RGB (>= 16
bits color) works fine!
----- Start of program ---------
...
Changes resolution to: 1024 x 768 x 32 bit
...
ChoosePixelFormat: Exactly as you gave but with 32 bits color depth in
pfd
ERROR! returns 0