I'm playing around with button presses in SDL and I'm ripping my hair out trying to figure out what I am doing wrong.
I have the following code:
//Headers
#include "Library.h"
int main(int argc, char* args[])
{
bool quit = false;
Graphics myGraphics; //create the object
Graphics *graphics; //create a variable pointer to store the address to the object
graphics = &myGraphics;
//Load the font
TTF_Font *font = TTF_OpenFont("fonts\\myFont.ttf",28);
SDL_Color textColor = {0,0,0};
//Load the images
message = NULL;
background = graphics->load_image("images\\background.bmp");
upMessage = TTF_RenderText_Solid(font, "UP", textColor);
downMessage = TTF_RenderText_Solid(font, "DOWN", textColor);
leftMessage = TTF_RenderText_Solid(font, "LEFT", textColor);
rightMessage = TTF_RenderText_Solid(font, "RIGHT", textColor);
graphics->apply_surface(0,0,background,screen);
SDL_Flip(screen);
while(quit == false)
{
if(SDL_PollEvent(&myEvent))
{
//If a key was pressed
if(myEvent.type == SDL_KEYDOWN)
{
//Set the correct message surface
switch(myEvent.key.keysym.sym)
{
case SDLK_UP: message = upMessage; break;
case SDLK_DOWN: message = downMessage; break;
case SDLK_LEFT: message = leftMessage; break;
case SDLK_RIGHT: message = rightMessage; break;
default:;
}
}
//If the program was "X"'d out
else if(myEvent.type == SDL_QUIT)
{
//Quit the program
quit = true;
}
}
if(message != NULL)
{
//Apply the images to the screen
graphics->apply_surface(0,0,message,background);
SDL_Flip(screen);
message = NULL;
}
}
//Free the surfaces
SDL_FreeSurface(message);
SDL_FreeSurface(background);
//Quit SDL
SDL_Quit();
return 0;
}
When I press a button, I see no text what so ever. Now, when I change the code (youll see where I change in the comments) it works, but I don't understand why it wont work the way I have it above.
//Headers
#include "Library.h"
int main(int argc, char* args[])
{
bool quit = false;
Graphics myGraphics; //create the object
Graphics *graphics; //create a variable pointer to store the address to the object
graphics = &myGraphics;
//Load the font
TTF_Font *font = TTF_OpenFont("fonts\\myFont.ttf",28);
SDL_Color textColor = {0,0,0};
//Load the images
message = NULL;
background = graphics->load_image("images\\background.bmp");
upMessage = TTF_RenderText_Solid(font, "UP", textColor);
downMessage = TTF_RenderText_Solid(font, "DOWN", textColor);
leftMessage = TTF_RenderText_Solid(font, "LEFT", textColor);
rightMessage = TTF_RenderText_Solid(font, "RIGHT", textColor);
graphics->apply_surface(0,0,background,screen);
SDL_Flip(screen);
while(quit == false)
{
if(SDL_PollEvent(&myEvent))
{
//If a key was pressed
if(myEvent.type == SDL_KEYDOWN)
{
//Set the correct message surface
switch(myEvent.key.keysym.sym)
{
case SDLK_UP: message = upMessage; break;
case SDLK_DOWN: message = downMessage; break;
case SDLK_LEFT: message = leftMessage; break;
case SDLK_RIGHT: message = rightMessage; break;
default:;
}
}
//If the program was "X"'d out
else if(myEvent.type == SDL_QUIT)
{
//Quit the program
quit = true;
}
}
if(message != NULL)
{
//Apply the images to the screen
graphics->apply_surface(0,0,message,background);
graphics->apply_surface(0,0,background,screen); //<---HERE IS THE LINE I ADDED
SDL_Flip(screen);
message = NULL;
}
}
//Free the surfaces
SDL_FreeSurface(message);
SDL_FreeSurface(background);
//Quit SDL
SDL_Quit();
return 0;
}
What happens is in the first block of code, the background appears like its supposed to, but after I blit the message and flip the screen nothing shows up.
In the second block, all I do is blit the background again and the messages show up. Anyone have any idea why?
Basically all I'm trying to do is render a background, then blit to it during button presses.
[Edited by - Chrono1081 on April 19, 2009 8:30:07 AM]