Page 1 of 3

SDL Tutorials

Posted: Sat Jul 09, 2005 8:57 pm
by Arce
Setting up SDL in MVC++

Getting SDL on your System:
To begin, you must have the SDL libraries somewhere on your system so that your development environment can see them. Because SDL is open source, you can obtain the libraries for free at http://www.libsdl.org.

Once you have the SDL libraries on your hard drive somewhere, I'd take a minute to look through them. There should be three folders: docs, include, and lib; along with a number of other files. Read the readme's and familiarize yourself with some of the contents.

In the docs folder, believe it or not, you can find documentations on various SDL functions. This is a very good resource when you need to know the parameters a certain function takes. It is very useful and detailed; I personally use these a bunch.

In the include folder, you can find the header files for SDL. I personally wouldn't spend much time looking through all of these; the only thing you will need to include to use SDL is SDL.h.

In the lib folder, you can find three files: SDL.lib, SDLmain.lib, and SDL.dll. SDLmain.lib and SDL.lib are static libraries to which you will link your application, and SDL.dll is a dynamic link library, which means you must put it in the same folder as your application. I will talk more about these later in the tutorial.


Setting up the VC++ Envirnment
Aight, now lets get to work on setting up the VC++ environment so you can begin using SDL. This section will assume you have Microsoft Visual C++ (because that's what I have. If I ever decide to get DevC++, I'll post that too). The first thing we need to do is let VC++ know where it can find the .lib and the .h files for SDL.

Okay, open up VC++ (no duh). Then, select Tools, Options, as shown below.
Image

The options box will appear. Select the directories tab, as shown below.
Image

Here you will see a list of directories (it may be different than mine, I have multiple libraries set up already). You need to add the path to the SDL include files to the list. To do so, just lick the first blank line in the list, and you should see something like the screen below.
Image

Here you either type in the directory to the include folder, or you browse it by clicking the dots. Though your directory may be different than mine, yours will look something like the below picture.
Image

Now that you did this, you must do the exact same thing for the SDL lib files. Select Library files from the dropdown menu. Then you should see something like the screen below.
Image

From here, the process is exactly the same as above. Click the next available space, and add the directory that contains SDLmain.lib and SDL.lib. When you are done, yours should look something like the below screen.
Image


Aight, you’re done with that. If you think about it, that was actually easy. :)

Creating an SDL Project
Okay, assuming that you are following this tutorial in order and have already set the VC++ environment, I am now going to walk you through the steps that you need to repeat each and every time you make a project that uses SDL.

1. The first thing to do is create the project. select File, New, chose Win32 Application, and then name it whatever you want. Hit okay, then you will be prompted for the type of WIN32 project you would like to make. Select Empty Project, then click finish.
Image

2. Next, you need to copy SDL.dll from the SDL libs directory into your project's directory.
Image

3. Now select Project, Settings, then select the Link tab in the dialog box that appears

4. In the Object/Library Modules text box, type in sdl.lib and sdlmain.lib. Make sure to use spaces to separate each item in the text box.
Image

5. Next, select the C/C++ tab. From the category dropdown menu, select the code generation option.
Image

6. From the Use Run-Time Library drop-down menu, select multithreaded DLL.
Image

7. Hit the okay button, then drink a keg of Bawls to celebrate. You can now begin deving in SDL. :)

Testing the Envirnment
Okay, the point of this tutorial is to set up SDL, not teach you how to use it. So I am not going to explain or comment my code, just bear with me.

Now, we're going to test your project just to be sure you set it up correctly. Make a new .cpp file. (file, new, cpp source file) and name it whatever.

Now, just paste the code below into the new .cpp file.

Code: Select all

#include "sdl.h"
#include <stdio.h>

int main(int argc, char* argv[]) {
     if(SDL_Init(SDL_INIT_VIDEO)==-1) {
          fprintf(stderr,"Something is screwed, couldn't initialize.\n");
    }
   else {
          fprintf(stderr,"It worx! W00t! Initialized properly!\n");
          SDL_Quit();
    }

     return(0);
}
What this program does is attempt to initialize video mode. If it works, then obviously you set up SDL correctly, so it writes to a text file called "stderr" and says it works. If it fails, then you screwed something up, so you should look back over the tutorial and see what you did wrong. If this happens, the program will write that it failed to the text file "stderr".

Aight, compile and run the code. If it works, then I have done my job well. If not, scroll up and try to find the problem. Good luck people, I hope you enjoyed my first tutorial. It would be appreciated if you Private Message me and tell me if this tutorial is of any help to anyone. Sorry if it’s a bit choppy and/or hard to follow, it is my first.

People, post SDL tutorial here if you ever feel good enough to make one. Just please, keep your information accurate. If there is a problem with mine, please contact me immidiately.

Posted: Sun Jul 10, 2005 6:14 pm
by Arce
Using SDL Image

What is it?
SDL image is a library that allows you to load images with extensions other than .BMP. As I'm sure you know, BMP's are huge in file size. As a programmer, I am sure you are trying to keep your game at as small a file size as possible. Using .PNG's as an alternate to a .BMP is generally a good idea when you want to load a 'rich' image with many colors.

Getting the Image library on your System
To get the Image library on your system, download it from http://www.libsdl.org.

Setting up SDL Imagein MVC++
To begin, we must be sure that our development envirnment can read the Image library folders. To do this, as you may know, you goto the Tools drop down menu, select options, and then click the directories tab. Here, you add and browse both the Include folder and the Lib folder.

Once you tell your envirnment where to locate the includes and libraries for SDL, you must statically link to SDL_image.lib. To do this, goto Project, Settings, Link, and add 'SDL_image.lib' to the list text box under 'Object/Library Modules.'

Then, you must dynamically link to jpeg.dll, libpng1.dll, zlib.dll, sdl_image.dll. To do this, you need to open the 'lib' folder in your Image library directories. Inside, you should find each of these files. Copy them into your project folder, and you are done.

Aight, you have it set up to use. :D

Using SDL Image
To use the Image library, as you may have guessed, you must #include "SDL_Image.h" first. Then, using SDL Image is exactly the same as using SDL_loadBMP, only you call the IMG_Load function rather than SDL_loadBMP.

The IMG_Load function is declared somewhere in the Image.h file, as shown below.

Code: Select all

SDL_Surface *IMG_Load(const char *file);
As you can see, this function takes one argument, which is a string that contains the name/directory of the image you want to load. It returns a pointer to an SDL Surface that now contains the image.

Below is an example of loading and drawing some non-BMP pants. In order to correctly compile the below example, you must have the Image library on your computer and have it set-up correctly. Also, you must have a .gif named "pants" in your project folder. To get a "pants.gif" right click on the below image, and then select 'save target as,' then browse your project folder.
Image

Code: Select all

//SDL_Image Turorial Example

//requires static linkage to:  
//sdl.lib, sdlmain.lib, sdl_image.lib

//requires dynamic linkage to: 
//sdl.dll, sdl_image.dll, zlib.dll, jpeg.dll, libpng1.dll


//include ability to exit program
#include <stdlib.h>

//include SDL stuff
#include "SDL.h"
//include sdl image stuff
#include "SDL_image.h"


//screen dimensions
const int SCREEN_WIDTH=480;
const int SCREEN_HEIGHT=640;

//display surface
SDL_Surface* g_pDisplaySurface = NULL;

//image surface
SDL_Surface* g_pImageSurface = NULL;

//event structure
SDL_Event g_Event;

//main function
int main(int argc, char* argv[]) {
	//initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO)==-1) { //error initializing SDL
		fprintf(stderr,"Could not initialize SDL!\n"); 	//report the error
		exit(1);	//end the program
	}
	else{ 	//SDL initialized
		//report success
		fprintf(stdout,"SDL initialized properly!\n");
		//set up to uninitialize SDL at exit
		atexit(SDL_Quit);
	}

	//create windowed environment
	g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_ANYFORMAT);

	//load the image
	g_pImageSurface=IMG_Load("pants.gif");

	//error check
	if (g_pDisplaySurface == NULL)	{
		//report error
		fprintf(stderr,"Could not set up display surface!\n");
		//exit the program
		exit(1);
	}

	//repeat forever
	for(;;)	{
		//wait for an event
		if(SDL_PollEvent(&g_Event)==0){
			//no event

			//draw the image
			SDL_Rect rc;
			rc.x=rc.y=0;
			rc.w=g_pImageSurface->w;
			rc.h=g_pImageSurface->h;
			SDL_BlitSurface(g_pImageSurface,&rc,g_pDisplaySurface,&rc);

			//update the screen
			SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
		}
		else {
			//event occurred, check for quit
			if(g_Event.type==SDL_QUIT) break;
		}
	}

	//normal termination
	fprintf(stdout,"Terminating normally.\n");

	//return 0
	return(0);
}
Here's the output of the above program:
Image

Posted: Mon Aug 01, 2005 9:09 pm
by Arce
I'll do alpha blending when I have some spare time. I love screwing around with it.

I doubt anyone here has bothered to read one of the tutorials. Oh well, they are still fun to write.

Posted: Fri Feb 22, 2008 11:55 am
by MarauderIIC
Made this post a bit more appropriate image-wise.

Also, for lack of a merge feature (couldn't find any merge mods I liked) :
JS Lemming wrote:There is a bad side to sdl. Its still quite new so there are very few tutorials on it. But I will do some searching and bring home some goodz.

Here are a few:

// Click on Lesson 1 to learn how to set up SDL with Dev C++
http://cone3d.gamedev.net/cgi-bin/index ... xsdl/index

// You can learn how to work with music and such here. But Fmod might be better?
http://kekkai.org/roger/sdl/

// This is a pretty cool effect made with SDL. Check it out.
http://www.libsdl.org/projects/bump/
Original post @
http://www.thechaosrift.com/phpBB2/view ... =2787#2787

Posted: Fri Feb 22, 2008 9:34 pm
by Arce
:guffaw: :guffaw: :guffaw:

Thanks, haven't visited this topic in over a year. Yes, the image is much improved, thanks. =P

BTW, how'd you do that? Did you run the SDL app with a different image, or did you just edit in in paint? Mostly curious as to whether you followed the tut. or not, as nobody actually responded.

8-)

Posted: Fri Feb 22, 2008 9:46 pm
by MarauderIIC
Paint =p I did it at school

Posted: Fri Feb 22, 2008 9:48 pm
by Arce
Herm...Still needa fix that. xD

Posted: Fri Feb 22, 2008 10:31 pm
by MarauderIIC
alldone

Posted: Sun Apr 06, 2008 10:29 am
by sirtom93
On devc++ you just have to go

- tools/packet manager

and then search for SDl or SDL/GL from the devpack server.

Posted: Sun Apr 06, 2008 11:08 am
by Falco Girgis
Thank you, Sirtom. :D

Posted: Sun Apr 06, 2008 2:22 pm
by sirtom93
GyroVorbis wrote:Thank you, Sirtom. :D
No problamo. I was just wandering why with the mingw compiler dl it doesnt have the image header file, see for yourselves. Also (and I probably should know this) but why when the header file is SDL.h
Do I have to type "SDL/SDL.h" and #include<SDL> or #include<SDL> does not work.

:|

Posted: Sun Apr 06, 2008 5:28 pm
by Falco Girgis
Because you don't have the SDL folder set as one of your includes folder. You have the folder above it.

SDL/SDL.h is saying to look in the SDL folder for SDL.h.

If you had set that folder to be an include folder, you could just include SDL.h

Posted: Mon Apr 07, 2008 7:27 am
by sirtom93
GyroVorbis wrote:Because you don't have the SDL folder set as one of your includes folder. You have the folder above it.

SDL/SDL.h is saying to look in the SDL folder for SDL.h.

If you had set that folder to be an include folder, you could just include SDL.h
I see. But how am I supposed to get hold of the sdl image header file?

Posted: Mon Apr 07, 2008 10:27 am
by sirtom93
Dont worry I found it, http://www.libsdl.org/projects/SDL_image

I dont see why they couldnt have just included the SDL_image header files in it aswell, most annoying.

Posted: Mon Apr 07, 2008 10:47 am
by Falco Girgis
It's an additional library created by random programmers. It's not an actual part of SDL, just an addition.