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:

  1. 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()
    {
  2. Here we define ...

Get SDL Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.