[Update 2015-11-14: This article is out of date. Check out the latest version at Gigi Labs.]
Yesterday I realised that earlier this month, SDL2 has been released. SDL is a fantastic library for cross-platform game development, and I had used SDL 1.2.x for several projects ranging from my Picaxo Image Viewer to early iterations of Ultima 1 Revenge, which I mentioned in yesterday's article. I had also written an article on setting up SDL 1.2.x in Linux on my other blog. SDL2 apparently brings about many improvements we were yearning for, including support for multiple windows.
This article is a tutorial on how to set up a Visual Studio 2010 project to work with SDL2. SDL2 is a C library, so SDL2 projects are normally written in C/C++, although it is possible to use other languages. This article is just about setting things up, so you don't really need to know anything. But for future SDL2 tutorials, some C/C++ knowledge will be expected.
Before we begin, you'll need to grab some files from the SDL2 download page. You'll need the Windows development libraries for Visual C++, and you'll also need the Windows runtime binaries (we'll use the x86 ones by default, but you might need the x64 ones if you eventually want to compile 64-bit versions of your game).
Extract the contents of the development libraries. You should have an include folder, a lib folder, and a few loose files. Locate the following directory, where Visual Studio keeps its header files and libraries for C++:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A
In C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include, create a folder called SDL2 and extract the contents of the development libraries' include folder there:
Next, copy the .lib files from the development libraries' lib\x86 folder into C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib:
...and copy the .lib files from the development libraries' lib\x64 folder into C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64:
This will allow Visual Studio to find the header foles and .lib's without any additional configuration. Alternatively, you can put them somewhere else and configure the paths accordingly (this article explains how), but I find the above method easier.
Next, create a project in Visual Studio. You need to select Empty Project, from Visual C++ -> General:
In Solution Explorer, right click on Source Files and add a new item. Select C++ File and name it main.cpp:
Still in Solution Explorer, right click on the project (not the solution) and select Properties.
In the treeview to the left, go to Configuration Properties -> Linker -> Input. In the field called Additional Dependencies, replace all the default crap with the following:
SDL2.lib;SDL2main.lib
Next, in Linker -> System, change SubSystem to Windows:
#include <SDL2/SDL.h> int main(int argc, char ** argv) { SDL_Init(SDL_INIT_EVERYTHING); SDL_Quit(); return 0; }
The project should compile, but when you try to run it, you get this:
That's because SDL2 executables need to have the SDL2.dll file in the same folder. Remember the runtime binaries you downloaded earlier? This is where they come in. Grab the SDL2.dll from the x86 runtime binaries package and put it in your project's Debug folder, where the executable is produced. If you try running it now, it should work.
Great, so it compiles and runs, but doesn't do anything. Given that there are no errors, it works, and you can use this as a starting point for further development. TwinklebearDev's tutorial even describes how to export a template from this project, so you don't need to do the same configuration every time.
I hope you found this useful, and check back for more articles in future! :)
OMG, it worked! Thanks!!!!
ReplyDeleteThank you, good article very useful, It worked
ReplyDelete