PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

newbie question re battery life

I'm porting an existing puzzle/logic game to PocketPC. In the game the user selects the action they want to take on their "turn" and then there is some animation that averages a second or so in length.

I'm just beginning to evaluate Pocketfrog and notice that the game loop appears to be running continuously. I'm wondering what impact this has on battery drain? I understand the constant looping is necessary for high FPS games, but since my game only requires high FPS for short bursts I thought I might be able to modify this. Of course, if it doesn't carry a high battery penalty in the first place, I'm not going to bother.

Thanks in advance,
LEJ
There probably is not a library out there that doesn't work this way. In fact, WinCE itself works this way.

As I'm sure you are well aware, all Win32 apps (and MFC apps at a lower level) work by entering an "endless" loop looking at a message que and dispatching incoming messages as they are received for the rest of your program to deal with.

PocketFrog is in essence just doing the same thing. 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.

Now, if you as the application developer are doing a bunch of things in your game loop that you don't really have to be doing, then of course the CPU is working more and this will affect battery life. If you don't update anything in your game loop and just exit immediately, the previous contents of the back buffer will be blitted to the screen and that's it (which I believe is being done with any WinCE app at a low level, you just never know it).

So in short, it's definitely not worth your time to try and modify PF's behavior in this regard, it's pretty much the key concept to how it works. Just be sure you don't do more than you have to in your game loop and you will be fine.
l99057j · 24 Jul 2003, 18:57 UTC — battery life
Thanks for the reply, fzammetti. Part of me felt that way all along but here is why I was hesitant.

When I write a regular program, or event-driven game in Win32 there is a WaitMessage() call in the message pump. For a high-performance game (i.e. look at any of the samples that come with DirectX) this goes away and the game is in a continuous loop.

Looking at the task manager, you can see that the CPU is experiencing low utilization using method 1. But switch to method 2 and the CPU jumps to 100%. Of course that makes perfect sense, but under a battery-powered scenario, 100% utilization isn't a good thing.

But if the consensus is that things are different under CE and I'm not sucking the battery dry, I'll gladly use the stock code... the less work the better!
I think it's fair to say that doing something each message pump iteration will certainly cost battery life where doing nothing won't. I think the bottom line though is that plenty of games out there are using PF or GapiDraw, and both use the same technique in this respect, so I wouldn't worry about it. If you gain two minutes of battery life over eight hours, your coding efforts weren't really worth it, and I suspect that's the only kind of difference you'd make, if any at all.

Yep, just concentrate on your game code instead for sure. Let Thierry, Johan and those guys that operate on a higher level (than me anyway) worry about this stuff. :)
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.
As a side note. When Presto asked us to evaluate the Pod game (very cool b.t.w.) I told him to add the Sleep since my desktop computer good overheated after a while..

He successfully reduced the average CPU workload from 50-53% down to 1-3% at the offgame pages (intro, settings, help etc) and still remaided good FPS for the animations. I suspect he never did bother to add the Sleep to the ingame GameLoop since I couldn't see any reduced work load there. I guess he figured he needed all the speed he could get.

If it were me, I'd add a few ms (say 10) at the end of every GameLoop anyways. It will make a differance on the desktop, trust me. Maybee not as much on a PDA though. Feel free to give this a try and let me know.

Cheers
Heh.. just being an ass here but anyways

FZametti wrote:Let Thierry, Johan and those guys that operate on a higher level (than me anyway) worry about this stuff.

Don't you mean lower level :wink:
Heh... I paused on the higher level vs. lower level thing as I typed it... I could see the logic either way.
I realize this is the PF forum, but I just thought it'd be an interesting thing to see... My latest project is actually developed with GapiDraw, and I just did a quick and dirty CPU workload check during gameplay... The game accounted for roughly 2% CPU utilization (I was hovering around 2% normally, jumped to 4% with the game running). Thierry, if you see this, can we assume that PF2 will have a better utilization curve than PF1? I can't go and compare a PF-based version of the game unfortunately, but it seems reasonable to assume, based on what adde posted about Pod, that it would be a higher utilization than what I'm seeing with GD. I'd love to see that improved in PF2.
Ha the joys of providing a framework =)

If someone doesn't need it, he should simply ditch it or modify it to its need. Should be pretty easy to modify the message loop to call GetMessage() instead of PeekMessage(). This way you have an event-based game. Really, that's all there is to it.

Then if you want to get out of the loop once in a while, a timer can be used to send WM_TIMER messages to the window. (Isn't this what GD does?)
I'd be interested to know if that's what GD does. Did you mean that you think WM_TIMER messages are used to do ProcessNextFrame? If that's what you meant, I'd be very surprised to learn that's the way GD does it because WM_TIMER I always thought wasn't guaranteed to be regular (but I'm betting I misunderstood what you meant)...
fzammetti: GapiDraw calls WaitForMultipleMessages on each sleep loop. If a windows message should be received the game loop resumes immediately... Works wonders on CPU and battery life...
Good deal, that sounds like a very smart choice indeed. Thanks for the info!
Thierry wrote: If someone doesn't need it, he should simply ditch it or modify it to its need. Should be pretty easy to modify the message loop to call GetMessage() instead of PeekMessage(). This way you have an event-based game. Really, that's all there is to it.

Yes, and thanks for supplying the source code so we can fully understand and modify it to our own likings.

Not everyone has a good knowledge about the interior of PF (or C++ for that matter) though, and that was one of the reasons for the Game class/framework as I understand it. As FZammetti pointed out, the framework should be modified and improved once PF2 arrives. I havn't looked at the versions after PF 0.6.0 so maybee I'm a bit late with these suggestions, but anyway:

1. Make it easier for the user to customize the appearance without modifying the Game class.
2. Make it easier for the user to incorporate external libraries (sound, font, 3D, etc).
3. Make it optional for the Game class to provide the WM event loop (active by default, but as a separate thread (or controlled by timers) if you want to use PF as a subwindow.) As an alternative, provide two frameworks: One for games, and one for GUIs/applications.

I'm not saying Thierry should do all this. Heck, I might even end up doing it myself once PF2 is released. If people think it's a good idea that is.