Sunday, 7 February 2010

Simple DirectMedia Layer

I thought today I'd try and do a blog post with a little bit more codey stuff in it as opposed to the high brow design malarky I've been prattling on about for the last few weeks.

What I've decided to talk about is Simple DirectMedia Layer (SDL). It is a fantastic set of multimedia libraries primarily for C++ that allow you to access an array of subsystems on your computer such as graphics, sound, event handling, input, threads and timers (among other things). You can find the libraries here: SDL.

The whole object of the library is to be as 'simple' (clues in the name) to use as possible, and in my opinion they do just that. SDL supports an array of operating systems so your code will be portable. As well as that there are extension libraries such as sdl_net and sdl_mixer for networking and sound respectively, with even more following those.

These libraries serve as a great alternative to using Windows and DirectX interfaces when on a Windows PC. That isn't to say these aren't a good choice, but SDL does remove a large amount of the complexity of windows programming, and lets you set up a window and start getting stuff working very quickly. If Windows is your development platform all that code will still be there, it is just SDL kindly hides some of it away from you through interfaces which makes some of the boring tasks of window creation a lot easier to deal with, especially for the beginner.

Here is some example code for setting up a window (this won't compile but you get the idea)

// Your handle to the pixels on the screen
SDL_Surface* screen;

 //Initialise SDL

  if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
 {
     return false;
 }

// Set up the surface for drawing to
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//Set the name displayed in the top of the window
 SDL_WM_SetCaption( "SDL Rocks!", NULL );

That pretty much gives you your window, that's all you need! Pretty neat right?

The event handling is also really useful, the calls look like this:

// Pointer to an event in SDL
SDL_Event* event

if( event->type == SDL_KEYDOWN)
{
         //Checks to see if the event was a being pressed;
         if( event->key.keysym.sym == SDLK_a );
}

This is only the briefest of brief introductions, I just really want to make you aware of SDL if you haven't already heard of it. There is a great book with lots of information on it called 'Focus on SDL' by Ernest Pazera which I strongly recommend if you are interested in learning more about SDL. There is also a wealth of information on the SDL website and on sites like GameProgrammingWiki which you can find links to on my blog.

I am using SDL as the base for my tower defence game and while I've had my ups and down with it I have found it to be a great set of libraries to work with.

Have fun and let me know what you think.

No comments: