Hello. I got a problem with wgl pixel format.
the code is quite simple, I choose a pixel format which has 16 color bits, 16 depth bits, and 8 alpha bits.
int pixelformat;
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;
pfd.cAlphaBits = 8;
pixelformat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelformat, &pfd);
hRC = wglCreateContext(hDC);
Before I choose pixel format, I called the ChangeDisplaySettings function to change the screen display settings to 1024*768*16.
The strange problem is, when my desktop display setting is 1280*800*16, the program works well (change display setting to 1024*760*16, choose a pixel format, use glRectf to draw a rectangle), but when my desktop display setting is 1280*800*32, all things go wrong. I use the glColor3f(1, 1, 1) color but I see (0, 1, 1) color on my screen. also, the FPS is quite low.
And more, if I delete the "pfd.cAlphaBits = 8;" from my code, all thing go well again.
I've tested my program code on two different PC, one is: intel 945G, and other is: nVidia Geforce Go 7300. but both have the same problem.
I don't know why.
complete code here: (language: "C", IDE: "Visual Studio 2005")
#include <windows.h>
#include <GL/gl.h>
#include <stdio.h>
#pragma comment (lib, "opengl32.lib")
static PCTSTR gs_CLASS_NAME = TEXT("Default Window Class");
static int width = 1024;
static int height = 768;
static int bpp = 16;
static int fullscreen = 1;
static HWND hWnd;
static HDC hDC;
static HGLRC hRC;
#define MAX_CHAR 128
void drawString(const char* str) {
static int isFirstCall = 1;
static GLuint lists;
if( isFirstCall ) {
isFirstCall = 0;
lists = glGenLists(MAX_CHAR);
wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
}
for(; *str!='\0'; ++str)
glCallList(lists + *str);
}
void showfps() {
char str[20];
double fps;
// cal fps
{
int current;
static int last;
static double last_fps;
static const int n = 50;
static int count = 0;
if( ++count < n )
fps = last_fps;
else {
count = 0;
current = GetTickCount();
fps = 1000.0 * n / (current - last);
last = current;
last_fps = fps;
}
}
// draw string
sprintf(str, "FPS: %g", fps);
glColor3f(1, 1, 1);
glRasterPos2f(-1.0f, 0.9f);
drawString(str);
}
static LRESULT CALLBACK wndProc(
HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if( msg == WM_KEYDOWN )
PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int main() {
DWORD style, exstyle;
int position;
// register class
{
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_NOCLOSE;
wc.lpfnWndProc = wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(0);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = gs_CLASS_NAME;
wc.hIconSm = 0;
if( !RegisterClassEx(&wc) )
return 1;
}
// set style & change resolution
{
if( fullscreen ) {
DEVMODE mode = {0};
style = WS_POPUP;
exstyle = WS_EX_APPWINDOW | WS_EX_TOPMOST;
position = 0;
mode.dmSize = sizeof(mode);
mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
mode.dmPelsWidth = width;
mode.dmPelsHeight = height;
mode.dmBitsPerPel = bpp;
if( ChangeDisplaySettings(&mode, CDS_FULLSCREEN) !=
DISP_CHANGE_SUCCESSFUL ) {
fullscreen = 0;
goto windowed;
}
} else {
RECT rect;
windowed:
style = WS_OVERLAPPEDWINDOW & (~WS_THICKFRAME);
exstyle = WS_EX_APPWINDOW;
position = CW_USEDEFAULT;
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
}
// create window
hWnd = CreateWindowEx(exstyle, gs_CLASS_NAME, TEXT(""), style,
position, position, width, height,
0, 0, GetModuleHandle(0), 0);
if( !hWnd )
return 1;
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
hDC = GetDC(hWnd);
if( !hDC )
return 1;
// initialize opengl
{
int pixelformat;
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;
pfd.cAlphaBits = 8;
pixelformat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelformat, &pfd);
hRC = wglCreateContext(hDC);
printf("pixel format: %d\n", pixelformat);
wglMakeCurrent(hDC, hRC);
}
// message loop
{
MSG msg;
for(;;) {
if( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) ) {
if( msg.message == WM_QUIT )
return 0;
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
showfps();
SwapBuffers(hDC);
}
}
}
// end
return 0;
}