• Advertisement
Sign in to follow this  

Windows message handling

This topic is 3765 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

This may be hard to follow as I have quite a few questions about developing a game under windows and while asking one question another may spring to mind but I will try my best to structure it so each of my question can be easily answered. I've developed a few console based games in C++ a terrible language to pick as you first it's taken close on a year to understand and i probably still write terrible code. I know alot of people recommend it but I would not there is way to much for a beginner to be aware of and I can see myself spending another year becoming comfortable with it before I can focus on the fundamentally principles on programming. my questions follow below: 1. lets start with placing a game into the winMain function it seems that you can completely program your game independent of windows then insert it into the winMain function located in or around a loop such as while(PeekMessage()).what if the game has an infinite loop how does the program manage to look up messages from windows and act upon them. 2. Follows on from the previous I stated above my games so far have had a infinite loop or at least one that continues until the end of the game this is due to the actions that continue to take place in the game even while the player is doing noting. Should this type of loop be removed and have the PeekMessage loop do this by constantly calling a function. 3. I read that it's good practice to have you program do as little as possible before returning power back to windows for various reasons. could some one give a small example of what the game may do before returning to windows and waiting for some more cpu time or is this not the case with games. 4. The next question is how does you program handle the messages from windows the peekmessage loop is constantly checking for messages but aren't all messages to do with you application already sent to WinProc function is the peekMesage function in win main just there to end your program and provide a constant loop. 5. What messages should you handle in the WinProc function i know WM_destroy is a must but lets say I have a FPS a bit ambitious for me to consider at the moment but a great example for my question. Lets say the camera is being controlled by the mouse as is common in most FPS games should I include a case to deal with mouse messages or is there a better way to do this. same goes for keyboard should I be adding cases for the relevant keys here eg. wasd as commonly used in FPS for movement. Is handling the input here the best way to do so. Thanks for taking the time to look over all of this.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ramearess
1. lets start with placing a game into the winMain function it seems that you can completely program your game independent of windows then insert it into the winMain function located in or around a loop such as while(PeekMessage()).what if the game has an infinite loop how does the program manage to look up messages from windows and act upon them.

It doesn't, which indicates that you're mistaken that you can completely write your program independent of Windows. You can if you don't process any Windows messages at all, but then your program will lock up eventually, when its queue gets filled or something else like that.

Your message pump is your game's main loop.

Quote:
2. Follows on from the previous I stated above my games so far have had a infinite loop or at least one that continues until the end of the game this is due to the actions that continue to take place in the game even while the player is doing noting. Should this type of loop be removed and have the PeekMessage loop do this by constantly calling a function.

Merge the two loops. Say your old game had the loop:
while(true)
{
DoStuff();
}

and your Windows code has the loop:
while(true)
{
if(PeekMessage(...))
{
GetMessage(...);
TranslateMessage(...);
DispatchMessage(...);
}

if(msg.message == WM_QUIT)
break;

...
}

then merge them into the loop:
while(true)
{
if(PeekMessage(..., PM_NOREMOVE))
{
GetMessage(...);
TranslateMessage(...);
DispatchMessage(...);

if(msg.message == WM_QUIT)
break;
}

DoStuff();

...
}


Quote:
3. I read that it's good practice to have you program do as little as possible before returning power back to windows for various reasons. could some one give a small example of what the game may do before returning to windows and waiting for some more cpu time or is this not the case with games.

That's just false. It's a matter of balance how frequently, in absolute, you should handle messages. Your top priorities are your frame rate and your game's heartbeat: you want those to be rock solid.

Quote:
4. The next question is how does you program handle the messages from windows the peekmessage loop is constantly checking for messages but aren't all messages to do with you application already sent to WinProc function is the peekMesage function in win main just there to end your program and provide a constant loop.

Windows is a multithreaded, object-oriented architecture. The key function here is DispatchMessage, which instructs Windows to send the message to your window procedure (which you set up and assign when creating/instantiating/subclassing your window class). PeekMessage and GetMessage give you an opportunity to modify the MSG object before dispatching (TranslateMessage, for example, converts any accelerators you may have registered, potentially even changing the message type - from a WM_CHAR, for instance, to a user message), as well as control the frequency of message handling.

Quote:
5. What messages should you handle in the WinProc function i know WM_destroy is a must but lets say I have a FPS a bit ambitious for me to consider at the moment but a great example for my question. Lets say the camera is being controlled by the mouse as is common in most FPS games should I include a case to deal with mouse messages or is there a better way to do this. same goes for keyboard should I be adding cases for the relevant keys here eg. wasd as commonly used in FPS for movement. Is handling the input here the best way to do so.

Minimally, WM_DESTROY and WM_CLOSE. For mouse input, you can either handle WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK, WM_MBUTTONDOWN, WM_MBUTTONUP, and WM_MBUTTONDBLCLK, or use Raw Input (WM_INPUT). For keyboard input, you can either handle WM_KEYDOWN, WM_KEYUP, optionally WM_CHAR (for text input), or use Raw Input (WM_INPUT). For gamepad input...

You get the picture.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement