Code:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *background = NULL;
SDL_Surface *board = NULL;
SDL_Surface *circle = NULL;
SDL_Surface *x = NULL;
SDL_Surface *title = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *arrowKey = NULL;
SDL_Surface *upMessage = NULL;
SDL_Surface *downMessage = NULL;
SDL_Surface *leftMessage = NULL;
SDL_Surface *rightMessage = NULL;
SDL_Event event;
TTF_Font *font = NULL;
SDL_Color textColor = {255, 255, 255};
SDL_Surface *load_image(string filename)
{
//Temporary storage for the image that's loaded.
SDL_Surface *loadedImage = NULL;
//The optimized image that will be used.
SDL_Surface *optimizedImage = NULL;
//Load the image.
loadedImage = IMG_Load(filename.c_str());
//If image loaded
if(loadedImage != NULL)
{
//Create an optimized image.
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old image.
SDL_FreeSurface(loadedImage);
/*
if(optimizedImage != NULL)
{
//Map the color key
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF, 0xFF);
//Set all pixels of that color to be transparent.
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
}
*/
}
return optimizedImage;
}
void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
//Make a temporary rectange to hold the offsets.
SDL_Rect offset;
//Give the offsets to the rectangle.
offset.x = x;
offset.y = y;
//Blit the surface.
SDL_BlitSurface(source, NULL, destination, &offset);
}
bool init()
{
//Initialize all SDL subsystems.
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//Set up screen.
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
//If there was an error.
if(screen == NULL)
{
return 1;
}
//Initialize SDL_ttf
if(TTF_Init() == -1 )
{
return false;
}
//Set caption.
SDL_WM_SetCaption("Event Test", NULL);
//If everything initialized fine.
return true;
}
bool load_files()
{
//Load the images.
background = load_image("blueBackground.png");
board = load_image("tBoard.png");
x = load_image("xMark.png");
circle = load_image("oMark.png");
//Open the font.
font = TTF_OpenFont("arial.ttf", 24);
if(font == NULL)
{
return false;
}
//If there was an error.
if(background == NULL)
{
return false;
}
//If everything loaded fine.
return true;
}
void clean_up()
{
//Free the surfaces.
SDL_FreeSurface(background);
SDL_FreeSurface(board);
SDL_FreeSurface(x);
SDL_FreeSurface(circle);
//Quit SDL.
SDL_Quit();
}
int main(int argc, char *args[])
{
//Make sure program waits for a quit.
bool quit = false;
//Initialize.
if(init() == false)
{
return 1;
}
//Load the files.
if(load_files() == false)
{
return 1;
}
//Render text.
title = TTF_RenderText_Solid(font, "This is the Title.", textColor);
upMessage = TTF_RenderText_Solid(font, "The up button was pressed.", textColor);
downMessage = TTF_RenderText_Solid(font, "The down button was pressed." , textColor);
leftMessage = TTF_RenderText_Solid(font, "The left button was pressed." , textColor);
rightMessage = TTF_RenderText_Solid(font, "The right button was pressed." , textColor);
if(title == NULL)
{
return 1;
}
ApplySurface(0,0,background, screen);
ApplySurface(500,25,title,screen);
//Update screen.
if(SDL_Flip(screen) == -1)
{
return 1;
}
while(quit == false)
{
//While there is an event to handle.
while(SDL_PollEvent(&event))
{
//If a key was pressed.
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: arrowKey = upMessage;
break;
case SDLK_DOWN: arrowKey = downMessage;
break;
case SDLK_LEFT: arrowKey = leftMessage;
break;
case SDLK_RIGHT: arrowKey = rightMessage;
break;
}
}
//If the user has Xed out of the window.
else if(event.type == SDL_QUIT)
{
//Quit the program.
quit = true;
}
}
//If a message needs to be displayed.
if(arrowKey != NULL)
{
ApplySurface(0,0,background, screen);
ApplySurface((SCREEN_WIDTH - arrowKey->w)/2, (SCREEN_HEIGHT - arrowKey->h)/2, arrowKey, screen);
arrowKey = NULL;
}
if(SDL_Flip(screen) == -1)
{
return 1;
}
}
clean_up();
return 0;
}
The title is displayed when the program is first started. Then, when I press a key, a new message is displayed. It appears as if the new message, along with a new copy of the background, are being placed over the title and original background. So, each time a key is pressed, a new copy of the background and message are applied. Is this the correct way to do this? It seems after a long time, this would be very inefficient. I was thinking whenever I would want to display a new background or message, that I would replace the old with the new. But, i'm not sure how I would do this here.