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

Author Topic: Loop in pollEvent  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Loop in pollEvent
« 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?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Loop in pollEvent
« Reply #1 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Loop in pollEvent
« Reply #2 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?
Laurent Gomila - SFML developer

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Loop in pollEvent
« Reply #3 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[]
                                        }
                        }
                       
                }
« Last Edit: March 22, 2015, 11:20:11 am by marianexyx »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Loop in pollEvent
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Loop in pollEvent
« Reply #5 on: March 22, 2015, 11:32:59 am »
Got it.