SDL Hello World Tutorial

Home --> Programming Projects --> FPS Game Code --> SDL Hello World Tutorial
Here is a tutorial for compiling and linking a simple C++ program that uses SDL (1.2) + OpenGL + SDL_Image on Windows using MinGW.

Installing MinGW + MSYS

First, install MinGW (and MSYS) in the directory c:/MinGW/, or whatever is the default install directory. There are various .exe files in the bin directory. For example, c:/MinGW/bin/g++.exe. Add c:/MinGW/bin to the PATH environment variable.

You also want to add c:/MinGW/msys/1.0/bin to the PATH environment variable to use things like bash.exe or make.exe (but if you have your own versions of these tools, then you could use those if you really want). At this point, one can compile a hello world c++ program using g++ in cmd. Using cmd on Windows for any longer than a couple seconds induces insanity. Because of this, I recommend using Console2 for a terminal and the program bash.exe as a shell (use c:/MinGW/msys/1.0/bin/bash.exe). You can set Console2 to use whatever program you want as the default shell. With this setup, tab completion should work correctly.

Installing SDL

Add a directory called SDL to c:/MinGW/include. Add a directory called SDL to c:/MinGW/lib. Go to the SDL website and download the version for MinGW on Windows. Copy the files in the include directory to c:/MinGW/include/SDL. Copy the SDL and SDLmain libraries to c:/MinGW/lib/SDL.

SDL Hello World

Extract the folder SDL_HelloWorld from HelloWorlds.zip. Within this folder is SDL.dll, but this can be downloaded from the SDL website as well (for Windows). You should probably use the most recent version of SDL.dll for sanity reasons. Open up a Console2 terminal and cd into the SDL_HelloWorld directory. Run the command make. This should create the program SDL_HelloWorld.exe. The linking command in the makefile is as follows:
	g++ -Lc:/MinGW/lib/SDL -o SDL_HelloWorld.exe main.o -static-libgcc -lmingw32 -lSDLmain -mwindows -lSDL

SDL + OpenGL Hello World

Extract the folder SDL_OpenGL_HelloWorld from HelloWorlds.zip. Fortunately, the OpenGL header files come with MinGW (they are located in c:/MinGW/include/GL), so nothing new needs to be downloaded. Note that GL/gl.h should NOT be included from an SDL program, but SDL/SDL_opengl.h should be included instead. The header file GL/glu.h, on the other hand, can be included.

Open up a Console2 terminal and cd into the SDL_OpenGL_HelloWorld directory. Run the command make. This should create the program SDL_OpenGL_HelloWorld.exe. The linking command in this makefile is the same as the one above except the library opengl32 is also being used. The library libopengl32.a should already be in the directory c:/MinGW/lib.

SDL + OpenGL + SDL_Image Hello World

Extract the folder SDL_OpenGL_SDLImage_HelloWorld from HelloWorlds.zip. SDL does not provide functions for loading textures from files. This is the purpose of the SDL_Image library. First, download SDL_Image. Add a directory called SDL_Image in c:/MinGW/lib. Add the SDL_image library here. Put the header file SDL_image.h in c:/MinGW/include/SDL. This location is important because SDL_image.h includes SDL.h.

Open up a Console2 terminal and cd into the SDL_OpenGL_SDLImage_HelloWorld directory. Run the command make. This should create the program SDL_OpenGL_SDLImage_HelloWorld.exe. The linking command in this makefile as the same as the one above except the library SDL_image is being used (as well as c:/MinGW/lib/SDL_Image being added to the library search path).

Make sure the file SDL_image.dll is in the same directory as SDL_OpenGL_SDLImage_HelloWorld.exe when it is run. It is also important that libjpeg8.dll (or jpeg.dll or something like this) is in the same directory in order for jpeg files to be loaded correctly.

That's it!