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

Author Topic: [SOLVED] sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick  (Read 3336 times)

0 Members and 1 Guest are viewing this topic.

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
[SOLVED] sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick
« on: September 18, 2014, 10:11:11 am »
Hey guys,

I'm using an instance of my GameObject class holding an instance of an InputComponent class, whichs checks sf::Keyboard/sf::Mouse/sf::Joystick to detect user input. So the input component can trigger further actions of the GameObject on each case. But I'm not sure whether this is the right solution for me.

Using this "oldschool" way (means: querying the entire input set on each frame) might cause additional overhead because of checking the entire set on each frame ^^ This seems not so great to me in case of detecting whether the key was pressed/released in this frame, because I need to do this on my self. This isn't a problem to me, but it might be not necessary at all, because SFML provides an event-driven API using sf::Event.

But pulling all events and calling all GameObject's InputComponent's handle()-method might also be not so clever, because each GameObject will be addressed on each event - even if the object would ignore it completly. But (but but but xD) ... but introducing some more intelligent pattern - like registering an object for a limited set of events - seems to be architectural overhead.

Am I wrong in any of this cases? Well, I'm not sure what's the right way for me.. And because this seems an architectural problem I'm gonna solve it now, before writing further input code using "the wrong" design.

Any suggestions? :)

Kind regards
Glocke
« Last Edit: January 26, 2015, 08:28:10 pm by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick
« Reply #1 on: September 18, 2014, 10:25:12 am »
One way to handle input, is in an abstract form to "register" some form of call-back functions. One then only has to wait for an event to be triggered and iterate over the registered call-back functions.

Another option would be to pass down an event object and implement the needed checks in the object itself.

As for overhead/performance it really isn't that important, you'll most likely never notice a difference. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: AW: sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick
« Reply #2 on: September 19, 2014, 01:20:20 pm »
As for overhead/performance it really isn't that important, you'll most likely never notice a difference. ;)

Meanwhile, I implemented my own input structure inside my InputComponent by using sf::Keyboard and elapsedTime (as sf::Time given to the update() method), so I can detect whether a key is held - and how long it is held. At least for the moment this works and seems to be the simplest way to satisfy my input needs :D I don't want to add too much complexity at the moment.

Well, KISS seems to be a great principle also for game dev ;D
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick
« Reply #3 on: September 20, 2014, 06:52:45 pm »
If you don't want to reinvent the wheel, you could have a look at Thor.Actions. It provides uniform mappings of events and real-time input to actions, logical combinations of actions and callbacks.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: sf::Event vs. sf::Keyboard/sf::Mouse/sf::Joystick
« Reply #4 on: September 21, 2014, 08:06:29 am »
If you don't want to reinvent the wheel, you could have a look at Thor.Actions. It provides uniform mappings of events and real-time input to actions, logical combinations of actions and callbacks.

Well, I just built something like this (but just a bit smaller). If I gonna rewrite the input controls to more complex situations, I'll check Thor!

Thanks :)
Current project: Racod's Lair - Rogue-inspired Coop Action RPG