Gui.cs: Require help with custom loop

Created on 21 Apr 2020  路  2Comments  路  Source: migueldeicaza/gui.cs

Hi there!

I'm trying to integrate Gui.cs with my game engine and am using the building block API to build out a custom loop.

The gui console and game window are both rendering correctly, but the gui console is not processing events.

I looked at the API docs, mainloop guide, and source code, but cannot figure out how to properly process my own custom loop.

Example of what I am currently doing

private Toplevel _top;
private Application.RunState _state;

...

void Load() // Called at engine startup
{
    _top = Application.Top;

    // Create and add controls to _top

    _state = Application.Begin(_top);
}

void Tick(deltaTime) // Called once per frame
{
    // Is this necessary?
    while (Application.MainLoop.EventsPending())
        Application.MainLoop.MainIteration();

    Application.RunLoop(_state, wait: false);
}

void ShutDown() // Called at engine shut down
{
    Application.End(_state);
}

Any help or a working example of a custom main loop would be appreciated.

Thank you!

Most helpful comment

Here a little example:

class MainClass {
    public class MyGame {
        private Toplevel _top;
        private Label label;
        private int counter;
        private bool requestStop;
        public bool IsShutDown;

        public void Load () // Called at engine startup
        {
            Application.Init ();

            _top = Application.Top;

            // Create and add controls to _top
            label = new Label ("frame");

            var startStop = new Button (0, 2, "Stop timer");
            startStop.Clicked = () => {
                if (startStop.Text == "Stop timer") {
                    requestStop = true;
                    startStop.Text = "Start timer";
                } else {
                    requestStop = false;
                    startStop.Text = "Stop timer";
                }
            };

            var restartTimer = new Button (0, 4, "Restart Timer");
            restartTimer.Clicked += () => {
                counter = 0;
                label.Text = "";
                Application.RequestStop ();
            };

            var shutDown = new Button (0, 6, "Click to Exit");

            shutDown.Clicked += ShutDown;

            Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), Tick);

            _top.Add (label, startStop, restartTimer, shutDown);

            Application.Run ();

        }

        private bool Tick (MainLoop deltaTime)
        {
            if (requestStop) {
                deltaTime.Stop ();
                return true;
            }

            label.Text = $"{deltaTime.ToString ()} {counter++}";

            return true;
        }

        void ShutDown () // Called at engine shut down
        {
            IsShutDown = true;
            Application.RequestStop ();
        }

    }

    public static void Main (string [] args)
    {
        while (true) {
            MyGame myGame = new MyGame ();

            myGame.Load ();

            if (myGame.IsShutDown)
                break;
        }
    }

}


All 2 comments

Here a little example:

class MainClass {
    public class MyGame {
        private Toplevel _top;
        private Label label;
        private int counter;
        private bool requestStop;
        public bool IsShutDown;

        public void Load () // Called at engine startup
        {
            Application.Init ();

            _top = Application.Top;

            // Create and add controls to _top
            label = new Label ("frame");

            var startStop = new Button (0, 2, "Stop timer");
            startStop.Clicked = () => {
                if (startStop.Text == "Stop timer") {
                    requestStop = true;
                    startStop.Text = "Start timer";
                } else {
                    requestStop = false;
                    startStop.Text = "Stop timer";
                }
            };

            var restartTimer = new Button (0, 4, "Restart Timer");
            restartTimer.Clicked += () => {
                counter = 0;
                label.Text = "";
                Application.RequestStop ();
            };

            var shutDown = new Button (0, 6, "Click to Exit");

            shutDown.Clicked += ShutDown;

            Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), Tick);

            _top.Add (label, startStop, restartTimer, shutDown);

            Application.Run ();

        }

        private bool Tick (MainLoop deltaTime)
        {
            if (requestStop) {
                deltaTime.Stop ();
                return true;
            }

            label.Text = $"{deltaTime.ToString ()} {counter++}";

            return true;
        }

        void ShutDown () // Called at engine shut down
        {
            IsShutDown = true;
            Application.RequestStop ();
        }

    }

    public static void Main (string [] args)
    {
        while (true) {
            MyGame myGame = new MyGame ();

            myGame.Load ();

            if (myGame.IsShutDown)
                break;
        }
    }

}


Hey BDisp!

Thanks for the answer! But I should have been more clear in my OP - I'm looking to use the building block API .

  • Begin(Toplevel)
  • End(Application+RunState)

Edit
I didn't read your code thoroughly enough and based my response off an assumption. :(
This implementation solves my problem. Thank you!

Was this page helpful?
0 / 5 - 0 ratings