diff options
| author | aldrikboy <aldrikboy@gmail.com> | 2017-12-11 22:02:13 +0100 |
|---|---|---|
| committer | aldrikboy <aldrikboy@gmail.com> | 2017-12-11 22:02:13 +0100 |
| commit | fd6fa4e5cebbe3edb65d50c78dcc8a97ce98ce64 (patch) | |
| tree | 8950f6b9023e0b47e22e1cd4869ab76de0803f4c /Penguloon/ContentManager.cs | |
| parent | c4c0f3c887d627b6432551e96009c7aeecd4cdd8 (diff) | |
First commit
Diffstat (limited to 'Penguloon/ContentManager.cs')
| -rw-r--r-- | Penguloon/ContentManager.cs | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/Penguloon/ContentManager.cs b/Penguloon/ContentManager.cs new file mode 100644 index 0000000..ad34ac3 --- /dev/null +++ b/Penguloon/ContentManager.cs @@ -0,0 +1,157 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework.Graphics; +using System.Threading; +using Microsoft.Xna.Framework.Audio; + +namespace Penguloon +{ + public static class ContentManager + { + private static Dictionary<string, Texture2D> TextureDictionary { get; set; } = new Dictionary<string, Texture2D>(); + private static Dictionary<string, SpriteFont> FontDictionary { get; set; } = new Dictionary<string, SpriteFont>(); + private static Dictionary<string, SoundEffect> SoundDictionary { get; set; } = new Dictionary<string, SoundEffect>(); + + public static int LoadPercentage { get; set; } = 0; + public static float LoadPercentageF { get; set; } = 0; + + public static bool DoneLoading { get; set; } + public static bool DonePreLoading { get; set; } + + public static void LoadContent(Main main) + { + new Thread(() => + { + Thread.CurrentThread.IsBackground = true; + + PreLoadContent(main); + + LoadContent_(main); + + }).Start(); + } + + private static void LoadContent_(Main main) + { + float totalItemsToLoad = ContentPathManager.TexturePaths.Count + ContentPathManager.FontPaths.Count + ContentPathManager.SoundPaths.Count; + float itemsLoaded = 0; + + for (int i = 0; i < ContentPathManager.SoundPaths.Count; i++) + { + SoundEffect ef = main.Content.Load<SoundEffect>(ContentPathManager.SoundPaths[i]); + SoundDictionary.Add(ContentPathManager.SoundPaths[i], ef); + + LoadPercentage = (int)((itemsLoaded / totalItemsToLoad) * 100); + LoadPercentageF = (itemsLoaded / totalItemsToLoad); + + Thread.Sleep(25); + } + + for (int i = 0; i < ContentPathManager.TexturePaths.Count; i++) + { + TextureDictionary.Add(ContentPathManager.TexturePaths[i], main.Content.Load<Texture2D>(ContentPathManager.TexturePaths[i])); + itemsLoaded++; + + LoadPercentage = (int)((itemsLoaded / totalItemsToLoad) * 100); + LoadPercentageF = (itemsLoaded / totalItemsToLoad); + + Thread.Sleep(25); + } + + for (int i = 0; i < ContentPathManager.FontPaths.Count; i++) + { + FontDictionary.Add(ContentPathManager.FontPaths[i], main.Content.Load<SpriteFont>(ContentPathManager.FontPaths[i])); + itemsLoaded++; + + LoadPercentage = (int)((itemsLoaded / totalItemsToLoad) * 100); + LoadPercentageF = (itemsLoaded / totalItemsToLoad); + + Thread.Sleep(25); + } + + DoneLoading = true; + } + + private static void PreLoadContent(Main main) + { + for (int i = 0; i < ContentPathManager.SoundPathsPreLoad.Count; i++) + { + SoundEffect ef = main.Content.Load<SoundEffect>(ContentPathManager.SoundPathsPreLoad[i]); + SoundDictionary.Add(ContentPathManager.SoundPathsPreLoad[i], ef); + } + + for (int i = 0; i < ContentPathManager.TexturePathsPreLoad.Count; i++) + { + TextureDictionary.Add(ContentPathManager.TexturePathsPreLoad[i], + main.Content.Load<Texture2D>(ContentPathManager.TexturePathsPreLoad[i])); + } + + for (int i = 0; i < ContentPathManager.FontPathsPreLoad.Count; i++) + { + FontDictionary.Add(ContentPathManager.FontPathsPreLoad[i], + main.Content.Load<SpriteFont>(ContentPathManager.FontPathsPreLoad[i])); + } + + DonePreLoading = true; + } + + public static Texture2D GetTexture(string name) + { + if (TextureDictionary.ContainsKey(name)) + return TextureDictionary[name]; + + return null; + } + + public static SoundEffect GetSound(string name) + { + if (SoundDictionary.ContainsKey(name)) + return SoundDictionary[name]; + + return null; + } + + public static SpriteFont GetFont(string name) + { + if (FontDictionary.ContainsKey(name)) + return FontDictionary[name]; + + return null; + } + + public static Texture2D GetTileTextureByType(int type) + { + switch (type) + { + case 0: + return GetTexture("Tiles/ice"); + case 1: + return GetTexture("Tiles/waterCornerLeftDown"); + case 2: + return GetTexture("Tiles/waterCornerLeftTop"); + case 3: + return GetTexture("Tiles/waterCornerRightDown"); + case 4: + return GetTexture("Tiles/waterCornerRightTop"); + case 5: + return GetTexture("Tiles/waterHorizontal"); + case 6: + return GetTexture("Tiles/waterVertical"); + } + + return null; + } + + public static void DisposeContent() + { + foreach (var entry in TextureDictionary) + entry.Value.Dispose(); + + foreach (var entry in SoundDictionary) + entry.Value.Dispose(); + + SoundDictionary.Clear(); + FontDictionary.Clear(); + TextureDictionary.Clear(); + } + } +}
\ No newline at end of file |
