Page 1 of 1

SDL C++ Problem

Posted: Wed Mar 03, 2010 9:48 am
by Sp3ke
Aye, well after little experience with SDL I've encountered a small problem.

Basically, I have a drop-down menu and when you click on the menu the drop-down list will appear. However, when you try to click away from the drop down menu to close it, the drop down menu should disappear. It does change back to it's orgininal image, however the old image is still located on the screen. Any ideas?

This is the input function : -

Code: Select all

void System::Handle_Input(SDL_Event event)
{
	SDL_Flip(Buffer);
	int x = 0;
	int y = 0;
	bool test = true;

	if(event.type == SDL_MOUSEMOTION)
	{
		x = event.motion.x;
		y = event.motion.y;

		if((x > CoordsSaveButton.x) && (x < CoordsSaveButton.x + CoordsSaveButton.w) && (y > CoordsSaveButton.y) && (y < CoordsSaveButton.y + CoordsSaveButton.h))
		{
			SaveMenuStatus = SaveMenuSelected;
		}
		else
		{
			SaveMenuStatus = SaveMenu;
		}

		if((x > CoordsLoadButton.x) && (x < CoordsLoadButton.x + CoordsLoadButton.w) && (y > CoordsLoadButton.y) && (y < CoordsLoadButton.y + CoordsLoadButton.h))
		{
			LoadMenuStatus = LoadMenuSelected;
		}
		else
		{
			LoadMenuStatus = LoadMenu;
		}
	}
	
	if(event.type == SDL_MOUSEBUTTONDOWN)
	{
		if(event.button.button == SDL_BUTTON_LEFT)
		{
			x = event.button.x;
			y = event.button.y;

			if((x > CoordsTileButton.x) && (x < CoordsTileButton.x + CoordsTileButton.w) && (y > CoordsTileButton.y) && (y < CoordsTileButton.y + CoordsTileButton.h))
			{
				TileMenuStatus = TileMenuSelected;
				
			}
			else
			{
				TileMenuStatus = TileMenu;
			}
		}
	}
}

This function will draw everything to the screen : -

Code: Select all


void System::show()
{
	if(SaveMenuStatus == SaveMenu)
	{
		Apply_Image(500,110, SaveMap, Buffer);
	}
	else if (SaveMenuStatus == SaveMenuSelected)
	{
		Apply_Image(500,110, SaveSelected, Buffer);
	}
	if (LoadMenuStatus == LoadMenu)
	{
		Apply_Image(500,80, LoadMap, Buffer);
	}
	else if (LoadMenuStatus == LoadMenuSelected)
	{
		Apply_Image(500,80, LoadSelected, Buffer);
	}
	if (TileMenuStatus == TileMenu)
	{
		Apply_Image(500,15, MenuSelection, Buffer);

	}
	else if (TileMenuStatus == TileMenuSelected)
	{
		Apply_Image(500,15, MenuSelection1 , Buffer);
	}

	SDL_Flip(Buffer);
}

Re: SDL C++ Problem

Posted: Wed Mar 03, 2010 11:00 am
by Bakkon
Why do you have SDL_Flip at the beginning of your Handle Input function? Typically you only make that call once per tick.

Re: SDL C++ Problem

Posted: Wed Mar 03, 2010 1:49 pm
by Sp3ke
Aye sorry, forgot to remove that was just testing around with something earlier.

Re: SDL C++ Problem

Posted: Wed Mar 03, 2010 2:53 pm
by dephbokks
Is your show() function all the render code?

Basically, what I got from your description is that when you initiate a drop down menu it displays, but when you close it out it still appears. Is this the correct interpretation?

In any case, the buffer is just like a giant chalkboard. When you write something to that surface, it will persist until you erase it. So what you want to do is to either blit some background image before any rendering for each frame or just blit a solid color to 'erase' this surface and redraw it each frame. You are currently not doing this.

Try and put

Code: Select all

SDL_FillRect(Buffer, NULL, SDL_MapRGB( Buffer->format, 0 , 0, 0 ) );
at the top of your show() function so that it looks like:

Code: Select all

void System::show()
{
   SDL_FillRect(Buffer, NULL, SDL_MapRGB( Buffer->format, 0 , 0, 0 ) );

   if(SaveMenuStatus == SaveMenu)
   {
      Apply_Image(500,110, SaveMap, Buffer);
   }
   else if (SaveMenuStatus == SaveMenuSelected)
   {
      Apply_Image(500,110, SaveSelected, Buffer);
   }
   if (LoadMenuStatus == LoadMenu)
   {
      Apply_Image(500,80, LoadMap, Buffer);
   }
   else if (LoadMenuStatus == LoadMenuSelected)
   {
      Apply_Image(500,80, LoadSelected, Buffer);
   }
   if (TileMenuStatus == TileMenu)
   {
      Apply_Image(500,15, MenuSelection, Buffer);

   }
   else if (TileMenuStatus == TileMenuSelected)
   {
      Apply_Image(500,15, MenuSelection1 , Buffer);
   }

   SDL_Flip(Buffer);
}
That will just 'clear' the screen to black before any rendering on the current frame. This will overwrite any previous invalid image displays, like the drop-down in your example, when the user closes 'em out. Of course this background color can be any color you want or you can use a background image if you want.

Try this and see if it works.

Re: SDL C++ Problem

Posted: Thu Mar 04, 2010 12:40 am
by Sp3ke
Thanks a bunch, it worked fine. MUCH MUCH MUCH appreciated! :worship: