Can't create a Rendering context (WIN32)

Started by
1 comment, last by trysil 22 years, 2 months ago
I have implemented a engine class based on the example in "OpenGL game programming" ch 20. But for some reason I cant get it to work Could someone take a look? the WinMain;
    

int WINAPI WinMain(	HINSTANCE	hInstance,
					HINSTANCE	hPrevInstance,
					LPSTR		lpCmdLine,
					int			nCmdShow)
{
	teEngine* theEngine;
	CoInitialize(NULL);
	if (!teGLWindow::registerWindowClass(hInstance))
	{
		MessageBox(NULL,"Could not register window class",0,0);
		return 0;
	}


	try{
		theEngine = new teEngine("The Terra Engine", 640,480,32);
		theEngine->Init();
		theEngine -> MessageLoop();
		theEngine->Shutdown();
	}
	catch (char* sz)
	{
		MessageBox(NULL,sz,0,0);
		return 0;
	}


	CoUninitialize();

	return 0;
}
    
The teEngine Class:
        

class teEngine  : public teGLWindow
{
public:
	LRESULT MessageLoop();
	void updateWindow(int width, int height);
	void Run();
	bool Init();
	void Shutdown();
	teEngine(char* name, int width, int height, int bits)  : teGLWindow(name, width,height,bits) {};
	virtual ~teEngine();



};
  
The glWindow class
        
class teGLWindow  
{
public:
	 teGLWindow();
	void paint();
	void endFullScreen();
	void beginFullScreen(int width, int height, int bits);
	int pixelFormat;
	void setupPixelFormat();
	static bool registerWindowClass(HINSTANCE hI);

	Screeninfo Screen;
	bool createGLWindow();
	bool killGLWindow();
	bool initGL();
	HINSTANCE hInstance;
	HGLRC hRC;
	HDC hDC;
	HWND hWnd;
	bool ReSizeGLScene(int width, int height);
	teGLWindow(char* name, int width, int height, int b);
	virtual ~teGLWindow();

};
  
The WinProc function
        
LRESULT APIENTRY WndProcGL(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	teGLWindow *glWindow = (teGLWindow*)GetWindowLong(hWnd, GWL_USERDATA);
	if ((glWindow == NULL) && (uMsg != WM_CREATE))
	{	
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	// dispatch messages

	switch (uMsg)
	{	
		case WM_CREATE:			// window creation

		{
			HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
			glWindow = (teGLWindow*)(((LPCREATESTRUCT)lParam)->lpCreateParams);

			if (!SetWindowLong(hWnd, GWL_USERDATA, (LONG)glWindow)) 
			{
				DWORD a = GetLastError();
				return a;
			}
			glWindow->hWnd = hWnd;

			return glWindow->createGLWindow(); (calls setPixelformat below and tries to create
                                                                                           the rendering context)
		}

<removed some code>
  
The pixelformat setup
  
void teGLWindow::setupPixelFormat()
{
	PIXELFORMATDESCRIPTOR pfd=				
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};
	pixelFormat = ChoosePixelFormat(hDC, &pfd);
	if(pixelFormat == 0)
	{
		throw "Could not setup pixelformat";
		return;
	}
	SetPixelFormat(hDC, pixelFormat, &pfd);
}
  
  
and finally the registerWindow class function
        
bool teGLWindow::registerWindowClass(HINSTANCE hI)
{
	WNDCLASS wc;
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	
	wc.lpfnWndProc		= WndProcGL;					        
	wc.cbClsExtra		= 0;									
	wc.cbWndExtra		= 0;									
	wc.hInstance		= hI;						
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= NULL;
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= "OpenGL";

	if (!RegisterClass(&wc))
	{
		return false;
	}
	return true;
}
    
What happens is that the window opens but the rendering context hasn't been initialized. Sorry about the amount of code, but I'm getting desperate "A witty and slightly sarcastic quote from an unkown source" -- The generic SIG /trysil Edited by - trysil on February 7, 2002 4:19:58 PM
"A witty and slightly sarcastic quote from an unkown source"-- The generic SIG/trysil
Advertisement
Doesn't some one know? Or do I have to return to MFC programming?

The main difference between my code and the example is that my WinProc isn't declared as a friend
to the Window class. My compiler VC++ 5.x complains about it when I try.



Edited by - trysil on February 7, 2002 9:28:11 PM
"A witty and slightly sarcastic quote from an unkown source"-- The generic SIG/trysil
Your code is a little hard to follow but I was having the exact same prob... I bet if you profile your code - it will spend about 2-3 seconds in your main loop each frame.

When this happened to me - it was because I was using the exact same DC to create the pixelformat that I was using to flip the buffers. Try creating a static DC in the window class and LEAVING IT ALONE! Use that one to call the SwitchBuffer method. Have a second DC to twiddle with and set the pixelformat... When I did this - It worked great.

If this is not the same proble - ignore me.

Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.

This topic is closed to new replies.

Advertisement