Results 1 to 2 of 2

Thread: Frame rate

  1. #1
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Frame rate

    Hey so I'm making a pong game. It is currently playable, but I'd like to have a way to have my framerate displayed. Is there a built in method for this somewhere?

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default 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:

    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.");
    	}
    }
    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.

    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

  1. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 11:40 AM
  2. How can I measure the data rate of my connection
    By islamfunny in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-25-2008, 04:27 PM
  3. calculating Bank interest rate.
    By dotnet007 in forum New To Java
    Replies: 10
    Last Post: 05-13-2008, 10:30 AM
  4. Telecommute only at half rate
    By Johnny Kewl in forum Jobs Wanted
    Replies: 0
    Last Post: 05-11-2008, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •