Content loading system functioning.

This commit is contained in:
2018-10-30 18:29:54 -05:00
parent 3a4dfb94ac
commit a9647f2146
14 changed files with 189 additions and 87 deletions

View File

@@ -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);
}
}
}

View File

@@ -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)
{

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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<LoadableContent> queue;
Dictionary<string, IDisposable> assets;
public readonly Dictionary<Type, IContentResolver> contentResolver;
public readonly Dictionary<Type, IContentPathModifier> contentPathModifier;
public ContentSystem(ContentManager contentManager)
{
this.contentManager = contentManager;
assets = new Dictionary<string, IDisposable>();
queue = new Queue<LoadableContent>();
contentResolver = new Dictionary<Type, IContentResolver>();
contentPathModifier = new Dictionary<Type, IContentPathModifier>();
}
private void Load(string assetName, Type type)
{
IContentResolver handler = contentResolver[type];
string path = handler.Load(assetName);
assets.Add(assetName, contentManager.Load<IDisposable>(path));
IContentPathModifier handler = contentPathModifier[type];
string path = handler.Modify(assetName);
try
{
assets.Add(assetName, contentManager.Load<IDisposable>(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<IDisposable>(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<T>(string assetName)
{
lock(queue)
lock (queue)
{
return (T)assets[assetName];
}
}
public void Queue(string assetName, Type type)
public void Queue<T>(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);
}
}
}
/// <summary>
/// Removes the asset from the list of assets in the system.
/// Cannot remove from queue.
@@ -100,9 +114,9 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
/// </summary>
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;
}
}
}
}

View File

@@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
public interface IContentResolver
public interface IContentPathModifier
{
/// <summary>
/// Returns the complete path with the content folder as root.
/// </summary>
/// <param name="assetName">Is the asset's name</param>
/// <returns></returns>
string Load(string assetName);
string Modify(string assetName);
}
}

View File

@@ -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;
}
}
}

View File

@@ -16,6 +16,10 @@ namespace RhythmBullet.Zer01HD.Utilities.DataTypes
Height = height;
}
public Resolution()
{
}
public int CompareTo(Resolution other)
{
return Area() - other.Area();

View File

@@ -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
{
}
}

View File

@@ -11,43 +11,67 @@ namespace RhythmBullet.Zer01HD.Utilities
{
public class PreferencesManager
{
private readonly Dictionary<Type, IPreferences> preferenceList;
private readonly Dictionary<Type, Preferences> 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<Type, IPreferences>();
foreach (IPreferences prefs in preferences)
preferenceList = new Dictionary<Type, Preferences>();
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<T>() where T : IPreferences
public T GetPreferences<T>() where T : Preferences
{
return (T) preferenceList[typeof(T)];
return (T)preferenceList[typeof(T)];
}
public void Load()
{
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
foreach (KeyValuePair<Type, Preferences> 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<Type, IPreferences> prefs in preferenceList)
foreach (KeyValuePair<Type, Preferences> 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();
}
}
}

View File

@@ -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
{
}
}

View File

@@ -30,7 +30,7 @@ namespace RhythmBullet.Zer01HD.Utilities.UI
}
public virtual void Resize()
public virtual void AssetLoadStateChange(bool state)
{
}