Should I put a delay in my main game loop?

I have a game with a main loop that looks like

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).

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.

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:

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.

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

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.

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