SFML community forums

Help => General => Topic started by: klaus1n3 on February 18, 2016, 11:17:30 pm

Title: Check events outside event-loop
Post by: klaus1n3 on February 18, 2016, 11:17:30 pm
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?
Title: Re: Check events outside event-loop
Post by: Arcade on February 19, 2016, 12:48:23 am
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.
Title: Re: Check events outside event-loop
Post by: eXpl0it3r on February 19, 2016, 03:30:48 am
If you want to process an event it needs to happen in the event loop. As Arcade said, you can pass it around like a normal variable in the event loop.
If you need some specific information from an event for a later point, you should store that separately.
Title: Re: Check events outside event-loop
Post by: Nexus on February 20, 2016, 11:55:27 am
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;
}