Book Image

SDL Game Development

By : Shaun Mitchell
5 (1)
Book Image

SDL Game Development

5 (1)
By: Shaun Mitchell

Overview of this book

SDL 2.0 is the latest release of the popular Simple DirectMedia Layer API, which is designed to make life easier for C++ developers, allowing you simple low-level access to various multiplatform audio, graphics, and input devices.SDL Game Development guides you through creating your first 2D game using SDL and C++. It takes a clear and practical approach to SDL game development, ensuring that the focus remains on creating awesome games.Starting with the installation and setup of SDL, you will quickly become familiar with useful SDL features, covering sprites, state management, and OOP, leading to a reusable framework that is extendable for your own games. SDL Game Development culminates in the development of two exciting action games that utilize the created framework along with tips to improve the framework.
Table of Contents (16 chapters)
SDL Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 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...