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?
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)