Friday 6 April 2012

Loop Speed / FPS

The panel movement works exactly as it is supposed to, but it takes a while to do it.  At the moment, the panel moves at a rate of 1 pixel for each loop of the program.

On my PC it takes about 4 seconds to complete the movement, ideally it should take about one second.

Increasing the value added to the Y position from 1 to 4 would make it run at the right speed on my PC, but what happens when we run the same project on a slower phone, or a faster PC?

In order to get the movement to look the same on every device, we need to know how many loops of the program are run in a particular period of time and adjust the speed accordingly.

A phone performing half as many loops, needs to move the objects twice as far per loop.

For example, if four pixels looks right on a phone running at 60 loops per second, then we would need to use a value of eight pixels on a phone running 30 loops per second.

AGK can perform many thousands of loops per second, the thing that is limiting the number of loops in our project is the Sync() command, which is pauses the program to redraw the display.

We can see how fast it is doing this with the screenFPS() command which returns an approximation of the current number of frames per second.

By getting this value at the start of each loop and storing it in a variable;

lastFPS# = screenFPS()

We can print this to see how fast the program is running at any time.

print( "FPS: " + str ( lastFPS# ) )

And calculate how fast the panel needs to move based on its size and how long we want it to take.

panelSpeed# = panelHeight# / lastFPS#

The line above will calculate the speed needed to move the panel its full distance in 1 second.

This is then used in place of the 1 value, in the lines which move the panel objects.

setSpriteY( panelSprite , getSpriteY( panelSprite ) - panelSpeed# )
setTextY( panelText , getTextY( panelText ) - panelSpeed# )
and

setSpriteY( panelSprite , getSpriteY( panelSprite ) + panelSpeed# )
setTextY( panelText , getTextY( panelText ) + panelSpeed# )

The first part of the loop now looks like this;
The panel will now take approximately the same amount of time to open and close an any device, irrespective of the frame rate of that device.

No comments:

Post a Comment