SDL 2.0 Tutorial-00: The Basic Sturcture

Throughout this series of weekly tutorials I’ll be guiding you through SDL 2.0 game development in C. I’m sticking with the C-programming language to keep things simple, and this will no doubt make learning C++ A LOT easier if you ever decide to step things up a bit.

Our first step is to setup a basic game structure. We need a chunk of memory where we can store all the components that will persist over the entire existence of our program. This is a reasonable thing to do since components like the game window will run for as long as the game runs.

First let’s include some fundamental header files. These files are part of the C standard library and provide some basic functions like printing statements to a standard output stream.

For a detailed description of the C standard library and how to use it check out this reference.

Now we have some bare-bones functionality. Next we need to include the SDL 2.0 function and variable definitions so that we can use the SDL library.

This first line here tells SDL that we won’t be needing any special entry point for our program. We’ll be handling the entry and library initiation ourselves within  int main(int argc, char* argv[]); .

The second line provides references to all the SDL functions and variables we will be using. It is worth noting that  #define SDL_MAIN_HANDLED should be before  #include "SDL2/SDL.h" so that when the SDL library is being preprocessed the custom entry points aren’t included.

The next part varies a bit based on personal preference but I have found it to be a nice and simple way of defining, what is essentially, a Singleton within C.

This provides us with a globally defined game object which will be used to house all persistent game information. Doing things this way will help cut down on function parameters, which can easily get out-of-hand if not kept in check.

Now it is time to actually implement the  void game_init(void); and  void game_quit(void); functions.

What we are doing here in the initialization step is printing the function name to the standard out stream, initializing the SDL library, and setting the game to a “running” state. During the SDL initialization the library will build and allocate all the objects and variables needed to make a game window, provide game audio, timers, events, joystick controls, etc.

Depending on how you are setting up your program it is possible that you might run into errors at this point due to SDL not being able to initialize correctly. In that case you will need to wrap your code in the following way so that SDL will tell you what the issue may be.

Once all the issues are resolved, we are ready to actually make a little magic happen. The following is a simple game loop which initializes and quits the game in one iteration. Not a fun game but it does provide the structure we’ll need for next week’s project when we actually get a window up and running!

Here our main function takes two parameters.  int argc stands for “argument count” and provides us with the number of command line arguments passed to our function during execution.  char* argv[] stands for “argument vector” and provides us with the actual string arguments which where passed to our function. Once the game has successfully executed our code we  return 0; to tell the operating system that we achieved everything we set out to do.

All together we get the following piece of code!

Till next week, take care!

Leave a Reply

Your email address will not be published.