Reputation: 41
So, I'm making a game, and I want an FPS Counter, however I've got no clue what the math I should do behind it is. My previous counter worked, but it only updated every second due to the math. I want something constantly updating, but I just don't know how to make it. As far as I can tell, the easiest way to do this is to have a frame counter, and every frame it looks at the frames it's done so far and the time elapsed/time left and models the current frames for the rest of the second and displays that. That seems like a good theory, but I don't really know how to put it into practice. I know I'll need to use System.currentTimeMillis
and such. Any help would be greatly appreciated! Thanks!
EDIT 1
Here's my FPS code here:
double getFPS(double oldTime) {
double newTime = System.nanoTime();
double delta = newTime - oldTime;
double FPS = 1 / (delta * 1000);
FPSOldTime = newTime;
return FPS;
}
Upvotes: 1
Views: 6900
Reputation: 1650
I implemented something like that in C++. I imagine it should work in Java also. The first line in the function has to use the Java time method tough.
double get_fps(double old_time) {
// Something like that in java double new_time = System.getTime().nanoseconds();
double delta = new_time - old_time;
double fps = 1 / (delta * 1000);
old_time = new_time;
return fps;
};
Format the double with something like this: %2.0f
PS. (Run it in your game loop and the old time has to be initialized before game loop. You will be changing it via a reference.)
Upvotes: 1