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

Author Topic: Centralized Input management.  (Read 4177 times)

0 Members and 2 Guests are viewing this topic.

Sidar

  • Newbie
  • *
  • Posts: 14
    • View Profile
Centralized Input management.
« on: June 16, 2012, 06:46:27 pm »
Hi there,

First of all, I would like to thank you guys for providing this library as it really has been motivating me to work more on C++.

I'm working on a Shmup ( side scrolling shoot'em up ). And as I'm working my way up to making a suitable system for my game I'm pretty much stuck on how I should handle the input.
I know there are global methods for input and I could just write the input directly in my player class. However I rather want  my player object to "register" to an input manager instead and let this manager dispatch the event to all my registered object.

The reason for this is that I might want to implement a multiplayer mechanic up to 4 players.

Two things that make it hard for me to come up with a solution:
1:with the sf::Joystick object I can see if a button is pressed, but it also demands that I specify a controller id.

2:With window.pollEvent(...) it would require me to pass the window object to my manager. However I'm not sure if this is a good idea. From what I understood pollEvent pops the event, so other events might get lost if the input manager is popping events that are of no interest.

Basically what I want to do is something like

InputManager.registerJoy(myPlayerObject, joyID);
InputManager.registerKbrd(myPlayerObject);

And let my objects implement an interface/abstract class which are called on event. something like:

void onInputEvent(Event evt){
 //do magic here.
}

Now maybe I'm overlooking something that already exists in this library. But at this point I can't come up with a solution. Does anyone have suggestions?
Any help is appreciated!

Sidar
« Last Edit: June 16, 2012, 06:59:02 pm by Sidar »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Centralized Input management.
« Reply #1 on: June 16, 2012, 07:34:56 pm »
You can take a look at my extension library Thor, it provides an advanced event-handling mechanism that allows you to connect callbacks to different events.

thor::ActionMap<std::string> actions;
actions["forward"] = thor::Action(sf::Keyboard::Up);

thor::ActionMap<std::string>::CallbackSystem callbacks;
callbacks.connect("forward", myMoveForwardFunction);

...

If you are interested, you can take a look at the tutorial.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Sidar

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Centralized Input management.
« Reply #2 on: June 16, 2012, 07:54:43 pm »
Thanks for replying,

I'll check it out. Even though I would have wanted to create such system myself. It's more from a learning point of view. But ill try it nonetheless.

Sidar

edit:

How do I get the equivalent of sf::Joystick::getAxisPosition ?

edit:
Aurora errors. I had to add the compiler options for -std=gnu++0x but then it errors out on me complaining about:
/Bromeon-Thor/extlibs/aurora/include/Aurora/Tools/SafeBool.hpp:64:32: error: 'nullptr' was not declared in this scope

I'm pretty much inexperienced with C++. And I honestly don't feel like chasing these errors down. I guess ill just try and build something myself. Suggestions are still welcome.
« Last Edit: June 16, 2012, 08:32:12 pm by Sidar »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Centralized Input management.
« Reply #3 on: June 16, 2012, 09:01:39 pm »
I'll check it out. Even though I would have wanted to create such system myself. It's more from a learning point of view. But ill try it nonetheless.

It's not that trivial but I wouldn't stand you in the way of trying and learning but if you're still inexperienced try first a few other things and stick with Thor if you need that feature.

How do I get the equivalent of sf::Joystick::getAxisPosition ?
Equivalent to what?

Aurora errors. I had to add the compiler options for -std=gnu++0x but then it errors out on me complaining about:
/Bromeon-Thor/extlibs/aurora/include/Aurora/Tools/SafeBool.hpp:64:32: error: 'nullptr' was not declared in this scope
Yes Aurora/Thor depend on C++11 features like smart pointers. So you probably need to get a newer version of GCC that supports already nullptr.

I'm pretty much inexperienced with C++. And I honestly don't feel like chasing these errors down. I guess ill just try and build something myself. Suggestions are still welcome.
You eventually have to learn to handle such errors because you'll always run into them. But at the beginning it's hard and often time consuming when stickying to it, you will soon understand much more and be able to fix them in no time.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sidar

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Centralized Input management.
« Reply #4 on: June 16, 2012, 09:51:42 pm »
Quote
Equivalent to what?

sf::Joystick::getAxisPosition directly returns either the x or y axis position. I read the tutorial on thor but couldn't see how I would ask the X en Y position. Is it just passing the sf::Joystick::X as the argument?

Quote
Yes Aurora/Thor depend on C++11 features like smart pointers. So you probably need to get a newer version of GCC that supports already nullptr.

I'm aware not everything is yer supported in MingWG 4.5.2?
http://gcc.gnu.org/gcc-4.5/cxx0x_status.html

Could you elaborate on this?

Quote
You eventually have to learn to handle such errors because you'll always run into them. But at the beginning it's hard and often time consuming when stickying to it, you will soon understand much more and be able to fix them in no time.
I'm fully aware of that. But I rather focus on technical details of my game rather than technical details on how the compiler works.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Centralized Input management.
« Reply #5 on: June 16, 2012, 10:07:28 pm »
sf::Joystick::getAxisPosition directly returns either the x or y axis position. I read the tutorial on thor but couldn't see how I would ask the X en Y position. Is it just passing the sf::Joystick::X as the argument?

Ah you're talking about Thor! xD
I'm not that familiar with Thor's action system but it's kinda useless to pass the axis position with the event, since you can always use the static functions of sf::Joystick i.e. getAxisPosition() and Thor can provide the specific joystick id from which you can get the axis position. Maybe I got that also all wrong since I've never used programed for joysticks... ;)


I'm aware not everything is yer supported in MingWG 4.5.2?
http://gcc.gnu.org/gcc-4.5/cxx0x_status.html

Could you elaborate on this?
If you look under Null pointer constant you'll see that it's not supported for GCC 4.5, but never versions do. ;)
« Last Edit: June 16, 2012, 10:09:02 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Centralized Input management.
« Reply #6 on: June 16, 2012, 10:07:54 pm »
I read the tutorial on thor but couldn't see how I would ask the X en Y position.
Still with sf::Joystick::getAxisPosition(). Thor's event module provides mainly actions, that is changes of input states (like mouse/keyboard pressed etc). When you directly want to know the current state, just ask SFML.

I'm aware not everything is yer supported in MingWG 4.5.2?
As far as I know, nullptr was implemented in g++ 4.6.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Sidar

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Centralized Input management.
« Reply #7 on: June 16, 2012, 10:38:56 pm »
Thanks for the replies.
I'm trying to update to the latest MingW compiler, but I still need to get familiar with CodeLite.

But i think I found a solution that works for my game:
Let a player object implement an interface IJoyEventListener with the method : onJoyEvent(JoyEvent event)
Where JoyEvent is a struct with all the gamepad data.

And then I do something like:


for(size_t i=0; i<controllerID.size(); i++)
{
                               
        event.axisX = sf::Joystick::getAxisPosition(i,sf::Joystick::X);
        event.axisY = sf::Joystick::getAxisPosition(i,sf::Joystick::Y);
                               
        event.buttonA = sf::Joystick::isButtonPressed(i,st::input::A);
        event.buttonB = sf::Joystick::isButtonPressed(i,st::input::B);
        event.buttonX = sf::Joystick::isButtonPressed(i,st::input::X);
        event.buttonY = sf::Joystick::isButtonPressed(i,st::input::Y);
        event.buttonStart = sf::Joystick::isButtonPressed(i,st::input::START);
        event.buttonSelect = sf::Joystick::isButtonPressed(i,st::input::SELECT);
                               
                                //etc etc
                               
        controllerID.at(i)->onJoyEvent(event);
                               
}
 

Not the most elegant way. But it's a start.
I will probably overhaul all of this in the end anyways.

Edit:

Well after updating to the latest mingw 4.7.x i can't run my project anymore.
Had to add -static-libstdc++ to my linker options all of a sudden. And it crashes on creating a window. But doesn't specify why.

Stack call:
0  0x691c2811  sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)   
1  0x00401514  main  C:\\Users\\Sidar\\Documents\\Sidar\\C++\\ST\\Shmup\\main.cpp  98
« Last Edit: June 17, 2012, 01:56:12 am by Sidar »