DavidW
|
 |
«
Posted
2011-10-11 23:36:24 » |
|
Hello  . 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!
|
|
|
DavidW
|
 |
«
Reply #1 - Posted
2011-10-11 23: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!
|
|
|
ra4king
|
 |
«
Reply #2 - Posted
2011-10-12 00: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!
|
|
DavidW
|
 |
«
Reply #3 - Posted
2011-10-12 00: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  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!
|
|
|
Eli Delventhal
|
 |
«
Reply #4 - Posted
2011-10-12 00:42:30 » |
|
That doesn't seem like correct behavior. Maybe post your render() function?
|
|
|
|
DavidW
|
 |
«
Reply #5 - Posted
2011-10-12 00: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!
|
|
|
ra4king
|
 |
«
Reply #6 - Posted
2011-10-12 00:53:38 » |
|
|
|
|
|
DavidW
|
 |
«
Reply #7 - Posted
2011-10-12 01: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.  Sorry if I am being a hassle  . 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!
|
|
|
Eli Delventhal
|
 |
«
Reply #8 - Posted
2011-10-12 01: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.
|
|
|
|
theagentd
|
 |
«
Reply #9 - Posted
2011-10-12 01: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!
|
|
counterp
|
 |
«
Reply #10 - Posted
2011-10-12 01:33:15 » |
|
look at how you're taking the fps (the time difference)
|
|
|
|
DavidW
|
 |
«
Reply #11 - Posted
2011-10-12 01:45:45 » |
|
look at how you're taking the fps (the time difference)
Sorry I don't know what you mean. 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!
|
|
|
ra4king
|
 |
«
Reply #12 - Posted
2011-10-12 01: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); |
|
|
|
|
DavidW
|
 |
«
Reply #13 - Posted
2011-10-12 02: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!
|
|
|
DavidW
|
 |
«
Reply #14 - Posted
2011-10-14 08: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!
|
|
|
|