1. Technology

Setting Up Visual Studio/Visual C++ 2010 Express for SDL Development on Windows

By

One of the slightly offputting things, especially for beginners with SDL is the complexity of configuring it. Actually it's not that complicated but SDL has a few libraries and if you are learning to program in C or C++, there's a little bit of a learning curve.

In this tutorial I'll show you how to setup Visual C++ 2010 Express to let you develop with SDL. This also applies to Visual Studio 2010.

If you are following the Game Programming Tutorials in C or C++, you'll know I'm developing Snake, a game that uses SDL, so you have to do this anyway.

Why All the Configuring?

SDL is open source and as well as the main SDl there are several other packages that you will need. For example the Basic SDL only handles BMP graphic file formats. If you want to load and display .gif images you need SDL_image.h which adds the capability for BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, XCF, XPM, and XV.

However if you want .jpg, or .png you need another couple of dlls to be included. It's probably best to copy all of the supplied files.

The Trinity of Development Files

When you add open source files into a project, there are usually three different files needed for the compiler, linker and compiled application to work. These are:

  • header files. These almost always end in .h (some C++ can be .hpp). The compiler needs these.
  • Library Files. These end in .lib and contain the code that the linker needs.
  • DLLs. These are the dlls loaded by your Application at runtime.

Many open source projects supply code in a format that can be built into your application, this is known as static linking. The SDL license however states that for it to LGPL licensed you must either provide the source code to your application or use SDL in a .dll or.so (Linux equivalent of a dll) library. I.e. for commercial games, unless you pay a license fee you must use a dynamic library.

There are a lot of SDL libraries, 179 currently so knowing how to use them is essential.

Configuring for SDL

In Visual C++ Create an empty Win 32 Console Application project, call it what you like and after it's created remove all the Microsoft cruft. that means removing the #include "stdafx.h", changing the main to this and from the solution Explorer removing stdafx.h, stdafx.cpp and targetver.h.

Now you have to decide where you are going to put all the SDL files. Create a folder c:\sdl or c:\gamedev\sdl. Under the sdl folder create two more folders include and lib. When you download SDL from their website (you need the development library version- for 1.2 this is SDL-devel_1.2.14-VC8.zip and for SDL_image it's SDLImage-devel-1.2.10-VC.zip. Both zip files have an include amd a lib folder and you should make sure they go into your c:\sdl\include and lib folders.

After unzipping both files you should see about 43 .h files in the include folder. Make sure SDL_image.h is there and three lib files SDL.lib, SDLmain.lib and SDL_image.lib in the lib folder.

Now go to the Configuration Tab and select Configuration Properties then VC++ Directories. Change the include Directories. Click the line, pull down the combo and click . You'll see a dialog Library Directories. Click the New folder symbol (the first one to the left of the X delete symbol) and type in c:\sdl\include or whatever the path to the include folder is.

Now Repeat for the line that says Library Directories and set it to the lib folder. After doing both, the paths look like:

  • c:\sdl\include;$(IncludePath)
  • c:\sdl\lib;$(LibraryPath)

In your project's Solution Explorer, (if you can't see it, click View then Solution Explorer), right click on header files and add SDL.h from your SDL includes folder.

Specify Which .libs to link in

Now we need to add the three libs to the project. We've told the linker where to find the libs but not which libs to use. Again get the Project settings up and this time go to Linker and click on input. The first line should be Additional Dependencies so again click the combo then click and enter these three values, either on one line separated by spaces or each on a line. SDL.lib, SDLmain.lib, SDL_image.lib. After you click ok the Additional Dependencies should show:

SDL.lib SDLmain.lib SDL_image.lib;%(AdditionalDependencies)

Now you can view the source code and paste it in to replace the default source. It should compile and build. If there are any errors, please check your include and libs.

Does It Run OK?

If you run it though it's likely that it will complain that it can't find SDL.dll. You can find this dll in the SDL devel zip file containing the include and lib folders. It's in the lib folder. Copy it into the same folder as your exe. Also copy SDL_image.dll there as well from the SDL image devel zip file. Try running it again. If you see a black window (or two Black windows), download this file about.gif and put it in the same folder as your exe.

Now you should see it loaded into one of the Windows. If you only see the one Window then that's great. If you see two windows, then it means your project system setting needs changed. Again select Project Properties and look for Linker on the left. Below it select System and it's probably showing Console (/SUBSYSTEM:CONSOLE), change this to Windows (/SUBSYSTEM:WINDOWS), rebuild your project and when you run it it should show only the one Window. Result!

Conclusion

This shows what is needed to add open source (or any 3rd party libraries to your Visual C++ project. The steps were:

  • Unzip into include and libs folders
  • Configure headers and libs
  • Add libs to project
  • Put dlls and needed files with exe
  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Programming Games
  5. Setting Up Visual Studio/Visual C++ 2010 Express

©2014 About.com. All rights reserved.