The code:
Code:
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
//Screen diameters
const int SCREEN_WIDTH = 720;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The event stucture that will be used
SDL_Event event;
//The font thats's going to be used
TTF_Font *font = NULL;
// the color of the text
SDL_Color textColor = {255, 255, 255};
//the portions of the sprite map to be blitted
SDL_Rect clip [ 4 ];
//The images
SDL_Surface* EAB = NULL;
SDL_Surface* Background = NULL;
SDL_Surface* TSS = NULL;
SDL_Surface* screen = NULL;
SDL_Surface* Message = NULL;
//image loading function
SDL_Surface *load_image( std::string filename)
{
//temporary storage for the image thats loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load Image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if ( loadedImage != NULL )
{
//create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//return the optimezed image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Hold the offsets
SDL_Rect offset;
//Giving the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface ( source, clip, destination, &offset );
}
bool init()
{
//initialize all SDL subsystems
if (SDL_Init ( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen= SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//if there was an error setting up the screen
if (screen == NULL)
{
return false;
}
//Initialize SDL_ttf
if ( TTF_Init() == -1 )
{
return false;
}
//Set window caption
SDL_WM_SetCaption( "Nekro Mezuka", NULL );
//if everything initizlized fine
return true;
}
bool load_files()
{
//Load the images
Background = load_image ( "Background.bmp" );
EAB = load_image( "EAB.png" );
TSS = load_image( "TSS.png" );
//IF NOTHING WENT WRONG WHEN LOADING THE IMAGE
if( Background == NULL )
{
return false;
}
//IF NOTHING WENT WRONG WHEN LOADING THE IMAGE
if( EAB == NULL )
{
return false;
}
// open the font
font = TTF_OpenFont( "lazy.ttf", 28 ) ;
// if there was an error loading the font
if ( font == NULL )
{
return false;
}
return true;
}
void clean_up()
{
//Remove loaded image
SDL_FreeSurface( EAB );
SDL_FreeSurface( Background);
SDL_FreeSurface( TSS );
SDL_FreeSurface( Message );
//Close the font that was used
TTF_CloseFont( font );
TTF_Quit();
//Quit SDL
SDL_Quit();
}
int main ( int argc, char* args[] )
{
//quit flag
bool quit = false;
//initialize
if( init() == false )
{
return 1;
}
//Load the files
if ( load_files() == false )
{
return 1;
}
//Clip for the top left
clip[ 0 ].x = 0;
clip[ 0 ].y = 0;
clip[ 0 ].w = 100;
clip[ 0 ].h = 100;
//Clip for the top right
clip [ 1 ].x = 100;
clip [ 1 ].y = 0;
clip [ 1 ].w = 100;
clip [ 1 ].h = 100;
//Clip for the bottom left
clip[ 2 ].x = 0;
clip[ 2 ].y = 100;
clip[ 2 ].w = 100;
clip[ 2 ].h = 100;
//Clip for the bottom right
clip[ 3 ].x = 100;
clip[ 3 ].y = 100;
clip[ 3 ].w = 100;
clip[ 3 ].h = 100;
//render the text
Message = TTF_RenderText_Solid( font, "Please feel free to eat as many bags as you would like", textColor );
//if there was an error rendering the text
if (Message == NULL)
{
return 1;
}
//Apply the background to the screen
apply_surface( 0, 0, Background, screen);
//Apply EAB to the screen
apply_surface( 230, 130, EAB, screen);
//Appply The test sprite sheet
apply_surface(0, 0, TSS, screen, &clip [ 0 ] ) ;
apply_surface(620, 0, TSS, screen, &clip [ 1 ] ) ;
apply_surface(0, 380, TSS, screen, &clip [ 2 ] ) ;
apply_surface(620, 380, TSS, screen, &clip [ 3 ] ) ;
//Update Screen
if (SDL_Flip( screen ) == -1)
{
return 1;
}
//while user hasnt quit
while (quit == false )
{
//while theres an event to handle
while ( SDL_PollEvent( &event ) )
{
// if the user has Xed out
if( event.type == SDL_QUIT )
{
//quit the game
quit = true;
}
}
}
//free up and quit
clean_up();
return 0;
}
/Not sure how copying and pasting code into code tags, as described in the sticky post entitled "Posting Code? Read This First!", is difficult.