Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
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  
  Problem with FPS Counter  (Read 1208 times)
0 Members and 1 Guest are viewing this topic.
Offline KONI

Junior Member




Java games rock!


« Posted 2004-12-03 08:21:16 »

Hi,

In my main game loop, I am using this simple method to count the number of frames:

                 long startTime = System.currentTimeMillis();
                 fireShots();
                 updatePosition();
                 manageFighter();
                 checkForGameEnd();
                 manageEnemies();
                 render();
                 usedTime = System.currentTimeMillis() - startTime;

where the functions are pretty self-explaining for a 2d shooter game. They contain all the world logic and render() just calls the repaint() method from my graphical interface.

The problem is that I keep getting extremely low usedTime's, +/- 1 msec. The problem is that it seems that a call to repaint() is pretty fast and that it doesn't take into account the actual time to do the paint().

So would you suggest to set usedTime() at the end of the paint() or is there a better way ?

Update:

I tried to move the usedTime calculation at the end of the paint() function and am getting better results. My framerate is now around 40 fps which seems ok. But I keep getting an odd problem:
I am using a thread with a timeout of 40 to run the main game loop and as described in the Java2D forum, I am trying to blur the foreground BufferedImage on the fly to make it look better.
This gives me a very rough animation and the smoothness of the animation (which I get without using the BLUR) is gone.
But my framecounter keeps telling me that I have 50 FPS which is impossible when I look at the actual result ?!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2004-12-03 17:19:54 »

System.currentTimeMillis() is a rather bad timer. It's accuracy equals the Operating Sytem's tick rate. 50-55msec on win9x, 10msec on 2k/xp/mac and 1msec (or so) on linux.

If you're using 1.5 you can use System.nanoTime() instead - it's a hi res timer.

弾幕 ☆ @mahonnaiseblog
Offline Grazer

Junior Member




My other avatar is much more flattering.


« Reply #2 - Posted 2004-12-05 19:52:03 »

KONI,

As far as I understand it, using repaint() is a really bad way to go for games. As you've probably realised by now, it simply schedules the targetted component for repainting the next time the Swing thread is free to do so, rather than actually repainting the GUI then and there.

The recommended way to do games is to turn off auto-repainting and force your own painting inside the render() method that you already have.

Chet's gaming articles should help:

http://www.sys-con.com/story/?storyid=46663&DE=1

http://www.sys-con.com/story/?storyid=46983&DE=1

Current Project: Easy Decal
Other Stuff: grlea online
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #3 - Posted 2004-12-07 21:38:49 »

Add this method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public void paintFps(Graphics2D g) {
          g.setFont( new Font("Arial",Font.BOLD,12));
          g.setColor(Color.white);
          if (usedTime > 0){
              g.drawString(String.valueOf(1000000000/usedTime)+" fps",screenWidth-50,screenHeight-10);
          }else{
              g.drawString("--- fps",screenWidth-50,screenHeight-10);
          }
    }


Then do what oNyx suggested.
long startTime = System.nanoTime();

usedTime = System.nanoTime()-startTime;

usedTime is a private global.

Call paintFps in your render loop.


Then follow Grazer's instructions, the componets should have all the setIgnoreRepaint(true);

And you should use a custom render loop.
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.

Riven (9 views)
2014-07-14 11:51:35

quew8 (15 views)
2014-07-13 13:57:52

SHC (42 views)
2014-07-12 17:50:04

Riven (34 views)
2014-07-10 20:20:18

CopyableCougar4 (34 views)
2014-07-10 02:26:14

CopyableCougar4 (36 views)
2014-07-09 02:55:38

Code Mage (35 views)
2014-07-08 23:57:00

Code Mage (26 views)
2014-07-08 23:49:08

AppleSauce (29 views)
2014-07-08 19:25:32

CopyableCougar4 (31 views)
2014-07-06 01:51:26
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!