From 32a30e1b6b013b5fb0bb4abd38ab105c04a502c2 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 29 Sep 2022 13:57:01 -0700 Subject: [PATCH] can change FrameLimiterSettings at runtime --- src/FrameLimiterSettings.cs | 14 ++++++++++ src/FramerateSettings.cs | 14 ---------- src/Game.cs | 54 ++++++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 src/FrameLimiterSettings.cs delete mode 100644 src/FramerateSettings.cs diff --git a/src/FrameLimiterSettings.cs b/src/FrameLimiterSettings.cs new file mode 100644 index 0000000..bb72d8f --- /dev/null +++ b/src/FrameLimiterSettings.cs @@ -0,0 +1,14 @@ +namespace MoonWorks +{ + public enum FrameLimiterMode + { + Uncapped, + Capped + } + + public struct FrameLimiterSettings + { + public FrameLimiterMode Mode; + public int Cap; + } +} diff --git a/src/FramerateSettings.cs b/src/FramerateSettings.cs deleted file mode 100644 index 8ebc580..0000000 --- a/src/FramerateSettings.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace MoonWorks -{ - public enum FramerateMode - { - Uncapped, - Capped - } - - public struct FramerateSettings - { - public FramerateMode Mode; - public int Cap; - } -} diff --git a/src/Game.cs b/src/Game.cs index 5c31153..2a37582 100644 --- a/src/Game.cs +++ b/src/Game.cs @@ -29,23 +29,16 @@ namespace MoonWorks private bool FramerateCapped = false; private TimeSpan FramerateCapTimeSpan = TimeSpan.Zero; - public Window Window { get; } public GraphicsDevice GraphicsDevice { get; } public AudioDevice AudioDevice { get; } public Inputs Inputs { get; } - private Dictionary moonWorksToRefreshPresentMode = new Dictionary - { - { PresentMode.Immediate, RefreshCS.Refresh.PresentMode.Immediate }, - { PresentMode.Mailbox, RefreshCS.Refresh.PresentMode.Mailbox }, - { PresentMode.FIFO, RefreshCS.Refresh.PresentMode.FIFO }, - { PresentMode.FIFORelaxed, RefreshCS.Refresh.PresentMode.FIFORelaxed } - }; + public Window MainWindow { get; } public Game( WindowCreateInfo windowCreateInfo, PresentMode presentMode, - FramerateSettings framerateSettings, + FrameLimiterSettings frameLimiterSettings, int targetTimestep = 60, bool debugMode = false ) @@ -53,12 +46,7 @@ namespace MoonWorks Timestep = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / targetTimestep); gameTimer = Stopwatch.StartNew(); - FramerateCapped = framerateSettings.Mode == FramerateMode.Capped; - - if (FramerateCapped) - { - FramerateCapTimeSpan = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / framerateSettings.Cap); - } + SetFrameLimiter(frameLimiterSettings); for (int i = 0; i < previousSleepTimes.Length; i += 1) { @@ -75,15 +63,18 @@ namespace MoonWorks Inputs = new Inputs(); - Window = new Window(windowCreateInfo); - GraphicsDevice = new GraphicsDevice( - Window.Handle, Backend.Vulkan, - moonWorksToRefreshPresentMode[presentMode], debugMode ); + MainWindow = new Window(windowCreateInfo, GraphicsDevice.WindowFlags); + + if (!GraphicsDevice.ClaimWindow(MainWindow, presentMode)) + { + throw new System.SystemException("Could not claim window!"); + } + AudioDevice = new AudioDevice(); } @@ -97,12 +88,26 @@ namespace MoonWorks Destroy(); AudioDevice.Dispose(); + MainWindow.Dispose(); GraphicsDevice.Dispose(); - Window.Dispose(); SDL.SDL_Quit(); } + public void SetFrameLimiter(FrameLimiterSettings settings) + { + FramerateCapped = settings.Mode == FrameLimiterMode.Capped; + + if (FramerateCapped) + { + FramerateCapTimeSpan = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / settings.Cap); + } + else + { + FramerateCapTimeSpan = TimeSpan.Zero; + } + } + protected abstract void Update(TimeSpan delta); protected abstract void Draw(double alpha); protected virtual void Destroy() {} @@ -225,7 +230,14 @@ namespace MoonWorks { if (evt.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED) { - Window.SizeChanged((uint) evt.window.data1, (uint) evt.window.data2); + var window = Window.Lookup(evt.window.windowID); + window.SizeChanged((uint) evt.window.data1, (uint) evt.window.data2); + } + else if (evt.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE) + { + var window = Window.Lookup(evt.window.windowID); + GraphicsDevice.UnclaimWindow(window); + window.Dispose(); } }