Window Main Loop
Every Gosu game contains a subclass of Gosu::Window
which overrides all the callback methods it is interested in. When you call window.show()
, Gosu enters this loop:
The Ruby Tutorial and C++ Tutorial explain how to use these callbacks.
Some more details:
-
draw
is very flexible. The usual flow is shown above, butdraw
might be called if the OS decides to redraw the window, or calls todraw
might be dropped if the window is hidden, etc. Your game logic should never rely ondraw
being called in perfect lockstep withupdate
, even if that is the normal case. -
You cannot know which callback will be called first.
initialize
(or the constructor in C++) must set up a valid state that will work no matter ifupdate
ordraw
will be called first. - You cannot rely on
Gosu::TextInput::text()
being the same betweenupdate
anddraw
. - Nothing in Gosu is multi-threaded, and no callback is ever interrupted for another.
If you want to make sure that your game works on slow computers, there are two solutions.
First, you can write your game logic in the style of the tutorial game. In every logical frame (Window::update
), all objects move by a fixed amount of pixels. This is usually the easiest way to write code.
The problem is that when the system is too slow to run your game at 60 FPS, everything will seem to move in slow-motion.
Solution: You should try to stay near the 60 FPS target by reducing visual effects when Gosu::fps()
drops below 60, and you can use the special callback Gosu::Window::needs_redraw()
( needs_redraw?
in Ruby) to skip calls to Window::draw
(e.g. every second frame).
You can call Gosu::milliseconds()
in every update
call, then update your game based on the time difference between two calls (delta time, dt). This allows physics to be written in a style that matches school physics (position += speed * dt
), and ensures that everything will move at the same speed regardless of the current frame rate.
However, this style of programming usually requires more code.
Gosu supports both styles of programming.
- Getting Started
- Tutorials
- In-Depth Documentation
- Deployment
-
Gosu Boards
- Gosu Exchange - Questions? Ask them here.
- Gosu Showcase - Show off your projects.
- Extending Gosu - Libraries that play nicely with Gosu.