SFML community forums

Help => Graphics => Topic started by: coding99 on May 31, 2012, 05:54:37 pm

Title: [solved] how to detect mouse if over a character?
Post by: coding99 on May 31, 2012, 05:54:37 pm
(http://uploadpic.org/storage/2011/thumb_WHQjVaXkbHVtKNJiS1gBLHK.png) (http://uploadpic.org/v.php?img=S8V3Q6Egzx)

this is my image(png format).
at center of image is my character(red color) and have a transparency background.

how to detect if mouse over my character(red color) not include over transparency background.

ps.sorry for my poor english.
Title: Re: how to detect mouse if over a character?
Post by: Laurent on May 31, 2012, 06:12:26 pm
Which version of SFML?
Title: Re: how to detect mouse if over a character?
Post by: coding99 on May 31, 2012, 06:22:29 pm
version 1.6
Title: Re: how to detect mouse if over a character?
Post by: eXpl0it3r on May 31, 2012, 06:37:03 pm
Get the pixel under the mouse position and check the alpha channel of that pixel.
Title: Re: [solved] how to detect mouse if over a character?
Post by: coding99 on May 31, 2012, 07:00:18 pm
thank you  :)
Title: Re: [solved] how to detect mouse if over a character?
Post by: vro on June 03, 2012, 06:26:00 am
What would the answer have been for sfml 2.0?
I'm sure I could be doing it better than the way I currently am.
Title: Re: [solved] how to detect mouse if over a character?
Post by: Laurent on June 03, 2012, 08:55:39 am
With SFML 2 you would have to store a "collision map" (a 2D array of booleans), created after you load the texture, because you can't easily read from a sf::Texture.
Title: Re: [solved] how to detect mouse if over a character?
Post by: vro on June 03, 2012, 09:45:23 am
The (likely worse) way that I was doing is just checking if mouse x and y lied between the min and max x and y values of the sprite's rectangle... is that pretty bad or is it alright?
Title: Re: [solved] how to detect mouse if over a character?
Post by: Laurent on June 03, 2012, 10:41:57 am
It's not good ar bad, it depends on what you want. If an approximate detection using the sprite's rectangle is all that you need then it's perfect, if you need a more precise detection then you'll have to play with the alpha channel of the image.