From a9647f2146ee90eead2733c063a4cb6a1dc00bda Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 30 Oct 2018 18:29:54 -0500 Subject: [PATCH] Content loading system functioning. --- RhythmBullet/RhythmBullet.csproj | 9 ++- RhythmBullet/RhythmBulletGame.cs | 53 +++++++++++--- .../ContentResolvers/FontContentResolver.cs | 6 +- .../ResolutionContentResolver.cs | 4 +- .../Zer01HD/Game/Preferences/Controls.cs | 20 +++--- .../Zer01HD/Game/Preferences/General.cs | 20 +++--- .../Utilities/ContentSystem/ContentSystem.cs | 69 ++++++++++++------- ...entResolver.cs => IContentPathModifier.cs} | 4 +- .../ContentSystem/NormalContentResolver.cs | 16 +++++ .../Zer01HD/Utilities/DataTypes/Resolution.cs | 4 ++ .../{IPreferences.cs => Preferences.cs} | 3 +- .../Persistence/PreferencesManager.cs | 54 +++++++++++---- .../Utilities/Renderer/ScaledRenderer.cs | 12 ++++ RhythmBullet/Zer01HD/Utilities/UI/Screen.cs | 2 +- 14 files changed, 189 insertions(+), 87 deletions(-) rename RhythmBullet/Zer01HD/Utilities/ContentSystem/{IContentResolver.cs => IContentPathModifier.cs} (82%) create mode 100644 RhythmBullet/Zer01HD/Utilities/ContentSystem/NormalContentResolver.cs rename RhythmBullet/Zer01HD/Utilities/Persistence/{IPreferences.cs => Preferences.cs} (75%) create mode 100644 RhythmBullet/Zer01HD/Utilities/Renderer/ScaledRenderer.cs diff --git a/RhythmBullet/RhythmBullet.csproj b/RhythmBullet/RhythmBullet.csproj index 59d09a0..d97fbe2 100644 --- a/RhythmBullet/RhythmBullet.csproj +++ b/RhythmBullet/RhythmBullet.csproj @@ -43,8 +43,13 @@ app.manifest + + true + - + + + @@ -60,7 +65,7 @@ - + diff --git a/RhythmBullet/RhythmBulletGame.cs b/RhythmBullet/RhythmBulletGame.cs index 54fc58b..fed9f6d 100644 --- a/RhythmBullet/RhythmBulletGame.cs +++ b/RhythmBullet/RhythmBulletGame.cs @@ -7,8 +7,10 @@ using RhythmBullet.Zer01HD.Game.Preferences; using RhythmBullet.Zer01HD.UI; using RhythmBullet.Zer01HD.Utilities; using RhythmBullet.Zer01HD.Utilities.ContentSystem; +using RhythmBullet.Zer01HD.Utilities.DataTypes; using RhythmBullet.Zer01HD.Utilities.UI; using System; +using System.Diagnostics; namespace RhythmBullet { @@ -17,6 +19,10 @@ namespace RhythmBullet /// public class RhythmBulletGame : Game { + public const int WORLD_WIDTH = 4; + public const int WORLD_HEIGHT = 3; + public static int pixels_per_WU; + public GraphicsDeviceManager Graphics; SpriteBatch spriteBatch; public readonly ContentSystem Assets; @@ -34,12 +40,11 @@ namespace RhythmBullet Assets = new ContentSystem(Content); resolutionContentResolver = new ResolutionContentResolver(); FontContentResolver fcr = new FontContentResolver(resolutionContentResolver); - Assets.contentResolver.Add(typeof(Texture2D), resolutionContentResolver); - Assets.contentResolver.Add(typeof(SpriteFont), fcr); - + Assets.contentPathModifier.Add(typeof(Texture2D), resolutionContentResolver); + Assets.contentPathModifier.Add(typeof(SpriteFont), fcr); preferencesManager = new PreferencesManager(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/RhythmBullet", - new Controls(), - new General()); + new General(), + new Controls()); } public Screen Screen @@ -67,7 +72,14 @@ namespace RhythmBullet /// protected override void Initialize() { - // TODO: Add your initialization logic here + //Load preferences + preferencesManager.Load(); + + Resolution resolution = preferencesManager.GetPreferences().Resolution; + resolutionContentResolver.Width = resolution.Width; + resolutionContentResolver.Height = resolution.Height; + QueueContent(); + Debug.WriteLine("Initial setup complete."); base.Initialize(); } @@ -80,7 +92,6 @@ namespace RhythmBullet // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Screen = new LoadingScreen(Content.Load("recrown")); - // TODO: use this.Content to load your game content here } @@ -95,12 +106,17 @@ namespace RhythmBullet /// /// Allows the game to run logic such as updating the world, - /// checking for collisions, gathering input, and playing audio. + /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { + if (!Assets.Done) + { + Assets.Update(); + } Screen.Update(gameTime); + base.Update(gameTime); } @@ -114,7 +130,7 @@ namespace RhythmBullet spriteBatch.Begin(); Screen.Draw(spriteBatch); spriteBatch.End(); - + base.Draw(gameTime); } @@ -124,16 +140,31 @@ namespace RhythmBullet resolutionContentResolver.Width = Graphics.PreferredBackBufferWidth; resolutionContentResolver.Height = Graphics.PreferredBackBufferHeight; QueueContent(); + Screen.AssetLoadStateChange(true); } public void PostAssetLoad() { - + Screen.AssetLoadStateChange(false); } void QueueContent() { - + Assets.Queue("Tech-Circle1"); + Assets.Queue("polyjet-standard"); + Assets.Queue("cybercircle3B"); + Assets.Queue("title"); + Assets.Queue("cybercircle1"); + Assets.Queue("defaultCover"); + Assets.Queue("laser"); + Assets.Queue("pellet"); + Assets.Queue("shard"); + Assets.Queue("bar"); + Assets.Queue("flake"); + Assets.Queue("void_circle"); + Assets.Queue("tpSelector"); + Assets.Queue("backgrounds/mainBG"); + Assets.Queue("magic1"); } } } diff --git a/RhythmBullet/Zer01HD/Game/ContentResolvers/FontContentResolver.cs b/RhythmBullet/Zer01HD/Game/ContentResolvers/FontContentResolver.cs index 1b75558..b52674f 100644 --- a/RhythmBullet/Zer01HD/Game/ContentResolvers/FontContentResolver.cs +++ b/RhythmBullet/Zer01HD/Game/ContentResolvers/FontContentResolver.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace RhythmBullet.Zer01HD.Game.ContentResolvers { - class FontContentResolver : IContentResolver + class FontContentResolver : IContentPathModifier { readonly ResolutionContentResolver rcr; @@ -16,9 +16,9 @@ namespace RhythmBullet.Zer01HD.Game.ContentResolvers this.rcr = rcr; } - public string Load(string assetName) + public string Modify(string assetName) { - return rcr.Load("fonts/" + assetName); + return rcr.Modify("fonts/" + assetName); } } } diff --git a/RhythmBullet/Zer01HD/Game/ContentResolvers/ResolutionContentResolver.cs b/RhythmBullet/Zer01HD/Game/ContentResolvers/ResolutionContentResolver.cs index 128a230..b3f3b25 100644 --- a/RhythmBullet/Zer01HD/Game/ContentResolvers/ResolutionContentResolver.cs +++ b/RhythmBullet/Zer01HD/Game/ContentResolvers/ResolutionContentResolver.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace RhythmBullet.Zer01HD.Game.ContentResolvers { - class ResolutionContentResolver : IContentResolver + class ResolutionContentResolver : IContentPathModifier { int width, height; bool updated; @@ -98,7 +98,7 @@ namespace RhythmBullet.Zer01HD.Game.ContentResolvers return best; } - public string Load(string path) + public string Modify(string path) { if (updated) { diff --git a/RhythmBullet/Zer01HD/Game/Preferences/Controls.cs b/RhythmBullet/Zer01HD/Game/Preferences/Controls.cs index b8d9d8d..12254c6 100644 --- a/RhythmBullet/Zer01HD/Game/Preferences/Controls.cs +++ b/RhythmBullet/Zer01HD/Game/Preferences/Controls.cs @@ -1,18 +1,14 @@ -using RhythmBullet.Zer01HD.Utilities.Persistence; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.Xna.Framework.Input; +using RhythmBullet.Zer01HD.Utilities.Persistence; namespace RhythmBullet.Zer01HD.Game.Preferences { - public class Controls : IPreferences + public class Controls : Utilities.Persistence.Preferences { - public int Forward; - public int Backward; - public int Left; - public int Right; - public int Shoot; + public Keys Forward = Keys.Up; + public Keys Backward = Keys.Down; + public Keys Left = Keys.Left; + public Keys Right = Keys.Right; + public Keys Shoot = Keys.Space; } } diff --git a/RhythmBullet/Zer01HD/Game/Preferences/General.cs b/RhythmBullet/Zer01HD/Game/Preferences/General.cs index 1d1eb16..8439f90 100644 --- a/RhythmBullet/Zer01HD/Game/Preferences/General.cs +++ b/RhythmBullet/Zer01HD/Game/Preferences/General.cs @@ -1,20 +1,16 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using RhythmBullet.Zer01HD.Utilities; +using System.Xml.Serialization; +using RhythmBullet.Zer01HD.Utilities.DataTypes; using RhythmBullet.Zer01HD.Utilities.Persistence; namespace RhythmBullet.Zer01HD.Game.Preferences { - public class General : IPreferences + public class General : Utilities.Persistence.Preferences { - public Resolution Resolution; - public bool Fullscreen; - public float MusicVolume; - public float FXVolume; - public string MusicDirectory; - public string GameDirectory; + public Resolution Resolution = new Resolution(1920, 1080); + public bool Fullscreen = false; + public float MusicVolume = 1f; + public float FXVolume = 1f; + public string MusicDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); } } diff --git a/RhythmBullet/Zer01HD/Utilities/ContentSystem/ContentSystem.cs b/RhythmBullet/Zer01HD/Utilities/ContentSystem/ContentSystem.cs index 50eaeac..df1c485 100644 --- a/RhythmBullet/Zer01HD/Utilities/ContentSystem/ContentSystem.cs +++ b/RhythmBullet/Zer01HD/Utilities/ContentSystem/ContentSystem.cs @@ -2,6 +2,8 @@ using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using System.Threading; @@ -11,57 +13,57 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem { public class ContentSystem { - volatile bool queued; Thread thread; readonly ContentManager contentManager; readonly Queue queue; Dictionary assets; - public readonly Dictionary contentResolver; + public readonly Dictionary contentPathModifier; public ContentSystem(ContentManager contentManager) { this.contentManager = contentManager; assets = new Dictionary(); queue = new Queue(); - contentResolver = new Dictionary(); + contentPathModifier = new Dictionary(); } private void Load(string assetName, Type type) { - IContentResolver handler = contentResolver[type]; - string path = handler.Load(assetName); - assets.Add(assetName, contentManager.Load(path)); + IContentPathModifier handler = contentPathModifier[type]; + string path = handler.Modify(assetName); + try + { + assets.Add(assetName, contentManager.Load(path)); + } catch (ContentLoadException cle) + { + Debug.WriteLine("Failed to load " + assetName + "with modified path: " + cle.Message); + Debug.WriteLine("Loading asset from root: " + assetName); + assets.Add(assetName, contentManager.Load(assetName)); + } + Debug.WriteLine("Loaded asset: " + assetName); } - private void LoadBatch() - { - while (queue.Count != 0) - { - lock (queue) - { - LoadableContent content = queue.Dequeue(); - Load(content.assetName, content.type); - } - queued = false; - } - } public T Get(string assetName) { - lock(queue) + lock (queue) { return (T)assets[assetName]; } } - public void Queue(string assetName, Type type) + public void Queue(string assetName) where T : IDisposable { - queued = true; lock (queue) { if (!assets.ContainsKey(assetName)) { - queue.Enqueue(new LoadableContent(assetName, type)); + queue.Enqueue(new LoadableContent(assetName, typeof(T))); + Debug.WriteLine("Queued asset: " + assetName); + } + else + { + Debug.WriteLine("Did not queue asset due to asset with same name being loaded: " + assetName); } } } @@ -77,10 +79,22 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem { ThreadStart threadStart = new ThreadStart(LoadBatch); thread = new Thread(threadStart); + thread.Start(); } } } + private void LoadBatch() + { + while (queue.Count != 0) + { + lock (queue) + { + LoadableContent content = queue.Dequeue(); + Load(content.assetName, content.type); + } + } + } /// /// Removes the asset from the list of assets in the system. /// Cannot remove from queue. @@ -100,9 +114,9 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem /// public void ClearQueue() { - lock(queue) + lock (queue) { - queue.Clear(); + queue.Clear(); } } @@ -119,9 +133,12 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem ClearQueue(); } - public bool Done() + public bool Done { - return queued; + get + { + return queue.Count == 0; + } } } } diff --git a/RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentResolver.cs b/RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentPathModifier.cs similarity index 82% rename from RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentResolver.cs rename to RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentPathModifier.cs index 631b88d..b864e0d 100644 --- a/RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentResolver.cs +++ b/RhythmBullet/Zer01HD/Utilities/ContentSystem/IContentPathModifier.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace RhythmBullet.Zer01HD.Utilities.ContentSystem { - public interface IContentResolver + public interface IContentPathModifier { /// /// Returns the complete path with the content folder as root. /// /// Is the asset's name /// - string Load(string assetName); + string Modify(string assetName); } } diff --git a/RhythmBullet/Zer01HD/Utilities/ContentSystem/NormalContentResolver.cs b/RhythmBullet/Zer01HD/Utilities/ContentSystem/NormalContentResolver.cs new file mode 100644 index 0000000..158cf2e --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/ContentSystem/NormalContentResolver.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.Utilities.ContentSystem +{ + class NormalContentResolver : IContentPathModifier + { + public string Modify(string assetName) + { + return assetName; + } + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/DataTypes/Resolution.cs b/RhythmBullet/Zer01HD/Utilities/DataTypes/Resolution.cs index 5c3f849..19f7fc9 100644 --- a/RhythmBullet/Zer01HD/Utilities/DataTypes/Resolution.cs +++ b/RhythmBullet/Zer01HD/Utilities/DataTypes/Resolution.cs @@ -16,6 +16,10 @@ namespace RhythmBullet.Zer01HD.Utilities.DataTypes Height = height; } + public Resolution() + { + } + public int CompareTo(Resolution other) { return Area() - other.Area(); diff --git a/RhythmBullet/Zer01HD/Utilities/Persistence/IPreferences.cs b/RhythmBullet/Zer01HD/Utilities/Persistence/Preferences.cs similarity index 75% rename from RhythmBullet/Zer01HD/Utilities/Persistence/IPreferences.cs rename to RhythmBullet/Zer01HD/Utilities/Persistence/Preferences.cs index 391f83a..7583b0e 100644 --- a/RhythmBullet/Zer01HD/Utilities/Persistence/IPreferences.cs +++ b/RhythmBullet/Zer01HD/Utilities/Persistence/Preferences.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Serialization; namespace RhythmBullet.Zer01HD.Utilities.Persistence { - public interface IPreferences + public class Preferences { } } diff --git a/RhythmBullet/Zer01HD/Utilities/Persistence/PreferencesManager.cs b/RhythmBullet/Zer01HD/Utilities/Persistence/PreferencesManager.cs index 45f3723..2e4a037 100644 --- a/RhythmBullet/Zer01HD/Utilities/Persistence/PreferencesManager.cs +++ b/RhythmBullet/Zer01HD/Utilities/Persistence/PreferencesManager.cs @@ -11,43 +11,67 @@ namespace RhythmBullet.Zer01HD.Utilities { public class PreferencesManager { - private readonly Dictionary preferenceList; + private readonly Dictionary preferenceList; public string SavePath; XmlSerializer xmlSerializer; - public PreferencesManager(string savePath, params IPreferences[] preferences) + public PreferencesManager(string savePath, params Preferences[] preferences) { this.SavePath = savePath; - preferenceList = new Dictionary(); - foreach (IPreferences prefs in preferences) + preferenceList = new Dictionary(); + + Type[] preferenceTypes = new Type[preferences.Length]; + for (int prefID = 0; prefID < preferences.Length; prefID++) { - preferenceList.Add(prefs.GetType(), prefs); + preferenceList.Add(preferences[prefID].GetType(), preferences[prefID]); + preferenceTypes[prefID] = preferences[prefID].GetType(); } + + xmlSerializer = new XmlSerializer(typeof(Preferences), preferenceTypes); } - public T GetPreferences() where T : IPreferences + public T GetPreferences() where T : Preferences { - return (T) preferenceList[typeof(T)]; + return (T)preferenceList[typeof(T)]; } public void Load() { - foreach (KeyValuePair prefs in preferenceList) + foreach (KeyValuePair prefs in preferenceList) { - Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Open); - preferenceList[prefs.Key] = (IPreferences) xmlSerializer.Deserialize(stream); - stream.Close(); + if (!LoadSpecific(prefs.Key)) + { + SaveSpecific(prefs.Key); + } } } public void Save() { - foreach (KeyValuePair prefs in preferenceList) + foreach (KeyValuePair prefs in preferenceList) { - Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Create); - xmlSerializer.Serialize(stream, prefs.Value); - stream.Close(); + SaveSpecific(prefs.Key); } } + + private bool LoadSpecific(Type preference) + { + string path = SavePath + "/" + preference.Name; + if (File.Exists(path)) + { + Stream stream = new FileStream(SavePath + "/" + preference.Name, FileMode.Open); + preferenceList[preference] = (Preferences)xmlSerializer.Deserialize(stream); + stream.Close(); + return true; + } + return false; + } + + private void SaveSpecific(Type preference) + { + Stream stream = new FileStream(SavePath + preference.Name, FileMode.Create); + xmlSerializer.Serialize(stream, preferenceList[preference]); + stream.Close(); + } } } diff --git a/RhythmBullet/Zer01HD/Utilities/Renderer/ScaledRenderer.cs b/RhythmBullet/Zer01HD/Utilities/Renderer/ScaledRenderer.cs new file mode 100644 index 0000000..7efb5e9 --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/Renderer/ScaledRenderer.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.Utilities.Renderer +{ + class ScaledRenderer + { + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Screen.cs b/RhythmBullet/Zer01HD/Utilities/UI/Screen.cs index e4b4cca..f2e3074 100644 --- a/RhythmBullet/Zer01HD/Utilities/UI/Screen.cs +++ b/RhythmBullet/Zer01HD/Utilities/UI/Screen.cs @@ -30,7 +30,7 @@ namespace RhythmBullet.Zer01HD.Utilities.UI } - public virtual void Resize() + public virtual void AssetLoadStateChange(bool state) { }