Cross-Platform Graphics with SDL and OpenGL
With alternative operating systems like Ubuntu and OS X more popular than ever, programming for Windows alone is no longer an option. Simple DirectMedia Layer (SDL) is a popular, cross-platform library that provides an interface for graphics, sound, and input. SDL and OpenGL can be used together to provide basic functionality to create graphics intensive games and simulations that build in Windows, Linux, and OS X. Many notable games have used this approach including Second Life, Doom 3, SimCity 3000, and Unreal Tournament 2004.
The following is a step-by-step procedure for setting up SDL and OpenGL in a Windows environment using Visual C++ 2008 Express Edition, a free IDE and compiler from Microsoft.
Download the SDL development libraries:
- Download the appropriate SDL development library from their download page. For this tutorial I use SDL-devel-1.2.13-VC8.zip (for Visual C++ 2005 SP1)
- Extract (unzip) the file to a convenient location. This tutorial assumes the location to be “C:\SDL”.
- At minimum, SDL.dll is needed to run SDL programs on Windows. The easiest way to use this is to copy SDL.dll from C:\SDL\lib to C:\Windows\System32. Alternatively, you may place a copy in the project directory or in the same directory as the compiled executable.
Run Visual C++ and create a new project:
- Under the File menu select New → Project (Ctrl+Shift+N).
- Select Win32 Project, enter a Name, and click OK.
- In the Wizard click Next, then check the box next to Empty Project, and click Finish.
Add a new source file for the project:
- Under the Project menu select Add New Item (Ctrl+Shift+A).
- Select C++ File (.cpp), enter a Name, and click OK.
- Under the Project menu select Project Properties (Alt+F7) at the bottom.
- Select Configuration Properties from the navigation panel on the left.
- Select All Configurations from the Configuration drop-down box at the top of the dialog. This ensures you are changing the settings for both the Debug and Release configurations.
- Under C/C++ → General, set Additional Include Directories to “C:\SDL\include”.
- Under C/C++ → Code Generation, set Runtime Library to “Multi-threaded DLL (/MD)”.
- Under Linker → General, set Additional Library Directories to “C:\SDL\lib”.
- Under Linker → Input, set Additional Dependencies to “sdl.lib sdlmain.lib opengl32.lib glu32.lib”.
- Click OK to save the project properties.
Visual C++ is now basically set up to use SDL and OpenGL. Coding for these libraries is beyond the scope of this tutorial, but you can use sdlopengl.cpp to get started with a simple demonstration program. When built successfully this code will draw a red square on a black background.
SDL and OpenGL documentation can be found on the SDL website including tutorials, doc wiki, and OpenGL resources. For other OpenGL tutorials and resources try NeHe Productions, OpenGL API Documentation, and GameDev.net.
Posted in Tutorials
August 30th, 2008 at 8:53 pm
This is great. Documentation is pretty good too. Have you created any 3d apps yourself using SDL?