How to setup SDL2 with codeblocks and the mingw compiler

Last updated 4/5/2014
Description: Project simply creates an SDL window.

Tutorial Color Key

button or links in codeblocks, file directories , file names , Text that needs to be copied , console commands

Downloading required files

The first thing you will need to do is download codeblocks. You can find it here at codeblocks.org. You will want to download the correct one. Click first on "download" then "Download the binary release". Select one of the ones listing the mingw in the name. codeblocks-[version number]mingw-setup.exe download from either BerliOS or Sourcefore.net. As of me writing this 4/5/2014 the current release is 13.12 available at sourceforge here codeblocks-13.12mingw-setup.exe

The next thing you will need to do is download SDL 2.0. You can find it at www.libsdl.org Go to download SDL 2.0 and then select the version that reads like SDL2-devel-2.0.3-mingw.tar.gz the 2.0.3 is the version number in this case shown. As of this being written 2.0.3 has a bug in it SDL_platform.h file and will cause compile errors.
You can download 2.0.3 here SDL2-devel-2.0.3-mingw.tar.gz
You can download 2.0.2 here SDL2-devel-2.0.2-mingw.tar.gz
If you wish to use 2.0.3 you should replace the SDL_platform.h file in it with the one from 2.0.2. There should be a fix in the next revision for the current bug discussed here.

Next you will want to down load any libraries that you may choose to use with SDL such as image, mixer and so on. Those can be found here http://www.libsdl.org/projects/. The more common ones use include SDL_image, SDL_mixer, SDL_net and SDL_ttf. ttf is for font handling. Mixer is used for sound. Image is used for images. Net is used for network. For use with code blocks you will want to go in and download the perspective file that has "mingw.tar.gz" in the name.
The current versions are as follows.
SDL2_image-devel-2.0.0-mingw.tar.gz
SDL2_mixer-devel-2.0.0-mingw.tar.gz
SDL2_net-devel-2.0.0-mingw.tar.gz
SDL2_ttf-devel-2.0.12-mingw.tar.gz

Installation Codeblocks

It is important to install the files in the directories I specify when you are learning. This will save you from the headache of having to fix issues later on.
Installing codeblocks is simple install it to its default directory and accept the agreements and so on.
If you think you may want to use glew to use opengl3+ later on you may want to follow the instructions here Install codeblocks SDL2.0 GLEW opengl3+

Installation SDL 2.0

Next create a directory in C: called SDL (case sensitive)
Copy all SDL downloads into C:\SDL
Extract them into the C:\SDL directory so that it looks like this.
If you need a utility to extract them you could I recommend 7-zip.

Setting up codeblocks to work with SDL2.0

Start codeblocks.
Click on Settings in the top menu. A menu box will open up.
In the menu box click on Linker Settings. In the other linker options panel type in the following:-lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
so that it looks like this.


Next click Search directories next to the Linker Settings tab.
You need to enter in each of the following lines by clicking add then entering it in.
C:\SDL\SDL2-2.0.3\i686-w64-mingw32\include\SDL2
C:\SDL\SDL2_mixer-2.0.0\i686-w64-mingw32\include\SDL2
C:\SDL\SDL2_image-2.0.0\i686-w64-mingw32\include\SDL2
C:\SDL\SDL2_ttf-2.0.12\i686-w64-mingw32\include\SDL2
You will want it to look as follows.


Next click the Linker tab next to the compiler tab.
You need to enter in each of the following lines by clicking add then entering it in.
C:\SDL\SDL2-2.0.3\i686-w64-mingw32\lib
C:\SDL\SDL2_mixer-2.0.0\i686-w64-mingw32\lib
C:\SDL\SDL2_image-2.0.0\i686-w64-mingw32\lib
C:\SDL\SDL2_ttf-2.0.12\i686-w64-mingw32\lib
You will want it to look as follows.


A simple project

The following sections will cover how to create a project and how to code a simple SDL window. You can download the project from here SDL201.zip If you want a C version cSDL001.zip

Creating a project

Start codeblocks. Then in the middle of the screen click Create a new project.

A menu should popup like this click Empyt project.

In this menu Name your project in Project title. Fill in Folder to create project in with the location you wan to save your projects. Then click Next.

Finally when you see this menu click Finish
.

DLL(s) Required for project

At this point you will want to go to all the following locations that you are using and grab all the DLL files and then either in your project directory or system location so they can be accessed when you run your projects. I tend to put them in my project folder this way when I go to ship something or bundle it they are right there.
C:\SDL\SDL2-2.0.3\i686-w64-mingw32\bin
C:\SDL\SDL2-2.0.2\i686-w64-mingw32\bin
You will need SDL2.dll at minimal.
If you are using SDL networking, font, image or mixer in a project you will also need to go to the following directories and get their perspective dll files.
C:\SDL\SDL2_image-2.0.0\i686-w64-mingw32\bin
C:\SDL\SDL2_mixer-2.0.0\i686-w64-mingw32\bin
C:\SDL\SDL2_net-2.0.0\i686-w64-mingw32\bin
C:\SDL\SDL2_ttf-2.0.12\i686-w64-mingw32\bin

Project Layout

There are a total of 9 files in this project. includes.h. game.h, game.cpp, oninit.cpp, loadcontent.cpp, onevent.cpp, onloop.cpp, onrender.cpp, cleanup.cpp

includes.h

#ifndef INCLUDES_H_INCLUDED
#define INCLUDES_H_INCLUDED
#include < SDL.h >
#include < SDL_image.h >
#include < iostream >
using namespace std;
#endif // INCLUDES_H_INCLUDED
/ *
Please not that if you use a later version of SDL you will need to update the linker directories and compiler directories under
Settings > Compiler > Search directories > Linker and Settings > Compiler > Search directories > compiler
The directories below are the ones that will need to be changed if you use a different version.
Under : settings > Search directories > Compiler
C : \SDL\SDL2 - 2.0.3\i686 - w64 - mingw32\include\SDL2
C : \SDL\SDL2_image - 2.0.0\i686 - w64 - mingw32\include\SDL2
Under : settings > Search directories > Linker
C : \SDL\SDL2 - 2.0.3\i686 - w64 - mingw32\lib
C : \SDL\SDL2_image - 2.0.0\i686 - w64 - mingw32\lib
My includes are
#include < SDL.h >
#include < SDL_image.h >
if you change
C : \SDL\SDL2 - 2.0.3\i686 - w64 - mingw32\include\SDL2
C : \SDL\SDL2_image - 2.0.0\i686 - w64 - mingw32\include\SDL2
to
C : \SDL\SDL2 - 2.0.3\i686 - w64 - mingw32\include
C : \SDL\SDL2_image - 2.0.0\i686 - w64 - mingw32\include
the include would need to be changed to
#include < SDL2 / SDL.h >
#include < SDL2 / SDL_image.h >
* /

game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include"includes.h"
class game
{
private :
bool Running;
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Event Event;
public :
game () ;
// virtual ~game () ;
int OnExecute () ;
bool OnInit () ;
bool LoadContent () ;
void OnEvent ( SDL_Event * Event ) ;
void OnLoop () ;
void OnRender () ;
void Cleanup () ;
} ;
#endif // GAME_H_INCLUDED

game.cpp

#include"game.h"
game : : game ()
{
window = NULL;
Running = true;
}
int game : : OnExecute () {
if ( OnInit () = = false ) {
return - 1;
}
while ( Running ) {
while ( SDL_PollEvent ( & Event ) ) {
OnEvent ( & Event ) ;
}
OnLoop () ;
OnRender () ;
}
Cleanup () ;
return 0;
}
int main ( int argc , char * argv [ ] ) {
game theGame;
return theGame.OnExecute () ;
}

oninit.cpp

#include"game.h"
bool game : : OnInit () {
if ( SDL_Init ( SDL_INIT_EVERYTHING ) < 0 ) {
return false;
}
// win = SDL_CreateWindow ( "Image Loading" , 100 , 100 , WIDTH , HEIGHT , SDL_WINDOW_RESIZABLE ) ;
if ( ( window = SDL_CreateWindow ( "SDL Render Clear" , 100 , 100 , 640 , 480 , SDL_WINDOW_SHOWN ) ) = = NULL ) {
return false;
}
// SDL Renderer
renderer = SDL_CreateRenderer ( window , - 1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ) ;
if ( renderer = = NULL ) {
cout << SDL_GetError () << endl;
return 1;
}
return true;
}

loadcontent.cpp

#include"game.h"
bool game : : LoadContent ()
{
return 1;
}

onevent.cpp

#include"game.h"
void game : : OnEvent ( SDL_Event * Event ) {
if ( Event - > type = = SDL_QUIT ) {
Running = false;
}
}

onloop.cpp

#include"game.h"
void game : : OnLoop ()
{
}

onrender.cpp

#include"game.h"
void game : : OnRender ()
{
}

cleanup.cpp

#include"game.h"
void game : : Cleanup ()
{
SDL_DestroyRenderer ( renderer ) ;
SDL_DestroyWindow ( window ) ;
SDL_Quit () ;
}