This is part 1 of a series of SDL game programming tutorials that I am going to release each week if possible. In this part I will cover the basics of getting SDL set up, writing an SDL hello world and the basics of a game.
What is SDL?
SDL is a cross-platform multimedia library designed to provide low-level access to audio, keyboard, mouse and joysticks. SDL also gives you access to 3D hardware via OpenGL.
One of the best things about SDL is that it is cross-platform which means you can write code for Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX.
In this tutorial we are going to focus on writing a few classes to create the basis for all of your 2D games, I am going to use C++ and lots of OOP concepts.
Setting up SDL
This can be very different for each individual OS or IDE so I will just give you a link here which should get you all started http://lazyfoo.net/S...son01/index.php.
SDL Hello World
Here is a basic SDL program that loads a window then draws a bitmap to it. Use this to test you have set up SDL properly.
#include "SDL.h" // include SDL
int main(int argc, char *argv[])
{
// the screen we will draw to.
SDL_Surface *screen;
// the surface to draw the bitmap on.
SDL_Surface *bmp;
// area to draw the bitmap to.
SDL_Rect targetarea;
// initialize SDL. // I use everything as it will load video as well.
SDL_Init(SDL_INIT_EVERYTHING);
/*
set up the screen pass in screen
width,height,bpp and set SDL to software
rendering
*/
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
// load a bitmap.
bmp = SDL_LoadBMP("test.bmp"); /
targetarea.x = 10; // target x
targetarea.y = 20; // target y
targetarea.w = bmp->w; // target width
targetarea.h = bmp->h; // target height
// Draw the bitmap to the target area
SDL_BlitSurface(bmp, NULL, screen, &targetarea);
// show the bitmap // double buffering
SDL_Flip(screen);
while(1);
}
Get a bitmap and name it whatever you like, make sure to change my test.bmp to the filename you have.
Hopefully you should now have a development environment for making an SDL program.
The basis of a game
There are things that every game has, some are for making it more fun and some are integral to the running of the game.
Every game program has a main loop, this can be something like this
// Main Loop Update Draw Check for Collisions
This is quite a simple loop, checking for collisions could also be implemented into the update function.
So thats the main loop, along with this future tutorials will deal with writing an object creation and management class, writing a sound manager, dealing with collisions, drawing tile based maps. I might also go for writing a game object factory tutorial for you guys if the demand is there.
Thanks for reading, the next tutorial will be here very soon.





MultiQuote





|