8

I'm making a 2D game for Android, and I've recently been looking at optimization.

I looked at the battery-use in Settings and found that after leaving my game in the foreground (with the screen on) for a little over an hour, I had drained 11% of the battery (Motorola Xoom Honeycomb).

Is this something that users should expect from a game on their phone/tablet?

A bit of Info:

  • My game uses Opengl-es for rendering (continuous render)

  • Logic runs in a separate thread for performance.

  • I used getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); thought it was necessary as the game requires a bit of thinking time from the user.

  • Simple Particles

So, is that kind of battery consumption normal for a game?

And what steps can I take to reduce the battery consumption?

jimmym715
  • 1,512
  • 1
  • 16
  • 25
Jack
  • 2,625
  • 5
  • 33
  • 56
  • If you can schedule small `Thread.wait(timeout)` in your various threads it will remove the tax on the CPU and therefore be better for battery. i.e. if you've updated the screen recently, you can actually wait, say, at least 20ms (50Hz) before updating again. – Stephen Quan Feb 09 '12 at 20:05
  • @BicycleDude thats a good idea. It would still need a good 30fps during certain parts of gameplay but that sounds good. – Jack Feb 09 '12 at 20:09
  • @BicycleDude is Thread.sleep a similar thing? – Jack Feb 09 '12 at 20:10
  • @Jack: Yes, for your purpose, using `Thread.wait(timeout)` or `Thread.sleep(timeout)` is pretty much the same thing. – Tudor Feb 09 '12 at 20:14
  • The problem with adding this delay is that if your rendering code takes 5ms usually and 20ms in extremes, your frame rate will dip. A solution to get constant render rate is to measure the time it took to render and subtract that from the time you sleep so that regardless or render time the next frame is evenly spaced and consistent. – gravitron Feb 09 '12 at 20:26
  • @Jack - wait and sleep are not the same thing. One requires a monitor that is released (wait), the other does not. If you simply want to pause a thread, without being in a synchronized block you have to use sleep. – Robin Feb 09 '12 at 20:26
  • @gravitron this is what i'm doing currently. During parts of gameplay the game needs this constant frame rate, but other times gameplay is a "bit like chess" in the sense that things only happen when there is input. Does this mean I could just sleep the entire thread and wait for input during these parts? – Jack Feb 09 '12 at 20:32
  • If an hour of being on, and playing, drains the battery by 10%, that's ~10 hrs of use. That doesn't seem that bad to me for full animation. – Dave Newton Feb 09 '12 at 21:41
  • @DaveNewton ^that is very true – Jack Feb 09 '12 at 21:43
  • @Jack that's pretty much it. BTW, in reviewing I think `sleep` is a better choice. If you don't need to update the screen, then, that's probably appropriate to schedule some pauses. In fact, I'd go as far to suggest pauses of 1 second so at a time. So, you can at least implement a thinking clock that updates the UI once a second. – Stephen Quan Feb 09 '12 at 21:46

2 Answers2

0

Justin's answer is good, but don't forget some old-school basics.

Having your program busy wait will drain the battery too. You must use the CPU to advance the game, but there is no need to have it busy wait. Be sure to put your process to sleep for the estimated time till the next iteration of the game's processing cycle.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

Leaving the screen on alone (without the game running) will generate similar battery usage.

My advice to you would be to Set the screen to Dim whenever there is a pause in your game, or if the user has not touched the screen in X amount of time as oppose to leaving it on at full brightness.

You can do this by adjusting screenBrightness/buttonBrightness,

The range of these fields is 0-> 1

you can see the Context of setting this here

Community
  • 1
  • 1
  • but doesn't the screen's battery usage get its own section in the chart? (which used 43% of the battery according to my stats) – Jack Feb 09 '12 at 20:28
  • yes! It does. This would still contribute to the total battery usage. Other than this and what BicycleDude has suggested, I would suggest reading this article: http://developer.android.com/training/monitoring-device-state/index.html –  Feb 09 '12 at 20:50
  • One thing to be aware of is that dimming the screen doesn't work well/at all if the user has brightness set to "Automatic" (I've never managed to get it to work although it's not a priority at the moment) – Basic Feb 16 '12 at 19:37