The benchmarking commands within AppGameKit Studio can be used to determine information about what's happening at runtime, such as the number of sprites being drawn or the amount of time that the physics simulation is using. This information is particularly useful if you are attempting to improve the performance of a game that may be running slowly on certain platforms, for example, these commands could be used to discover that the physics simulation is taking up a great deal of time, therefore you can act on this by doing something like reducing the number of active physics entities.
The benchmarking commands
Here's a quick overview of the benchmarking commands. All of these commands return values and have no parameters:
GetDrawingSetupTime - Find out how long was taken getting the scene ready for drawing, includes calculating what is visible and transforming sprites.
GetDrawingTime - Returns the time spent drawing the scene.
GetManagedSpriteCount - Returns the number of sprites that are being managed by AppGameKit Studio.
GetManagedSpriteDrawnCount - Tells you how many sprites are being drawn. The higher this is the more of an impact it will have on performance.
GetManagedSpriteSortedCount - Determines how many sprites are being sorted. Sprites are sorted when their depth or texture changes.
GetParticleDrawnPointCount - Returns the number of individual particles that were drawn using point sprites.
GetParticleDrawnQuadCount - Returns the number of individual particles that were drawn using quads.
GetPhysicsTime - Returns the time spent updating the physics simulation.
GetUpdateTime - The time spent updating everything except physics e.g. sprite animation.
Getting started
This example examines the usage of several commands within the benchmarking section. It will create 500 sprites and scroll them across the screen and have a particle explosion being fired off on a continual basis. Within the main loop statistics about the drawing time etc. will get displayed on screen.
The code prior to the main loop takes the following steps:
Sets a virtual resolution of 320 x 480.
Creates a background sprite.
Loads an image for the particles.
Loads an image for the sprites.
Creates a particle entity and places it off screen.
Has a for loop that will create 500 sprites, set their position off screen, apply a random color and set two values in an array that are later used for movement.
Here's the code:
SetVirtualResolution ( 320, 480 )
CreateSprite ( LoadImage ( "background1.jpg" ) )
LoadImage ( 1, "shrapnel3.png" )
LoadImage ( 2, "ball1.png" )
CreateParticles ( 1, -100, -100 )
for i = 1 to 500
CreateSprite ( i, 2 )
SetSpritePosition ( i, Random ( 320, 2000 ), Random ( 60, 480 ) )
SetSpriteColor ( i, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), Random ( 50, 255 ) )
speed# [ i ] = Random ( 10, 40 ) / 10.0
move# [ i ] = 0.0
next i
fire = 1
The first block of code within the main loop displays the drawing set up time, the amount of time it took to draw the scene, the number of sprites being drawn, the number of particles being displayed and the current frame rate:
The next section deals with the 500 sprites. They are all moved from left to right and a wavy effect is applied to their Y position, rotation is also applied. The final part checks whether the sprite has gone off screen and then repositions it over to the right.
for i = 1 to 500
x# = GetSpriteX ( i )
y# = GetSpriteY ( i )
y# = y# + cos ( move# [ i ] )
move# [ i ] = move# [ i ] + speed# [ i ]
SetSpritePosition ( i, x# - speed# [ i ], y# )
SetSpriteAngle ( i, GetSpriteAngle ( i ) + speed# [ i ] )
if x# < -100
SetSpritePosition ( i, Random ( 320, 2000 ), Random ( 60, 480 ) )
endif
next i
for ( i = 0; i < 500; i++ )
{
x = agk::GetSpriteX ( i );
y = agk::GetSpriteY ( i );
y = y + agk::cos ( move [ i ] );
move [ i ] = move [ i ] + speed [ i ];
agk::SetSpritePosition ( i, x - speed [ i ], y );
agk::SetSpriteAngle ( i, agk::GetSpriteAngle ( i ) + speed [ i ] );
if ( x < -100 )
{
agk::SetSpritePosition ( i, agk::Random ( 320, 2000 ), agk::Random ( 60, 480 ) );
}
}
The final block of code within the main loop is responsible for the particles. It begins by creating an explosion and when this explosions ends the particles are reset and restarted.
While only a small section within AppGameKit Studio, the benchmarking commands are incredibly helpful in attempting to understand performance issues within games and can play an important part in developing a game.