Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Game loop  (Read 4495 times)
0 Members and 1 Guest are viewing this topic.
Offline zeroone
« Posted 2006-10-20 14:55:01 »

I was considering using the following for my game loop.  Anyone have an alternative?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
long nextFrameStart = System.nanoTime(); 
while(true) {
  do {
     // ... update the game state ...
     nextFrameStart += FRAME_PERIOD;
  } while(nextFrameStart < System.nanoTime());
  // ... render the state to the screen ...
  long remaining = nextFrameStart - System.nanoTime();
  if (remaining > 0) {
     Thread.sleep(remaining / 1000000);
  }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2006-10-20 17:35:22 »

You are pausing your loop twice.

1  
2  
3  
4  
5  
6  
7  
8  
long currentTime;
while(true) {
  currentTime = System.nanoTime();
  .
  . //all game stuff
  .
  while(System.nanoTime - currentTime < FRAME_TIME);
}

Offline zeroone
« Reply #2 - Posted 2006-10-20 17:43:32 »

If you don't call Thread.sleep(), CPU will be pegged to 100%

[edit] Which is especially bad if you want it to be responsive to the keyboard or mouse.  Also, the inner loop is required to skip rendering (frame skipping) if it gets behind.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2006-10-20 17:48:48 »

That will not affect the responsiveness of the mouse or keyboard.  The JVM has full control over thread resources.  All you are doing is telling the JVm that you are no taking a pause.  I since we are talking about 4K, less is more.  Your loop will take at least twice as much space as mine.  Don't worry about treating the rest of the system properly in that respect.  Just make sure your game runs.

I have already made game loops like this with no loss of response from the mouse or keyboard.

Offline zeroone
« Reply #4 - Posted 2006-10-20 17:52:54 »

Let's assume that is the case.  What about frame skipping?
Offline CommanderKeith
« Reply #5 - Posted 2006-10-20 18:07:57 »

Your loop is OK but why make multiple updates between rendering?

About the sleep() call & mouse input, I've found that on some computers it does make the mouse lag so I always do sleep(1).  Note that this does kill performance a little though since on MS Windows the sleep time actually turns out to be about 20milliseconds regardless of what number you give to sleep() that's less than 20.

Offline zeroone
« Reply #6 - Posted 2006-10-20 18:17:43 »

The idea behind the inner-loop is to update the model more than once if the game is lagging behind.  Meaning, on slower computers, it will occationally skip drawing one or more frames.

[edit] Note, this means that you need to separate out your rendering code from the code that updates the model.  Unfortunately, that often means extra loops.  For instance, to update the position of all the sprites in the game, you'll need to iterate over the collection of sprite positions.  Then later when you draw the sprites, you'll need to iterate over that collection again to access those sprite positions.  You could kill 2 birds at the same time and do the position updates and drawing within the same loop, but you'll never be able to skip frames if you do that.

[edit] Also, this design assumes that it takes a lot less time to update the model than it does to render the frame to the screen.  The inner-loop can be modified to put a cap on the number of times the model can be updated per rendering.  Meaning, you can set the max number of frames to ever skip.  That way for computationally complex games it may occationally slow down (lag behind), but at least it won't be skipping so many frames that the screen effectively keeps freezing, making the game unplayable.

Thread.sleep() is more accurate than you think at least under windows.  You can measure the sleep time using System.nanoTime().  I found it often sleeps about 0.1 extra milliseconds than specified.  But, that still doesn't matter.  If it sleep too long on one iteration, it will sleep less the successive iteration. 
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #7 - Posted 2006-10-20 19:55:36 »

I completely agree with what you are saying.  However, we are talking about 4K.  It is very very unlikely that you will have that kind of lag.  You can also lock the frame rate to a sufficiently low count, such as 40 frames/sec that it should work fine.  The way you want to do it will add upwards of 100bytes or more to your final size.  This is very significant in the 4K compo.  You can do it whichever way you would like.  And if you can fit all you want into it with the extended loop logic, then more power to you.

Offline woogley
« Reply #8 - Posted 2006-10-21 03:43:30 »

frame skipping is like.. the least of your worries in 4K... O_o
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #9 - Posted 2006-10-21 18:21:43 »

Frame skipping is a huge worry in 4K. =)

Miners4k, for examples, needs to run at the same speed on all computers to make sure the level times are "fair".

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

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #10 - Posted 2006-10-21 19:21:00 »

Frame skipping is a huge worry in 4K. =)

Miners4k, for examples, needs to run at the same speed on all computers to make sure the level times are "fair".

If your clock displayed simulation time, rather than real-time - this would not be a concern.
Though I suppose it depends on whether it is acceptable for a 1 second count-down to be potencially longer than 1 second.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline woogley
« Reply #11 - Posted 2006-10-22 14:06:33 »

Miners4K is one specific example where frame skipping would be a worry (if you didn't use simulation time as Abuse suggested)

with the timing code provided by oNyx, a few frame skips (like, in Goomba4K) are not a huge deal, the timing code more or less accounts for such things
Offline zeroone
« Reply #12 - Posted 2006-10-30 15:21:09 »

I was experimenting with the game loop posted above, when I noticed that my Windows clock was slowly gaining time.  After a few hours, I had to move the clock back 20 minutes.  I found a workaround snippet of code that works well in this link:

http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=80aeda52b83875e57063f604054c:WuuT?bug_id=6435126

Obviously, we can't paste that into a 4K game, but if you're developing something larger, this may be relevant.
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.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
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!