Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Calculating the FPS on a simple game  (Read 2429 times)
0 Members and 1 Guest are viewing this topic.
Offline rbervini

Junior Newbie




Java games rock!


« Posted 2005-02-21 04:42:41 »

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..
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #1 - Posted 2005-02-21 19:40:34 »

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

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Virum

Junior Duke




Like a leaf in an icy world, memories will fade


« Reply #2 - Posted 2005-02-26 07:17:13 »

Here's my simplified game loop:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
     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. 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. Sad

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).

It's time to prove to your friends that your worth a damn. Sometimes that means dying; sometimes that means killing a whole lotta people.

Blog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline role

Junior Duke




Java manmoth is coming!


« Reply #3 - Posted 2005-03-01 08:25:24 »

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 Grin
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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();
   }
}

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
Offline guseppe

Junior Newbie




Java games rock!


« Reply #4 - Posted 2005-04-12 10:57:09 »

i do my fps calculating this way

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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?
Offline guseppe

Junior Newbie




Java games rock!


« Reply #5 - Posted 2005-04-12 11:11:44 »

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
Offline gcsaba2

Junior Duke




Hello world


« Reply #6 - Posted 2005-04-24 08:21:47 »

role's FPS counter increases the game speed!!!!
Click to Play
 Grin
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #7 - Posted 2005-05-10 09:41:28 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
            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();

            }

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #8 - Posted 2005-05-10 09:43:25 »

And this is using GAGE timer:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
//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;
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!