diff --git a/src/SlatedGameToolkit.Framework/GameEngine.cs b/src/SlatedGameToolkit.Framework/GameEngine.cs index 441c6be..fc938c3 100644 --- a/src/SlatedGameToolkit.Framework/GameEngine.cs +++ b/src/SlatedGameToolkit.Framework/GameEngine.cs @@ -163,7 +163,7 @@ namespace SlatedGameToolkit.Framework { timePassedFromLastUpdate += difference; while (timePassedFromLastUpdate > updateDeltaTime) { //Updates. - manager.update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds); + manager.Update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds); timePassedFromLastUpdate -= updateDeltaTime; if (updateDeltaTime.TotalSeconds <= 0) { timePassedFromLastUpdate = TimeSpan.Zero; @@ -174,14 +174,14 @@ namespace SlatedGameToolkit.Framework { timePassedFromLastRender += difference; if (timePassedFromLastRender > frameDeltaTime) { //Draw calls. - manager.render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime)); + manager.Render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime)); timePassedFromLastRender = TimeSpan.Zero; } else { Thread.Yield(); } } stopped = true; - manager.Dispose(); + manager.Deinitialize(); SDL.SDL_Quit(); SoLoudEngine.deinit(); WindowContextsManager.DisposeAllWindowContexts(); diff --git a/src/SlatedGameToolkit.Framework/Logging/LogLevel.cs b/src/SlatedGameToolkit.Framework/Logging/LogLevel.cs index fd0448f..45edfa5 100644 --- a/src/SlatedGameToolkit.Framework/Logging/LogLevel.cs +++ b/src/SlatedGameToolkit.Framework/Logging/LogLevel.cs @@ -4,7 +4,7 @@ namespace SlatedGameToolkit.Framework.Logging { FATAL, INFO, - DEBUG, WARNING, + DEBUG, } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/StateSystem/StateManager.cs b/src/SlatedGameToolkit.Framework/StateSystem/StateManager.cs index e268c2c..324cb41 100644 --- a/src/SlatedGameToolkit.Framework/StateSystem/StateManager.cs +++ b/src/SlatedGameToolkit.Framework/StateSystem/StateManager.cs @@ -5,18 +5,18 @@ using System.Threading; using SlatedGameToolkit.Framework.Graphics.Window; using SlatedGameToolkit.Framework.StateSystem.States; using SlatedGameToolkit.Framework.Utilities; -using SlatedGameToolkit.Framework.Graphics; using SlatedGameToolkit.Framework.Graphics.OpenGL; using SlatedGameToolkit.Framework.Logging; +using System.Collections.Concurrent; namespace SlatedGameToolkit.Framework.StateSystem { - public sealed class StateManager : IDisposable { + public sealed class StateManager { public Thread thread; public Color backgroundColour; private IState currentState; private IState nextState; - private Dictionary states; + private ConcurrentDictionary states; /// /// 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() { backgroundColour = Color.Orange; - this.states = new Dictionary(); + this.states = new ConcurrentDictionary(); } internal void Initialize(IState initialState) { if (initialState == null) throw new ArgumentNullException("initialState"); + Logger.Log("Initialized state manager with state: " + initialState.getName()); thread = Thread.CurrentThread; currentState = initialState; - addState(initialState); + AddState(initialState); } - internal void update(double timeStep) { + internal void Update(double timeStep) { if (nextState != null) { if (currentState.Deactivate() & nextState.Activate()) { currentState = nextState; @@ -48,7 +49,7 @@ namespace SlatedGameToolkit.Framework.StateSystem currentState.Update(timeStep); } - internal void render(double delta) { + internal void Render(double delta) { WindowContext windowContext = WindowContextsManager.CurrentWindowContext; windowContext.Context.ClearColor(backgroundColour.RedAsFloat(), backgroundColour.GreenAsFloat(), backgroundColour.BlueAsFloat(), 1f); 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. /// /// The name of the state that we are turning to. - 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 (!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 /// /// The states to add. /// True if all the states were unique, and false if there were repetitions determined by name. - 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"); bool unique = true; foreach (IState state in states) { - unique = unique && addState(state); + unique = unique && AddState(state); } return unique; } @@ -90,13 +91,11 @@ namespace SlatedGameToolkit.Framework.StateSystem /// /// The state to be added to this manager. /// False if a state of this name has already been registered. - 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."); - try { - Logger.Log("Adding state: " + state.getName(), LogLevel.WARNING); - this.states.Add(state.getName(), state); - } catch (ArgumentException) { - return false; + Logger.Log("Adding state: " + state.getName(), LogLevel.DEBUG); + if (!this.states.TryAdd(state.getName(), state)) { + Logger.Log(string.Format("State with name \"{0}\" already exists in manager.", state.getName()), LogLevel.WARNING); } state.Initialize(this); return true; @@ -109,7 +108,7 @@ namespace SlatedGameToolkit.Framework.StateSystem /// /// The name of the state. /// False if the state is being used, or the state doesn't exist. - 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 (states[name] == currentState) return false; IState state = states[name]; @@ -120,27 +119,26 @@ namespace SlatedGameToolkit.Framework.StateSystem Logger.Log(e.ToString(), LogLevel.WARNING); Logger.Log("Failed to deinitialize state: " + state.getName(), LogLevel.WARNING); } - return states.Remove(name); + IState removedState; + return states.Remove(name, out removedState); } /// /// Removes all the states in this state manager except for the current one. - /// Disposes of the removed states. + /// Deinitializes the removed states. /// - public void removeAllStates() { - Logger.Log("Beginning to remove all states...", LogLevel.DEBUG); + public void RemoveAllStates() { 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; currentState = null; - removeAllStates(); + RemoveAllStates(); + Logger.Log("Deinitializing state manager."); } } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs b/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs index 7d69131..0b0868f 100644 --- a/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs +++ b/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs @@ -10,7 +10,7 @@ namespace SlatedGameToolkit.Tools.Commands public class GraphicalPlaygroundCommand : IInvocable { private readonly string[] invokers = new string[] {"playground"}; - private ConsoleLogListener logListener; + private ConsolePlaygroundListener logListener; private bool debugging; public bool Execute(IInteractable interactable, string[] args) @@ -22,8 +22,6 @@ namespace SlatedGameToolkit.Tools.Commands interactable.Tell("Engine is already running!"); return true; } - Logger.AddLogListener((logListener = new ConsoleLogListener(interactable as ConsoleInteraction))); - logListener.Debug = debugging; GameEngine.Ignite(new MainState()); return true; } else if (args[0].Equals("stop")) { @@ -31,7 +29,6 @@ namespace SlatedGameToolkit.Tools.Commands interactable.Tell("Engine was never running!"); return true; } - Logger.RemoveLogListener(logListener); GameEngine.Stop(); return true; } 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")); } 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; } diff --git a/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs b/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsolePlaygroundListener.cs similarity index 75% rename from src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs rename to src/SlatedGameToolkit.Tools/Utilities/Playground/ConsolePlaygroundListener.cs index d75fda1..275bd44 100644 --- a/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs +++ b/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsolePlaygroundListener.cs @@ -4,21 +4,21 @@ using SlatedGameToolkit.Tools.CommandSystem.Interaction; namespace SlatedGameToolkit.Tools.Utilities.Playground { - public class ConsoleLogListener : ILogListener + public class ConsolePlaygroundListener : ILogListener { public bool Debug { get; set; } public LogLevel Level => Debug ? LogLevel.DEBUG : LogLevel.INFO; private ConsoleInteraction interaction; - public ConsoleLogListener(ConsoleInteraction interaction = null) { + public ConsolePlaygroundListener(ConsoleInteraction interaction = null) { this.interaction = interaction; } public void LogMesesage(string message, DateTime time, LogLevel level) { 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) { Console.SetCursorPosition(0, Console.CursorTop); Console.Write(interaction.Prefix + "> ");