Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  [SOLVED] FPS too low or just normal?  (Read 2597 times)
0 Members and 1 Guest are viewing this topic.
Offline co_Opernicus

Senior Newbie





« Posted 2012-04-18 18:06:05 »

Hi all!

  I've added some debug messages on my "little creature" to test FPS. I don't know if this is normal, but when I comment the "thread.sleep" part in my main loop I get about 8200 fps which I guess is not a bad signal... But, when I toggle Thread.sleep (10ms per iteration), it goes down to an average of 105-110 fps.

This is the code of the game main loop (thanks ra4king for your good advices!!):

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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
public abstract class Game {

   ...blah blah blah...

   public final void run() {
     
      //debug:
     int currFPS = 0;
      int avgFPS = 0;
      double currTime = HiresTimer.getTime();
      graphics.setDrawColor(Color.white);  
           
      while ( gameRunning ) {
         
         deltaMsecs = HiresTimer.getTime() - lastLoopMsecs; //don't mind this.. it's used elsewhere
        lastLoopMsecs = HiresTimer.getTime();

         update();
         
         do {
           
            do {
               
               Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
               render();
               
               //debug: show fps
              graphics.drawText("FPS: " + avgFPS, 10, 10);
               
               g.dispose();
               
            } while ( strategy.contentsRestored() );

            strategy.show();
           
            try {
               //Take exactly (hopefully) 10ms per loop
              Thread.sleep( (long) lastLoopMsecs + 10 - (long) HiresTimer.getTime() );
            } catch (Exception e) {}
           
           
         } while ( strategy.contentsLost() );
         
         //debug:
        currFPS++;
         if ( HiresTimer.getTime() - currTime >= 1000 ) {
            avgFPS = currFPS;
            currFPS = 0;
            currTime = HiresTimer.getTime();
         }
      }      
   }


Right now I'm only drawing an animated sprite that moves and jumps depending on keyboard input and showing the fps message...

To me it looks that it should be more than just 110 FPS, but maybe that's normal... or maybe it's just a wrong FPS calculation method???

Thanks in advance for your comments!
Offline Regenuluz
« Reply #1 - Posted 2012-04-18 18:09:09 »

Well there's only so many 10ms pr second, so if you think about it then it makes perfect sense. You're doing nothing for ~10ms every loop. ^^
Offline co_Opernicus

Senior Newbie





« Reply #2 - Posted 2012-04-18 18:14:44 »

Well, yes, but I was wondering if maybe as I get more and more logic into the game will that mean that those 110 FPS will remain?

In the sleep I'm calculating the remaining msecs to get to those 10ms per loop, so with more update/drawing operations it'll just sleep for the rest of the msecs needed...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #3 - Posted 2012-04-18 20:39:03 »

I think 110 is a lot. I used to make games with game maker, and I put the default at 30 FPS. Only when I began development in java did I learn that 60 FPS is default now. Your'e probably fine with what you have right now. (And, if for some reason you need to know, I didn't look at the code at all. Although, if you don't sleep, you'll overheat your CPU I think)
Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-04-18 20:41:13 »

In the sleep I'm calculating the remaining msecs to get to those 10ms per loop, so with more update/drawing operations it'll just sleep for the rest of the msecs needed...

That's basically how you reach a target FPS, yes.  As you do more work, your sleeps will get shorter.  There's a long thread here about Sync.sync() in LWJGL, which you should be able to copy and use in your code (it's not specific to opengl).  

You should consider a target framerate no higher than 60 or so.  Maybe 120, but only if your game is ridiculously fast-moving and you want to cater to the bleeding edge.  People on laptops and phones will be happier with lower framerates if it preserves their battery.

If there's a sticky FAQ around here somewhere, "How do I keep a game loop at a target framerate?" should definitely be part of it.


Offline Rorkien
« Reply #5 - Posted 2012-04-18 22:18:50 »

Nah, its pretty normal
But if your loop sleeps for 10ms after every iteration, and you are getting 105-110 frames per second, it's just a signal that something is innacurate: The loop itself or maybe the FPS counter is taking more than a second to count frames  Tongue
Offline Danny02
« Reply #6 - Posted 2012-04-19 08:05:32 »

why does nobody here enable/use vsync?
Offline gimbal

JGO Knight


Medals: 25



« Reply #7 - Posted 2012-04-19 09:13:40 »

why does nobody here enable/use vsync?


Who says they aren't? but vsync is to reduce tearing, its not to reduce framerate even though that is a byproduct. Its unreliable at best too; it can be disabled through the driver and there is no standard vsync rate; it is totally dependent on the refresh rate of the display device.

On top of that, even if you use vsync to lock a framerate, you may still want to have a different "update rate". I like to sample input device state more often than I update world state / rendering frame for example.

(I use 50fps btw; it was good enough for Amiga games, its good enough for me Smiley )
Offline co_Opernicus

Senior Newbie





« Reply #8 - Posted 2012-04-19 18:29:51 »

On top of that, even if you use vsync to lock a framerate, you may still want to have a different "update rate". I like to sample input device state more often than I update world state / rendering frame for example.

  That sounds interesting... what do you do that for? So you get more accurate response to your input? I've always been concerned about creating a "smooth flow" between your brain ordering your fingers to press a key and getting an immediate reaction in the game, I think it's one of the keys of making a good game... at least that's what I want games to feel like Smiley
Offline gimbal

JGO Knight


Medals: 25



« Reply #9 - Posted 2012-04-19 22:46:17 »

On top of that, even if you use vsync to lock a framerate, you may still want to have a different "update rate". I like to sample input device state more often than I update world state / rendering frame for example.

  That sounds interesting... what do you do that for? So you get more accurate response to your input?

Exactly that. Its a bit pricky to be honest as it basically means having the chance to have updated input state "on time" for the current frame more often, but on the other hand precision is something you don't want to take away from a gamer Wink
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.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!