can change FrameLimiterSettings at runtime

pull/27/head
cosmonaut 2022-09-29 13:57:01 -07:00
parent 0034b24eff
commit 32a30e1b6b
3 changed files with 47 additions and 35 deletions

View File

@ -0,0 +1,14 @@
namespace MoonWorks
{
public enum FrameLimiterMode
{
Uncapped,
Capped
}
public struct FrameLimiterSettings
{
public FrameLimiterMode Mode;
public int Cap;
}
}

View File

@ -1,14 +0,0 @@
namespace MoonWorks
{
public enum FramerateMode
{
Uncapped,
Capped
}
public struct FramerateSettings
{
public FramerateMode Mode;
public int Cap;
}
}

View File

@ -29,23 +29,16 @@ namespace MoonWorks
private bool FramerateCapped = false; private bool FramerateCapped = false;
private TimeSpan FramerateCapTimeSpan = TimeSpan.Zero; private TimeSpan FramerateCapTimeSpan = TimeSpan.Zero;
public Window Window { get; }
public GraphicsDevice GraphicsDevice { get; } public GraphicsDevice GraphicsDevice { get; }
public AudioDevice AudioDevice { get; } public AudioDevice AudioDevice { get; }
public Inputs Inputs { get; } public Inputs Inputs { get; }
private Dictionary<PresentMode, RefreshCS.Refresh.PresentMode> moonWorksToRefreshPresentMode = new Dictionary<PresentMode, RefreshCS.Refresh.PresentMode> public Window MainWindow { get; }
{
{ 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 Game( public Game(
WindowCreateInfo windowCreateInfo, WindowCreateInfo windowCreateInfo,
PresentMode presentMode, PresentMode presentMode,
FramerateSettings framerateSettings, FrameLimiterSettings frameLimiterSettings,
int targetTimestep = 60, int targetTimestep = 60,
bool debugMode = false bool debugMode = false
) )
@ -53,12 +46,7 @@ namespace MoonWorks
Timestep = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / targetTimestep); Timestep = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / targetTimestep);
gameTimer = Stopwatch.StartNew(); gameTimer = Stopwatch.StartNew();
FramerateCapped = framerateSettings.Mode == FramerateMode.Capped; SetFrameLimiter(frameLimiterSettings);
if (FramerateCapped)
{
FramerateCapTimeSpan = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / framerateSettings.Cap);
}
for (int i = 0; i < previousSleepTimes.Length; i += 1) for (int i = 0; i < previousSleepTimes.Length; i += 1)
{ {
@ -75,15 +63,18 @@ namespace MoonWorks
Inputs = new Inputs(); Inputs = new Inputs();
Window = new Window(windowCreateInfo);
GraphicsDevice = new GraphicsDevice( GraphicsDevice = new GraphicsDevice(
Window.Handle,
Backend.Vulkan, Backend.Vulkan,
moonWorksToRefreshPresentMode[presentMode],
debugMode debugMode
); );
MainWindow = new Window(windowCreateInfo, GraphicsDevice.WindowFlags);
if (!GraphicsDevice.ClaimWindow(MainWindow, presentMode))
{
throw new System.SystemException("Could not claim window!");
}
AudioDevice = new AudioDevice(); AudioDevice = new AudioDevice();
} }
@ -97,12 +88,26 @@ namespace MoonWorks
Destroy(); Destroy();
AudioDevice.Dispose(); AudioDevice.Dispose();
MainWindow.Dispose();
GraphicsDevice.Dispose(); GraphicsDevice.Dispose();
Window.Dispose();
SDL.SDL_Quit(); 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 Update(TimeSpan delta);
protected abstract void Draw(double alpha); protected abstract void Draw(double alpha);
protected virtual void Destroy() {} protected virtual void Destroy() {}
@ -225,7 +230,14 @@ namespace MoonWorks
{ {
if (evt.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED) 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();
} }
} }