This lesson is less about Direct3D and more about timing your game loop.
Once crucial thing of a game is how long has elapsed since your last frame has been shown to the user. By knowing this, you know how much to update your game by. To find the time elapsed since last frame known as delta time or dt, we can use some Windows funcitons, specifically QueryPerformanceCounter and QueryPerformanceFrequency.
Before we enter the game loop we set up a few variables.
Next in the game loop we want to calculate delta time for the frame. To do this we take the current performance count again, and find what it is relative to the start counter, then convert it to seconds. This will now be the time since we started our game loop. Now we can then find the difference between the time now and the time from the last frame.
By doing everything relative to the start performance count instead of counts since time of computer boot, we help improve the possible precision of the time when it's converted into a double value.
We now have the delta time for our frame which we can print out. If you have a 60fps monitor you should see a time around 0.0166 if vy-sync is on (the first argument in swapBuffer being 1). However it won't be exact. The reasons behind this can be quite complex: you can read more behind the intricacies of frame timing and getting smooth gameplay in the notes [1].
Here is the code we use to print out our dt value for each frame.
Also have to include stdio.h to use sprintf.
Now that we have dt available to us we can use it to animate the position of our Quad.
We can use dt to update the position of the quad by changing the constant buffer position.
This code will make our quad move in a circle.
Also have to #include the Math.h header to use cosf and sinf.
We did it, we're now timing our frame so we can update our game correctly.
[1] More articles on the intricacy's of frame timing
The Elusive Frame Timing: A Case Study for Smoothness Over Speedvideo):
The Elusive Frame Timing: A Case Study for Smoothness Over SpeedPDF):
Myths and Misconceptions of Frame Pacing
Fixing Time.deltaTime in Unity 2020.2 for smoother gameplay
Android - Achieve proper frame pacing
Low Latency Mode in NVIDIA Reflex SDK
Sign up to my Newsletter to get a weekly email about what Iām up to, what Iām learning and what Iām teaching.