Heh. Just thought I'd set things straight.
As l99057j pointed out, in event driven programming the program waits for events to happen before doing anything. It could be that a button or a menu has been pressed. The mouse moved. The canvas has been painted on and it needs to be updated. This means that the CPU is idle most of the time, and when the CPU is idle it's using less power (and producing much less heat).
PF also uses events to handle stuff such as closing the window, mouse/stylus movement and button presses. BUT, instead of waiting for the events, it peeks at the event queue (or window message queue which is a more proper name for it) to see if its empty. If it's not, then the event (or message) is handled by the appropriate message handler. But if it is empty, then PF runs the GameLoop.
- Code:
// Main loop
for(;;)
{
MSG msg;
if (::PeekMessage( &msg;, 0, 0, 0, PM_REMOVE ))
{
if (msg.message == WM_QUIT)
break;
::TranslateMessage( &msg; );
::DispatchMessage( &msg; );
}
else
{
if (!m_bSuspended)
GameLoop();
}
}
What this means is that the CPU is never idle. Either it is taking care of the events, or it is running the GameLoop. And if the GameLoop is updating the screen every time (like I do) then the CPU will will have a constant high workload.
But, as FZammetti suggested, the smart thing to do is to figure out if the screen content needs to be updated, and if it's not then you simply exit the GameLoop. By doing this, you avoid a lot of expensive calculation (or whatever you're doing in the loop) and all the data movement from memory to the backbuffer (i.e. the blits).
Unfortunately, this doesn't completely remedy the fact that PF never alows the CPU to catch its breath (i.e. to idle). There is really not much differance between running the GameLoop a few times calculating stuff and running it many many times doing nothing. The calls to GameLoop alone will keep the CPU busy.
What you can do if you don't need to squeeze that last drop of performance out of your application is to call Sleep(int milliseconds) at the end of your game loop (or preferably after you concluded that the screen should not be updated and are about to exit the loop). The Sleep function will force the CPU to idle, since it waits for a specified amount of time to pass before continuing execution. I found this little trick to be VERY usefull for lowering the CPU work load (on my app, a brief sleep of 50 ms lowered the average CPU level on the desktop from 40-50% down to 3-4%). This will however affect the theoretical maximum framerate, so you have to figure out a good value for your app.
But if you don't need 30+ in FPS and can live with around 20 FPS, then sleeping 40-50 ms the next time you don't need to update the screen (or a few ms every time you call GameLoop) won't hurt.
So, the thing FZammetti said about "There's no more battery life penalty to this than sitting at a dialog waiting for the user to click an Ok button in any standard WinCE app." is not entirely true. How much battery time you can save by reducing the average CPU work load is another question though, and I havn't got the slightest idea. The best advice I can give here is try running your game WITHOUT the sleep trick I just explained, and then WITH it until the battery runs out. Compare the two times and you'll get a rough idea of how effective this is. Feel free to post the results here, since I'm very keen on knowing this myself. I don't own a PDA device myself so I can't do this test.
Bah.. a long note again.. Sorry guys.