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.

#include <stdio.h>
#include <stdlib.h>

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.

#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"

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.

void game_init(void);
void game_quit(void);

static struct {
	// define "attributes"
	SDL_bool running;

	// define "methods"
	void (*init)(void);
	void (*quit)(void);
} Game = {
	SDL_FALSE,
	game_init,
	game_quit
};

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.

void game_init(void) {
	printf("game_init()n");
	SDL_Init(SDL_INIT_EVERYTHING);
	Game.running = SDL_TRUE;
}

void game_quit(void) {
	printf("game_quit()n");
	SDL_Quit();
	Game.running = SDL_FALSE;
}

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.

void game_init(void) {
	printf("game_init()n");

	if(SDL_Init(SDL_INIT_EVERYTHING)!=0) {
		printf("SDL error -> %sn", SDL_GetError());
		exit(1);
	}

	Game.running = SDL_TRUE;
}

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!

int main(int argc, char* argv[]) {

	Game.init();

	printf("running = %dn", Game.running);

	while(Game.running) {
		Game.quit();
	}

	printf("running = %dn", Game.running);

	return 0;
}

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!

/* Stephen's Tutorials (https://stephenmeier.net/)
gcc main.c -o game.exe -I./include -L./lib -lSDL2main -lSDL2
running on gcc 4.8.1, SDL 2.0.1
*/

//----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

//----------------------------------------------------------------------
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"

//----------------------------------------------------------------------
void game_init(void);
void game_quit(void);

static struct {
	// define "attributes"
	SDL_bool running;

	// define "methods"
	void (*init)(void);
	void (*quit)(void);
} Game = {
	SDL_FALSE,
	game_init,
	game_quit
};

//----------------------------------------------------------------------
void game_init(void) {
	printf("game_init()n");

	if(SDL_Init(SDL_INIT_EVERYTHING)!=0) {
		printf("SDL error -> %sn", SDL_GetError());
		exit(1);
	}

	Game.running = SDL_TRUE;
}

//----------------------------------------------------------------------
void game_quit(void) {
	printf("game_quit()n");
	SDL_Quit();
	Game.running = SDL_FALSE;
}

//----------------------------------------------------------------------
int main(int argc, char* argv[]) {

	Game.init();

	printf("running = %dn", Game.running);

	while(Game.running) {
		Game.quit();
	}

	printf("running = %dn", Game.running);

	return 0;
}

Till next week, take care!

4 thoughts on “SDL 2.0 Tutorial-00: The Basic Sturcture

  1. Pingback: SDL 2.0 Tutorial-06: Networking 3 | Stephen Meier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s