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

Author Topic: Event queue [DONE]  (Read 8415 times)

0 Members and 1 Guest are viewing this topic.

Andrei15193

  • Guest
Event queue [DONE]
« on: July 10, 2012, 12:10:50 pm »
Is it possible to have a method that takes an event out of a queue (if any) instead of a stack?
« Last Edit: July 11, 2012, 09:01:02 am by Andrei15193 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event queue
« Reply #1 on: July 10, 2012, 01:15:03 pm »
I'm not sure that I understand what you mean. Would you mind explaining your request with more details?
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Event queue
« Reply #2 on: July 10, 2012, 01:16:44 pm »
you can poll them all and sort in order you want

Andrei15193

  • Guest
Re: Event queue
« Reply #3 on: July 10, 2012, 02:01:59 pm »
At this moment you can only poll events from the top of the stack. Being a stack means last in first out, however this isn't always satisfactory as in some situations it is preferred to pop events in the same order they occurred. For instance if my application freezes for a split second and the user doesn't notice and taps these keys in this exact order: "X Y Z" the following happens. The application unfreezes and it starts taking events from the top meaning it will see "Z Y X" instead of "X Y Z".

There are workarounds like Acrobat suggested. However when I poll events I can only poll one at the time, meaning that while I'm polling another event can be added on the stack and ruin my event reading (because of polling the event I want I will be polling the new one). For instance "X Y Z" is the event stack with Z on the top, I poll two events and get "Z Y" then the a new event happens (event "A"). At this point the stack is "X A" and the reading queue "Z Y", I continue reading until the stack is empty (another issue, what if the stack can never be empty because something is continuously generating events?) and get my reading queue like this "Z Y A X" and what I want is "Z Y X A". More to that, a pass through the stack will mean Θ(n) steps to get an inverted order (assuming no events are added on the stack in the mean time), and Ω(n log n) for worst case sorting.

What I'm asking is: can there be a method that will take from the base of the stack (actually turn it into a queue) without having to go through the whole stack (from top to bottom)? This will solve the issues mentioned above and reduce the time to Θ(1) for getting the first in event.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event queue
« Reply #4 on: July 10, 2012, 02:08:09 pm »
In fact it's a queue, not a stack. Using a stack wouldn't make sense.

Why did you have the feeling that it was a stack?
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Event queue
« Reply #5 on: July 10, 2012, 02:10:45 pm »
You are doing it wrong, how can new events added to queue while you polling them ?
can you show that lines ?

Andrei15193

  • Guest
Re: Event queue
« Reply #6 on: July 10, 2012, 02:40:32 pm »
This makes me think it's a stack: bool sf::Window::pollEvent(Event& event) .
And the documentation for 1.6 says the same thing: bool sf::Window::GetEvent(Event& EventReceived).
They both pop from an event stack.

Also events are added by the OS, a mouse click on the Window will put an event on the stack, even if it's an infinite loop. Here is the test code:
#include <SFML/Graphics.hpp>

int main()
{
    bool p = true;
    sf::Event event;
    sf::RenderWindow window(sf::VideoMode(300, 200), "Event polling");
    sf::Text text("It works");

    do{
        window.clear();
        window.draw(text);
        window.display();
        do{
            text.setString("Exited loop");
            if (window.pollEvent(event) && event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape){
                window.close();
                p = false;
            }
        }while (p);
    }while (window.isOpen());

    return EXIT_SUCCESS;
}
 
Polling the events will do the same thing, it will block the program for a few milliseconds and then resume it, no event can be added on the event stack right? Well above is an infinite loop and the app stops with an event even if it is found in infinite loop.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event queue
« Reply #7 on: July 10, 2012, 02:45:18 pm »
Quote
This makes me think it's a stack: bool sf::Window::pollEvent(Event& event) .
And the documentation for 1.6 says the same thing: bool sf::Window::GetEvent(Event& EventReceived).
They both pop from an event stack.
This is just a (big) mistake in the doc. It is not a stack.

So you reported an issue just by looking at the doc, you don't even have any code issue? :P
Laurent Gomila - SFML developer

Andrei15193

  • Guest
Re: Event queue
« Reply #8 on: July 10, 2012, 02:54:03 pm »
Some things jump into the eye, lately I refer to documentations before actually writing code. The code issues I got so far (nothing much, just a bunch of warnings from the compiler) are about sf::Window and related classes. I can post all compiler output if you think it's of any use.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event queue
« Reply #9 on: July 10, 2012, 03:13:53 pm »
Quote
I can post all compiler output if you think it's of any use.
If you think that there's something that needs to be fixed in SFML rather than in your code, please do.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Event queue
« Reply #10 on: July 11, 2012, 04:29:53 pm »
This makes me think it's a stack: bool sf::Window::pollEvent(Event& event) .
And the documentation for 1.6 says the same thing: bool sf::Window::GetEvent(Event& EventReceived).
They both pop from an event stack.

Why are you reading SFML 1.6 documentation when programming for SFML 2.0? ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Andrei15193

  • Guest
Re: Event queue [DONE]
« Reply #11 on: July 11, 2012, 04:52:01 pm »
I was reading 1.6 when I was having my go at 1.6 :P

 

anything