Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've got a thread (gameloop) in my game that is super complicated, and is built with the help of tutorials when I was still a beginner. But now that I understand a bit more, I find myself wondering why I use over 300 lines of code in my thread when this is enough:

    While(true){
        paint();
        update();    
        thread.sleep(17)
}

Obviously this isnt the exact code one would use, but you probably get my point. I just dont like having something in my game that I dont understand 100%, and I hate cluttered classes that could be done with much simpler code.

So my question is, are there any downsides to using a simple loop like this? Because to me it looks like it does the job, but why would then tutorials have much more in theirs?

share|improve this question
1  
Did you search the site? One of these might answer your question: gamedev.stackexchange.com/questions/53805/… gamedev.stackexchange.com/questions/8623/… –  Byte56 Jun 17 '13 at 23:13
3  
If it uses a Sleep call for controlling framerate it's not a valid game loop. –  Darth Satan Jun 17 '13 at 23:51
1  
gafferongames.com/game-physics/fix-your-timestep That is pretty much the de facto article on game loops. Its well worth the read. –  mobo Jun 18 '13 at 2:25
3  
You should update() before you paint(), that way what you see isn't 17+ milliseconds behind the game state. –  congusbongus Jun 18 '13 at 5:05

3 Answers 3

up vote 4 down vote accepted

You shouldn't ever sleep manually like that - you're assuming that your game loop always takes the same amount of time to complete.. so you're always sleeping by the same amount. This might be true for games that are simple and run the same logic mostly all the time - but this is not true for more complicated games or games with varying levels, objects, and scenes. To avoid this problem, there's a lot of different game loops but I think reading up on them might be serve you better.

A more popular one that gets around this issues is using delta timing and basing your game off that. You can read more about these types of loops at this pretty good resource.

share|improve this answer
While(true){
    paint();
    update();    
    thread.sleep(17)
}

This isn't going to run at 60 fps.

  • What if your paint method takes 2ms to run? What if it's time is not totally consistent across different frames?
  • What if your update method takes 2ms to run? What if it's time is not totally consistent across different frames?
  • What if the timer your sleep call is based on has poor resolution? What if it's resolution is something like 15ms?
  • What if the implementation of sleep on your platform can only guarantee a minimum time to sleep for but may actually sleep for any arbitrary longer time?
  • What if the player is vsyncing at 60fps? What if the player is actually vsyncing at 72fps?

As I said in my comment: if it uses a sleep call for controlling framerate it's not a valid game loop.

Sleep calls are perfectly fine for reducing CPU usage if you really need to (but if your API or framework has better options available you should investigate them first); sleep calls are not appropriate under any circumstances whatsoever for controlling framerate. Framerate should be controlled based on a high resolution timer

share|improve this answer

typically using sleep() at all in a game loop is a bad idea. the OS will sleep for at least the time requested. so even doing sleep(0); means that the OS could sleep for as long as it wants to even though you're asking it to sleep for no time.

these days, we dont bother with sleeping at all within game loops, and typically rely on the OS to timeslice us. there is usually a yeild call of some sort (im not sure what the equivalent in java is?) to instruct the OS that "hey this would be a swell point to do something else for a bit".

see something akin to http://www.javamex.com/tutorials/threads/yield.shtml and note the difference between java 5 and java 6 ;)

moving on from the sleep thing. game "loops" tend to be as complex as they need to be. as you end up adding more and more complexity, the loop will tend to grow. If you're really keen on understanding how to deal with production level software, have a read of the CSP guidebook (communicating serial processes). the general gist is to have a number of "steps" within your loop, and tick each system in order.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.