Translate

Friday, April 18, 2014

SDL 2.0 - Integrating with MinGW

Today, I’d like to start working with a multimedia library known as Simple Direct Media Layer (SDL.) SDL is a cross platform development library that provides access to low-level functionality, such as audio, graphics, and HID (human interaction devices) such as a keypad and a mouse. Written in C, it has become one of the most popular and widely used libraries for multiplatform development available, and utilizing it will help us understand all the components that make up a game to a whole.

We will be downloading SDL 2.0, the latest release, and extracting the library into our MinGW folder. GO to the sdl homepage and click on ‘SDL 2.0’ near the left, and click the link that says SDL2-devel-2.0.3-mingw.tar.gz to start the download for SDL 2.0. When it finishes, move the .tar file to a directory on you C:\ hard drive. From this directory, we will be utilizing the tar command (tape archive) to extract this file, using the following flags:

Z: Indicates using the g-zip command to extract the files using the DEFLATE algorithm.
X: Flag that tells tar to extract to the disk from the archive.
V: Verbose; a command used to show the progress and file names while extracting from a file to the disk.
F: File flag, points to the string of the object being extracted, indicating it’s a file.

While in the directory that .tar file sits in, type the following command :

Tar –zxvf SDL2-devel-2.0.3-mingw.tar.gz

This will extract the files for SDL2 from the .tar file, into the directory. You should now see SDL2-2.0.3 for a folder in this directory. This contains all the files to work with SDL 2.0 in your projects, including header files, library objects and the necessary dll.

Now then, we will be extracting all these files into you MinGW directory and moving the .dll object into the Windows system, that way our compiler can utilize SDL without having to look through different directories. First, use the move command from one of our earlier tutorials, and find you MinGW directory (likely right on the C:\ drive) to find it’s include directory in the x86_64-w64-mingw32 folder. This directory holds the include files for your current platform (Windows 64-bit) which will be searched through for the libraries and header files. When you know where this file’s location is, write down or memorize this string name at the top, in the address bar – you will be moving a lot of files from your SDL 2.0 directory into this directory here.

Navigate to the SDL directory and find and enter the same x86_64-w64-mingw32 folder as found in the MinGW directory, and from there find the include folder. Enter this folder, there should be a folder named SDL2, move this folders into the include directory of MinGW with the move command, like so:

Move SDL2 C:\MinGW\x86_64-w64-mingw32\include\

This should be a quick procedure for the computer, since the files won’t be copied, but simply moved instead. From here, enter cd .. to move back to the previous directory, and then cd lib to move into the lib folder, containing the necessary .a files for MinGW to utilize. From here, take advantage of the wildcard character ‘*’ and move the .a files by typing:

 move *.a C:\MinGW\x86_64-w64-mingw32\lib\
 
Then, with the libraries and header files in the right places, simple navigate back with cd .. to the bin folder (cd bin) and type:

 move SDL2.dll C:\Windows\sysWOW64\

This will move the .dll into the windows directory for the dll files (You may need to restart the command prompt with administrator privileges to be able to do this right.)

With all the files in the right places, the only thing left to do is test this with a simple program. Move to an empty directory or create one, and from there, enter notepad sdl-test.cpp to create a file for testing sdl. In this file, enter the following source:

#include "SDL2/SDL.h"

int main( int argc, char* args[] ) {
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    //Quit SDL
    SDL_Quit(); 
    return 0;    }

This code will simply initialize SDL and then quit SDL. If this simple program compiles and links properly, at least we will know that SDL2 has integrated with MinGW. Enter the following into the command prompt:

g++ -o sdl2.exe sdl-test.cpp -lmingw32 -lSDLmain –lSDL.

If you get an error stating that the header file cannot be found, then the include folder is likely in the wrong include folder in the MinGW directory. If the compilation states that the libraries are missing, then the libraries are in the wrong lib folder. Remember to insert these files into the x86_64-w64-mingw32 directory in MinGW.


Well, this was quite a bit of work, but assuming it went as planned, we should finally be ready to begin coding with C/C++, making a game with SDL (and OpenGL for graphics later on). Have a great day, and read up on SDL for future reference! :D

No comments:

Post a Comment