SFML community forums
Help => General => Topic started by: noodlesgc on June 27, 2009, 03:33:59 am
-
I am trying to create a window that is full of Sprites.
The user will have to click on the sprites. As far as I can tell, the only possible way to do this is check for a Event.MouseButtonPressed, and then loop through the objects, and determine if the click was on top of the object.
Is there a better way to do this? Ideally there would be a way to create a function for each sprite e.g. OnMouseButtonPressed for each Sprite.
Thanks for any input.
(also first post! :D )
-
It works the same either way.... if you give the object a "Collision Method" the container will still need to iterate through the list of entities.
-
if you build a gui around it, it will be nothing more than cycling through every sprite, only that it looks more pretty. so no, there's no other way.
-
Thanks for the replies. I guess I'll just continue the way that I'm doing it now.
-
so no, there's no other way.
Yes, there is.
@ noodlesgc:
Instead of iterating the full container, which requires linear complexity, you could store the sprites inside an associative container like std::set. Sorting criterion might be the position (for example first x, then y coordinate). Like this, you only have O(log(n)) to determine the right sprite. If the sprites are even aligned in a rectangular wire, you can calculate the corresponding sprite subscript by its position in O(1).
-
so no, there's no other way.
Yes, there is.
@ noodlesgc:
Instead of iterating the full container, which requires linear complexity, you could store the sprites inside an associative container like std::set. Sorting criterion might be the position (for example first x, then y coordinate). Like this, you only have O(log(n)) to determine the right sprite. If the sprites are even aligned in a rectangular wire, you can calculate the corresponding sprite subscript by its position in O(1).
That's a good idea, I never would have thought of that.
Thanks