ajoz
ajoz

Reputation: 116

Frame per second counting in Android game

I'm looking for a method of counting Frames Per Second in game applications, the problem is that i dont have the source code for any of those apps, they dont have such functionality implemented inside. To be honest i dont really expect this to be possible but it was worth to ask.

And to be perfectly clear i understand that i need a basic knowledge and code of the main painting loop to count FPS.

Best Regards

Upvotes: 1

Views: 6391

Answers (3)

bezmax
bezmax

Reputation: 26122

What is the problem of making FPS counting when you have a main paint loop (don't you?).

I'm not familiar with Android features, but I'm sure it is done there the same in any game on any OS. The easiest way to make a game - is to make 2 threads. First one will be for logic actions (calculating trajectories, object position change over time, etc.). Second thread will take current game state (calculated by first thread) and draw it on the screen.

Here is the commented code on both threads with FPS counter:

//Class that contain current game state
class GameState {
    private Point objectPosition;

    public Point getObjectPosition() {
        return objectPosition;
    }

    public void setObjectPosition(Point pos) {
        this.objectPosition = pos;
    }
}

//Runnable for thread that will calculate changes in game state
class GameLogics implements Runnable {
    private GameState gameState; //State of the game

    public GameLogics(GameState gameState) {
        this.gameState = gameState;
    }

    public void run() {
        //Main thread loop
        while (true) { //Some exit check code should be here
            synchronize (gameState) {
                gameState.setObjectPosition(gameState.getObjectPosition()+1); //Move object by 1 unit
            }
            Thread.sleep(1000); //Wait 1 second until next movement
        }
    }
}

//Runnable for thread that will draw the game state on the screen
class GamePainter implements Runnable {
    private Canvas canvas; //Some kind of canvas to draw object on
    private GameState gameState; //State of the game

    public GamePainter(Canvas canvas, GameState gameState) {
        this.canvas = canvas;
        this.gameState = gameState;
    }

    public void drawStateOnCanvas() {
        //Some code that is rendering the gameState on canvas
        canvas.drawLine(...);
        canvas.drawOtherLine(...);
        ....
    }

    public void run() {
        //Last time we updated our FPS
        long lastFpsTime = 0;
        //How many frames were in last FPS update
        int frameCounter = 0;

        //Main thread loop
                    //Main thread loop
        while (true) { //Some exit check code should be here
            synchronize (gameState) {
                //Draw the state on the canvas
                drawStateOnCanvas();
            }

            //Increment frame counter
            frameCounter++;

            int delay = (int)(System.getCurrentTimeMillis() - lastFpsTime);
            //If last FPS was calculated more than 1 second ago
            if (delay > 1000) {
                //Calculate FPS
                double FPS = (((double)frameCounter)/delay)*1000; //delay is in milliseconds, that's why *1000
                frameCounter = 0; //Reset frame counter
                lastFpsTime = System.getCurrentTimeMillis(); //Reset fps timer

                log.debug("FPS: "+FPS);
            }
        }
    }
}

Upvotes: 2

SteD
SteD

Reputation: 14027

This is a good read on game loop. It explains the various implementations and pros/cons of game loops.

And this is a good tutorial on implementing the gameloop + FPS counter in Android.

Upvotes: 0

s5d
s5d

Reputation: 43

I'm not too knowledgeable with Android Game development, but in Java2D (it's a bit hacky but it works...) I would xboot a jar with the edited JRE class which controls the rendering (eg Canvas) into the process.

Upvotes: 0

Related Questions