Using Windows
First off, what is SDL?
SDL stands for Simple Direct-Media Layer.
As the name suggests, it is quite a simple library to use! Good to know for graphics beginners
What do I need to download?
* The reason that I want you to download it with the MinGW compiler is that I can give you the specific paths. If you want to, you can install MinGW seperately, but it's just easier to explain if you get it with
Once you have downloaded Code::Blocks, install it to the default directory: C:\Program Files\CodeBlocks
Then, and this is the important bit, you need to copy the SDL files into the correct places.
- Copy the contents of \SDL-1.2.13\include\ to C:\Program Files\CodeBlocks\MinGW\include\
- Copy the contents of \SDL-1.2.13\lib\ to C:\Program Files\CodeBlocks\MinGW\lib\
And there we have it! But we're not done yet... unfortunately. We still need to set up the build options in Code::Blocks, so fire up the IDE.
Now go to File>New>Project and select Console application (Don't worry, we are doing graphics, this is just a generic thing)
Call the project "New SDL Project" (you'll see why we give it such a generic name later on)
Copy \SDL-1.2.13\bin\SDL.dll to the project folder. (You need to do this for each project) ** See note at the end of this tutorial
Now go to Project>Build Options and make sure that New SDL Project is selected in the tree menu on the left, this is so that it is set for both Debug and Release compile options.
Now, click the Linker Settings tab, and click the add button. Copy and paste this exactly as it is:
C:\Program Files\CodeBlocks\MinGW\lib\libmingw32.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDL.dll.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDLmain.a;
All that we're doing here is setting up our build options to build our program with the SDL stuff in it.
Now, click OK, and OK again, to return to the main window of Code::Blocks.
Nearly done!
Now go to Settings>Compiler & Debugger and click the Linker Settings tab (again) and copy and paste this exactly as it is (again) :
C:\Program Files\CodeBlocks\MinGW\lib\libmingw32.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDL.dll.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDLmain.a;
Click OK and OK again, and you're back to the code window.
Just to test that it all works, try this code:
cpp
#include <SDL/SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_DEPTH = 32;
int main(int argc, char *argv[]) {
SDL_Surface *screen;
Uint8 *p;
int x = 10; //x coordinate of our pixel
int y = 20; //y coordinate of our pixel
/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO);
/* Initialize the screen / window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
/* Make p point to the place we want to draw the pixel */
p = (Uint8 *)screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
/* Draw the pixel! */
*p=0xff;
/* update the screen (aka double buffering) */
SDL_Flip(screen);
while(true);
}
Now we're getting somewhere! Hit F9 to compile and run this, and you should get a window with a pixel drawn somewhere in the top left corner.
WOOHOO! YOUR FIRST SDL PROJECT!
OK, now that we know it works, go to File>Save project as user-template and choose a descriptive name for it. (This is why we called the project "New SDL Project")
Now, if you go to File>New>From user template you should see your project template. Select it, and you should have a duplicate of the project that we just created. From now on, you can select this whenever you want to create an SDL application! Nifty, huh? BUT: Don't forget to copy SDL.dll to your new project!
A useful note:
You can copy SDL.dll to your system32 directory, BUT your programs will rely on it being there... so, if you run it on another computer that doesn't have SDL.dll already installed, then your program won't work. This is why I recommend that you copy your drivers to your project each time
Another note:
Wherever your .exe program is, SDL.dll needs to be in the exact same directory. Remember this, it's important!
Good luck with SDL coding!
Edit: Fixed a typo... thanks to Locke37 for pointing it out