Allow resizable screen

This commit is contained in:
Alessandro Proto 2023-02-03 22:59:20 +01:00
parent 15e700008e
commit bf6424c0d1
4 changed files with 47 additions and 21 deletions

View file

@ -33,7 +33,7 @@ public class Capy64 : Game, IGame
public float Scale { get; set; } = 2f; public float Scale { get; set; } = 2f;
public Drawing Drawing { get; private set; } public Drawing Drawing { get; private set; }
public LuaState LuaRuntime { get; 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 DiscordIntegration Discord { get; set; }
public Borders Borders = new() public Borders Borders = new()
@ -71,11 +71,14 @@ public class Capy64 : Game, IGame
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
} }
public void UpdateSize() public void UpdateSize(bool resize = true)
{ {
_graphics.PreferredBackBufferWidth = (int)(Width * Scale) + Borders.Left + Borders.Right; if (resize)
_graphics.PreferredBackBufferHeight = (int)(Height * Scale) + Borders.Top + Borders.Right; {
_graphics.ApplyChanges(); _graphics.PreferredBackBufferWidth = (int)(Width * Scale) + Borders.Left + Borders.Right;
_graphics.PreferredBackBufferHeight = (int)(Height * Scale) + Borders.Top + Borders.Right;
_graphics.ApplyChanges();
}
renderTarget = new RenderTarget2D( renderTarget = new RenderTarget2D(
GraphicsDevice, GraphicsDevice,
@ -96,17 +99,27 @@ public class Capy64 : Game, IGame
private void OnWindowSizeChange(object sender, EventArgs e) private void OnWindowSizeChange(object sender, EventArgs e)
{ {
var bounds = Window.ClientBounds; var bounds = Window.ClientBounds;
Console.WriteLine(bounds);
if (Window.IsMaximized())
{
}
Width = (int)(bounds.Width / Scale); Width = (int)(bounds.Width / Scale);
Height = (int)(bounds.Height / 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() protected override void Initialize()
@ -115,7 +128,7 @@ public class Capy64 : Game, IGame
UpdateSize(); UpdateSize();
Window.AllowUserResizing = false; Window.AllowUserResizing = true;
Window.ClientSizeChanged += OnWindowSizeChange; Window.ClientSizeChanged += OnWindowSizeChange;
NativePlugins = GetNativePlugins(); NativePlugins = GetNativePlugins();

View file

@ -17,13 +17,13 @@ public interface IGame
GameWindow Window { get; } GameWindow Window { get; }
Drawing Drawing { get; } Drawing Drawing { get; }
LuaState LuaRuntime { get; set; } LuaState LuaRuntime { get; set; }
EventEmitter EventEmitter { get; } Eventing.EventEmitter EventEmitter { get; }
void ConfigureServices(IServiceProvider serviceProvider); void ConfigureServices(IServiceProvider serviceProvider);
int Width { get; set; } int Width { get; set; }
int Height { get; set; } int Height { get; set; }
float Scale { get; set; } float Scale { get; set; }
void UpdateSize(); void UpdateSize(bool resize = true);
event EventHandler<EventArgs> Exiting; event EventHandler<EventArgs> Exiting;
void Run(); void Run();

View file

@ -56,6 +56,7 @@ internal class Term : IPlugin
UpdateSize(); UpdateSize();
_game.EventEmitter.OnOverlay += OnOverlay; _game.EventEmitter.OnOverlay += OnOverlay;
_game.EventEmitter.OnScreenSizeChange += OnScreenSizeChange;
} }
private LuaRegister[] TermLib = new LuaRegister[] private LuaRegister[] TermLib = new LuaRegister[]
@ -160,12 +161,16 @@ internal class Term : IPlugin
return 1; return 1;
} }
public static void UpdateSize() public static void UpdateSize(bool resize = true)
{ {
_game.Width = RealWidth; Array.Resize(ref CharGrid, Width * Height);
_game.Height = RealHeight;
_game.UpdateSize(); if (resize)
CharGrid = new Char?[Width * Height]; {
_game.Width = RealWidth;
_game.Height = RealHeight;
_game.UpdateSize();
}
} }
public static Vector2 ToRealPos(Vector2 termPos) public static Vector2 ToRealPos(Vector2 termPos)
@ -252,6 +257,14 @@ internal class Term : IPlugin
UpdateCursor(); UpdateCursor();
} }
private void OnScreenSizeChange(object sender, EventArgs e)
{
Width = _game.Width / CharWidth;
Height = _game.Height / CharHeight;
UpdateSize(false);
}
private static void UpdateCursor() private static void UpdateCursor()
{ {
if (!enableCursor) if (!enableCursor)

View file

@ -16,7 +16,7 @@ namespace Capy64.Runtime;
internal class RuntimeManager : IPlugin internal class RuntimeManager : IPlugin
{ {
private LuaState luaState; private LuaState luaState;
private InputEmitter emitter; private EventEmitter emitter;
private int step = 0; private int step = 0;
private static bool close = false; private static bool close = false;