SFML community forums

Bindings - other languages => DotNet => Topic started by: Zuzanno on April 24, 2020, 06:41:00 am

Title: How do I use pollEvent Method with C#
Post by: Zuzanno on April 24, 2020, 06:41:00 am
I'm trying to convert this
while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
                app.close();
        }
From C++ to C#, However, When I'm trying to do the pollevent part I dont see such method, and this is basically the heart of the game I'm making, may someone please tell me what would be a proper way to translate this into C#
Title: Re: How do I use pollEvent Method with C#
Post by: G. on April 24, 2020, 07:49:20 am
Take a look at some of the examples (https://github.com/SFML/SFML.Net/blob/master/examples/shader/Shader.cs), I think events are done through C# delegates.
Title: Re: How do I use pollEvent Method with C#
Post by: Zuzanno on April 25, 2020, 06:17:40 am
May you please give some more detailed information on how to do it? I started with sfml soon so I barely understand, and with no documentation, things come to be very difficult
Title: Re: How do I use pollEvent Method with C#
Post by: Laurent on April 25, 2020, 07:46:17 am
Look at this much simpler example (https://github.com/SFML/SFML.Net/blob/master/examples/window/Program.cs).

The relevant parts, with added comments:
// registers a handler for the KeyPressed event
window.KeyPressed += Window_KeyPressed;

// the event handler
private void Window_KeyPressed(object sender, SFML.Window.KeyEventArgs e)
{
    var window = (SFML.Window.Window)sender;
    if (e.Code == SFML.Window.Keyboard.Key.Escape)
    {
        window.Close();
    }
}

// this call checks events and triggers the registered handlers
window.DispatchEvents();

The rest (syntax, etc.) is pure C#, in case you're not familiar with delegates.