Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

SOLVED! [SDL] - Why wont my surface blit?

This topic is 3500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
It seems like you blitted the text to the background but didnt blit the background to the screen. If you update a surface you have to re-blit it on screen for the change to be visible; previously blitted surfaces are not 'bound' to the screen.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!