Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  FPS slower at the start?  (Read 1258 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Posted 2011-10-12 01:36:24 »

Hello  Smiley. I have a quick question about frames per second.

I am working on a game and am trying to implement a constant frames per second.  Everything is fine if I set it to about 50 fps, but anything faster and it will have a lower fps for the first 10 seconds or so, then it jumps up to what I want.  Now, 50 fps is fine for this game, but I have a pretty fast computer and don't know what is going to happen on a slower machine.  Any reason this could happen?

Here is my code if it helps, but I think this is pretty standard.

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  
      long lastTime = System.nanoTime();
      long thisTime;
     
      long sleepTime = 1000/TARGET_FPS;

      while (going ) {

         thisTime = System.nanoTime();
         
         updateStuff();
         render();
         
         long passedTime = thisTime - lastTime;
         
         fps = (int) (1.0/((1.0)*passedTime)*1000000000L);
         
         sleepTime = 1000/TARGET_FPS;
         
         if(sleepTime > 0) {
            try {
               Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
               e.printStackTrace();
               System.exit(-1);
            }
         }
         secs += 1.0/fps;

         
         lastTime = thisTime;
      }

Hello!
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #1 - Posted 2011-10-12 01:57:41 »

I should also say that as of right now there is no file reading of images or anything like that.  All the graphics are just colored squares at this point.

Hello!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-10-12 02:21:06 »

Thread.sleep(long) isn't reliable in Windows since the default timer resolution is unacceptably low.
Use this trick to force the JVM to the high resolution timer:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
if(System.getProperty("os.name").startsWith("Win")) {
    new Thread() {
        {
            setDaemon(true);
            start();
        }
       
        public void run() {
            while(true) {
                try {
                    Thread.sleep(Long.MAX_VALUE);
                }
                catch(Exception exc) {}
            }
        }
    }
}

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

Junior Member


Medals: 3
Exp: 7 years



« Reply #3 - Posted 2011-10-12 02:41:36 »

Thanks for your info.  I added in your trick but still seem to be having the same problem, although not as bad now.  I tired commenting out my rendering and now I get the fps I want at the start, but of course have no picture Sad  Is there some optimization going on in rendering that I don't know about?  Even if all I do is
1  
g.fillRect(0,0,width, height);

the problem happens.

Hello!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #4 - Posted 2011-10-12 02:42:30 »

That doesn't seem like correct behavior. Maybe post your render() function?

See my work:
OTC Software
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #5 - Posted 2011-10-12 02:47:19 »

"entities" is just an array list of everything that moves around, so far just squares and a few circles.

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  
   
private BufferStrategy bs;

private void render() {

      if (bs == null) {
         try {
            this.createBufferStrategy(2);
            bs = this.getBufferStrategy();
         } catch (IllegalStateException e) {
            e.printStackTrace();
            System.exit(-1);
         }
      }

      Graphics g = null;

      try {
         g = bs.getDrawGraphics();
         drawGraphics(g);
      } finally {
         g.dispose();
      }

      bs.show();
   }

   private void drawGraphics(Graphics g) {

      drawBackground(g);
     
      level.drawMe(g);
     
      for(int i = 0; i < entities.size(); i++)
         entities.get(i).drawMe(g);
     
      g.setColor(Color.BLACK);
      g.drawString("Lives Remaining: "+player.getLives(), 600, 20);
      g.drawString("FPS: "+fps, 600, 40);
      if(player.getLives() <= 0)
         g.drawString("GAME OVER", 600, 60);
     
      g.drawString("Time Passed: "+(int)count, 600, 80);
   }

   private void drawBackground(Graphics g) {
      g.setColor(Color.LIGHT_GRAY);
      g.fillRect(0, 0, TDFrame.WIDTH, TDFrame.HEIGHT);
   }

Hello!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-10-12 02:53:38 »

Please follow BufferStrategy's JavaDocs on how to properly layout your drawing code.

Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #7 - Posted 2011-10-12 03:13:14 »

Okay.  I changed my render() method to function similarly to the one on JavaDocs and am now setting up the BufferStrategy outside of render(), but I am still seeing the same issue.  Huh Sorry if I am being a hassle Sad.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   private void render() {

      if (bs == null)
         return;
     
      do {
            do {
               Graphics g = null;
         
               try {
                  g = bs.getDrawGraphics();
                  drawGraphics(g);
               } finally {
                  g.dispose();
               }

            } while (bs.contentsRestored());
            bs.show();
      } while (bs.contentsLost());  
   }

Hello!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #8 - Posted 2011-10-12 03:20:34 »

Nothing you are doing should be causing problems. My only guess is that the JVM is warming up or something like that. Or you have a logic error. But you said when you turn the rendering off that fixes it. Weird.

See my work:
OTC Software
Offline theagentd
« Reply #9 - Posted 2011-10-12 03:27:08 »

Try the server VM? (add -server to VM parameters).
If you have an Nvidia card it could be that it tries out multithreaded rendering (which sucks balls) in the beginning until it switches back to singlethreaded rendering after a while. I've only had that problem when using LWJGL though... You can disable it in the Nvidia Control Panel.

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

Senior Member


Medals: 11



« Reply #10 - Posted 2011-10-12 03:33:15 »

look at how you're taking the fps (the time difference)
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #11 - Posted 2011-10-12 03:45:45 »

look at how you're taking the fps (the time difference)
Sorry I don't know what you mean.  Undecided

Try the server VM? (add -server to VM parameters).
If you have an Nvidia card it could be that it tries out multithreaded rendering (which sucks balls) in the beginning until it switches back to singlethreaded rendering after a while. I've only had that problem when using LWJGL though... You can disable it in the Nvidia Control Panel.
I have an ATI card.  I tried the -server param but nothing changed.

Hello!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-10-12 03:54:27 »

Oh yeah that fps variable looks a little fishy. You are getting a decimal that is less than 1 and converting to an int so fps will equal 0. Then you are dividing by 0!

Also, subtract the time update() and render() took from the sleep time so your loop runs smoother:

1  
2  
3  
4  
5  
6  
long now = System.currentTimeMillis();

update();
render();

long sleepTime = 1000/TARGET_FPS - (System.currentTimeMillis()-now);

Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #13 - Posted 2011-10-12 04:30:01 »

Any reason to use System.currentTimeMilis() instead of System.nanoTime() ? I tried what you said with currentTimeMilis() and was getting divide by zero errors.  This is what I have now.  I changed fps to a double type and just cast it to an int when I output it.

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 void run() {
     
      setUpBuffer();
     
      while (going) {
         
         long now = System.nanoTime();
         
         updateStuff();
         render();
         
         long sleepTime = 1000/TARGET_FPS - (System.nanoTime() - now)/1000000;
         
         if(sleepTime > 0) {
            try {
               Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
               e.printStackTrace();
               System.exit(-1);
            }
         }
         
         fps = 1000000000.0/(System.nanoTime() - now);
         
         count += 1.0/fps;
      }
   }


I seem to be having the same issue as before, but it's not as bad.  For some reason I always have about 3 or so fps more than I request, but this may get better if update() and render() have more to do.  I don't know.  Thanks again for everyone's help!

Hello!
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #14 - Posted 2011-10-14 10:16:21 »

UPDATE: I have added image to my game, and the frame rate problem I described above no longer happens.  I have no idea why, but figured I would post here to let people know.  Thanks again to everyone who offered help.

Hello!
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

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

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

SHC (66 views)
2014-07-12 17:50:04
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!