How do you display fps with opengl es (android)
Asked Answered
S

3

5

I am new to java and opengl and still learning so forgive me for my ignorance. I have made an looming object apk file. Now i want to view the fps as the application is playing on my device. I have been following this tutorial http://www.youtube.com/watch?v=1MYgP8Oj5RY&list=UUKwypsRGgHj1mrlQXhLXsvg&index=67 and attempted to modify it for android but i can't get it to work.

Could anyone show me an example of how to add a simple fps indicator on the corner or on the title page and how to control the fps.

Thanks

Stillhunt answered 13/3, 2013 at 23:11 Comment(0)
K
9

if you use this class and call logFrame() it will tell you the fps in a log on your logcat:

import android.util.Log;

public class FPSCounter {
    long startTime = System.nanoTime();
    int frames = 0;

    public void logFrame() {
        frames++;
        if(System.nanoTime() - startTime >= 1000000000) {
            Log.d("FPSCounter", "fps: " + frames);
            frames = 0;
            startTime = System.nanoTime();
        }
    }
}

from what ive read you optimise your code to get a better framerate by minimising the calls to the gl states

here is a link for drawText()

Khotan answered 13/3, 2013 at 23:16 Comment(1)
is there a way to combine this with drawText() so it is displayed on the glsurface?Stillhunt
B
2

I know it's late, but this is Google's first suggestion. So I just want to make it better:

Why count the frames that one second passes? I need in my application a live value of the FPS (as you should use it too). And so far I know it says

Frames/Second

That means that you can measure the time between one frame and the next and say:

Second/Frame

To get to FPS it's simply

1/(Second/Frame).

Here is the code. I used the code from above and edited it to get to my result:

public class FPSCounter {
    private long lastFrame = System.nanoTime();
    public float FPS = 0;

    public void logFrame() {
        long time = (System.nanoTime() - lastFrame);
        FPS = 1/(time/1000000000.0f);
        lastFrame = System.nanoTime();
    }
}
Beckybecloud answered 25/9, 2015 at 23:22 Comment(0)
S
0

MainActivity

import java.util.Timer;
import java.util.TimerTask;
....
....
public static int fps = 0;

protected void onCreate(...) {
        ....
   Timer timer = new Timer();
   timer.schedule(new TimerTask() {
      public void run() {
         fps();
      }
   },0,1000);
}

public void fps() {
   runOnUiThread(new Runnable() {
      public void run() {
         text.setText("FPS: " + fps);
         fps = 0;
      }
   });
}

Renderer method onDrawFrame()

MainActivity.fps++;
Scarron answered 1/1, 2023 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.