endlesstravel / Love2dCS

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

Changing windows size and fps at runtime (smoothly) #80

Open Shadowblitz16 opened 5 years ago

Shadowblitz16 commented 5 years ago

can support for changing window size, position and fps at runtime be fixed to do so instantly instead of taking 2 seconds?

I think it might allow for some cool games. currently this is what I am doing but the app freezes because I am setting the fps every frame..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Love;

namespace BBEngine.Library
{
    public class Game
    {
        private static GameManager Manager = new GameManager();
        private class  GameManager : Scene
        {
            bool Initialized = false;

            private Game   Game;
            public Canvas  Canvas;

            private int   _w     = 256;
            private int   _h     = 240;
            private int   _speed = 60;
            private int   _scale = 1;
            private float _delta = 0;

            public int   W
            {
                get
                {
                    return _w;
                }
                set
                {
                    _w = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H /Scale);
                }
            }
            public int   H
            {
                get
                {
                    return _h;
                }
                set
                {
                    _h = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H / Scale);
                }
            }
            public int   Scale
            {
                get
                {
                    return _scale;
                }
                set
                {
                    _scale = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H / Scale);
                }
            }
            public int   Speed
            {
                get
                {
                    return _speed;
                }
                set
                {
                    _speed = value;
                    if (Initialized) Timer.EnableLimitMaxFPS(1f / Speed);
                }
            }
            public float Delta
            {
                get
                {
                    return _delta;
                }
                set
                {
                    _delta = value;
                }
            }

            public GameManager()
            {

            }

            public void Run(Game game)
            {
                Game = game;
                Boot.Run(this, new BootConfig() { WindowWidth = W, WindowHeight = H });
            }

            public override void Load()
            {
                Game.OnInit();
                Timer.EnableLimitMaxFPS(1f / Speed);
                Canvas = Graphics.NewCanvas(W/Scale, H/Scale);
            }
            public override void Update(float dt)
            {
                Delta = Timer.GetDelta();
                Game.OnTick();
            }
            public override void Draw()
            {
                Graphics.SetCanvas(Canvas);
                Game.OnDraw();
                Graphics.SetCanvas();
                Graphics.Draw(Canvas, 0, 0, 0, Game.Scale, Game.Scale);
            }
            public override bool Quit()
            {
                Game.OnQuit();
                return true;
            }
        }

        //Properties
        public int   W    
        {
            get
            {
                return Manager.W;
            }
            set
            {
                Manager.W = value;
            }
        }
        public int   H    
        {
            get
            {
                return Manager.H;
            }
            set
            {
                Manager.H = value;
            }
        }
        public int   Scale
        {
            get
            {
                return Manager.Scale;
            }
            set
            {
                Manager.Scale = value;
            }
        }
        public int   Speed
        {
            get
            {
                return Manager.Speed;
            }
            set
            {
                Manager.Speed = value;
            }
        }
        public float Delta
        {
            get
            {
                return Manager.Delta;
            }
        }

        public static void Run(Game game)
        {
            Manager.Run(game);
        }

        //Events
        protected virtual void OnInit() { }
        protected virtual void OnTick() { }
        protected virtual void OnDraw() { }
        protected virtual void OnQuit() { }
    }   
}
using BBEngine.Library;
using Love;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBEngine
{
    public class Program : Game
    {
        static void Main(string[] args)
        {
            Run(new Program());
        }
        int i = 0;

        protected override void OnTick()
        {
            i++;
            Speed = Math.Max(Speed - 1, 1);
        }
        protected override void OnDraw()
        {
            Graphics.Rectangle(DrawMode.Fill, 0 + i, 0 + i, 0 + i, 0 + i);
        }
    }
}