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

Author Topic: [Rookie] DispatchEvents() used in the Window Tutorial ?  (Read 4249 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
[Rookie] DispatchEvents() used in the Window Tutorial ?
« on: August 10, 2014, 09:56:33 am »
Hey Guys!

I wander through the window tutorial for SFML and already came across pollEvents(Event event) and therefore I found out that there is DispatchEvents() for us C# users.

sadly I just can't find a way to do this tutorial with the DispatchEvents() example. I already tried to figure it out but couldn't find a way. Even tho searching here for this issue I only found other code chunks which weren't helpful enough for me (cause I'm a slow mind).

there my wish is - can anybody morphe the following tutorial code to the working solution with C# (and the DispatchEvents()) ?

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

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }

    return 0;
}

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: [Rookie] DispatchEvents() used in the Window Tutorial ?
« Reply #1 on: August 10, 2014, 10:04:23 am »

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: [Rookie] DispatchEvents() used in the Window Tutorial ?
« Reply #2 on: August 10, 2014, 10:23:42 am »
Ha bro,

thats exactly that kind of example I can't really use much because I'm getting dizzy. Can't u change the above code to implement DispatchEvents() ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Rookie] DispatchEvents() used in the Window Tutorial ?
« Reply #3 on: August 10, 2014, 11:38:40 am »
It's not hard to extract only the event specific code:

        static void Main()
        {
            ...

            window.Closed     += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
            window.Resized    += new EventHandler<SizeEventArgs>(OnResized);

            ...

            while (window.IsOpen)
            {
                window.DispatchEvents();

                ...
            }
        }

        static void OnClosed(object sender, EventArgs e)
        {
            Window window = (Window)sender;
            window.Close();
        }

        static void OnKeyPressed(object sender, KeyEventArgs e)
        {
            Window window = (Window)sender;
            if (e.Code == Keyboard.Key.Escape)
                window.Close();
        }

        static void OnResized(object sender, SizeEventArgs e)
        {
            Gl.glViewport(0, 0, (int)e.Width, (int)e.Height);
        }

 
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: [Rookie] DispatchEvents() used in the Window Tutorial ?
« Reply #4 on: August 10, 2014, 12:14:45 pm »
thanks - just took me a while to remind me on that C# event stuff with delegates etc

 

anything