SFML community forums

Help => Window => Topic started by: 6Peppered9 on March 12, 2019, 04:39:30 am

Title: application generates multiple key release event
Post by: 6Peppered9 on March 12, 2019, 04:39:30 am
hello,

I'm dealing with the sfml events and have a problem that my application generates several KeyReleased events.

My pollEvent loop

while(rootWindow->pollEvent(event)) {
    for(auto it : drawStack) {
        if(it->eventable) {
            it->handleEvent(event);
        }
    }

 

drawStack is a stack of pointers of objects that I want to draw on the screen.
The polled event should be forwarded to the respective objects, which then decide what happens.

handleEvent

void handleEvent(sf:: Event) {
    switch(event.type) {
        case sf::Event::KeyReleased:
            cout << "obj 1 KeyReleased" << endl;
            break;
    }
}

 

everything works as long as there is only one object on the stack.
But with two objects on the stack I get such an output:
obj 1 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
...
 

This is what i want:
obj 1 KeyReleased
obj 2 KeyReleased
 

is my procedure right?
Is there another way to do it?
Or did I not understand sf :: Event?
Title: Re: application generates multiple key release event
Post by: eXpl0it3r on March 12, 2019, 07:34:25 am
You get one event through polling, but then you handle the same event for each object and since they all use the same code, you'll get the same message printed n times.
Title: Re: application generates multiple key release event
Post by: 6Peppered9 on March 12, 2019, 08:05:28 am
no, every object have another KeyRelease function.
I can assign a different function to each object via function pointer.

But I tried something and saved the objects in a fixed array, so I can access the objects via an index and it works.
no idea why it does not work by iterator

i had used this class for drawStack:
#include <stack>
#include <deque>

template<typename T, typename Container = std::deque<T>>
class IStack : public std::stack<T, Container>
{
    using std::stack<T, Container>::c;

        public:
        // expose just the iterators of the underlying container
        auto begin() {return std::begin(c);}
        auto end() {return std::end(c);}

        auto begin() const {return std::begin(c);}
        auto end() const {return std::end(c);}
};
 
https://stackoverflow.com/questions/525365/does-stdstack-expose-iterators (https://stackoverflow.com/questions/525365/does-stdstack-expose-iterators)
Title: Re: application generates multiple key release event
Post by: eXpl0it3r on March 12, 2019, 08:13:12 am
I'm sure we could figure it out if you provide a minimal but complete example. ;)