# Functions as parameters to classes?

## Recommended Posts

mengha    130
I've only messed around with really simple classes but I wanted to have a go at making a Window Class for myself, just to create the Window and have the window procedure etc. What stumped me though is that when you have no message, its time to work on the game code etc. but how can I tell the class what function to run? Can I tell the class what function to un when its not processing windows messages? Is it a CALLBACK function I need? thanks.

##### Share on other sites
Danny9821    122
From what I've read so far functions like that one you are wanting to create (for games) run some predefined functions using a game loop.

StartGame();

while(TRUE)
{
if(!msg)
GameLoop();
}

EndGame();

Where StartGame(), GameLoop() and EndGame() are all defined somewhere else, in a separate file for example. That would have created a very simple game engine.

I'm not sure if that's exactly what you were asking, but I hope it helps.

Danny

##### Share on other sites
mengha    130
I've written a class to handle a window for me.

	while(true)	{			if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))		{			if(msg.message == WM_QUIT)			{				break; 			}			::TranslateMessage(&msg); 			::DispatchMessage(&msg); 		}		else		{                      //Call Game Function.		      Render();		}	}	return msg.wParam;}

How could I tell the class that I want to call the Render() function there? Because I will probably reuse this class so I wont have to rewrite it again.

I mean kind of like where you have to specify the Window Procedure function in the Window Class structure.

wcMain.lpfnWndProc = WindowProc;

##### Share on other sites
Monder    993
Do you want to be able to specify a function that is called when your class is not processing messages without explicitly calling it? If so what you need is a function pointer, see here for more info. As a quick example you could do something like this:

class WindowClass{public:   void (*RenderFunction)( );   void WindowLoop( );   //other WindowClass stuff};void Render( ){   //Render things}void WindowClass::WindowLoop( ){	while(true)	{			if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))		{			if(msg.message == WM_QUIT)			{				break; 			}			::TranslateMessage(&msg); 			::DispatchMessage(&msg); 		}		else		{                      //Call Game Function.		      RenderFunction( );		}	}	return msg.wParam;}...WindowClassInstance.RenderFunction = &Render;

##### Share on other sites
mengha    130
Thats exactly what I was fter. thanks.