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 | No Comments »

OpenGL with Visual C++ 2008 Express Edition

March 30th, 2008 by Matt Moen

OpenGL is a popular cross-platform library for 2-D and 3-D graphics programming. OpenGL comes with implementations on most operating systems and many compilers, including Visual C++ 2008 Express Edition, a modern, free IDE and compiler from Microsoft. Once VC++ 2008 is installed you simply need to create a new project and link to the correct libraries and headers to start using OpenGL.

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.

Linker InputLink to the OpenGL libraries:

  • Under the Project menu select Project Properties (Alt+F7) at the bottom.
  • Select Configuration Properties → Linker → Input 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.
  • Type “opengl32.lib glu32.lib” in Additional Dependencies and click OK.

Red Square using OpenGLNow that your project is set up to use OpenGL you can start coding. Download opengl.cpp to begin with a basic Win32/OpenGL demonstration that compiles on VC++ 2008.

If everything is correct you should see a red square when running the code. Credit for this code goes mostly to examples from MSDN and NeHe.

To learn more about OpenGL programming there are several great online resources including NeHe Productions, OpenGL API Documentation, and GameDev.net.


Posted in Tutorials | 8 Comments »

Discretization Error

March 26th, 2008 by Matt Moen

From Wikipedia, the free encyclopedia:

“In numerical analysis, computational physics, and simulation, discretization error is error resulting from the fact that a function of a continuous variable is represented in the computer by a finite number of evaluations, for example, on a lattice. Discretization error can usually be reduced by using a more finely spaced lattice, with an increased computational cost.”

Posted in General | No Comments »

Welcome to Mrmoen.com

March 26th, 2008 by Matt Moen

The purpose of this website is to document and share my technical projects and interests. Additionally, this site will serve as a central contact point for my online presence. Please excuse any instability while I continue to set up and modify this site.

Thanks,

Matt Moen

Posted in General | No Comments »