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

Author Topic: My situation with events  (Read 3200 times)

0 Members and 1 Guest are viewing this topic.

pitchShifter

  • Newbie
  • *
  • Posts: 3
    • View Profile
My situation with events
« on: August 14, 2013, 11:12:52 pm »
PROBLEM FIXED. IGNORE THIS!!!!

So I'm currently working on a menu system. A problem I'm running into is event handling when trying to hover over/click buttons I placed on the screen.

The menu system is based on "Scenes" which are like pages. The scenes contain the information to create and draw buttons as well as draw anything else I want to display.

Each scene has "register_mouseclick" and "register_mousemove" override methods. They look like this:

public override void register_mouseclick( object sender, MouseButtonEventArgs e )
        {
            RenderWindow window = (RenderWindow)sender;
            if( e.Button.GetHashCode() == 0 )
            {
                foreach( Button btn in buttons )
                {
                    btn.check_mouse_click();
                }
            }
        }

        public override void register_mousemove( object sender, MouseMoveEventArgs e )
        {
            foreach( Button btn in buttons )
            {
                btn.check_hover_state( e );
            }
        }

And I register these events through the SceneHandler:
public void register_events( ref RenderWindow window )
                {
                        foreach( Scene s in scenes )
                        {
                                if( s.id == current_scene )
                                {
                                        window.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(s.register_mouseclick);
                                        window.MouseMoved += new EventHandler<MouseMoveEventArgs>(s.register_mousemove);
                                }
                        }
                }

Now the problem I'm running into is when I switch scenes; the events are not registered. They're registered on the default scene but once I change to a different scene the events are no longer registered.

I'm calling the register_events function like so:
public void init()
{
    // ...

    SceneHandler SceneManager = new SceneHandler();

    SceneManager.register_events( ref window );

    // ...

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

        window.Clear();
        SceneManager.draw_scene( ref window );
        window.Display();
    }
}

Now if I were to put the register_events function into the while "window is open" loop it works BUT the game will obviously lag.

How could I solve this problem?
« Last Edit: August 16, 2013, 08:18:30 am by pitchShifter »

Gonzilla

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: My situation with events
« Reply #1 on: August 16, 2013, 10:48:52 am »
I hope you have planned a "deregister" method to remove the event handlers otherwise you'll get memory leaks after some time.  ;)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: My situation with events
« Reply #2 on: August 16, 2013, 11:14:11 am »
I hope you have planned a "deregister" method to remove the event handlers otherwise you'll get memory leaks after some time.  ;)

This is manged C# with a built in GC. Even if you don't manually stop handling events (fooObject.somethingHappened -= HandlesomethingHappened;) the GC will clean up everything when disposing the class that is associated with the events.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

pitchShifter

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: My situation with events
« Reply #3 on: August 16, 2013, 09:10:07 pm »
I already fixed the code before you guys replied but thanks for the replies :)

The solution was that I could put the register events function in the while "window is open" loop but after I dispatch the events I needed to deregister them. Yes, zsbzsb, C# has that feature but with the way my code was setup, I had to deregister them manually. The reason for this was because the events would carry on into other scenes; so I would have clickable buttons that should not exist in other scenes. In order to stop the game from lagging, I had to set a framerate cap.
« Last Edit: August 16, 2013, 09:11:51 pm by pitchShifter »