Update Game, add IGame.
This commit is contained in:
@@ -1,43 +1,51 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Voile.Input;
|
||||
using Voile.Rendering;
|
||||
using Voile.Resources;
|
||||
|
||||
namespace Voile
|
||||
{
|
||||
public interface IGame
|
||||
{
|
||||
string Name { get; }
|
||||
/// <summary>
|
||||
/// Resource root path for this game.
|
||||
/// </summary>
|
||||
string ResourceRoot { get; }
|
||||
void Start();
|
||||
void Run();
|
||||
void Shutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entry point for the Voile game.
|
||||
/// </summary>
|
||||
public abstract class Game
|
||||
public abstract class Game : IGame
|
||||
{
|
||||
public double UpdateTimeStep { get; set; } = 1.0 / 60.0;
|
||||
public TimeSpan UpdateFrameTime { get; private set; }
|
||||
public TimeSpan RenderFrameTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ResourceManager associated with this game.
|
||||
/// </summary>
|
||||
protected ResourceManager ResourceManager { get; private set; }
|
||||
protected ResourceManager ResourceManager { get; private set; } = new();
|
||||
/// <summary>
|
||||
/// The InputHandler associated with this game. Uses <see cref="RaylibInputSystem"/> by default.
|
||||
/// </summary>
|
||||
protected InputSystem Input { get; private set; }
|
||||
protected InputSystem? Input { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Renderer associated with this game. Uses <see cref="RaylibRenderSystem"/> by default.
|
||||
/// </summary>
|
||||
protected RenderSystem Renderer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Resource root path for this game.
|
||||
/// </summary>
|
||||
public virtual string ResourceRoot => "Resources/";
|
||||
|
||||
/// <summary>
|
||||
/// Path to the engine configuration file.
|
||||
/// </summary>
|
||||
public virtual string EngineConfigPath => "engine.config";
|
||||
protected RenderSystem? Renderer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of this game. Also used as a default window title.
|
||||
/// </summary>
|
||||
public virtual string Name => "";
|
||||
public virtual string Name => "Voile Game";
|
||||
public virtual string ResourceRoot => "Resources/";
|
||||
|
||||
/// <summary>
|
||||
/// Default game constructor.
|
||||
@@ -45,19 +53,16 @@ namespace Voile
|
||||
public Game()
|
||||
{
|
||||
ResourceManager = new ResourceManager();
|
||||
Input = new RaylibInputSystem();
|
||||
Renderer = new RaylibRenderSystem();
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Game constructor with custom systems.
|
||||
/// </summary>
|
||||
/// <param name="resourceManager"></param>
|
||||
/// <param name="inputSystem"></param>
|
||||
/// <param name="renderSystem"></param>
|
||||
public Game(ResourceManager resourceManager, InputSystem inputSystem, RenderSystem renderSystem)
|
||||
public Game(InputSystem inputSystem, RenderSystem renderSystem)
|
||||
{
|
||||
ResourceManager = resourceManager;
|
||||
Input = inputSystem;
|
||||
Renderer = renderSystem;
|
||||
}
|
||||
@@ -83,83 +88,77 @@ namespace Voile
|
||||
throw new NullReferenceException("No ResourceManager provided.");
|
||||
}
|
||||
|
||||
Initialize();
|
||||
LoadResources();
|
||||
Ready();
|
||||
Run();
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes subsystems using default types and settings.
|
||||
/// Initializes systems using default types and settings.
|
||||
/// </summary>
|
||||
public void InitializeDefault()
|
||||
public void InitializeSystemsDefault()
|
||||
{
|
||||
ResourceManager.ResourceRoot = ResourceRoot;
|
||||
|
||||
Input = new RaylibInputSystem();
|
||||
Renderer = new RaylibRenderSystem();
|
||||
if (Renderer is null)
|
||||
{
|
||||
Renderer = new RaylibRenderSystem();
|
||||
}
|
||||
|
||||
if (Input is null)
|
||||
{
|
||||
Input = new RaylibInputSystem();
|
||||
}
|
||||
|
||||
Input.Start();
|
||||
|
||||
InitializeRenderer();
|
||||
}
|
||||
|
||||
public void ShutdownDefault()
|
||||
{
|
||||
Input.Dispose();
|
||||
Renderer.Dispose();
|
||||
Input?.Dispose();
|
||||
Renderer?.Dispose();
|
||||
ResourceManager.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when it's time to initialize the subsystems, or modify default game settings or system settings.
|
||||
/// Adds a system that will start together with the game.
|
||||
/// </summary>
|
||||
public abstract void Initialize();
|
||||
/// <param name="system"></param>
|
||||
public void AddSystem([DisallowNull] IStartableSystem system)
|
||||
{
|
||||
_startableSystems.Add(system);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when it's safe to manipulate the resources or/and systems.
|
||||
/// You can safely create game objects, scenes, etc. in this method.
|
||||
/// Adds a system to the Update step.
|
||||
/// </summary>
|
||||
protected abstract void Ready();
|
||||
/// <param name="system"></param>
|
||||
public void AddSystemToUpdate([DisallowNull] IUpdatableSystem system)
|
||||
{
|
||||
_updatableSystems.Add(system);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a system to the render step.
|
||||
/// </summary>
|
||||
/// <param name="system"></param>
|
||||
public void AddSystemToRender([DisallowNull] IUpdatableSystem system)
|
||||
{
|
||||
_renderableSystems.Add(system);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when everything has been readied to start the main loop.
|
||||
/// </summary>
|
||||
protected abstract void Run();
|
||||
/// <summary>
|
||||
/// Called when the application quits and it's safe to clean up.
|
||||
/// </summary>
|
||||
public abstract void Shutdown();
|
||||
|
||||
private void InitializeRenderer()
|
||||
public void Run()
|
||||
{
|
||||
var windowSettings = WindowSettings.Default;
|
||||
if (Renderer is null)
|
||||
{
|
||||
throw new NullReferenceException("No renderer provided.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
windowSettings.Title = Name;
|
||||
|
||||
Renderer.Start(RendererSettings.Default);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseGame : Game
|
||||
{
|
||||
public double UpdateTimeStep { get; set; } = 1.0 / 60.0;
|
||||
public TimeSpan UpdateFrameTime { get; private set; }
|
||||
public TimeSpan RenderFrameTime { get; private set; }
|
||||
public override void Initialize()
|
||||
{
|
||||
LoadResources();
|
||||
InitializeDefault();
|
||||
}
|
||||
|
||||
public override void Shutdown() => ShutdownDefault();
|
||||
|
||||
/// <summary>
|
||||
/// Called when it's time to load the game's resources, such as images or sounds.
|
||||
/// </summary>
|
||||
protected abstract void LoadResources();
|
||||
|
||||
protected override void Run()
|
||||
{
|
||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||
double previousTime = stopwatch.Elapsed.TotalSeconds;
|
||||
|
||||
@@ -173,11 +172,22 @@ namespace Voile
|
||||
|
||||
while (_accumulator >= UpdateTimeStep)
|
||||
{
|
||||
foreach (var system in _updatableSystems)
|
||||
{
|
||||
system.Update(UpdateTimeStep);
|
||||
}
|
||||
|
||||
Update(UpdateTimeStep);
|
||||
_accumulator -= UpdateTimeStep;
|
||||
}
|
||||
|
||||
Renderer.BeginFrame();
|
||||
|
||||
foreach (var system in _renderableSystems)
|
||||
{
|
||||
system.Update(Renderer.FrameTime);
|
||||
}
|
||||
|
||||
Render(Renderer.FrameTime);
|
||||
Renderer.EndFrame();
|
||||
|
||||
@@ -185,6 +195,21 @@ namespace Voile
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when it's time to initialize the subsystems, or modify default game settings or system settings.
|
||||
/// </summary>
|
||||
public abstract void Initialize();
|
||||
/// <summary>
|
||||
/// Called when it's safe to manipulate the resources or/and systems.
|
||||
/// You can safely create game objects, scenes, etc. in this method.
|
||||
/// </summary>
|
||||
protected abstract void Ready();
|
||||
|
||||
/// <summary>
|
||||
/// Called when it's time to load the game's resources, such as images or sounds.
|
||||
/// </summary>
|
||||
protected abstract void LoadResources();
|
||||
|
||||
/// <summary>
|
||||
/// Triggered on each fixed timestep. Update your game's state here.
|
||||
/// </summary>
|
||||
@@ -196,6 +221,28 @@ namespace Voile
|
||||
/// <param name="deltaTime"></param>
|
||||
protected abstract void Render(double deltaTime);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the application quits and it's safe to clean up.
|
||||
/// </summary>
|
||||
public abstract void Shutdown();
|
||||
|
||||
private void InitializeRenderer()
|
||||
{
|
||||
var windowSettings = WindowSettings.Default;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
windowSettings.Title = Name;
|
||||
|
||||
var renderSettings = RendererSettings.Default;
|
||||
renderSettings.WindowSettings = windowSettings;
|
||||
|
||||
Renderer?.Start(renderSettings);
|
||||
}
|
||||
|
||||
private List<IStartableSystem> _startableSystems = new();
|
||||
private List<IUpdatableSystem> _updatableSystems = new();
|
||||
private List<IUpdatableSystem> _renderableSystems = new();
|
||||
|
||||
private double _accumulator;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user