SOLVED: SDL: Mouse button events not behaving as expected

Started by
1 comment, last by matrisking 14 years, 8 months ago
I've been following Lazy Foo's (excellent) tutorials and ran into a snag on lesson 9, which deals with mouse driven events: http://lazyfoo.net/SDL_tutorials/lesson09/index.php (Foo's sample code is available on the bottom of the screen). The idea is that there is a button on the screen that will change images depending on if the mouse is over or out of the image, as well as if the mousebutton is down or up while the mouse coordinates are within the bounds of the button image. Mouseover and mouseout work fine. However, when I click on the button, the "Mouse Up" image displays instead of the "Mouse Down" Here are the relevant chunks of code: main: Calls the button object's event handler

//MAIN
int main(int argc, char* args[]) 
{
	//Make sure the program waits for a quit
	bool quit = false;

	//Initialize
	if (init() == false) 
	{
		return 1;
	}

	//Load the files
	if (load_files() == false)
	{
		return 1;
	}

	if (buttonSheet == NULL)
	{
		return 1;
	}

	//Clip the sprite sheet
	set_clips();

	//Make the button
	Button myButton(170, 120, 320, 240);

	//Run until user closes the window
	while (quit == false)
	{
		//If there's an event to handle
		if (SDL_PollEvent(&event))
		{
			//Handle button events
			myButton.handle_events();

			//If user X's out the window...
			if (event.type == SDL_QUIT)
			{
				quit = true;
			}
		}

		//Fill the screen white
		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF) );

		myButton.show();

		//Update the screen
		if (SDL_Flip(screen) == -1)
		{
			return 1;
		}
	}

	//Clean up
	clean_up();

	return 0;
}


Button::handle_events(): Event handler for the button class

void Button::handle_events()
{
	//Mouse offsets
	int x = 0;
	int y = 0;

	//If the mouse moved
	if (event.type == SDL_MOUSEMOTION)
	{
		//Get the mouse offsets
		x = event.motion.x;
		y = event.motion.y;

		//If the mouse is over the button
		if ( (x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h) )
		{
			//Set the button sprite
			clip = &clips[CLIP_MOUSEOVER];
		}
		//otherwise, mouse is off the button
		else
		{
			//Set the button sprite
			clip = &clips[CLIP_MOUSEOUT];
		}
	}
	//If a mouse button was pressed
	if (event.type = SDL_MOUSEBUTTONDOWN)
	{
		//If the left button was pressed
		if (event.button.button == SDL_BUTTON_LEFT)
		{
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the button
			if ( (x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h) )
			{
				//Set the button sprite
				clip = &clips[CLIP_MOUSEDOWN];
			}
		}
	}
	//If a mouse button was released
	if (event.type = SDL_MOUSEBUTTONUP)
	{
		//If the left button was released
		if (event.button.button == SDL_BUTTON_LEFT)
		{
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the button
			if ( (x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h) )
			{
				//Set the button sprite
				clip = &clips[CLIP_MOUSEUP];
			}
		}
	}
}



I'm assuming I'm doing something simple wrong. I did doublecheck to ensure that the clips are mapped properly. Any thoughts? Thanks! EDIT: The SDL_QUIT event that was previously invoked by clicking the "X" in the title bar no longer works either. Have to close it with task manager. [Edited by - matrisking on August 17, 2009 8:06:18 PM]
Advertisement
Note the difference between the = and == operators in C++, and make sure you're using the right one in the right place :)
ugh, the assignment operator will be the end of me. I knew it'd be something simple... thanks a ton!

This topic is closed to new replies.

Advertisement