LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Route all Windows messages to the game engine
return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}
Here i am routing windows messages to my game engine so that HandleEvent can be the function to process the messagess. My question is:
I dont quite understand how returning HandleEvent(hWindow, msg, wParam, lParam); to WndProc() routes windows messages to the HandleEvent() function, could you explain to me how windows reads this code so that it routes the messages?
Think about this by such way: you call HandleEvent function with hWindow, msg, wParam, lParam parameters. It makes all necessary handling and returns some value. This value is returned by WndProc function.
Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent, because with a normal function if we returned a value that function would hold that value, like in this example:
int sum()
{
int a = 5 + 2;
return a;
}
x = sum();
Here, because we have returned int a to sum(), we can assign its returned value to a
variable, now, x is equal to 7.
So i cant see how wndproc knows to route all messages to handleevent.
Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent
Rather than think of it as 'returning HandleEvent', think of it as 'calling HandleEvent, then returning the return value of HandleEvent. These concepts -- calling functions && function return values, are the *basics* of C++. Looks like you skipped past your 'beginning C++' book and jumped ahead to 'game development in Win32/MFC & c++' or something. Thats...gonna be difficult for you.
Hmm, im still abit confused on how wndproc actually knows to route the messages to handleevent just by returning handleevent, because with a normal function if we returned a value that function would hold that value, like in this example:
What you are showing has nothing to do with how the Windows OS works. You are comparing apples to elephants.
The operating system calls WndProc, then inside of your WndProc, you're calling function X (doesn't matter what X is). X returns a value that you are now to send back to the OS. Then the OS, given the return value, does something with it -- what that something is, we can only speculate since we don't have the source code to the OS kernel.
I have a little look on the internet, but i havnt found an explanation of what DefWindowproc() does in term which i can understand, could you explain it to me please. Thanks
when a function calls another funtion, it can pass values to that function (either by copy, by reference or by pointer). So here we are passing the variables from WndProc() to the HandleEvent() function.
As to WndProc -- it is very important you return the correct value from WndProc. If you don't do that, the OS kernel function that called WndProc will not work correctly. That kernel function is relying on your function to return a proper value. If you don't return a value, you are then returning an unknown item back to the kernel, and you have no idea what can happen after that.
What is the correct value to return from WndProc? That is where reading the documentation (i.e. MSDN, for example) comes into play. Depending on the message parameter, you return a different value, and that value is whatever the documentation tells you to return. Don't just make up your own number -- it isn't a "don't care" scenario. Windows programs that act erratically can trace the erratic behaviour back to not return proper values from WndProc.
The bottom line is this -- your WndProc returns a value back to the OS kernel -- not just any value, but a value that means something to Windows after that WndProc is called.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 27th, 2011 at 08:18 PM.
"when a function calls another funtion, it can pass values to that function (either by copy, by reference or by pointer). So here we are passing the variables from WndProc() to the HandleEvent() function."
...i didnt know that before, i now understand whats going on here.
Bookmarks