So from what I understand (please correct me if I'm wrong) in SMFL 2.0 you should use sf::Event::KeyPressed and sf::Event::KeyReleased when dealing with single key presses and releases, but if you want continuous input you should use the sf::Keyboard::isKeyPressed function. So in my input manager class I have done so for a single key press:
InputManager.h
class InputManager
{
public:
static bool IsKeyPressed(const sf::Keyboard::Key key);
static sf::Event event;
};
InputManager.cpp
sf::Event InputManager::event;
bool InputManager::IsKeyPressed(const sf::Keyboard::Key key)
{
return event.type == sf::Event::KeyPressed && event.key.code == key ? true : false;
}
The static event member is being updated in a game loop labeled "here" like so:
StateManager.cpp
void StateManager::GameLoop()
{
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
InputManager::event = event; //here
//states[0]->Update(); //here 2
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
for(size_t i = 0; i < states.size(); i++)
{
states[i]->Update();
window.clear();
states[i]->Draw(window);
window.display();
}
}
}
Then anywhere I wan't to use input I can put this inside a state:
void SomeState::Update()
{
if(InputManager::IsKeyPressed(sf::Keyboard::Key::Space))
{
//...
}
}
The problem is that this doesn't work, the InputManager::IsKeyPressed function never returns true. However if I uncomment the code you saw above labeled "here 2" then the function executes fine. I don't know why it doesn't work updating the state after the poll event loop and I was hoping you guys could explain why. Another problem with this method is that once the event object leaves the poll event loop and there are no more events to be polled then the event object will never be resigned so the InputManager::IsKeyPressed function will return what ever it was previously.
I don't think updating the states inside the poll event loop is efficient so what is the best way to structure this game loop to fix the problems I'm having? Also why doesn't the event object work outside the poll event loop?
Thanks.