Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Clicking On Items  (Read 2653 times)

0 Members and 1 Guest are viewing this topic.

noodlesgc

  • Guest
[SOLVED] Clicking On Items
« 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 )

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
[SOLVED] Clicking On Items
« Reply #1 on: June 27, 2009, 05:20:40 pm »
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.

heishe

  • Full Member
  • ***
  • Posts: 121
    • View Profile
[SOLVED] Clicking On Items
« Reply #2 on: June 27, 2009, 06:08:23 pm »
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.

noodlesgc

  • Guest
[SOLVED] Clicking On Items
« Reply #3 on: June 27, 2009, 06:16:30 pm »
Thanks for the replies. I guess I'll just continue the way that I'm doing it now.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED] Clicking On Items
« Reply #4 on: June 28, 2009, 01:00:25 am »
Quote from: "heishe"
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).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

noodlesgc

  • Guest
[SOLVED] Clicking On Items
« Reply #5 on: June 28, 2009, 06:10:26 pm »
Quote from: "Nexus"
Quote from: "heishe"
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

 

anything