Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 1184 times)
0 Members and 1 Guest are viewing this topic.
Offline kitfox

Junior Devvie




Java games rock!


« Posted 2013-02-23 15: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 16: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

JGO Kernel


Medals: 202



« Reply #2 - Posted 2013-02-23 16: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
« Reply #3 - Posted 2013-02-23 19: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

JGO Kernel


Medals: 202



« Reply #4 - Posted 2013-02-23 23: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
« Reply #5 - Posted 2013-02-24 13: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  
 
 
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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26: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
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!