Sign in to follow this  
mengha

Functions as parameters to classes?

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 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