Creating fixed frames per second
Earlier in the book we put in an SDL_Delay
function to slow everything down and ensure that our objects weren't moving too fast. We will now expand upon that by making our game run at a fixed frame rate. Fixed frames per second (FPS) is not necessarily always a good option, especially when your game includes more advanced physics. It is worth bearing this in mind when you move on from this book and start developing your own games. Fixed FPS will, however, be fine for the small 2D games, which we will work towards in this book.
With that said, let's move on to the code:
Open up
main.cpp
and we will create a few constant variables.const int FPS = 60; const int DELAY_TIME = 1000.0f / FPS; int main() {
Here we define how many frames per second we want our game to run at. A frame rate of 60 frames per second is a good starting point as this is essentially synced up to the refresh rate of most modern monitors and TVs. We can then divide this by the number of milliseconds...