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

Author Topic: How do I use pollEvent Method with C#  (Read 1076 times)

0 Members and 1 Guest are viewing this topic.

Zuzanno

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
How do I use pollEvent Method with C#
« 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#

G.

  • Hero Member
  • *****
  • Posts: 1491
    • View Profile
Re: How do I use pollEvent Method with C#
« Reply #1 on: April 24, 2020, 07:49:20 am »
Take a look at some of the examples, I think events are done through C# delegates.

Zuzanno

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: How do I use pollEvent Method with C#
« Reply #2 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32466
    • View Profile
    • SFML's website
    • Email
Re: How do I use pollEvent Method with C#
« Reply #3 on: April 25, 2020, 07:46:17 am »
Look at this much simpler example.

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.
Laurent Gomila - SFML developer