Refactoring, added more logging, minor command structure change.
This commit is contained in:
parent
63a31c491d
commit
b2a47ebefc
@ -163,7 +163,7 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
timePassedFromLastUpdate += difference;
|
timePassedFromLastUpdate += difference;
|
||||||
while (timePassedFromLastUpdate > updateDeltaTime) {
|
while (timePassedFromLastUpdate > updateDeltaTime) {
|
||||||
//Updates.
|
//Updates.
|
||||||
manager.update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds);
|
manager.Update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds);
|
||||||
timePassedFromLastUpdate -= updateDeltaTime;
|
timePassedFromLastUpdate -= updateDeltaTime;
|
||||||
if (updateDeltaTime.TotalSeconds <= 0) {
|
if (updateDeltaTime.TotalSeconds <= 0) {
|
||||||
timePassedFromLastUpdate = TimeSpan.Zero;
|
timePassedFromLastUpdate = TimeSpan.Zero;
|
||||||
@ -174,14 +174,14 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
timePassedFromLastRender += difference;
|
timePassedFromLastRender += difference;
|
||||||
if (timePassedFromLastRender > frameDeltaTime) {
|
if (timePassedFromLastRender > frameDeltaTime) {
|
||||||
//Draw calls.
|
//Draw calls.
|
||||||
manager.render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime));
|
manager.Render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime));
|
||||||
timePassedFromLastRender = TimeSpan.Zero;
|
timePassedFromLastRender = TimeSpan.Zero;
|
||||||
} else {
|
} else {
|
||||||
Thread.Yield();
|
Thread.Yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stopped = true;
|
stopped = true;
|
||||||
manager.Dispose();
|
manager.Deinitialize();
|
||||||
SDL.SDL_Quit();
|
SDL.SDL_Quit();
|
||||||
SoLoudEngine.deinit();
|
SoLoudEngine.deinit();
|
||||||
WindowContextsManager.DisposeAllWindowContexts();
|
WindowContextsManager.DisposeAllWindowContexts();
|
||||||
|
@ -4,7 +4,7 @@ namespace SlatedGameToolkit.Framework.Logging
|
|||||||
{
|
{
|
||||||
FATAL,
|
FATAL,
|
||||||
INFO,
|
INFO,
|
||||||
DEBUG,
|
|
||||||
WARNING,
|
WARNING,
|
||||||
|
DEBUG,
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,18 +5,18 @@ using System.Threading;
|
|||||||
using SlatedGameToolkit.Framework.Graphics.Window;
|
using SlatedGameToolkit.Framework.Graphics.Window;
|
||||||
using SlatedGameToolkit.Framework.StateSystem.States;
|
using SlatedGameToolkit.Framework.StateSystem.States;
|
||||||
using SlatedGameToolkit.Framework.Utilities;
|
using SlatedGameToolkit.Framework.Utilities;
|
||||||
using SlatedGameToolkit.Framework.Graphics;
|
|
||||||
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
||||||
using SlatedGameToolkit.Framework.Logging;
|
using SlatedGameToolkit.Framework.Logging;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace SlatedGameToolkit.Framework.StateSystem
|
namespace SlatedGameToolkit.Framework.StateSystem
|
||||||
{
|
{
|
||||||
public sealed class StateManager : IDisposable {
|
public sealed class StateManager {
|
||||||
public Thread thread;
|
public Thread thread;
|
||||||
public Color backgroundColour;
|
public Color backgroundColour;
|
||||||
private IState currentState;
|
private IState currentState;
|
||||||
private IState nextState;
|
private IState nextState;
|
||||||
private Dictionary<string, IState> states;
|
private ConcurrentDictionary<string, IState> states;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Instantiates a game state manager with an initial state, and a set of states to be added at the start.
|
/// Instantiates a game state manager with an initial state, and a set of states to be added at the start.
|
||||||
@ -28,17 +28,18 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
internal StateManager() {
|
internal StateManager() {
|
||||||
backgroundColour = Color.Orange;
|
backgroundColour = Color.Orange;
|
||||||
|
|
||||||
this.states = new Dictionary<string, IState>();
|
this.states = new ConcurrentDictionary<string, IState>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Initialize(IState initialState) {
|
internal void Initialize(IState initialState) {
|
||||||
if (initialState == null) throw new ArgumentNullException("initialState");
|
if (initialState == null) throw new ArgumentNullException("initialState");
|
||||||
|
Logger.Log("Initialized state manager with state: " + initialState.getName());
|
||||||
thread = Thread.CurrentThread;
|
thread = Thread.CurrentThread;
|
||||||
currentState = initialState;
|
currentState = initialState;
|
||||||
addState(initialState);
|
AddState(initialState);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void update(double timeStep) {
|
internal void Update(double timeStep) {
|
||||||
if (nextState != null) {
|
if (nextState != null) {
|
||||||
if (currentState.Deactivate() & nextState.Activate()) {
|
if (currentState.Deactivate() & nextState.Activate()) {
|
||||||
currentState = nextState;
|
currentState = nextState;
|
||||||
@ -48,7 +49,7 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
currentState.Update(timeStep);
|
currentState.Update(timeStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void render(double delta) {
|
internal void Render(double delta) {
|
||||||
WindowContext windowContext = WindowContextsManager.CurrentWindowContext;
|
WindowContext windowContext = WindowContextsManager.CurrentWindowContext;
|
||||||
windowContext.Context.ClearColor(backgroundColour.RedAsFloat(), backgroundColour.GreenAsFloat(), backgroundColour.BlueAsFloat(), 1f);
|
windowContext.Context.ClearColor(backgroundColour.RedAsFloat(), backgroundColour.GreenAsFloat(), backgroundColour.BlueAsFloat(), 1f);
|
||||||
windowContext.Context.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
windowContext.Context.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
@ -60,7 +61,7 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
/// Begins a state change. The current stage will be notified, and so will the state that is being changed to.
|
/// Begins a state change. The current stage will be notified, and so will the state that is being changed to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">The name of the state that we are turning to.</param>
|
/// <param name="name">The name of the state that we are turning to.</param>
|
||||||
public void changeState(string name) {
|
public void ChangeState(string name) {
|
||||||
if (thread != Thread.CurrentThread) throw new ThreadStateException("State cannot be changed from a different thread.");
|
if (thread != Thread.CurrentThread) throw new ThreadStateException("State cannot be changed from a different thread.");
|
||||||
if (!states.TryGetValue(name, out nextState)) throw new ArgumentException("The requested state to change to does not exist in this manager.");
|
if (!states.TryGetValue(name, out nextState)) throw new ArgumentException("The requested state to change to does not exist in this manager.");
|
||||||
}
|
}
|
||||||
@ -73,12 +74,12 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="states">The states to add.</param>
|
/// <param name="states">The states to add.</param>
|
||||||
/// <returns>True if all the states were unique, and false if there were repetitions determined by name.</returns>
|
/// <returns>True if all the states were unique, and false if there were repetitions determined by name.</returns>
|
||||||
public bool addStates(IState[] states) {
|
public bool AddStates(IState[] states) {
|
||||||
if (states == null || states.Length == 0) throw new ArgumentException("The array of states cannot be null, and cannot be of size 0");
|
if (states == null || states.Length == 0) throw new ArgumentException("The array of states cannot be null, and cannot be of size 0");
|
||||||
bool unique = true;
|
bool unique = true;
|
||||||
foreach (IState state in states)
|
foreach (IState state in states)
|
||||||
{
|
{
|
||||||
unique = unique && addState(state);
|
unique = unique && AddState(state);
|
||||||
}
|
}
|
||||||
return unique;
|
return unique;
|
||||||
}
|
}
|
||||||
@ -90,13 +91,11 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="state">The state to be added to this manager.</param>
|
/// <param name="state">The state to be added to this manager.</param>
|
||||||
/// <returns>False if a state of this name has already been registered.</returns>
|
/// <returns>False if a state of this name has already been registered.</returns>
|
||||||
public bool addState(IState state) {
|
public bool AddState(IState state) {
|
||||||
if (thread != Thread.CurrentThread) throw new ThreadStateException("Cannot add a state from a different thread.");
|
if (thread != Thread.CurrentThread) throw new ThreadStateException("Cannot add a state from a different thread.");
|
||||||
try {
|
Logger.Log("Adding state: " + state.getName(), LogLevel.DEBUG);
|
||||||
Logger.Log("Adding state: " + state.getName(), LogLevel.WARNING);
|
if (!this.states.TryAdd(state.getName(), state)) {
|
||||||
this.states.Add(state.getName(), state);
|
Logger.Log(string.Format("State with name \"{0}\" already exists in manager.", state.getName()), LogLevel.WARNING);
|
||||||
} catch (ArgumentException) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
state.Initialize(this);
|
state.Initialize(this);
|
||||||
return true;
|
return true;
|
||||||
@ -109,7 +108,7 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">The name of the state.</param>
|
/// <param name="name">The name of the state.</param>
|
||||||
/// <returns>False if the state is being used, or the state doesn't exist.</returns>
|
/// <returns>False if the state is being used, or the state doesn't exist.</returns>
|
||||||
public bool removeState(string name) {
|
public bool RemoveState(string name) {
|
||||||
if (thread != Thread.CurrentThread) throw new ThreadStateException("Cannot remove a state from a different thread.");
|
if (thread != Thread.CurrentThread) throw new ThreadStateException("Cannot remove a state from a different thread.");
|
||||||
if (states[name] == currentState) return false;
|
if (states[name] == currentState) return false;
|
||||||
IState state = states[name];
|
IState state = states[name];
|
||||||
@ -120,27 +119,26 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
Logger.Log(e.ToString(), LogLevel.WARNING);
|
Logger.Log(e.ToString(), LogLevel.WARNING);
|
||||||
Logger.Log("Failed to deinitialize state: " + state.getName(), LogLevel.WARNING);
|
Logger.Log("Failed to deinitialize state: " + state.getName(), LogLevel.WARNING);
|
||||||
}
|
}
|
||||||
return states.Remove(name);
|
IState removedState;
|
||||||
|
return states.Remove(name, out removedState);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all the states in this state manager except for the current one.
|
/// Removes all the states in this state manager except for the current one.
|
||||||
/// Disposes of the removed states.
|
/// Deinitializes the removed states.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void removeAllStates() {
|
public void RemoveAllStates() {
|
||||||
Logger.Log("Beginning to remove all states...", LogLevel.DEBUG);
|
|
||||||
foreach (String state in this.states.Keys)
|
foreach (String state in this.states.Keys)
|
||||||
{
|
{
|
||||||
removeState(state);
|
RemoveState(state);
|
||||||
}
|
}
|
||||||
Logger.Log("Completed removing all states...", LogLevel.DEBUG);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
internal void Deinitialize() {
|
||||||
{
|
|
||||||
if (currentState == null) return;
|
if (currentState == null) return;
|
||||||
currentState = null;
|
currentState = null;
|
||||||
removeAllStates();
|
RemoveAllStates();
|
||||||
|
Logger.Log("Deinitializing state manager.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ namespace SlatedGameToolkit.Tools.Commands
|
|||||||
public class GraphicalPlaygroundCommand : IInvocable
|
public class GraphicalPlaygroundCommand : IInvocable
|
||||||
{
|
{
|
||||||
private readonly string[] invokers = new string[] {"playground"};
|
private readonly string[] invokers = new string[] {"playground"};
|
||||||
private ConsoleLogListener logListener;
|
private ConsolePlaygroundListener logListener;
|
||||||
private bool debugging;
|
private bool debugging;
|
||||||
|
|
||||||
public bool Execute(IInteractable interactable, string[] args)
|
public bool Execute(IInteractable interactable, string[] args)
|
||||||
@ -22,8 +22,6 @@ namespace SlatedGameToolkit.Tools.Commands
|
|||||||
interactable.Tell("Engine is already running!");
|
interactable.Tell("Engine is already running!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Logger.AddLogListener((logListener = new ConsoleLogListener(interactable as ConsoleInteraction)));
|
|
||||||
logListener.Debug = debugging;
|
|
||||||
GameEngine.Ignite(new MainState());
|
GameEngine.Ignite(new MainState());
|
||||||
return true;
|
return true;
|
||||||
} else if (args[0].Equals("stop")) {
|
} else if (args[0].Equals("stop")) {
|
||||||
@ -31,7 +29,6 @@ namespace SlatedGameToolkit.Tools.Commands
|
|||||||
interactable.Tell("Engine was never running!");
|
interactable.Tell("Engine was never running!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Logger.RemoveLogListener(logListener);
|
|
||||||
GameEngine.Stop();
|
GameEngine.Stop();
|
||||||
return true;
|
return true;
|
||||||
} else if (args[0].Equals("status")) {
|
} else if (args[0].Equals("status")) {
|
||||||
@ -49,6 +46,17 @@ namespace SlatedGameToolkit.Tools.Commands
|
|||||||
interactable.Tell(string.Format("Debug will be turned {0} when playground is started.", debugging ? "on" : "off"));
|
interactable.Tell(string.Format("Debug will be turned {0} when playground is started.", debugging ? "on" : "off"));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
} else if (args[0].Equals("log")) {
|
||||||
|
if (logListener == null) {
|
||||||
|
Logger.AddLogListener((logListener = new ConsolePlaygroundListener(interactable as ConsoleInteraction)));
|
||||||
|
logListener.Debug = debugging;
|
||||||
|
interactable.Tell("Listening to game engine's logging.");
|
||||||
|
} else {
|
||||||
|
Logger.RemoveLogListener(logListener);
|
||||||
|
logListener = null;
|
||||||
|
interactable.Tell("Stopped listening to game engine's logging.");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -4,21 +4,21 @@ using SlatedGameToolkit.Tools.CommandSystem.Interaction;
|
|||||||
|
|
||||||
namespace SlatedGameToolkit.Tools.Utilities.Playground
|
namespace SlatedGameToolkit.Tools.Utilities.Playground
|
||||||
{
|
{
|
||||||
public class ConsoleLogListener : ILogListener
|
public class ConsolePlaygroundListener : ILogListener
|
||||||
{
|
{
|
||||||
|
|
||||||
public bool Debug { get; set; }
|
public bool Debug { get; set; }
|
||||||
public LogLevel Level => Debug ? LogLevel.DEBUG : LogLevel.INFO;
|
public LogLevel Level => Debug ? LogLevel.DEBUG : LogLevel.INFO;
|
||||||
private ConsoleInteraction interaction;
|
private ConsoleInteraction interaction;
|
||||||
|
|
||||||
public ConsoleLogListener(ConsoleInteraction interaction = null) {
|
public ConsolePlaygroundListener(ConsoleInteraction interaction = null) {
|
||||||
this.interaction = interaction;
|
this.interaction = interaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LogMesesage(string message, DateTime time, LogLevel level)
|
public void LogMesesage(string message, DateTime time, LogLevel level)
|
||||||
{
|
{
|
||||||
Console.SetCursorPosition(0, Console.CursorTop);
|
Console.SetCursorPosition(0, Console.CursorTop);
|
||||||
Console.WriteLine(string.Format("Playground [{0}] [{1}]: {2}", level, time.ToString("H:mm:ss"), message));
|
Console.WriteLine(string.Format("Engine [{0}] [{1}]: {2}", level, time.ToString("H:mm:ss"), message));
|
||||||
if (interaction != null && interaction.Listening) {
|
if (interaction != null && interaction.Listening) {
|
||||||
Console.SetCursorPosition(0, Console.CursorTop);
|
Console.SetCursorPosition(0, Console.CursorTop);
|
||||||
Console.Write(interaction.Prefix + "> ");
|
Console.Write(interaction.Prefix + "> ");
|
Loading…
Reference in New Issue
Block a user