Using SDL with Code::Blocks
(Original contribution by Ubergeek, ultd. Adapted, corrected and improved by Thomas)
"The Good, the Bad, and the Ugly" - Setting up SDL
This article deals with SDL using Code::Blocks with the MinGW compiler, however the same steps will likely work with a different compiler (such as MSVC2003 toolkit) or under Linux, too (Linux users will of course not worry about DirectX).
There are many ways to get the necessary libraries and include files to set up SDL to be used with Code::Blocks, each of them having its own advantages and disadvantages. In memory of Sergio Leone's best movie, I will call these "the Good", "the Bad", and "the Ugly".
The uglier variants have the advantage of better configurability, but are more error-prone and may take significantly more time to set up.
"The Good"
The easiest of all possibilities is to just download the SDL binary distribution directly from libsdl.org.
If you are only interested in writing a SDL application and care little about esoteric compiler switches, then the pre-built libraries are probably good enough for all your needs, so there is little reason to waste your time compiling your own.
Pro/Con
- pro: need to download one file, setup time ~20 seconds
- pro: it works
- con: not configurable
Prerequisites
- Internet Connection
- Some archiving program (tar, 7z, WinZip, PowerArchiver, ...)
The Process
- Download SDL-devel-1.2.8-mingw32.tar.gz from the SDL version 1.2.8 download page. There are binary packages for several Linux distributions, too (rpm may have problems downloading directly from libsdl.org, use wget to get the packages in that case).
- Decompress the archive
- Copy SDL.dll from inside the bin folder to where you keep your dll files (could be C:\MinGW\dll or C:\MinGW\lib, whatever).
- Copy the contents of lib to your compiler's lib folder. If MinGW is installed under C:\MinGW, then this will be C:\MinGW\lib.
- Copy the complete folder SDL from inside the folder include to your compiler's include directory (for example C:\MinGW\include). Be sure to copy the folder, not the individual files.
- Optionally, copy SDL.dll to C:\Windows, too. This will save you from copying the dll to each and every project directory while developing. Be cautious, however, not to find yourself in DLL hell afterwards.
- Screw the rest
It is of course possible to keep the library and include files in different locations than the ones stated above. In that case, you will have to add those locations to the compiler options. However, be sure not to add the SDL folder to include paths, but its parent folder, as all SDL programs expect the headers to be inside a separate SDL folder.
Alternative
Code::Blocks comes with support for reading .DevPak files of late. This allows for an even more comfortable setup. Recent DevPaks (1.2.8) for SDL, SDL-mixer, and SDL-ttf can be found at http://www.devpaks.org/category.php?category=SDL.
Important: Please do note that the packages at devpaks.org are not original packages created by the SDL team. Be aware that devpaks.org theoretically allows anyone to upload anything under the name SDL. Although I know of no case of malware being distributed over this channel, this is a potential risk you have to be aware of.
"The Bad"
With a little more effort, you can build SDL from sources using Code::Blocks. While this may not be the desirable solution for Joe Everybody writing Hello World for his computer science class in week one, it has certainly its advantages.
Pro/Con
- pro: still relatively easy, build time ~5-6 minutes, setup time ~30 seconds
- pro: it's cool
- pro: it works
- pro: can tune optimization/target settings, release and debug libraries
- con: does not compile out-of-the box (yet)
Prerequisites
- An installation of the Code::Blocks IDE
- A working MinGW installation
- Some archiving program (tar, 7z, WinZip, PowerArchiver, ...)
- DirectX development headers and libraries (version 5 or later)
- in particular, the files d3dtypes.h, ddraw.h, dinput.h, directx.h, and dsound.h are needed. The libraries come with a standard MinGW install, but the headers were strangely found to be missing on my system.
- these files are contained in directx-devel.tar.gz which is significantly less painful to download than the Microsoft DirectX SDK (300 MB!)
- it is unlikely that your MinGW installation lacks the headers for OpenGL, but in any case, there is an OpenGL package too.
The Process
- Download SDL-1.2.8.tar.gz from the SDL version 1.2.8 download page
- Decompress the archive somewhere (I will assume C:\SDL-1.2.8 from now on)
- Inside C:\SDL-1.2.8, locate the file VisualC.zip, decompress it, and open the workspace C:\SDL-1.2.8\VisualC\SDL.dsw
- As of RC1-1, Code::Blocks does not parse Visual-C projects 100% correctly, so you have to check that link libraries and include path are specified correctly.
- SDL needs winmm, gdi32, and dxguid.
- Also, you have to add the include directory inside the SDL source directory to compiler paths.
- Rebuild all (~3 minutes)
- Inside C:\SDL-1.2.8\VisualC\SDL and C:\SDL-1.2.8\VisualC\SDLmain, you will find the DLLs inside the Debug and Release folders. Copy the DLLs to where you want to keep them and the static libraries to your compiler's lib folder.
- Make a folder SDL inside your compiler's include directory. Copy all files from C:\SDL-1.2.8\include into that directory
"The Ugly"
SDL can be built from sources using MSYS. This is certainly the most lenghty and painful way, but it also allows the most flexible configuration options.
Pro/Con
- pro: best tuning capabilities, can easily include or exclude individual parts of SDL
- con: painful, build time ~10-15 minutes, setup time ~30 seconds
- con: unreliable, build sometimes fails with strange errors
- con: have to take care of several possible pitfalls
Caveats
- SDL explicitely needs MSYS 1.0.8 - earlier versions will not work.
- Path names may not contain spaces, or you will utterly fail compiling.
- Some of the files in the archive may be read-only for some obscure reason. make will choke when trying to overwrite these.
The Process
- Download SDL-1.2.8.tar.gz from the SDL version 1.2.8 download page
- Decompress the archive somewhere (I will assume C:\SDL-1.2.8 from now on). The path may not contain any spaces.
- Run ./configure along with all options that you wish. (~5-6 minutes)
- ./configure --help shows all possible packages to include as well as all available options
- you can trim down your SDL library by excluding unwanted features using --without-PACKAGE.
- Run make (~5-8 minutes)
- Pray that the build succeeds
- Copy the libraries and header files to your preferred places as described above.
"Arch Stanton" - Using SDL
Now that we have the libraries built and headers installed, we want to build a SDL application.
Prerequisites
- The Code::Blocks IDE
- A working MinGW installation
- The Good, the Bad, or the Ugly
The Process
- Code::Blocks comes with a template to create SDL projects. Use that template from "New Project...".
- Open Build options and make sure the necessary libraries are included.
- You have to link with mingw32, SDL, and SDLmain to use SDL.
- Also, you may want to add -mwindows to "Other linker options" to prevent a DOS window from showing (actually this is controlled by the build target type: "console" or "GUI").
- Compile and run, you should now see a small window showing the Code::Blocks logo.