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

Author Topic: How should inputs be handled properly on a game?  (Read 1376 times)

0 Members and 1 Guest are viewing this topic.

Artfloriani

  • Newbie
  • *
  • Posts: 6
    • View Profile
How should inputs be handled properly on a game?
« on: June 22, 2013, 07:41:38 pm »
Hello,

I'm making my first game and I until now I have been handling inputs (keyboard and mouse) in each class separately. So in my Sprite Editor class I have a method called "handler" which check the inputs and make actions depending on what are them:

void SpriteEditor::handler(sf::Vector2i mousePosition)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
         //Code here

    }
}
 

(the mouse position is sent to handler as I don't want to send the sf::RenderWindow to that method.)

But when I use the mouse in another class I also treat it in the same way as above. Of course I take care so that both classes wouldn't be in conflict using the same input at the same time.

The way I'm using is working, but something inside me tells that it is the worst way of doing it. How should I properly handle inputs with SFML?

Thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
Re: How should inputs be handled properly on a game?
« Reply #1 on: June 23, 2013, 01:32:12 pm »
It really depends on your overall layout, which we don't know, so we won't be able to help you in detail.

If you're Editor is using events to determine the mouse position, then you'll have to think about how to dispatch it to all the other components. I'd immediately think of a message bus design (see Tank's article on this), but it might as well be overkill.
On the other side you can simply use the sf::Mouse class. Since it provides static functions, you can call them where ever they are needed.

Again it depends on the overall design and in many cases you should with the solution you can think of at this point and only refactor it, if you run into a better solution or you get some problems with the current design. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Artfloriani

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How should inputs be handled properly on a game?
« Reply #2 on: June 23, 2013, 04:38:18 pm »
Thank you,

I tought it could be somthing everyone does in a way I didn't think of.

 

anything