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

Author Topic: Event polling; different implementations  (Read 4973 times)

0 Members and 1 Guest are viewing this topic.

complexity

  • Guest
Event polling; different implementations
« on: August 07, 2013, 11:19:53 am »
What's the reason of using while loops upon event polls? Why I can't use an if statement instead?

Event polling w/ a while loop:

#include <SFML/Graphics.hpp>

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

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


Event polling w/o a while loop:

#include <SFML/Graphics.hpp>

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

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

I tested both, and I haven't encountered any complications. (Why I came up w/ this question: many of you including Laurent - the SFML creator - are using while loops for implementing the poll. Furthermore, one while loop is running, so why put another one inside?).

PS: The SFML documentation in general is good in terms of structure and easy accessibility, nevertheless I have no idea how every method behaves in detail. It seems to me that many methods are running implicitly on iterations.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Event polling; different implementations
« Reply #1 on: August 07, 2013, 11:26:20 am »
Several events can be triggered from the same frame.
If you use an if statement, you'll process only one event and remaining events will be process on next frames, one at a time.
If you use a while loop, every event in the event queue will be processed in the same frame. So they will be processed as soon as they are triggered.

if statement: process ONE event if there is at least one event in the event queue
while loop: process EVERY events until the event queue is empty

So yes, use a while loop. There is no reason to use an if, and it isn't harder to use a while lol. :p
« Last Edit: August 07, 2013, 11:28:57 am by G. »

complexity

  • Guest
Re: Event polling; different implementations
« Reply #2 on: August 07, 2013, 12:28:20 pm »
First, thanks for your answer.

Second, let me make sure that I got that right:

So, if I am using the if statement, I am actually polling only one event from the event queue of one single frame.

By using while for polling, I can poll several events from the queue within one displayed frame period, right?

Third, so the program (by referring to the while version) must run as follows:

I am creating  a window, and the window is open, so the outer while loop is going to run.
(The outer while loop is necessary because otherwise the window gets closed after reaching the return statement. Catched that from yesterday. :P)

Due to the fact that there are many things to poll (cursor position, clicks etc.) the inner while loop goes on to swat as long as the close-the-window event is not triggered.

If it's triggered the window is closed, and the outer while loop is terminated because the statement is no longer true.

So far, so good.

Now, where is such an event queue stored? Is it always activated? Does the OS handle it - so that I have only to poll things via SFML?

Phew, I am finished. It took me hours to get this. I guess I will get SFML not in days or months but in years maybe even in light years. The fact that other guys who are younger than me (19 or even younger) get the whole stuff quickly, and can write good software is depressing me. Am I'm getting old?! Am I obsolete in this business? I am studying like a snail. :'(


« Last Edit: August 07, 2013, 12:31:03 pm by complexity »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event polling; different implementations
« Reply #3 on: August 07, 2013, 12:36:25 pm »
Your problem, in my opinion, is that you're trying to learn SFML whereas you don't even understand the basics of C++, or even generic programming structures (such as loops). This is a very bad idea.

How could you write a game if you don't understand how a simple program structure made of to loops behave?

You should buy a good C++ book, practice with simple exercises, and forget about SFML for a while ;)
Laurent Gomila - SFML developer

Sonkun

  • Full Member
  • ***
  • Posts: 241
    • View Profile
Re: Event polling; different implementations
« Reply #4 on: August 07, 2013, 03:19:14 pm »
Phew, I am finished. It took me hours to get this. I guess I will get SFML not in days or months but in years maybe even in light years. The fact that other guys who are younger than me (19 or even younger) get the whole stuff quickly, and can write good software is depressing me. Am I'm getting old?! Am I obsolete in this business? I am studying like a snail. :'(
Don't worry, it happens to get lost in obvious details like these, especially when you never dealt with a main loop. Look at me, once I asked something similar about key events. I couldn't get how were TextEvent and KeyEvent events related, and Laurent simply explained me a TextEvent is the result of a combination of KeyEvent, that's why you might have these two events in a row, generated in a single frame (I was confused because my loop was handling both events and was sure KeyEvent was the way to go for simple characters like A, B, etc.). It didn't prevent me from writing the Android port of SFML and I was your age.

So.. don't give up! :) You can't learn programming and APIs overnight, you'll always need to step back. And when everything goes wrong, that means you need a fresh start.

Good luck!
Interested in using SFML with Python ? Try out its Python binding!

complexity

  • Guest
Re: Event polling; different implementations
« Reply #5 on: August 08, 2013, 10:56:41 am »
Well, I thought my rusty C knowledge from about 6 years ago would be enough for SFML programming.
Since then I never practiced programming. That's why I got out of practice.
I am a bit familiar with the concepts of OOP, and I wanted to learn C++ while learning SFML at the same time, because I hated starting again with basic things like console applications only.

I thought maybe the combination with SFML would help to keep my interest of learning C++ upright, but Laurent told me that this is a bad idea indeed. I should learn the boring C++ first before SFML.

Guess I have to be patient...


Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Event polling; different implementations
« Reply #6 on: August 09, 2013, 05:32:32 am »
Patience and a desire to learn and push forward will be an immense help. It's not all about getting a sprite to draw to moving your character, it's also about understanding why that sprite is drawing and why the character moves when you press the button. Don't be afraid to dig in and get your hands dirty.

For myself it took me 6 months to truly understand what programming really was, and when I did, things became so much easier. Push on and don't give up!

 

anything