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

Author Topic: Help! I'm an idiot (and new) pollEvent()  (Read 4879 times)

0 Members and 1 Guest are viewing this topic.

Scotchdew

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Help! I'm an idiot (and new) pollEvent()
« on: April 16, 2016, 09:10:49 pm »
Hi, guys. I'm new and a terribly slow learner...learning to program...c++. Great combination. I get hung up on stuff like why functions are given the names they are. For example in the following code I understand everything until I get to where we access the "pollEvent" function.

int main()
{
   sf::Window window;
   window.create(sf::VideoMode(600, 600), "Window");

   while (window.isOpen())
   {
       sf::Event event;
       while (window.pollEvent(event))
       {
           if (event.type == sf::Event::Closed)
            window.close();
       }
   }

}

Being an idiot, and a visual learner, I think of the window object accessing a function called pollEvent. Now when I think of pollEvent, I think of an event where someone is voting...that doesn't make sense to me, so then I research the definition of poll and other than voting, it can be described as a noun being "a head". I am such a dumbass. This doesn't make any sense to me. Can someone explain to me in layman terms why pollEvent is called what it is so I can remember and understand exactly what it does? Thanks guys.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Help! I'm an idiot (and new) pollEvent()
« Reply #1 on: April 16, 2016, 09:47:05 pm »
Sfml has an event queue when you call
window.pollEvent();
you take one
sf::Event
from queue and process it, then you usually repeat using while it until queue gets empty, after this goes something to related to event polling.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Help! I'm an idiot (and new) pollEvent()
« Reply #2 on: April 16, 2016, 10:06:51 pm »
agree, pollEvent doesn't make sense but it seems being widely used even SDL has pollEvent too. in order to understand it better i prefer waitEvent() over pollEvent() since they are identical of each other and you can use it like this:

while (window.isOpen())
{
        sf::Event event;
        while (window.waitEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        window.close();
        }
}

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Help! I'm an idiot (and new) pollEvent()
« Reply #3 on: April 16, 2016, 10:16:00 pm »
Thing is waitEvent and pollEvent are not the same. waitEvent is blocking whereas pollEvent is not, hence waitEvent is usually used if you have a thread dedicated to event handling. And I don't see why the name wouldn't make sense when you are polling the event queue to see if it has any events, and if it does, get what they are. That seems like a perfectly good use of the word.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Help! I'm an idiot (and new) pollEvent()
« Reply #4 on: April 16, 2016, 10:28:52 pm »
Thing is waitEvent and pollEvent are not the same. waitEvent is blocking whereas pollEvent is not, hence waitEvent is usually used if you have a thread dedicated to event handling.

Apologies, you are right, i had to look to source code and it's indeed different.
« Last Edit: April 16, 2016, 10:32:49 pm by MORTAL »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help! I'm an idiot (and new) pollEvent()
« Reply #5 on: April 16, 2016, 11:36:16 pm »
Polling here is a verb that, in this context, means to test the event queue and see if an event is available. The (wanted) side effects of this function is that it also changes the parameter to the first event in that queue and also removes that event from the queue. However, the actual polling/testing is why the the function returns a boolean. That is, if the polling test is not true (there are no events), no event should be processed whereas otherwise, this should be repeated until no events remain.

According to Google's definition of "poll" (note the "computing" definition), polling means:
Quote
check the status of (a device), especially as part of a repeated cycle.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything