Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,772 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,464 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Intro to SDL

 
Reply to this topicStart new topic

> Intro to SDL, Loading and optimising bitmaps

gabehabe
Group Icon



post 11 Jul, 2008 - 10:25 AM
Post #1


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:Attached File  myBMP.bmp ( 301.05k ) Number of downloads: 12


Edit: Added a note about main, it has to accept parameters!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/21/08 03:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month