Results 1 to 2 of 2
Thread: Frame rate
- 11-18-2011, 12:48 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 28
- Rep Power
- 0
- 11-18-2011, 11:02 PM #2
Member
- Join Date
- Feb 2011
- Location
- 'Mhericuh
- Posts
- 13
- Rep Power
- 0
Re: Frame rate
It all depends on how you have set up your game. Posting some code would be very helpful.
A typical way to do it would be to keep a list of when you've rendered the game (System.nanoTime() is the easiest and most accurate way) and use that to find framerate. Here's some example code of what it might look like:
Depending on how you've organized your game you will have to adapt this code. With the setup that I would use, this would go in a main class with the run() loop and update() and draw() methods, but you'll have to adapt it yourself. I also didn't make anything efficient, you can definitely save some space and computation time by combining some lines in getFramerate() but this demostrates what's going on better.Java Code:import java.util.ArrayList; import java.util.List; public class FrameRate { ArrayList<Long> drawTimes = new ArrayList<Long>(); int recentNum = 10; public void run() { while(true) { long beforeUpdate = System.nanoTime(); update(); draw(); long afterUpdate = System.nanoTime(); drawTimes.add(afterUpdate - beforeUpdate); } } private long getFramerate() { List<Long> recentDrawTimes = drawTimes.subList(drawTimes.size() - recentNum, drawTimes.size()); int averageFramerate = 0; for (int i = 0; i < recentNum; i++) { averageFramerate += recentDrawTimes.get(drawTimes.size() - recentNum + i); } averageFramerate /= recentNum; averageFramerate /= 1000000000; // convert from nanoseconds to seconds return averageFramerate; } public void update() { // game update code here } public void draw() { // drawing code here System.out.println("The framerate over the last " + recentNum + " frames has been " + getFramerate() + " fps."); } }
I made the framerate account for more than the last update/draw cycle, otherwise it would be inconsistent and less useful. This way, it should be more consistent. The way I made it, it prints the framerate over the last however many updates to the console; you'll have to change that. I wouldn't recommend making it update the framerate and redraw it every time it draws though- you won't be able to make out any of the numbers. Just add a variable for the framerate, a little if statement that updates said framerate every certain amout of time or update loops, and then use the framerate variable instead of method when you're drawing. Or do whatever you want.
Hope the code helps some, if you can give more specific examples of how you've set up the game there might be an easier way.
Similar Threads
-
Java slave Frame access to its owner main frame problem
By cagdaseckin in forum New To JavaReplies: 0Last Post: 12-10-2010, 10:40 AM -
How can I measure the data rate of my connection
By islamfunny in forum CLDC and MIDPReplies: 0Last Post: 09-25-2008, 03:27 PM -
calculating Bank interest rate.
By dotnet007 in forum New To JavaReplies: 10Last Post: 05-13-2008, 09:30 AM -
Telecommute only at half rate
By Johnny Kewl in forum Jobs WantedReplies: 0Last Post: 05-11-2008, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks