Page 1 of 1
How does the game loop work?
Posted: Thu Nov 26, 2009 12:42 am
by Stormfury
I'm used to have a layout similar to this:
Init()
Loop:
UserInput()
Update()
Render()
CheckForGameOver()
EndLoop
Cleanup
In all of the tutorial examples, we just use one load function, and one update function. Is there a game loop built in somewhere, or must I code this looping functionality myself? I'll have to figure out deltaTime and make sure I can set a desired FPS.
Thanks for the help
Re: How does the game loop work?
Posted: Thu Nov 26, 2009 7:11 am
by bmelts
load() is the equivalent of the Init() function - get everything set up.
UserInput(), as it were, is handled by separate callbacks. keypressed(), keyreleased(), mousepressed(), and mousereleased() are all called when the appropriate event happens, and you can take action there. There's also love.keyboard.isDown and similar functions for checking outside of the callbacks.
update() is where you (surprise) update everything. That function is run once a frame.
Rendering is handled in the draw() function, also run once a frame.
You would check for game over in the update() function. (e.g. if player:isDead() then love.system.exit() end)
LÖVE handles the cleanup itself.
In the upcoming LÖVE 0.6.0, you can override the default game loop if you like and replace it with your own (just declare your own love.run() ). However, the default loop works pretty well for most games.
deltaTime (which is passed to the update function as the dt variable) is just the number of seconds that have elapsed since the last frame. It's fairly trivial to write an arbitrary FPS limiter (make sure you turn vsync off, though).