Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  Should I put a delay in my main game loop?  (Read 547 times)
0 Members and 1 Guest are viewing this topic.
Offline kitfox

Junior Member




Java games rock!


« Posted 2013-02-23 16:49:47 »

I have a game with a main loop that looks like

1  
2  
3  
4  
5  
while(true)
{
    doGameLogic();
    drawLevel();
}


Someone on another forum said that this is a bad idea because it would lead to 100% CPU usage which would 'be a killer to power and heat consumption'.  Is this true?  Right now my game is running at about 1000 FPS, but I could add a Thread.sleep(1) command in there if that would make things better for the user.  (The game thread is already running at MIN_PRIORITY).
Offline Regenuluz
« Reply #1 - Posted 2013-02-23 17:49:30 »

Yes, that is true. You really should sleep in your main loop. There's *no* reason what so ever to run wild like that.

And there's absolutely no reason to render 1k frames a second.
Offline sproingie
« Reply #2 - Posted 2013-02-23 17:52:32 »

You want to set a target framerate, something that's sane (lower than 1000 FPS for sure) One of only two FAQs I've collected (really need to start this up again) has pertinent links to topics on the subject:

http://www.java-gaming.org/topics/jgo-frequently-asked-questions/26316/view.html#post_how_do_i_sync_framerate
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava

JGO Knight


Medals: 32



« Reply #3 - Posted 2013-02-23 20:22:52 »

60 FPS seems to be the norm. Did you know hollywood movies are shot at 24 FPS?

60 FPS means 60 frames per second, this doesn't mean that you should render those 60 frames in 0.1 seconds and then pause for 0.9 seconds. You should space them out. So considering 60 FPS each frame should be spaced between 1/60 seconds ~0.016667 seconds or ~16.667 milliseconds.

1  
2  
3  
4  
5  
6  
7  
8  
9  
while (true) {
   update();
   render();
   try {
     Thread.sleep(16); // sleep for 16 milliseconds
  } catch (InterruptedException ie) {
     e.printStackTrace();
   }
}


That's a very basic (not good) loop that gets around 60 FPS. It doesn't separate rendering from logic either. Check this thread out for info on good loops:

http://www.java-gaming.org/index.php?topic=24220.0

Offline sproingie
« Reply #4 - Posted 2013-02-24 00:57:44 »

You're only going to reach 60 fps with that loop if update and render take zero time.  And I don't see where you determine that "it doesn't separate rendering from logic" either, but that's neither here nor there. 

Just look at LWJGL's Sync class, which is linked in the FAQ post I referenced.
Offline jonjava

JGO Knight


Medals: 32



« Reply #5 - Posted 2013-02-24 14:33:16 »

Well yes, obviously it's not a good loop. But for some it might not be so obvious.

Which is why you should read http://www.java-gaming.org/topics/game-loops/24220/view.html

Once you understand the basics of good loops, you should be able to understand the LWJGL Sync class. Here's the latest Sync class with markup:

http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/opengl/Sync.java?revision=3800&view=markup

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (49 views)
2013-05-17 21:29:12

alaslipknot (58 views)
2013-05-16 21:24:48

gouessej (88 views)
2013-05-16 00:53:38

gouessej (86 views)
2013-05-16 00:17:58

theagentd (95 views)
2013-05-15 15:01:13

theagentd (88 views)
2013-05-15 15:00:54

StreetDoggy (133 views)
2013-05-14 15:56:26

kutucuk (154 views)
2013-05-12 17:10:36

kutucuk (154 views)
2013-05-12 15:36:09

UnluckyDevil (164 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.1 seconds with 21 queries.