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

Author Topic: SFML and event management (solved)  (Read 4248 times)

0 Members and 1 Guest are viewing this topic.

Tigrou

  • Newbie
  • *
  • Posts: 15
    • View Profile
SFML and event management (solved)
« on: March 14, 2017, 12:11:36 am »
Consider the following (very simple) SFML app.
It creates a render window, poll events and print to console each mouse move position:

public static void Main()
{                      
        RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML window");
       
        window.MouseMoved += (sender, e) => {
                Console.WriteLine(e.X + " " + e.Y);
        };
                       
        // Start the game loop
        while (window.IsOpen())
        {
                window.DispatchEvents();                                               
                window.Clear();                        
                window.Display();
                //System.Threading.Thread.Sleep(1000);
        }
}

Imagine we remove the line in comment (Thread.Sleep(...)).
Now the display in the console is very laggy (which is expected).

However it only prints one mouse position per second. I would rather expect that it would wait one second, then print a bunch of mouse positions (actually all the ones send to the window while sleeping), then wait one second , then print some messages and so on.

I have taken a look at source code (both C++ and C# github repos) and AFAIK SFML behavior is to pump all messages from the window (using PeekMessageW()) then store in it's own queue (a C++ vector) and dispatch them one by one. So I don't understand behavior that I got. I expect Console.WriteLine() to be called several times between two Thread.Sleep().
« Last Edit: March 14, 2017, 07:48:48 pm by Tigrou »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML and event management (unexpected behavior)
« Reply #1 on: March 14, 2017, 06:32:35 am »
I may be wrong, but I'm quite sure that the OS internally discards all previous events of the same type and only delivers the very last one.
Laurent Gomila - SFML developer

Tigrou

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: SFML and event management (unexpected behavior)
« Reply #2 on: March 14, 2017, 12:36:57 pm »
Actually you are right, messages are normally queued, but not WM_MOUSEMOVE, because it is a special case :

See this answer :
http://stackoverflow.com/questions/17512457/missing-mouse-movement-messages-in-win32#answer-17515095