1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <sdl/sdl.h>
#include <string>
//Define constant variables, such as screen dimensions, and bpp.
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 32;
//Define all surfaces
SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;
//Function used to load images and convert them from 24 bit to 32 bit
SDL_Surface *load_image(std::string filename)
{
SDL_Surface* loaded_image = NULL;
SDL_Surface* opt_image = NULL;
loaded_image = SDL_LoadBMP(filename.c_str());
if (loaded_image != NULL)
{
opt_image = SDL_DisplayFormat(loaded_image);
SDL_FreeSurface(loaded_image);
}
return opt_image;
}
//Get the x/y coordinates of the image and blit them to the correct surface.
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
int main(int argc, char* args[])
{
//Start SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1);
{
return 1;
}
//Draw the window
screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);
if (screen == NULL)
{
return 1;
}
//Window caption
SDL_WM_SetCaption("<caption here>", NULL);
//Load the background image
SDL_Surface* background = SDL_LoadBMP("C:/resources/background_index/background.bmp");
//apply the background image to the appropriate surface
apply_surface(0, 0, background, screen);
//Render the screen
if (SDL_Flip(screen) == -1)
{
return 1;
}
//Wait...
SDL_Delay(2000);
//Clear the background surface
SDL_FreeSurface(background);
//Clear the screen and quit SDL
SDL_Quit();
return 0;
}
|