Mrmoen.com / Matt Moen
Technical Projects, Interests, and Tutorials

Cross-Platform Graphics with SDL and OpenGL

April 8th, 2008 by Matt Moen

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.

New ProjectRun 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.

Configure the compiler:Compiler Settings

  • 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)”.

DependenciesConfigure the linker:

  • 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.

Red Square using SDL/OpenGLVisual 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

4 Responses

  1. jrsauer

    Interesting and informative site.

  2. Sam

    Thank you for this short up to date tutorial !

    Everything works perfectly up to the sdlopengl.cpp test function.

    Thank you again,
    Sam

  3. Stephen Brown

    Thanks for the great tutorial/article. I want to start learning OpenGL but was put off by the ugliness of win32. Good stuff =)

  4. Mike

    Short and sweet. Thanks for posting this information!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.