Sunday, January 18, 2015

Anansi: Debug Information


One of the most important things during coding is to get some information during runtime.

We like to see the FPS (Frames Per Second) in order to see if our game is performing well.

We proceed like in the pervious lessons: Create a layer for that information and add a node to it.

 
Pane debugLayer;
...
Label debugLabel;
...
debugLayer = new Pane();
...
root.getChildren().add( debugLayer);
...
// debug information
debugLabel = new Label();
debugLabel.setTextFill(Color.RED);
debugLayer.getChildren().add( debugLabel);
...
In the AnimationTimer we calculate the FPS and write that information into the debugLabel. We create some global variables
 
// counter for game loop
int frameCount = 0;
int fpsCurrent = 0;
long prevTime = -1;
and add this code in the AnimationTimer's handle method:
 
// calculate fps
// ---------------------------
frameCount++;

long currTime = System.currentTimeMillis();

if( currTime - prevTime >= 1000) {
 
 // get current fps
 fpsCurrent = frameCount;

 // reset counter every second
 prevTime = currTime;
 frameCount = 0;
}

// show debug info
// ---------------------------
debugLabel.setText("FPS: " + fpsCurrent);

In our game we can see on the top left that it runs indeed with 60 FPS.

No comments:

Post a Comment