diff --git a/Capy64/Capy64.cs b/Capy64/Capy64.cs index bc46a6a..1c922cc 100644 --- a/Capy64/Capy64.cs +++ b/Capy64/Capy64.cs @@ -33,7 +33,7 @@ public class Capy64 : Game, IGame public float Scale { get; set; } = 2f; public Drawing Drawing { get; private set; } public LuaState LuaRuntime { get; set; } - public EventEmitter EventEmitter { get; private set; } + public Eventing.EventEmitter EventEmitter { get; private set; } public DiscordIntegration Discord { get; set; } public Borders Borders = new() @@ -71,11 +71,14 @@ public class Capy64 : Game, IGame _serviceProvider = serviceProvider; } - public void UpdateSize() + public void UpdateSize(bool resize = true) { - _graphics.PreferredBackBufferWidth = (int)(Width * Scale) + Borders.Left + Borders.Right; - _graphics.PreferredBackBufferHeight = (int)(Height * Scale) + Borders.Top + Borders.Right; - _graphics.ApplyChanges(); + if (resize) + { + _graphics.PreferredBackBufferWidth = (int)(Width * Scale) + Borders.Left + Borders.Right; + _graphics.PreferredBackBufferHeight = (int)(Height * Scale) + Borders.Top + Borders.Right; + _graphics.ApplyChanges(); + } renderTarget = new RenderTarget2D( GraphicsDevice, @@ -96,17 +99,27 @@ public class Capy64 : Game, IGame private void OnWindowSizeChange(object sender, EventArgs e) { var bounds = Window.ClientBounds; - Console.WriteLine(bounds); - - if (Window.IsMaximized()) - { - - } Width = (int)(bounds.Width / Scale); Height = (int)(bounds.Height / Scale); - UpdateSize(); + if (Window.IsMaximized()) + { + var vertical = bounds.Height - Height * Scale; + var horizontal = bounds.Width - Width * Scale; + + Borders.Top = (int)Math.Floor(vertical / 2d); + Borders.Bottom = (int)Math.Ceiling(vertical / 2d); + + Borders.Left = (int)Math.Floor(horizontal / 2d); + Borders.Right = (int)Math.Ceiling(horizontal / 2d); + } + else + { + Borders = new Borders(); + } + + UpdateSize(false); } protected override void Initialize() @@ -115,7 +128,7 @@ public class Capy64 : Game, IGame UpdateSize(); - Window.AllowUserResizing = false; + Window.AllowUserResizing = true; Window.ClientSizeChanged += OnWindowSizeChange; NativePlugins = GetNativePlugins(); diff --git a/Capy64/IGame.cs b/Capy64/IGame.cs index c516c5c..fedda4a 100644 --- a/Capy64/IGame.cs +++ b/Capy64/IGame.cs @@ -17,13 +17,13 @@ public interface IGame GameWindow Window { get; } Drawing Drawing { get; } LuaState LuaRuntime { get; set; } - EventEmitter EventEmitter { get; } + Eventing.EventEmitter EventEmitter { get; } void ConfigureServices(IServiceProvider serviceProvider); int Width { get; set; } int Height { get; set; } float Scale { get; set; } - void UpdateSize(); + void UpdateSize(bool resize = true); event EventHandler Exiting; void Run(); diff --git a/Capy64/Runtime/Libraries/Term.cs b/Capy64/Runtime/Libraries/Term.cs index 4e51be9..17a58b0 100644 --- a/Capy64/Runtime/Libraries/Term.cs +++ b/Capy64/Runtime/Libraries/Term.cs @@ -56,6 +56,7 @@ internal class Term : IPlugin UpdateSize(); _game.EventEmitter.OnOverlay += OnOverlay; + _game.EventEmitter.OnScreenSizeChange += OnScreenSizeChange; } private LuaRegister[] TermLib = new LuaRegister[] @@ -160,12 +161,16 @@ internal class Term : IPlugin return 1; } - public static void UpdateSize() + public static void UpdateSize(bool resize = true) { - _game.Width = RealWidth; - _game.Height = RealHeight; - _game.UpdateSize(); - CharGrid = new Char?[Width * Height]; + Array.Resize(ref CharGrid, Width * Height); + + if (resize) + { + _game.Width = RealWidth; + _game.Height = RealHeight; + _game.UpdateSize(); + } } public static Vector2 ToRealPos(Vector2 termPos) @@ -252,6 +257,14 @@ internal class Term : IPlugin UpdateCursor(); } + private void OnScreenSizeChange(object sender, EventArgs e) + { + Width = _game.Width / CharWidth; + Height = _game.Height / CharHeight; + + UpdateSize(false); + } + private static void UpdateCursor() { if (!enableCursor) diff --git a/Capy64/Runtime/RuntimeManager.cs b/Capy64/Runtime/RuntimeManager.cs index 7e9e49d..4c66f32 100644 --- a/Capy64/Runtime/RuntimeManager.cs +++ b/Capy64/Runtime/RuntimeManager.cs @@ -16,7 +16,7 @@ namespace Capy64.Runtime; internal class RuntimeManager : IPlugin { private LuaState luaState; - private InputEmitter emitter; + private EventEmitter emitter; private int step = 0; private static bool close = false;