Sign in to follow this  
arczev

OpenGL Initializing Opengl with class wrapped window

Recommended Posts

Hi, recently I've been writing an object wersion of opengl window, and i have a problem. The most important parts of my code:
LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam){

	if (message == WM_NCCREATE) {
		LPCREATESTRUCT cs = (LPCREATESTRUCT) lParam;
		SetWindowLong(hwnd, GWL_USERDATA, (long) cs->lpCreateParams);
	}

     COGLWindow *window = (COGLWindow *) GetWindowLong(hwnd, GWL_USERDATA);

     if (window)
		return window->OGLWndProc(hwnd, message, wParam, lParam);
	else
		return DefWindowProc(hwnd, message, wParam, lParam);



}
LRESULT COGLWindow::OGLWndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam){
switch(message){
case WM_CREATE:
       	hDC = GetDC(hwnd);			
     g_HDC = hDC;
   SetupPixelFormat(hDC);		
   wglCreateContext(hDC);
   wglMakeCurrent(hDC, hRC);
break;
    . . . 
With this code(copied from tutorials) i managed to use window procedure as a class method, but Opengl initialization code simply doesnt work now. Does anybody know how to solve it? thx 4 response

Share this post


Link to post
Share on other sites
"Doesn't work" is pretty vague. What do you mean it doesn't work? Does it never get called? Do you get an exception or a crash? What happens (or doesn't happen)? Anyway, I noticed you're window creation handling is a little different than how I do it, so I'll do a little explaining.

First, here is the code I use when handling the creation of a render window (i.e. what's in your WndProc function)
        if (uMsg == WM_CREATE)
{
SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG((LPCREATESTRUCT(lParam))->lpCreateParams));
return 0;
}

RenderWindow *win = (RenderWindow*)(GetWindowLongPtr(hwnd, GWLP_USERDATA));

if (!win)
return DefWindowProc(hwnd, uMsg, wParam, lParam);


Ok, you'll probably want to use SetWindowLongPtr rather than SetWindowLong because SetWindowLongPtr has officially superseded the SetWindowLong function (and the same goes for GetWindowLongPtr and GetWindowLong).

Aside from that, it looks like the only real difference is that you call SetWindowLong on the WM_NCCREATE message and don't return anything (specific to the processing of that message, at least), whereas I listen for the WM_CREATE message and that I return 0 after handling it. I don't think there is anything wrong with listening for WM_NCCREATE, but if you do listen for it and process it, MSDN says you should return TRUE. Since you don't return TRUE after handling the WM_NCCREATE message, I'd guess that the rest of the code might be causing the callback to return some value other than TRUE that might be screwing up the creation of the window. Anyway, try adding return TRUE; after you SetWindowLong (or rather, SetWindowLongPtr).

Share this post


Link to post
Share on other sites
First, tkanks for response.
Unfortunately, adding return true after SetWindowLong(...) doesnt change the situation.
The problem is that there is no error message, just OpenGL doesnt initialize, application behaves as if there was no initializing code(GetDC, SetupPixelFormat,etc.)

Share this post


Link to post
Share on other sites
Quote:
Original post by arczev
First, tkanks for response.
Unfortunately, adding return true after SetWindowLong(...) doesnt change the situation.
The problem is that there is no error message, just OpenGL doesnt initialize, application behaves as if there was no initializing code(GetDC, SetupPixelFormat,etc.)

How do you know that the initializing doesn't get called? Have you stepped through the program with a debugger to see exactly what's going on?

Share this post


Link to post
Share on other sites
I knew because when I normally use non-object version of window procedure, this code makes the screen black and than I can draw in opengl mode.

Ok, problem solved, The last parameter of CreateWindowEx(...) should be (void*)this, I had NULL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this