Not rendering when there is no update battery optimisation - libgdx - android

I'm looking a minimising battery usage in a game I'm developing, and battery usage is something that's very hard to measure accurately, so I was hoping someone in the know might be able to answer this.
Is it likely to be worth my implementing a system where I don't render when there are no updates to what's displayed on the screen? Something like
updated = false;
for each component
updated |= component.update();
if (updated)
render();
This is the kind of thing that's much easier to add at the start, if it'll be worthwhile, than to try to add in at the end if it's required.
If I was rendering through the CPU then I'd definitely say it was worth doing, but through the GPU I'm not so sure. Will in save an insignificant amount of battery compared to things like the screen being on?
edit:
A bit of clarification. I'm creating a word game, so a lot of the time the user is going to be staring at a motionless screen looking for words. Depending on the mode the game is being played in, something could happen on the screen that isn't triggered by user input. For example, a new object appearing on the screen every 5 seconds. Rendering 300 frames where nothing happens every 5 seconds seems a real waste and is something I'd like to avoid.

I don't know if your method does minimize the battery usage, but i think it would be better to disable continuous Rendering by calling Gdx.graphics.setContinuousRendering(false); in the Applicationlisteners create or something like this.
By doing this you stop the gameloop from calling your render() method. It is then only called in the following situations:
On Input (Keypress, touchdown...)
On a call of Gdx.graphics.requestRendering();
On a call to Gdx.app.postRunnable()
Read this for more informations.
EDIT:
I have now read your edit. Well "Puzzle game" sounds a bit generally for me. If you are sure, that the user will be staring at a motionless screen for a long time, to find a possible solution, then maybe the non-continuous rendering is the right way. But if it is (for example) a platformer with some puzzle elements, the user will probably move the whole time and solve the puzzle "on the fly". In this case i would redner normally.
Also you have to think about the other logic, outside of the current view:
Again the example with the platformer: If you have to solve a puzzle to open a door or whatever, behind this door maybe some monsters are waiting for you to come. So if they patrol arround, you have to update their possitions, even if they are outside of the view (behind the door, outside of the building...). In this case again i would not use non-continuous rendering.
The timer you mentioned, will call render(), if it is fired. See this for reference.
To run something, without calling render() you need to run it on another Thread, so for this use java.util.Timer instead of libgdx Timer.
EDIT: As you have now replaced "puzzle game" with "word game" i think i know what you mean.
Normally you only need to render, when an input event is fired. But as much as i understood you want some objects to appear on screen (to make it more interesting than a motionless picture?). For this you need to use a Timer, i am not sure if libgdx Timer could do this, if not you can use java.util.Timer and when a Timerevent gets fired you just call requestRendering() and in your render update gamelogic and the redraw.

Related

Animation ist only smooth when ontouchevent is called

i'm making my first 2d sidescroller game using a surfaceview and a canvas to draw things on (a lot of primitives put in different path objects).
my game loop uses fixed timesteps with linear interpolation. i don't create any objects during the game. i've been improving my code for 3 weeks now, but my animation is still not all the time smooth. it's ok, but every few seconds there are a lot of little hicks for about 1 or 2 seconds.
what i recognized is when i move my player (this means touching the screen), the little hicks disappear for as long as i touch the screen and move my player.
this means as long as ontouchevent of the surfaceview is called, the animation is smooth.
i dont understand this and i want a smooth animation. can somebody help me?
This sounds like a known issue on certain devices. See e.g.:
Android SurfaceView slows if no touch events occur
Animation glitches while rendering on SurfaceView
Android thread performance/priority on Galaxy Note 2 when screen is touched/released
The problem is that the system is aggressively reducing clock speeds to save power when it doesn't detect interaction with the user. (Qualcomm in particular seems fond of this.) The workaround is to drop frames when necessary. See this article on game loops, and a Choreographer-based trick demonstrated in Grafika's "record GL app" activity (in doFrame()).

Can I Spawn objects in Corona SDK with different distances between them using a Horizontally Scrolling background?

I need to generate objects for my little character to jump over. I have the artwork for these obsticles and my character can jump and I have a scrolling background.
How can I spawn my artwork for my obsticles on my x axis with spacing inbetween them?
Can anyone provide me with some sample code or atleast try and point me in the right direction?
Many Thanks,
James
Yes. You can. You want to use some sort of loop that generates them:
2 options you can use:
local function frameHandler()
if should_I_make_object() then
createObstacle()
end
end
Runtime:addEventListener("enterFrame", frameHandler)
this approach will create new objects according to frame rate. IE, lets
you create objects every 100 frames lets say. This will make levels play the
same (have the same number of obstacles) on different devices that have varied frame-rate
Option 2:
local function createObstacle()
--your_create_obstacle_code()
if game_is_still_playing() then
timer.performWithDelay(object_spawn_delay, createObstacle)
end
end
This option will create a new object every object_spawn_delay milliseconds.
This is easy to code, and is a nice solution when you need things to happen on
a time-dependent interval. But you do need code to decide if the game is still playing.
Also, be aware that if the game ends, there might still be a lingering callback to
createObstacle() that can create nasty bugs. Make sure to do proper cleanup when
the level / game ends and be aware this callback may be an issue.

Android: for canvas animation, better have 1 big thread, or a number of small threads

i make an app which would fire different small animation, such a falling rotating star or a like a text floating from bottom to top, animated with canvas on a surfaceview
they are all constant once fired,
so i planned 2 ways for that, 1 is a big thread which
handles them all animations, updates each after each other animation 1 after 1
or having for each kind of animation such as a falling star, have it's own thread set up, and at each given point the main activity could have a command go on one of the threads, creating him, and starting, and than forgetting about him, letting him run in the background and at the end of the animation that thread will end it self.
so i don't have to worry about storing each fired threads and such, cause i don't know how many could start at a given time.
so will it be more memory efficient to fire random small short threads or have 1 big thread to handle it all? will it work more smoothly and better?
the animations are simply for visual enchantment and does nothing else, neither does it interact with other animations it simply starts, loops, ends.
i hope u understood :\
You probably want a SurfaceView with one thread handling all of your drawing. Having one thread per animation seems overly-complicated. You can certainly have one method per animation type, e.g. renderStars(), renderText(), etc. that are called whenever your one animation thread renders.
Recommended reading:
The SurfaceView documentation
The Canvas and Drawables API Guide - I've linked to the "On a SurfaceView" section, but you should probably read most of the document.
The Lunar Lander sample app - The online code is currently 404-ing after Google recently revamped their developer site, but this link shows you where to find the code in the SDK manager.
During animation you will have to update your UI on UI Thread you cannot use a different thread for running animations. However, for intensive animation you must use a Gaming Engine or write your own Game Thread, that will first update data of objects as per animation formulas and afterwards draw them on UI thread.

How to make a consistant game loop

So in my game my View gets drawn an inconsistent rates. Which in turn makes it glitchy. Ive been running into alot of problems with the invalidate(); meathod. Any simple ideas- Everywhere i look I get thrown up on by tons of intense code.
You haven't provided us with much information, specifically code.
A few things you could do are:
Set the initial frame rate to the lowest value you observe your application runs at, i.e., if currently set to 1/60, but the frame rate continuously dips to 1/30, set to 1/30 etc.
Rework your drawing calls to be more efficient.
Try to combine multiple transformations into a single transformation by multiplying matrices, i.e. if you need to scale, translate, and rotate, multiply those three matrices together and apply that single transformation to the vertices instead of applying three separate transformations.
Try not to iterate through entire lists/arrays if unnecessary.
Attempt to use the lowest level / most primitive structure possible for anything you have to process in the loop to avoid the overhead of unboxing.
[edit on 2012-08-27]
helpful link for fixing you timestep: http://gafferongames.com/game-physics/fix-your-timestep/
It sounds like your game loop doesn't take into account the actual time that has passed between iterations.
The problem is the assumption that there is a fixed amount of time between loop iterations. But this time can be variable depending on the number of objects in the scene, other processes on the computer, or even the computer itself.
This is a common, somewhat subtle, mistake in game programming, but it can easily be remedied. The trick is to store the time at the end of each draw loop and then take the difference of the last update with the current time at the start. Then you should scale all animations and game changes based on the actual elapsed time.
I've wrote more about this on my blog a while back here: http://laststop.spaceislimited.org/2008/05/17/programming-a-pong-clone-in-c-and-opengl-part-i/
Part II specifically covers this issue:
http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/

Timed Triggers and Animation

Can you please help me answer the following questions?
How do people usually implement a timed event in games?
Example: You reach some point in time and something happens like a new wave of enemies appears or something similar.
What is the best way to make sprite animations?
Example: Explosions in an android game.
1) Every time round your game loop you will want to record the elapsed time (or delta time.) This means you could keep track of the time past since the game start and the time it took to execute one iteration of your loop (the latter is good for time based movement.)
2) Depends really, I'm assuming this is 2D so I personally use sprite maps (google it) and then after x seconds I will change the image that is being rendered to the next on in the animation sequence. Animation sequences normally consist of "frames" and after x seconds have passed then move to the next frame
1 - Each frame/cycle of your game, record the time, then get the difference between the previous cycle's and the current cycle's time. This is how much time has elapsed since the last frame/cycle. Objects can then have timers that subtract the elapsed time each cycle, and do things when their timers are <= 0.
2 - My preference for game development is flixel, which does a lot of the dirty work for you. If you want to use pixel images instead of vector graphics, you should take a look at flixel. It has a framework that makes working with images and spritesheet animations a breeze. At the very least you can figure out how flixel handles them and write your own methods based on what you learned. If you want vector graphics, however, you'll need some other solution.

Categories

Resources