Page 1 of 1

Intro to SDL Loading and optimising bitmaps Rate Topic: ***** 1 Votes

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 11 July 2008 - 10:25 AM

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.

:!: 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:
#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:
#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:Attached File  myBMP.bmp (301.05K)
Number of downloads: 342

Edit: Added a note about main, it has to accept parameters!

Is This A Good Question/Topic? 0
  • +

Replies To: Intro to SDL

#2 Solid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-August 08

Posted 12 October 2008 - 01:56 PM

Nice work I really like how you told WHY you do the different pieces of code.

This post has been edited by Solid: 12 October 2008 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 rooski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-August 09

Posted 22 September 2009 - 07:24 AM

i would really like to see more tutorials for sdl , the ones that are floating around the net are pretty old, and very buggy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1