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
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 (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-actions.html).
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