SDL Version is 1.2.5 as of writing this tutorial.
In this tutorial we will walk through a basic sdl program and show you how to create the screen, load an image and draw it to the screen. We will also show how to add user input through the keyboard as well!
The first 5 lines of code are taken from our very first tutorial...setting up SDL. These are for Windows users, and include the SDL libraries. The next 3 lines include our stdlib and our SDL libraries. If you have not done so already, install SDL_image the same way you installed SDL in our first tutorial. TRUE and FALSE are declared for convenience.
/* -- Include the precompiled libraries -- */ #ifdef WIN32 #pragma comment(lib, "SDL.lib") #pragma comment(lib, "SDLmain.lib") #pragma comment(lib, "SDL_image.lib") #endif #include <stdlib.h> #include "SDL.h" #include "SDL_Image.h" #define TRUE 1 #define FALSE 0
Main MUST be defined this way or SDL will not compile correctly. Our two SDL_Surfaces are our screen which we draw pixels to, and our picture where we load our image into. Our SDL_Event keeps track of user input. pictureLocation is an SDL_Rect, a struct that defines a rectangle by the x position, y position, as well as the width and height. In SDL (0,0) starts from the upper left corner of the window. Our integers are used to store wether a key is being pressed. Finally, SDL_VideoInfo is what SDL uses to tell us about our screen.
int main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_Surface *picture;
SDL_Event event;
SDL_Rect pictureLocation;
int leftPressed, rightPressed, upPressed, downPressed;
const SDL_VideoInfo* videoinfo;
The function atexit() calls SDL_Quit when exit() is called. This resets videomodes and cleans up after SDL before you exit. Then we call SDL_Init() with the flag SDL_INIT_VIDEO to initalize basic SDL with video. It has basic error handling so if your computer doesn't work for some reason it will exit.
atexit(SDL_Quit);
/* Initialize the SDL library */
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
Next, we take our screen and declare a videomode and renderig method. The first two parameters set the resolution in pixels. The next declares the depth in bits. Finally, we ask for double buffered hardware surface. SDL may not be able to give us this but even if it can't, it will emulate it all in software.
screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
We use SDL_GetVideoInfo() to find out what bit depth we actually set. We could optionally find out more from the videoinfo structure as well. The structure contains lots of useful information on the videosurface. Next we load the picture into memory. IMG_Load is a function from SDL_image, and while SDL can load BMPS SDL_image supports many image formats. We check for errors, and make sure the image was loaded.
videoinfo = SDL_GetVideoInfo();
printf("%i", videoinfo->blit_hw);
// Load Picture
picture = IMG_Load("SDL_now.bmp");
if (picture == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", "SDL_now.bmp", SDL_GetError());
return 0;
}
In this section we initalize default values for our state variables and the location of the image.
pictureLocation.x = 0; pictureLocation.y = 0; leftPressed = FALSE; rightPressed = FALSE; upPressed = FALSE; downPressed = FALSE;
This is our main, infinite loop. The if statements update our picture location depending on what keys are pressed as long as it is inside the window.
while(1) {
if (leftPressed) {
if (pictureLocation.x > 0)
pictureLocation.x--;
}
if (rightPressed) {
if (pictureLocation.x < 640-picture->w)
pictureLocation.x++;
}
if (upPressed) {
if (pictureLocation.y > 0) {
pictureLocation.y--;
}
}
if (downPressed) {
if (pictureLocation.y < 480-picture->h) {
pictureLocation.y++;
}
}
Some of our most important code. SDL_FillRect fills our screen with the color blue (the value 1000). This covers up whatever was drawn last to the screen. This is a very simple way of doing this. The next function, SDL_BlitSurface copies one surface to another using an SDL_Rect to determine the location. picture is what is being copied, NULL is the the section of it which is being copied. Sending null sends the whole surface. The third variable, screen, is what we are drawing to. The fourth is our SDL_Rect which is the location on the screen where our first surface is going.
SDL_FillRect(screen, NULL, 1000); SDL_BlitSurface(picture, NULL, screen, &pictureLocation); SDL_Flip(screen);
This next huge chunk of code is what manages our key variables. SDL_Pollevent gets the current events from the system, and we use that to see what has changed from our last loop. We have a switch statement which sets if a key has been pressed or depressed. This event system handles multiple keypresses. Our system is also is somewhat velocity based, where while the key is down the figure will continue moving in that direction. A reference for every key definition (i.e. SDLK_LEFT for the left key) is available at http://sdldoc.csn.ul.ie/sdlkey.php
if( SDL_PollEvent( &event ) ){
/* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
switch( event.type ){
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_LEFT:
leftPressed = TRUE;
break;
case SDLK_RIGHT:
rightPressed = TRUE;
break;
case SDLK_DOWN:
downPressed = TRUE;
break;
case SDLK_UP:
upPressed = TRUE;
break;
case SDLK_ESCAPE:
exit(0);
default:
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_LEFT:
leftPressed = FALSE;
break;
case SDLK_RIGHT:
rightPressed = FALSE;
break;
case SDLK_DOWN:
downPressed = FALSE;
break;
case SDLK_UP:
upPressed = FALSE;
break;
default:
break;
}
break;
default:
break;
}
}
}
Every program has an exit!
return 0; }
Congratulations! You can now build this code, and run it. You can use the source listed at the beginning of our page and the SDL Now! image we used also...courtesy of the libsdl home page. While this was simple, hopefully its enough to get you started.
Back to the Purdue Game Development Club