I want to write an input-manager to implement some functions: isKeyReleased etc
My idea was to have a function called update, this function polls for events and "saves" them.
Then later I can call isKeyReleased to check the event:
Update function:
void InputManager::update(sf::RenderWindow& mainWindow)
{
rWindow = &mainWindow;
while (rWindow->pollEvent(iEvent))
{
}
}
and then I want for example check the event type:
bool InputManager::isEventType(sf::Event::EventType eventType)
{
if (iEvent.type == eventType)
return true;
else
return false;
}
So in my HandleInput function in main:
InputManager::update(mainWindow);
if (InputManager::isEventType(sf::Event::KeyPressed))
std::cout << "Key Pressed" << std::endl;
But it is not working! I don't know where the problem is.
Is there a possibility to "save" the polled events and check them later?
You should be able to save sf::Event variables just like any other variable. Is iEvent a member variable of InputManager?
while (rWindow->pollEvent(iEvent))
{
}
This is a while loop, meaning it is going to go through the whole queue of events and continuously overwrite your iEvent variable until it runs out of events to process. If there is more than one event in the queue then you will only end up saving the last one. If you are wanting to save all of your events then you should push them onto a vector inside of the while loop and save that vector instead of just the one event.
bool InputManager::isEventType(sf::Event::EventType eventType)
{
if (iEvent.type == eventType)
return true;
else
return false;
}
is equivalent to
bool InputManager::isEventType(sf::Event::EventType eventType)
{
return iEvent.type == eventType;
}