Calculating the FPS on a simple game

I have a problem calculating the fps of my game.

I have a checktime method checking how many times the update() method is called within a second.
Is this the right way to have the fps?

This checktime method is just after the update() method and has an incrementer and when 1001ms are passed the method will return the number of fps.

If my isometric engine is drawing only the grid of the game (made with polygons) the fps are over 100 but I fill my map with grass tiles the fps slow down to 17-18.
Is that possible?

Please note that the grasstiles are loaded only one time as an ImageIcon and the preloaded with a MediaTracker.

I guessed that I had an error writing the fps code a because isn’t possible that a small filled grid can go down to 17fps on a P4 2.8Ghz 512mb…

I would think going from polygon filling to image drawing would be faster :stuck_out_tongue: Try posting some code

Here’s my simplified game loop:


     public void gameLoop()
     {
          nextCheck = System.nanoTime() + 1000000000;
          frameCounter = 0;

          while(gameRunning)
          {
               calculateFPS();
               game.tick();
               game.getFrame().render();                  

               while(System.nanoTime() < beforeRender + (1000000000 / game.FPS))
               {
                    Thread.yield();
               }
          }
     }

     private void calculateFPS()
     {
          beforeRender = System.nanoTime();

          if(beforeRender >= nextCheck)
          {
               nextCheck = beforeRender + 1000000000;
               fps = frameCounter;
               frameCounter = 0;
          }
        
          frameCounter++;
     }

game.tick() , this is just how I’m delegating things. :stuck_out_tongue:

game.getFrame().render(); = me showing the bufferstrategy and disposing the graphics object.

Yes, I know I’m a freak for putting the calculate FPS code in it’s own method. :frowning:

Edit: eh, no idea why the indentation doesn’t work right. Sorry.

Edit 2: This code locks the fps at 60 fps (the value of my constant).

And this is my fps counter that working with pre Java 1.5.
Now who’s more freak?! I make it in its own class ;D


public class FPSCounter {

    private long astCount;   // last time the fps is counted
    private int  currentFPS, // the real fps achieved
                 frameCount;

    public FPSCounter() { }

    public void refresh() {
        frameCount = 0;
        lastCount = System.currentTimeMillis();
    }

    public void calculateFPS() {
        frameCount++;
        if (System.currentTimeMillis()-lastCount > 1000) {
            lastCount = System.currentTimeMillis();
            currentFPS = frameCount;
            frameCount = 0;
        }
    }

    public int getCurrentFPS() {
        return currentFPS;
    }

}

Instantiate it and call calculateFPS() in your game loop.
To get the fps, call getCurrentFPS().
Some code of how to use it :


public YourGame {

   FPSCounter fpsCounter = new FPSCounter();

   public void gameLoop() {
      //........ your game update, render
      // getting the game fps
      int fps = fpsCounter.getCurrentFPS();


      // after Thread.sleep(), Thread.yield()
      fpsCounter.calculateFPS();
   }
}

i do my fps calculating this way


public class FPSCounter implements Runnable{
      protected int fps = 0, fpscounter = 0;
      Thread proces = new Thread(this);
      public FPSCounter(){
            proces.start();
      }
      public void run(){
            while(true){
                  try{Thread.sleep(1000);}catch(InterruptedException ie){}
                  fps = fpscounter;
                  fpscounter = 0;
            }
      }
      
      public void addFPS(){
            fpscounter++;
      }
      public int getFPS(){
            return fps;
      }
}

and then i implement it this way


public class game{
    FPSCounter fps
    int intfps= 0; //initially start with 0

    game(){
        fps = new FPScounter;
    }

    renderloop(){
        while(true){
             intfps = fps.getFPS();
             ---->startrendering
              renderstring(intfps" fps");//draw a string in the topleft corner
             ----> end of rendering
             fps.addFPS()
        }
    }



}

i have read once that the sleep waits at least the given time but sometimes it waits a little longer is this true?

what would you think of this or is it too inefficent or inaccurate?

by the way i tried both in my game and the results were like this
threaded fpscounter result : 120-140
role fpscounter result : 130 - 150

role’s FPS counter increases the game speed!!!
http://fenomas.com/tomatobb/images/smiles/nnf_yikes.gif
;D

Here is the way how I lock my game at 60FPS


            int FPS=60;
            int MILI_FRAME_LENGTH = 1000/FPS;
            long startTime = System.currentTimeMillis();
            int frameCount = 0;

            long lastLoopTime = System.currentTimeMillis(); 
            long lastFpsTime=0;
            long fps=0;

            while (true) {

                  long delta = System.currentTimeMillis() - lastLoopTime;
                  lastLoopTime = System.currentTimeMillis();
                   lastFpsTime += delta;
                  fps++;
 
                   if (lastFpsTime >= 1000) {
                        this.setTitle(" (FPS: " + fps + ")");
                        lastFpsTime = 0;
                        fps = 0;
                  } 

                     logic(delta);
                        draw((Graphics2D) strategy.getDrawGraphics());
                        strategy.show();

                  frameCount++;
                  while((System.currentTimeMillis()-startTime)/MILI_FRAME_LENGTH <frameCount) {
                        Thread.yield();
                  } 

                  //Toolkit.getDefaultToolkit().sync(); 

            }

And this is using GAGE timer:


//these variables will be used to help keep the framerate constant
AdvancedTimer at = new AdvancedTimer();
long tick = 0;
at.start();
long ticksPerFrame = AdvancedTimer.getTicksPerSecond()/60;

//System.out.println(AdvancedTimer.getTicksPerSecond() + " " + AdvancedTimer.getResolution());

//these variables will be used to keep an average value of the frames
//per second of this game
long lastTime = System.currentTimeMillis();
int paints = 0;

        while(true) { 
            /* fps calculation */
            if(System.currentTimeMillis()-lastTime > 1000) {
                lastTime += 1000;
              this.setTitle(" (FPS: " + paints + ")");
                paints = 0;
             }
                 /* end fps calculation */                     

            logic();

                        draw((Graphics2D) strategy.getDrawGraphics());
                  paints++;
                        strategy.show();

//Wait for a little while so that we can ensure we've occupied
     //the right amount of time for this frame
     at.sleepUntil(tick + ticksPerFrame);

     //Doesn't account for drag
     //tick = at.getClockTicks();

     //Does account for drag
     tick += ticksPerFrame;
}