Full Version: Intro to SDL
Dream.In.Code > Programming Tutorials > C++ Tutorials
gabehabe
Introduction to SDL
Drawing to the screen

If you take a look at my "Setting up Code::Blocks to work with SDL" tutorial, I have already covered drawing a pixel to the screen. Sooo I'm gonna go ahead and show you how to load and optimise a bitmap.

exclamation.gif Note:
When writing an SDL program, it is vital that you remember that main must accept parameters, as follows:
int main (int argc, char *argv[]) or int main (int argc, char **argv)
both of which do the same thing. Remember this! I can't stress enough how important it is... if you don't have args for main, you will get the following error:
undefined reference to winMain@16

Here is #1, which will load a bitmap, without optimising:
cpp
#include <SDL/SDL.h> /* Standard SDL include */

const int SCREEN_WIDTH = 640; /* Set the screen width variable */
const int SCREEN_HEIGHT = 480; /* Set the screen height variable */
const int SCREEN_DEPTH = 32; /* Set the screen depth variable */

int main(int argc, char *argv[]) { /* In SDL, all programs 'main' function MUST accept these parameters */
SDL_Surface *screen = NULL; /* The surface on which we will load the screen */
SDL_Surface *bitmap = NULL; /* The surface on which we will load the bitmap */
SDL_Rect offset; /* The rectangle (target area) to which we will load the bitmap */
/* Remember, when we create a pointer, it's always good practice to assign them to NULL */

/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO);
/* Alternatively, we could use SDL_Init (SDL_INIT_EVERYTHING);
* but, we only need video in this application, so we'll just initialise the video.
* Makes sense, right? */

/* Initialise the screen, by passing the screen width, height, depth, and surface type */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
/* Two of the common surface types are:
* SDL_SWSURFACE - which will load the main screen surface as a window
* SDL_FULLSCREEN - which will load the application in full screen */

/* Now to set up the offset variables, where we will draw the bitmap to */
offset.x = 0;
offset.y = 0;
offset.h = SCREEN_HEIGHT;
offset.w = SCREEN_WIDTH;

/* Now to witness the supreme power of SDL when you want to load an image... ONE LINE! */
bitmap = SDL_LoadBMP ("myBMP.bmp");

/* Now we need to apply the bitmap to the screen... the syntax is:
* SDL_BlitSurface (SDL_Surface*, SDL_Rect, SDL_Surface*, SDL_Rect*) */
SDL_BlitSurface (bitmap, NULL, screen, &offset);

/* update the screen (aka double buffering) */
SDL_Flip(screen);
while(true); /* Infinite loop, just to keep the window open here */
}

Now, when we want to optimise the surface, we need to make a call to one of my snippets, which can be found here

Add that function in, and change bitmap = SDL_LoadBMP ("myBMP.bmp"); to bitmap = Load_Optimal("myBMP.bmp");

The final code should look like this:
cpp
#include <SDL/SDL.h> /* Standard SDL include */

const int SCREEN_WIDTH = 640; /* Set the screen width variable */
const int SCREEN_HEIGHT = 480; /* Set the screen height variable */
const int SCREEN_DEPTH = 32; /* Set the screen depth variable */

SDL_Surface *Load_Optimal (char *filename); /* Function prototype, to return an optimised image */

int main(int argc, char *argv[]) { /* In SDL, all programs 'main' function MUST accept these parameters */
SDL_Surface *screen = NULL; /* The surface on which we will load the screen */
SDL_Surface *bitmap = NULL; /* The surface on which we will load the bitmap */
SDL_Rect offset; /* The rectangle (target area) to which we will load the bitmap */
/* Remember, when we create a pointer, it's always good practice to assign them to NULL */

/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO);
/* Alternatively, we could use SDL_Init (SDL_INIT_EVERYTHING);
* but, we only need video in this application, so we'll just initialise the video.
* Makes sense, right? */

/* Initialise the screen, by passing the screen width, height, depth, and surface type */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
/* Two of the common surface types are:
* SDL_SWSURFACE - which will load the main screen surface as a window
* SDL_FULLSCREEN - which will load the application in full screen */

/* Now to set up the offset variables, where we will draw the bitmap to */
offset.x = 0;
offset.y = 0;
offset.h = SCREEN_HEIGHT;
offset.w = SCREEN_WIDTH;

/* Now to witness the supreme power of SDL when you want to load an image... ONE LINE! */
bitmap = Load_Optimal ("myBMP.bmp");

/* Now we need to apply the bitmap to the screen... the syntax is:
* SDL_BlitSurface (SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*) */
SDL_BlitSurface (bitmap, NULL, screen, &offset);

/* update the screen (aka double buffering) */
SDL_Flip(screen);
while(true); /* Infinite loop, just to keep the window open here */
}

SDL_Surface *Load_Optimal (char *filename)
{
SDL_Surface *loaded = NULL; /* The surface to load the basic image */
SDL_Surface *optimal = NULL; /* The optimised surface */
loaded = SDL_LoadBMP (filename); /* Load the basic image */
if (loaded != NULL) /* This is why NULL is important, now we can check if it was loaded successfully */
{
optimal = SDL_DisplayFormat (loaded); /* Load an optimised version of the basic surface */
SDL_FreeSurface (loaded); /* Delete the basic one, it's no longer needed */
}
return optimal; /* Return the optimised image */
)

Attached is the bitmap that I loaded in the sample:Click to view attachment

Edit: Added a note about main, it has to accept parameters!
Solid
Nice work I really like how you told WHY you do the different pieces of code.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.