SDL Tutorials

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

SDL Tutorials

Postby Arce on Sat Jul 09, 2005 9:57 pm

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.
Last edited by MarauderIIC on Fri Sep 26, 2008 2:08 pm, edited 1 time in total.
Reason: arce changed account names, changed poster credit
Arce
 

Postby Arce on Sun Jul 10, 2005 7:14 pm

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
Arce
 

Postby Arce on Mon Aug 01, 2005 10:09 pm

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.
Arce
 

Postby MarauderIIC on Fri Feb 22, 2008 1:55 pm

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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
 
Posts: 3430
Joined: Sat Jul 10, 2004 4:05 pm
Location: Maryland, USA

Postby Arce on Fri Feb 22, 2008 11:34 pm

: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-)
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Arce
Chaos Rift God
Chaos Rift God
 
Posts: 2180
Joined: Mon Jul 10, 2006 10:29 pm

Postby MarauderIIC on Fri Feb 22, 2008 11:46 pm

Paint =p I did it at school
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
 
Posts: 3430
Joined: Sat Jul 10, 2004 4:05 pm
Location: Maryland, USA

Postby Arce on Fri Feb 22, 2008 11:48 pm

Herm...Still needa fix that. xD
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Arce
Chaos Rift God
Chaos Rift God
 
Posts: 2180
Joined: Mon Jul 10, 2006 10:29 pm

Postby MarauderIIC on Sat Feb 23, 2008 12:31 am

alldone
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
 
Posts: 3430
Joined: Sat Jul 10, 2004 4:05 pm
Location: Maryland, USA

Postby sirtom93 on Sun Apr 06, 2008 11:29 am

On devc++ you just have to go

- tools/packet manager

and then search for SDl or SDL/GL from the devpack server.
User avatar
sirtom93
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 49
Joined: Sun Dec 09, 2007 11:34 am
Location: England.

Postby Falco Girgis on Sun Apr 06, 2008 12:08 pm

Thank you, Sirtom. :D
"So what happens if the Elysian Shadows Kickstarter fails?"
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
 
Posts: 10693
Joined: Thu May 20, 2004 3:04 pm
Location: Studio Vorbis, AL

Postby sirtom93 on Sun Apr 06, 2008 3:22 pm

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.

:|
User avatar
sirtom93
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 49
Joined: Sun Dec 09, 2007 11:34 am
Location: England.

Postby Falco Girgis on Sun Apr 06, 2008 6:28 pm

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
"So what happens if the Elysian Shadows Kickstarter fails?"
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
 
Posts: 10693
Joined: Thu May 20, 2004 3:04 pm
Location: Studio Vorbis, AL

Postby sirtom93 on Mon Apr 07, 2008 8:27 am

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?
User avatar
sirtom93
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 49
Joined: Sun Dec 09, 2007 11:34 am
Location: England.

Postby sirtom93 on Mon Apr 07, 2008 11:27 am

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.
User avatar
sirtom93
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 49
Joined: Sun Dec 09, 2007 11:34 am
Location: England.

Postby Falco Girgis on Mon Apr 07, 2008 11:47 am

It's an additional library created by random programmers. It's not an actual part of SDL, just an addition.
"So what happens if the Elysian Shadows Kickstarter fails?"
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
 
Posts: 10693
Joined: Thu May 20, 2004 3:04 pm
Location: Studio Vorbis, AL

Next

Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron