SDL and OpenGL

Author: Mark Szymczyk
Last Update: September 19, 2012

This article provides an introduction to the Simple DirectMedia Library (SDL) and using it for OpenGL programs. Although I focus on using SDL with Mac OS X in the article, there is also a lot of good information for SDL developers on any platform.

Updates

The Xcode setup instructions in the article no longer apply in Xcode 4. I added a link to my Using SDL with Xcode 4 article to the HTML version of this article. (September 19, 2012)

I removed the last three sentences of the first paragraph in the Setting Up SDL in Xcode section. SDL 1.2.14 removed the Project Builder templates so there was no need to explain the difference between the Xcode and Project Builder templates. SDL now has Xcode project templates for Mac OS X 10.4 (Tiger), 10.5 (Leopard), and 10.6 (Snow Leopard). (December 29, 2009)

I revised the material on including the OpenGL header files. The new material makes it very clear that including SDL_opengl.h is superior from a cross-platform perspective than including gl.h manually. (December 4, 2007)

The Mac OS X version of SDL 1.2.10 no longer includes an installer. The article has been updated to reflect that information. (June 9, 2006)

SDL 1.2.10 has been released. The Mac OS X version supports universal binaries. You no longer have to download the SDL source and build the framework to create universal binaries that run on Intel and PowerPC Macs. (May 17, 2006)

Introduction

SDL is an open source library to handle the operating system dependent parts of game development. It can handle things like creating a window, reading mouse, keyboard, and joystick input from the player, playing audio, and creating threads.

SDL shines for writing games on multiple operating system. Suppose you wanted to write an OpenGL game that ran on Linux, Mac OS X, and Windows. Without a library like SDL, you would have to learn the following technologies:

  • WGL, GLX, and either AGL or Cocoa OpenGL to create OpenGL windows.
  • DirectX for Windows audio and reading player input.
  • QuickTime or Core Audio for Mac OS X audio.
  • Either Carbon or Cocoa event handling to read the keyboard and mouse on Mac OS X.

SDL saves you the hassle of having to become intimately familiar with the details of programming multiple operating systems. Learn SDL and your code can run on multiple operating systems in the time it takes to write a game for one operating system.

SDL comes with functions for 2D graphics, but I’m going to be focusing on using SDL with OpenGL in this article. 2D drawing with OpenGL is faster than using SDL’s functions because OpenGL takes advantage of graphics hardware acceleration. Plus, OpenGL can be used for both 2D and 3D graphics, which makes OpenGL more versatile than SDL’s 2D drawing functions.

Next (Setting Up SDL in Xcode)