Game Development Reference
In-Depth Information
With that in mind, it should be obvious how to measure the frame time. We initialize a
Clock variable outside the game loop, and at the beginning of the frame, we get the
elapsed time and restart the clock. This gives us the amount of time that the previous
frame took, so that we can use it to advance out objects. The amount of time between the
beginning of the last frame and the beginning of the current frame is typically called
deltaTime (or dt in short). Here is what the code looks like:
Now that we know how to capture time, we should make sure to use it wherever the game
logic requires it—in any sort of movement, animation, time-based events (to destroy an
object after N seconds), and so on. Since we know the delta time between frames, we can
also accumulate time in a different variable to measure time outside the frame structure.
Here is an example where we want to close the window 5 seconds after opening it:
Search WWH ::

Custom Search