Files
Voile/Voile/Source/Game.cs

146 lines
4.5 KiB
C#

using Voile.Input;
using Voile.Rendering;
using Voile.Resources;
namespace Voile
{
/// <summary>
/// Entry point for the Voile game.
/// </summary>
public abstract class Game
{
/// <summary>
/// The ResourceManager associated with this game.
/// </summary>
protected ResourceManager ResourceManager { get; private set; }
/// <summary>
/// The InputHandler associated with this game. Uses <see cref="RaylibInputSystem"/> by default.
/// </summary>
protected InputSystem Input { get; private 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";
/// <summary>
/// Name of this game. Also used as a default window title.
/// </summary>
public virtual string Name => "";
/// <summary>
/// Default game constructor.
/// </summary>
public Game()
{
ResourceManager = new ResourceManager();
Input = new RaylibInputSystem();
Renderer = new RaylibRenderSystem();
}
/// <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)
{
ResourceManager = resourceManager;
Input = inputSystem;
Renderer = renderSystem;
}
/// <summary>
/// Starts the game application.
/// This involves initializing the required subsystems, loading resources from the disk, and then preparing the subsystems for running in the main loop.
/// </summary>
public void Start()
{
if (Renderer is null)
{
throw new NullReferenceException("No renderer provided.");
}
if (Input is null)
{
throw new NullReferenceException("No input system provided.");
}
if (ResourceManager is null)
{
throw new NullReferenceException("No ResourceManager provided.");
}
Initialize();
LoadResources();
Ready();
Run();
Shutdown();
}
/// <summary>
/// Initializes subsystems using default types and settings.
/// </summary>
public void InitializeDefault()
{
ResourceManager.ResourceRoot = ResourceRoot;
Input = new RaylibInputSystem();
Renderer = new RaylibRenderSystem();
Input.Start();
InitializeRenderer();
}
public void ShutdownDefault()
{
Input.Dispose();
Renderer.Dispose();
ResourceManager.Dispose();
}
/// <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 time to load the game's resources, such as images or sounds.
/// </summary>
protected abstract void LoadResources();
/// <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 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()
{
var windowSettings = WindowSettings.Default;
if (!string.IsNullOrWhiteSpace(Name))
windowSettings.Title = Name;
Renderer.Start(RendererSettings.Default);
}
}
}