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

Author Topic: Check events outside event-loop  (Read 1234 times)

0 Members and 1 Guest are viewing this topic.

klaus1n3

  • Newbie
  • *
  • Posts: 1
    • View Profile
Check events outside event-loop
« 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?
« Last Edit: February 18, 2016, 11:20:19 pm by klaus1n3 »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Check events outside event-loop
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Check events outside event-loop
« Reply #2 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.
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: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Check events outside event-loop
« Reply #3 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;
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: