SFML community forums

Help => General => Topic started by: marianexyx on March 22, 2015, 10:51:53 am

Title: Loop in pollEvent
Post by: marianexyx on March 22, 2015, 10:51:53 am
I've got someting like this:

while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed || event.key.code == Keyboard::Escape)
                        {
                                window.close();
                        }
                        else if (option[1].getGlobalBounds().contains(mouse) &&
                                eventtype == Event::MouseButtonPressed && event.key.code == Mouse::Left)
                        {
                                //many functions based on matrix used in option[]
                        }
                        else if (option[2].getGlobalBounds().contains(mouse) &&
                                event.type == Event::MouseButtonPressed && event.key.code == Mouse::Left)
                        {
                                //many functions based on matrix used in option[]
                        }
                        //else if (option[3]... else if (option[4]... else if (option[5] (...)
                }

Can I use some kind of for loop in window.pollEvent?
Title: Re: Loop in pollEvent
Post by: zsbzsb on March 22, 2015, 10:55:48 am
Can I use some kind of for loop in window.pollEvent?

Do you mean polling events with a for loop? No, the way you are doing it is the correct way.

if (event.type == Event::Closed || event.key.code == Keyboard::Escape)

And this is wrong, you need to ensure the event is a keyboard event before checking the key.
Title: Re: Loop in pollEvent
Post by: Laurent on March 22, 2015, 11:04:24 am
Quote
Can I use some kind of for loop in window.pollEvent?
This is a really strange question. What are you trying to do/solve?
Title: Re: Loop in pollEvent
Post by: marianexyx on March 22, 2015, 11:15:56 am
if (event.type == Event::Closed || event.key.code == Keyboard::Escape)

Quote
And this is wrong, you need to ensure the event is a keyboard event before checking the key.

Thanks! You've found my main bug. Didn't know why the window crash, when cursor was near left side of screen.

And yep, I meant polling events with a for loop. Thought I can make my code little more beautiful w/o making event for all almost the same options.

edit: If it's still incomprehensible, I wanted to do something like this (I know it won't work):
while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed || event.key.code == Keyboard::Escape)
                        {
                                window.close();
                        }
                        for (int i=0; i<6; i++)
                        {
                                else if (option[i].getGlobalBounds().contains(mouse) &&
                                eventtype == Event::MouseButtonPressed && event.key.code == Mouse::Left)
                                        {
                                                //many functions based on matrix used in option[]
                                        }
                        }
                       
                }
Title: AW: Loop in pollEvent
Post by: eXpl0it3r on March 22, 2015, 11:25:32 am
You of course can loop inside another loop. This is C++.

However you shouldn't put your game logic into the event loop.
If you need the state you can use a variable or use sf::Mouse and alike.
Title: Re: Loop in pollEvent
Post by: marianexyx on March 22, 2015, 11:32:59 am
Got it.