SetPixelFormat

Started by
3 comments, last by Tim Cowley 20 years ago
I decided to learn OpenGL coding from scratch, so I dont have to look up NeHe every time i start a new project, but I have a problem. My computer crashes whenever I call SetPixelFormat. Not the program, the computer. Screen goes black, and comes up with Windows protection error, or BSOD with no hope of recovery This is depressing, any ideas why this happens? Code:
bool globalQuit = false;

struct OpenGL_ {
	OpenGL_() { hWnd = NULL; hDC = NULL; hRC = NULL; }
	HWND hWnd;
	HDC hDC;
	HGLRC hRC;
} OpenGL;

bool StartupGL(HWND wnd)
{
	OpenGL.hWnd = wnd;

	PIXELFORMATDESCRIPTOR pfd = { 
    sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd 

    1,                     // version number 

    PFD_DRAW_TO_WINDOW |   // support window 

    PFD_SUPPORT_OPENGL |   // support OpenGL 

    PFD_DOUBLEBUFFER,      // double buffered 

    PFD_TYPE_RGBA,         // RGBA type 

    32,                    // 24-bit color depth 

    0, 0, 0, 0, 0, 0,      // color bits ignored 

    0,                     // no alpha buffer 

    0,                     // shift bit ignored 

    0,                     // no accumulation buffer 

    0, 0, 0, 0,            // accum bits ignored 

    32,                    // 32-bit z-buffer     

    0,                     // no stencil buffer 

    0,                     // no auxiliary buffer 

    PFD_MAIN_PLANE,        // main layer 

    0,                     // reserved 

    0, 0, 0                // layer masks ignored 

    }; 
    

	OpenGL.hDC = GetDC(wnd);

	if (!OpenGL.hDC)
		return false;

	GLuint pixelFormat = ChoosePixelFormat(OpenGL.hDC,&pfd);
	
	if (!pixelFormat)
		return false;

	char buf[80];
	sprintf(buf,"pf: %u",pixelFormat);

	MessageBox(NULL,buf,NULL,MB_OK);

	if (!SetPixelFormat(OpenGL.hDC,pixelFormat,&pfd))
	{

		return false;
	}
 
	OpenGL.hRC = wglCreateContext(OpenGL.hDC);

	if (!OpenGL.hRC)
		return false;

	if (!wglMakeCurrent(OpenGL.hDC,OpenGL.hRC))
		return false;
	
	return true;
}

void CleanupGL()
{
	if (OpenGL.hDC)
	{
		wglMakeCurrent(OpenGL.hDC,NULL);
		if (OpenGL.hRC)
		{
			wglDeleteContext(OpenGL.hRC);
		}
		ReleaseDC(OpenGL.hWnd,OpenGL.hDC);
	}

	OpenGL.hDC = NULL;
	OpenGL.hRC = NULL;
	OpenGL.hWnd = NULL;
}

LRESULT WndProc(HWND wnd, UINT message, WPARAM w, LPARAM l)
{
	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(wnd,message,w,l);
		break;
	}
	return 0;
}

int APIENTRY WinMain(HINSTANCE curInst, HINSTANCE prevInst, LPSTR cmdLine, int show)
{
	WNDCLASSEX wc;
	ZeroMemory (&wc, sizeof (WNDCLASSEX));						// Make Sure Memory Is Cleared

	wc.cbSize			= sizeof (WNDCLASSEX);					// Size Of The wc Structure

	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraws The Window For Any Movement / Resizing

	wc.lpfnWndProc		= (WNDPROC)(WndProc);				// WindowProc Handles Messages

	wc.hInstance		= curInst;				// Set The Instance

	wc.hbrBackground	= (HBRUSH)(COLOR_APPWORKSPACE);			// Class Background Brush Color

	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer

	wc.lpszClassName	= "fonger_wnd";				// Sets The Applications Classname

	if (RegisterClassEx (&wc) == 0)							// Did Registering The Class Fail?

	{
		// NOTE: Failure, Should Never Happen

		MessageBox (HWND_DESKTOP, "RegisterClassEx Failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;													// Return False (Failure)

	}

	HWND wnd = CreateWindowEx(WS_EX_APPWINDOW,"fonger_wnd","fonger''s window",WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,CW_USEDEFAULT,CW_USEDEFAULT,640,480,NULL,NULL,curInst,NULL);

	if (!wnd)
		return 0;

	ShowWindow(wnd,SW_SHOWNORMAL);

	if (!StartupGL(wnd))
	{
		MessageBox(NULL,"failed",NULL,MB_OK);
		CleanupGL();
		//PostQuitMessage(0);

		DestroyWindow(wnd);
		return 0;
	}

	//UpdateWindow(wnd);


	MSG msg;

	while (!globalQuit)
	{
		while (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
		{
			if (GetMessage(&msg,NULL,0,0))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			} else
				globalQuit = true;
		}
	}

	CleanupGL();

	return 0;
}
I searched the forums for it, found heeeeaps of ppl had the same problem, but NONE of them had a solution posted - the threads just died :s
Advertisement
The code doesn''t crash on my system: WinXP Pro Nvidia GF4 4200

I never had this issue before but I can tell you what I do that is different from yours.

I do the gl startup in the WM_CREATE message. I don''t know if it would make any difference but at least the window won''t be created if you can''t create the gl context.

I noticed you did a ShowWindow(wnd,SW_SHOWNORMAL); before you even called StartupGL. I suspect there may be the problem. If nothing else, the message box (for displaying the pixelformat) will cause your window to update before StartupGL was called.

Other than that.. umm.. I''m off to bed
quote:Original post by JotDot
The code doesn't crash on my system: WinXP Pro Nvidia GF4 4200

I never had this issue before but I can tell you what I do that is different from yours.

I do the gl startup in the WM_CREATE message. I don't know if it would make any difference but at least the window won't be created if you can't create the gl context.

I noticed you did a ShowWindow(wnd,SW_SHOWNORMAL); before you even called StartupGL. I suspect there may be the problem. If nothing else, the message box (for displaying the pixelformat) will cause your window to update before StartupGL was called.

Other than that.. umm.. I'm off to bed

Thanks for the ideas, but neither of these is my problem. I've tried moving ShowWindow before and after the code, tried with and without the message box, and it doesn't work either way. I'm pretty much doing exactly what NeHe does in his basecode, which incidentally runs perfect on my system :|

edit: btw im on 98se



[edited by - Tim Cowley on March 29, 2004 3:19:10 AM]
halp
try and rearrange your code a bit. make it more like the nehe code if that works on your computer. I havn''t had time to look at your code yet, but I will try and find the time to do it

Programmers Resource Central
____________________________________________________________Programmers Resource Central

This topic is closed to new replies.

Advertisement