SDL is a library particularly used to create 2D games. We will in this first chapter learn more about this library and find out how to install it.
The SDL is said to be a “third party library”. You should know that there are two types of libraries.
- The standard library: this is the basic library which works on all OS (hence the word “standard”) and which allows you to do very basic things like desprintf. It was automatically installed when you downloaded your IDE and compiler.
Throughout parts I and II, we only used the standard library (stdlib.h, stdio.h, string.h, time.h…). We haven’t studied it in its entirety, but we’ve seen a pretty big chunk of it. If you want to know all about the standard library, do a Google search, for example, typing “C standard library”, and you will have the list of prototypes as well as a brief explanation of each function
- Third-party libraries: these are libraries that are not installed by default. You need to download them from the Internet and install them on your computer.
Why you should choose SDL?
You Can Read The article From Here
SDL: How to install SDL on Windows
- Downloading the SDLThe SDL website www.libsdl.org should soon become essential for you. There you will find everything you need, especially the library itself to download as well as its documentation.On the SDL website, go to the menu on the left, section Download.
Download the most recent version of SDL you see (SDL 1.2 as of this writing).The download page is separated into several parts.
- Source code: you can download the source code of the SDL. As I told you, we are not interested in the source code. I know you are curious and would like to know how it is done inside, but currently it won’t do you any good. Worse, it will confuse you and that is not the point.
- Runtime libraries: these are the files that you will need to distribute along with your executable when you give your program to other people. On Windows, it is simply an SDL.dll file. This must be found:
–either in the same folder as the executable (which I recommend). The ideal is to always give the DLL with your executable and to leave it in the same folder. If you place the DLL in the Windows folder, you will no longer need to attach a DLL in every folder that contains an SDL program. However, this can cause version conflict issues if you overwrite a newer DLL;
-either in the c: \ Windows folder.
If you are on Windows, we offer you three versions depending on your compiler:-VC6: for those who use Visual Studio for a fee in an old version (which is unlikely to concern you); there you will find .lib files;
- Development libraries: these are the .a (or.libs in Visual) and.h files allowing you to create SDL programs. These files are only needed for you, the programmer. So you won’t have to distribute them with your program once it’s finished.
-VC8: for those using Visual Studio 2005 Express or later; you will find .lib files there;
-mingw32: for those who use Code :: Blocks (so there will be .a files)The particularity is that the “Development libraries” contain everything you need: les.het.a (ou.lib) of course, but also theSDL.dll to distribute with your application as well as the SDL documentation!
In short, all you have to do in the end is download the “Development libraries”. Everything you need is there.
A documentation is the complete list of functions of a library. All documentation is written in English (yes, even libraries written by French people have their documentation in English). This is one more reason to progress in the language of Shakespeare!
Documentation is not a course, it is generally quite austere. The advantage over a course is that it is complete. It contains the list of all the functions, so it is THE reference for the programmer.
Quite often you will come across libraries for which there are no courses. You’ll only have the “doc” as it’s called, and you’ll need to be able to get by with just that (although sometimes it’s a bit hard to start without help). A real good programmer can therefore discover how a library works only by reading its doc ‘.
A priori, you will not need the SDL documentation right away because I myself will explain how it works. However, it’s like the standard library: I can’t tell you about all the functions. So you will definitely need to read the doc later.
The documentation is already in the “Development libraries” package, but if you want you can download it separately by going to the menuDocumentation / Downloadable.
I recommend that you put the HTML files of the documentation in a special folder (named for example SDL Doc) and make a shortcut to the sommaireindex.html. The goal is for you to be able to quickly access the documentation when you need it.
- Create an SDL project: Windows
Setting up a library is usually a bit more complicated than the setup you are used to. Here there is no automatic installer which just asks you to click on Next – Next – Next – Finish.
In general, installing a library is quite difficult for a beginner. However, if it may cheer you up, installing SDL is much simpler than many other libraries I have had the opportunity to use (usually you are only given the source code for the library, and it’s up to you to recompile it!).
In fact, the word “install” may not be the right one. We’re not going to install anything at all: we just want to get to create a new SDL-like project with our IDE.
Now depending on the IDE you use the handling will be a little different. I will introduce the manipulation for each of the IDEs that I presented to you at the beginning of the course for everyone to follow.
Now I’ll show you how to create an SDL project under each of these three IDEs.
Creating an SDL project under Code :: Blocks
Extract files from the SDLOpen the compressed “Development Libraries” file that you downloaded.
This file is a.zip for Visual and a.tar.gz for mingw32 (you will need software like Winrar or 7-Zip to unzip the.tar.gz).The compressed file contains several subfolders. The ones that interest us are the following:
You need to extract all these files and folders somewhere on your hard drive. For example, you can place them in the Code :: Blocks folder, in an SDL subfolder (following fig.).
- bin: contains the SDL.dll;
- docs: contains the documentation of the SDL;
- include: contains les.h;
- lib: contains the.a (or.lib for Visual).
In my case, the SDL will be installed in the folder:
C: \ Program Files \ CodeBlocks \ SDL-1.2.13
Remember the name of the folder in which you installed it, you will need it to configure Code :: Blocks.
Now, we will have to do a little manipulation to simplify the rest. Go to the include / SDL subfolder (in my case it is in C: \ Program Files \ CodeBlocks \ SDL-1.2.13 \ include \ SDL). You should see many small .h files there. Copy them to the parent folder, i.e. to:
C: \ Program Files \ CodeBlocks \ SDL-1.2.13 \ include
The SDL is installed! You must now configure Code :: Blocks.
Creation of the SDL project
Now open Code :: Blocks and ask to create a new project.
Instead of creating a Console Application project like you used to do, you will ask to create a project of type SDL project.
The first wizard window that appears is useless, click Next.
You will then be asked for the name of your project and the folder in which it should be placed, as you have always done (following fig.).Then comes the part where you must indicate where the SDL is installed (following fig.)
Click th… button on the right. A new, somewhat complex window opens (following fig.).
You just need to fill in the field named base. Indicate the folder where you unzipped the SDL. In my case, it is:
C: \ Program Files \ CodeBlocks \ SDL-1.2.13
Then click on Next in the wizard, then choose to compile in Release or Debug mode (whatever) and finally, choose Finish.
Code :: Blocks will create a small test SDL project including unmain.c and a.bmp file. Before trying to compile it, copy the SDL DLL (you should have copied it to C: \ Program Files \ CodeBlocks \ SDL-1.2.13 \ bin \ SDL.dll) to your project folder.
Then try to compile: a window with an image should appear. Well done, it works!If you are told “This application could not start because SDL.dll could not be found”, you have not copied the file SDL.dll into your project folder!
You will need to remember to provide this.dllen in addition to your.exe to your friends if you want them to be able to run the program as well. On the other hand, you don’t need to contact them with the.het.which interests only you.As for the main.c file, it’s a bit long, we’re not going to start with that. Delete all of its content and replace it with:
int main(int argc, char *argv)
}It is in fact a basic code very similar to those which we know (unincludedestdlib, another destdio, unmain…).
The only thing that changes is the inclusion of an SDL.h file. The SDL base.h file will take care of including all the other SDL .h files.
Creating an SDL project in Visual C ++
1.Extraction des fichiers de la SDL
On the SDL website, download the latest version of the SDL. In the “Development Libraries” section, check out the version for Visual C ++ 2005 Service Pack 1.
Open the zip file.
It contains the doc ‘(dossierdocs), les.h (dossierinclude), and les.lib (dossierlib) which are the equivalent of.a for the Visual compiler. You will also find the SDL.dll file in this lib folder.
-CopySDL.dll to your project folder.
-Copy the.libs to the Visual C ++ libfolder. For example at home this is the folder:
C:\Program Files\Microsoft Visual Studio 8\VC\lib.And put the .h in: C: \ Program Files \ Microsoft Visual Studio 8 \ VC \ include \ SDL.
2.Creation of a new SDL project
under Visual C ++, create a new project of type Win32 Console Application. Call your projettestsdl for example. Click on OK.
A wizard will open. Go to Application settings and check that Empty project is checked (following fig.).The project is then created. It’s empty. Add a new file to it by right-clicking on Source files, Add / New element (following fig.).
In the window that opens, ask to create a new file of type C ++ file (.cpp) that you will call main.c. Using the extension.c in the file name, Visual will create a .file, not a.cpp file.
Write (or copy and paste) the previously mentioned “base” code into your new, empty file.
3-Configuring the SDL project in Visual C ++Go to the properties of your project: Project / Properties of testsdl.-In the C / C ++ => Code Generation section, set the Runtime Library field to multithreaded DLL (/ MD).–In the C / C ++ => Advanced section, select Compilation under and choose the value Compile as C code (/ TC) (otherwise Visual will compile your project as C ++).
-In the Linker => Entry section, modify the value of Additional dependencies to add SDL.lib SDLmain.lib.
-In the Linker => System section, modify the value of Subsystem and set it to Windows (following fig.).
Then validate your modifications by clicking on OK and save everything.
You can now compile by going to the menu Generate / Generate the solution.
Go to your project folder to find your executable (it may be in a Debug subfolder). Remember that the SDL.dll file must be in the same folder as the executable. Double-click on your.exe: if all is well, nothing should happen. Otherwise, if there is an error it is probably that the fileSDL.dll is not in the same folder.
- SDL is a low-level library that allows you to open windows and do 2D graphics manipulations.
- It is not installed by default, you have to download it and configure your IDE to use it.
- It is open and free, which allows you to use it quickly and guarantees its durability.
- There are thousands of other libraries, many of which are of very good quality. The SDL was selected for the rest of this course for its fun aspect. If you want to develop complete interfaces with menus and buttons later, I invite you to look at the GTK + library for example.