Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bzroom

WndProc in class?

This topic is 5488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you put a winproc call back in a class? I have this

class cOpengl
{
public:
	cEngine *parent;

	HDC			hDC;												
	HGLRC		hRC;												
	HWND		hWnd;		
	HINSTANCE	hInstance;

	void ReSizeGLScene(GLsizei width, GLsizei height);
	void KillGLWindow(GLvoid)	;
	bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag);

	LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam);

	cOpengl(cEngine *engine);
	~cOpengl();
};

for my class... and here are the functions

bool cOpengl::CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
//......


	hInstance			= GetModuleHandle(NULL);					
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	
	wc.lpfnWndProc		= (WNDPROC) WndProc;						
	wc.cbClsExtra		= 0;										
	wc.cbWndExtra		= 0;							
	wc.hInstance		= hInstance;		
//...


}


LRESULT CALLBACK cOpengl::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)					
{
///

}

i get this error on this line wc.lpfnWndProc = (WNDPROC) WndProc; C:\Documents and Settings\Matt\Desktop\Engine\Engine\opengl.cpp(98) : error C2440: ''type cast'' : cannot convert from '''' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' how do i fix this?

Share this post


Link to post
Share on other sites
Advertisement
WndProc can''t be a __thiscall (member) function, although you can have a pointer to your WndProc in your class.


For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!

Share this post


Link to post
Share on other sites
Basicaly i need the wndproc to access members of the class, i could just make some global class pointer but i would like to maybe have mulitple instances, so i want to keep everything in it.

Share this post


Link to post
Share on other sites
what you have to do is use a function that is not encapsulated in your class to redirect to your wndproc function in your class... this is the only way i know to do this... so you'll have something like this:

(taken from Advanced 3D Game Programming with DirectX 8 by Peter Walsh)


LRESULT CALLBACK GlobalWndProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
assert( MainWindow() );

return MainWindow()->WndProc( hWnd, uMsg, wParam, lParam );
}

LRESULT cWindow::WndProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
/**
* If we get another message after a thrown
* error, return immediately
*/

if( g_pThrownError )
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


bool bRunDefault = false;

try
{
switch( uMsg )
{
case WM_CREATE:
{
MainWindow()->m_hWnd = hWnd;
MainWindow()->m_bActive = true;
break;
}
case WM_PAINT:
{
// don't do anything.

ValidateRect( hWnd, NULL );
break;
}
case WM_KEYDOWN:
{
switch( wParam )
{
// Quit if the escape key is pressed

case VK_ESCAPE:
{
PostQuitMessage(0);
break;
}
default:
{
break;
}
}
}

case WM_SETFOCUS:
{
if( Input() )
{
Input()->SetFocus();
}
break;
}
case WM_KILLFOCUS:
{
if( Input() )
{
Input()->KillFocus();
}
break;
}
case WM_ERASEBKGND:
{
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
bRunDefault = true;
break;
}
}
catch( cGameError& err )
{
g_pThrownError = new cGameError( err );
return 0;
}

if( bRunDefault )
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

return 0;
}


and when you create your window...


void cWindow::RegisterClass( WNDCLASSEX* pWc )
{
WNDCLASSEX wcex;
if( pWc )
{
wcex = *pWc;
}
else
{
// fill in some default values

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = GlobalWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = AppInstance();
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = m_name.c_str();
wcex.hIconSm = 0;
}

if( 0 == RegisterClassEx(&wcex) )
{
throw cGameError("[cWindow::RegisterClass]: Registering window class failed!\n");
}
}


hope it helps

PS: btw whats the command you use to put code blocks like you did? (im new to gamedev)

Edit: nm got it

Yann L POWNS Carmack

[edited by - sross on June 16, 2003 6:22:19 PM]

Share this post


Link to post
Share on other sites
sross that dosn''t solve the problem since you have MainWindow as like a global variable, which is what im trying to avoid.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!