endlesstravel / Love2dCS

C# Wrapper for LÖVE, a 2d game engine
MIT License
164 stars 14 forks source link

provide non hook abstraction for custom update #100

Open Shadowblitz16 opened 4 years ago

Shadowblitz16 commented 4 years ago

so I just was messing around with RayLibCS and I really liked how the main loop worked. it allowed the user to control the flow of the program and draw inside the things like load and update functions.

here is a example..

using Raylib;
using rl = Raylib.Raylib;

static class Program
{
    public static void Main() 
    {
        rl.InitWindow(640, 480, "Hello World");

        while (!rl.WindowShouldClose())
        {
            rl.BeginDrawing();

            rl.ClearBackground(Color.WHITE);
            rl.DrawText("Hello, world!", 12, 12, 20, Color.BLACK);

            rl.EndDrawing();
        }

        rl.CloseWindow();
    }
}

I think Love2d cs should allow something like this even if it was a optional thing. it Love could update the stuff behind the scenes and allow the user to control the flow of the program.

for example..

using Love = Love.Love;
using MyEvents = MyFramework.MyEvents;

static class Program
{
    public static void Main() 
    {
        Love.InitWindow(640, 480, "Hello World");

        //allow drawing in load
        Love.BeginDraw();
        MyEvents .Load();
        Love.EndDraw();

        while (!Love.IsWindowOpen())
        {
            Love.BeginDraw();
            //my custom logic
            MyEvents.Step();
            MyEvents.Draw(); 
            Love.EndDraw();
        }
        MyEvents.Free(); 
        Love.CloseWindow();
    }
}
endlesstravel commented 4 years ago

actually you can do this now:

using System;
using Love;

namespace Project1
{
    class Issue_100: Scene
    {
        bool sholdClosed = false;
        public override bool Quit()
        {
            sholdClosed = true; // mark it as true to break loop
            return false; // return true will lead Application.Exit(); 
        }

        static void Main()
        {
            Boot.Init();
            var scene = new Issue_100();
            while (!scene.sholdClosed)
            {
                Boot.SystemStep(scene);   // poll the event

                Graphics.Clear(); // clean the window
                Graphics.Line(Vector2.Zero, Mouse.GetPosition());
                Graphics.Present();  // show the next frame

                if (Keyboard.IsPressed(KeyConstant.Space)) // update logic
                    scene.sholdClosed = true;

                Window.SetTitle("fps:" + FPSCounter.GetFPS());
            }
            Console.WriteLine("-end-");
        }
    }
}

Dose it can meet your requirements ❓

Shadowblitz16 commented 4 years ago

kinda ya but It would be better if it required less implementation. I mean I could get around it but something more of what I suggested where you don't need to create a instance of a class.

endlesstravel commented 4 years ago

may be you mean:

Main(...)
{
            Boot.Init();
            while (!Boot.QuitFlag)
            {
                Boot.SystemStep();
                // or do this .....
                //Boot.SystemStep(new Boot.SystemStepConfig(){
                //    OnKeyPressed = (k, s, i) => Console.WriteLine(k),
                //});

                Graphics.Clear();
                Graphics.Line(Vector2.Zero, Mouse.GetPosition());
                Graphics.Present();

                if (Keyboard.IsPressed(KeyConstant.Space))
                    Boot.QuitFlag = true;

                Window.SetTitle("fps:" + FPSCounter.GetFPS());
            }
            Console.WriteLine("-end-");
}
Shadowblitz16 commented 4 years ago

@endlesstravel yep but Boot.QuitFlag should be a method called Boot.IsRunning() and Boot.SystemStep(); should a be abstracted so it doesn't need to be called. also Boot should accept a optional window size and title


Main(...)
{
            Boot.Init(800, 600, "fps:" + FPSCounter.GetFPS());
            ///Window.SetTitle(); //optional Boot.Init() can Initialize window width, height and title.
            while (!Boot.IsRunning())
            {
                // Boot.SystemStep is not needed at all it is updated by Boot.Init(); using a object

                Graphics.Clear();
                Graphics.Line(Vector2.Zero, Mouse.GetPosition());
                Graphics.Present();

                if (Keyboard.IsPressed(KeyConstant.Space))
                    Boot.QuitFlag = true;
            }
            Console.WriteLine("-end-");
}``
endlesstravel commented 4 years ago

i will not push the function : Boot.IsRunning() I think it's confusing. A function should only be as literal as it means, otherwise function name matching is not the actual task.

I had add new way to Boot.SystemStep(new Boot.SystemStepConfig() ... in version 11.0.45

Shadowblitz16 commented 4 years ago

I'm not sure what you mean by Boot.IsRunning() is confusing. I agree it is probably not the best class to put it in since something like.. Window.IsOpen() is probably better. also I do agree now that the running check and the events should be separated maybe in something like Window.PollEvents() but idk.