can change FrameLimiterSettings at runtime

cosmonaut 2022-09-29 13:57:01 -07:00
parent 7d3bf4843a
commit 1a07a71745
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 TimeSpan FramerateCapTimeSpan = TimeSpan.Zero;
public Window Window { get; }
public GraphicsDevice GraphicsDevice { get; }
public AudioDevice AudioDevice { get; }
public Inputs Inputs { get; }
private Dictionary<PresentMode, RefreshCS.Refresh.PresentMode> moonWorksToRefreshPresentMode = new Dictionary<PresentMode, RefreshCS.Refresh.PresentMode>
{
{ 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();
}
}