Tutorial 1: Setting up SDL

Posted by Astryl on Nov. 29, 2012, 6:33 a.m.

I've been thinking of doing a blog series on Game Development for a while now, though originally I wanted to do so on Tumblr. But quite frankly, every time I think of posting something on Tumblr, another part of me thinks it's just not worth the effort. 64Digits wins hands down, mostly because I find it easier to use. Faster too.

This is going to be an in-depth series, all about low level game development. Personally, I began at this level, using compilers such as Turbo Pascal and DJGPP, and thus working in a DOS environment.

I wanted to write a series of tutorials that take you from the low level 'In video mode, what now?' up to some basic 3D software rendering.

A lot of modern development kits and APIs create an abstraction layer around the primitive tools available to us. Allegro comes with ready-made line drawing functions… but how are lines drawn?

Game Maker handles all sorts of collision detection for us, but what is the science behind it?

You may ask as well, "And what is the point of learning this?". Answer: Because you never know when you'll have to test for an intersection between two arbitrary line segments and calculate the exact reflection vector for the intersection, use a raytrace to test for a collision or LOS, or create your own draw_line function. Because programming is like that: Stuff happens.

Anyway, let us begin with part one….

SDL, and getting it set up

I had two options here; either use a library, or go back to DOS. And while getting a DOS window into mode 13h, and blitting your first sprite is very satisfying, it's also time consuming and constitutes knowledge that's nice to know, but won't be very useful in most cases… Unless we suffer through a Nuclear Armageddon that magically destroys all trace of the modern operating system leaving us with nothing but DOS 5. This is not going to happen.

So I decided on SDL, for a few reasons:

> It's readily available

> It's easy to set up

> It's cross-platform

> It's about as low-level as you can go without descending into DOS

I'm using Code::Blocks. It's free, cross-platform and stable. What more can you ask for?

As for compiler, I'm currently running GCC 4.4.0 on Windows, and GCC 4.4.3 on Linux.

We'll be focusing on the Windows setup. Most Linux users don't have a problem getting

libraries and setting 'em up.

First things first, get the latest SDL development package here.

Unzip this somewhere. I put it in C:\Dev\SDL.

Our next job is to set Code::Blocks up to use it, and to create a template project so

we don't have to do it again.

I opt against using the built in SDL project template. Personal reasons, feel free to try

using it, but no guarantees that it'll work.

Creating a template project that uses SDL

Open Code::Blocks, and start a new Empty Project.

Create a new source file, and call it main.cpp.

Put the following into it:

#include <cstdlib>
#include <cmath.h>
#include <SDL.h>

int main(int argc, char** argv)
{
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
        return -1;

    atexit(SDL_Quit);
	
	bool keys[256];
	for(int i = 0; i < 256; i++)keys[ i ] = false;
    
    SDL_Surface* screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE|SDL_DOUBLEBUF);
    if(!screen)
        return -2;

    bool done = false;
    while(!done)
    {
        SDL_Event event;
        while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					done = true;
					break;
					
				case SDL_KEYDOWN:
					keys[event.key.keysym.sym] = true;
				break;
				
				case SDL_KEYUP:
					keys[event.key.keysym.sym] = false;
				break;
			}
			
			// Allows you to press Escape to exit
			if(keys[SDLK_ESCAPE])done = true;
			
			// All drawing takes place here ----------
			SDL_FillRect(screen, 0, SDL_MapRGB(screen->format,0,0,0));
			
			// Put your code here
			
			SDL_Delay(10);
			SDL_Flip(screen);
			// End drawing ----
		}
    }
	
	return 0;
}

Copy and paste this if you want, or type it out. Don't worry about the specifics of the code, if you're not familiar with SDL. We're not aiming to learn all about SDL here, we just want to

play around with low level graphics a bit. SDL can wait for another tutorial.

Of course, this code won't compile yet. We haven't told the compiler where to find the SDL libraries, nor the headers. Let's do that now.

Click on the Project menu and then on Build Options…

In the window that appears, notice the tree on the left. The project name will be the root, and under it will be the 'Release' and 'Debug' branches. Select the parent root (In my case, SDLProject)

and then click on the 'Search Directories' tab.

In the Compiler sub-page, we want to add the SDL include directory. Click on 'Add', and navigat to <wherever you put the SDL folder>/include, and press OK. On my system, the following appears:

C:\Dev\SDL\include

Now, switch to the Linker sub-page, and add the SDL\lib folder.

The compiler can now find the libraries and headers, but we still need to tell it what ones to include.

Switch to the 'Linker Settings' tab. You can choose to either manually include the requires libraries now, or, as I do, use the "Other linker options" box to type them in quickly.

Copy and paste the following into the "Other linker options" box:

-lmingw32
-lSDLmain
-lSDL.dll
-luser32
-lgdi32
-lwinmm
-ldxguid

Finally, we need to copy SDL.dll from the SDL\bin directory to the project directory. Do this, then open the Project menu, click on "Add Files…" and select the DLL (The one you copied to the project

directory, not the one that's in the SDL directory). This will make the IDE automatically include the DLL in any projects you create using this template.

Now that all is well, hit CTRL-F9 with a vengeance and watch as your masterpiece compiles. Or should. Throw bricks at me if it doesn't and I'll try to step you through it.

If it compiles successfully, run the project and you should get two blank windows. Hit escape and the program will exit.

As a finishing touch, save the project, then go to File->Save Project as template…

And that's it. You now have a custom template you can use by going to File->New->From Template and selecting this project.

Next time

Pixels, axis aligned lines and rectangles. Also: Death rays…

Jujedit: Fixed a formatting problem caused by an i in square brackets.

Comments

Pirate-rob 11 years, 12 months ago

Quote: Mega
Answer: Because you never know when you'll have to test for an intersection between two arbitrary line segments and calculate the exact reflection vector for the intersection, use a raytrace to test for a collision or LOS, or create your own draw_line function. Because programming is like that: Stuff happens.
… You lost me…

Toast 11 years, 12 months ago

Someone should start compiling a list of all the useful programming/art/music tutorial blogs on 64digits. Someone…

Astryl 11 years, 12 months ago

Someone should add a facility to add Tutorials as a separate media type… We're all just too lazy…

Toast 11 years, 12 months ago

I still vote for Stevenup's system of blog tags, eg. "a platformer game", "a silly story", "a programming tutorial", etc.

Then you can just filter the blog search and find everything in whatever category you want.

eagly likes this.

Astryl 11 years, 12 months ago

That would work too.