From 67ab74899fa60e78bc5cde08f180fbd225451280 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 30 Dec 2014 22:07:05 -0600 Subject: [PATCH] Removing cache from Gleed2D as ContentManager automatically does this Adding cache Adding XOR Shift random class --- axios/Axios_WP7.csproj | 2 + axios/Axios_Windows.csproj | 3 ++ axios/Axios_Xbox_360.csproj | 2 + axios/Axios_settings.cs | 3 ++ axios/Engine/AxiosGameScreen.cs | 10 +++-- axios/Engine/AxiosRandom.cs | 53 ++++++++++++++++++++++++ axios/Engine/Data/Cache.cs | 55 +++++++++++++++++++++++++ axios/Engine/GameServices.cs | 2 +- axios/Engine/Gleed2D/Item.cs | 2 +- axios/Engine/Gleed2D/PathItem.cs | 4 +- axios/Engine/Gleed2D/TextureItem.cs | 10 ++--- axios/ScreenSystem/PhysicsGameScreen.cs | 2 +- axios/ScreenSystem/ScreenManager.cs | 13 ++++++ 13 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 axios/Engine/AxiosRandom.cs create mode 100644 axios/Engine/Data/Cache.cs diff --git a/axios/Axios_WP7.csproj b/axios/Axios_WP7.csproj index c192166..66f98d6 100644 --- a/axios/Axios_WP7.csproj +++ b/axios/Axios_WP7.csproj @@ -164,6 +164,7 @@ + @@ -179,6 +180,7 @@ + diff --git a/axios/Axios_Windows.csproj b/axios/Axios_Windows.csproj index 0108daf..63afc82 100644 --- a/axios/Axios_Windows.csproj +++ b/axios/Axios_Windows.csproj @@ -213,9 +213,11 @@ + + @@ -230,6 +232,7 @@ + diff --git a/axios/Axios_Xbox_360.csproj b/axios/Axios_Xbox_360.csproj index 7b86e17..1373fd7 100644 --- a/axios/Axios_Xbox_360.csproj +++ b/axios/Axios_Xbox_360.csproj @@ -158,6 +158,7 @@ + @@ -172,6 +173,7 @@ + diff --git a/axios/Axios_settings.cs b/axios/Axios_settings.cs index 1ea3cae..8be8321 100644 --- a/axios/Axios_settings.cs +++ b/axios/Axios_settings.cs @@ -131,6 +131,9 @@ * * 1.0.1.11 - * - Adding game services static class + * - Removing cache from Gleed2D as ContentManager automatically does this + * - Adding cache + * - Adding XOR Shift random class * */ #endregion diff --git a/axios/Engine/AxiosGameScreen.cs b/axios/Engine/AxiosGameScreen.cs index 6b5a274..92eb07c 100644 --- a/axios/Engine/AxiosGameScreen.cs +++ b/axios/Engine/AxiosGameScreen.cs @@ -78,8 +78,7 @@ namespace Axios.Engine this._uiobjects = new List(); prevuiobj = null; prevuifocusobj = null; - GameServices.AddService(this.ScreenManager.GraphicsDevice); - GameServices.AddService(this.ScreenManager.Game.Content); + } public void LoadLevelFromStream(Stream s) @@ -494,8 +493,10 @@ namespace Axios.Engine public override void Unload() { + //this.IsExiting = true; //System.Diagnostics.Debugger.Break(); base.Deactivate(); + ScreenState = GameStateManagement.ScreenState.TransitionOff; AxiosLog.Instance.AddLine("Memory usage before cleanup: " + GC.GetTotalMemory(true).ToString(), LoggingFlag.DEBUG); foreach (AxiosGameObject g in _gameObjects) g.UnloadContent(this); @@ -523,6 +524,7 @@ namespace Axios.Engine _console = null; } #endif + } @@ -539,7 +541,7 @@ namespace Axios.Engine public virtual void LoadPathItem(PathItemProperties pathitem, Layer l) { PathItem p = new PathItem((PathItemProperties)pathitem); - p.load(this, ref cache); + p.load(this); PathItems[pathitem.Name] = p; } @@ -551,7 +553,7 @@ namespace Axios.Engine public virtual void LoadTextureItem(TextureItemProperties textureitem, Layer l) { TextureItem i = new TextureItem((TextureItemProperties)textureitem); - i.load(this, ref cache); + i.load(this); TextureItems[textureitem.Name] = i; } diff --git a/axios/Engine/AxiosRandom.cs b/axios/Engine/AxiosRandom.cs new file mode 100644 index 0000000..de09bf0 --- /dev/null +++ b/axios/Engine/AxiosRandom.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Axios.Engine +{ + // Implemenation of an XORShift + // http://en.wikipedia.org/wiki/Xorshift + // http://stackoverflow.com/questions/6275593/how-to-write-you-own-random-number-algorithm + public class AxiosRandom + { + private static int x; + private static int y; + private static int z; + private static int w; + private static int t = 0; + + public void init(int x, int y, int z, int w) + { + AxiosRandom.x = x; + AxiosRandom.y = y; + AxiosRandom.z = z; + AxiosRandom.w = w; + } + + public static void init() + { + AxiosRandom.x = generateVector(); + AxiosRandom.y = generateVector(); + AxiosRandom.z = generateVector(); + AxiosRandom.w = generateVector(); + } + + public static int next() + { + t = x ^ (x << 11); + x = y; y = z; z = w; + return w = w ^ (w >> 19) ^ (t ^ (t >> 8)); + } + + private static int generateVector() + { + int[] x = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] val = new int[9]; + for (int i = 0; i < 9; i++) + val[i] = x[GameServices.GetService().Next(x.Count() - 1)]; + return int.Parse(String.Join("", val)); + } + + + } +} diff --git a/axios/Engine/Data/Cache.cs b/axios/Engine/Data/Cache.cs new file mode 100644 index 0000000..bc9fe0b --- /dev/null +++ b/axios/Engine/Data/Cache.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Axios.Engine.Data +{ + // Q. What is the point of this? + // A. This is not to cache textures loaded by content manager + // but other data/content that isn't. Use cases include: + // - Any graphics generated during runtime (such as dialogs) + // - Any data that is loaded in during run time (such as maps) + // Content manager performs it's own caching so anything loaded by it + // or the Gameservice - then attempted to load again will not be loaded + // again but rather a reference to it will be returned + // ************************************************ + // DANGER WILL ROBINSON DANGER + // ************************************************ + // Only store stuff here that you want during the FULL lifecycle of your game + // The cache is never cleared - so a reference will exist for the objects you leave + // You MAY clear the cache by using the clear method or unset + // + // You probably don't want this + // There is no cache... + // This is not the cache you are looking for... + // + public class Cache : Singleton + { + private Dictionary _cache; + public Cache() + { + _cache = new Dictionary(); + } + + public object get(string key) + { + return _cache[key]; + } + + public void set(string key, object obj) + { + _cache[key] = obj; + } + + public void unset(string key) + { + _cache.Remove(key); + } + + public void clear() + { + _cache = new Dictionary(); + } + } +} diff --git a/axios/Engine/GameServices.cs b/axios/Engine/GameServices.cs index 4807ff9..6a5073c 100644 --- a/axios/Engine/GameServices.cs +++ b/axios/Engine/GameServices.cs @@ -5,7 +5,7 @@ namespace Axios.Engine public static class GameServices { private static GameServiceContainer container; - private static object lockobj; + private static object lockobj = new object(); public static GameServiceContainer Instance { get diff --git a/axios/Engine/Gleed2D/Item.cs b/axios/Engine/Gleed2D/Item.cs index 46d81ac..48f021d 100644 --- a/axios/Engine/Gleed2D/Item.cs +++ b/axios/Engine/Gleed2D/Item.cs @@ -20,7 +20,7 @@ namespace Axios.Engine.Gleed2D /// Called by Level.FromFile(filename) on each Item after the deserialization process. /// Should be overriden and can be used to load anything needed by the Item (e.g. a texture). /// - public virtual void load(AxiosGameScreen gameScreen, ref Dictionary cache) + public virtual void load(AxiosGameScreen gameScreen) { } diff --git a/axios/Engine/Gleed2D/PathItem.cs b/axios/Engine/Gleed2D/PathItem.cs index 8f22ac3..248c7ef 100644 --- a/axios/Engine/Gleed2D/PathItem.cs +++ b/axios/Engine/Gleed2D/PathItem.cs @@ -30,9 +30,9 @@ namespace Axios.Engine.Gleed2D this._item = i; } - public override void load(AxiosGameScreen gameScreen, ref Dictionary cache) + public override void load(AxiosGameScreen gameScreen) { - base.load(gameScreen, ref cache); + base.load(gameScreen); Vertices v = new Vertices(LayerItem.LocalPoints.Count); foreach (Vector2 vec in LayerItem.LocalPoints) diff --git a/axios/Engine/Gleed2D/TextureItem.cs b/axios/Engine/Gleed2D/TextureItem.cs index e5f4da3..22dee9a 100644 --- a/axios/Engine/Gleed2D/TextureItem.cs +++ b/axios/Engine/Gleed2D/TextureItem.cs @@ -39,20 +39,20 @@ namespace Axios.Engine.Gleed2D /// You must provide your own implementation. However, you can rely on all public fields being /// filled by the level deserialization process. /// - public override void load(AxiosGameScreen gameScreen, ref Dictionary cache) + public override void load(AxiosGameScreen gameScreen) { - base.load(gameScreen, ref cache); + base.load(gameScreen); //throw new NotImplementedException(); //TODO: provide your own implementation of how a TextureItem loads its assets //for example: //this.texture = Texture2D.FromFile(, texture_filename); //or by using the Content Pipeline: - if (!cache.ContainsKey(LayerItem.AssetName)) + /*if (!cache.ContainsKey(LayerItem.AssetName)) { cache[LayerItem.AssetName] = gameScreen.ScreenManager.Game.Content.Load(LayerItem.AssetName); - } - this.texture = cache[LayerItem.AssetName]; + }*/ + this.texture = gameScreen.ScreenManager.Game.Content.Load(LayerItem.AssetName); //Visible = gameScreen.LoadTextureItem(this); //this.texture = cm.Load(asset_name); diff --git a/axios/ScreenSystem/PhysicsGameScreen.cs b/axios/ScreenSystem/PhysicsGameScreen.cs index a88c5cc..0fdd037 100644 --- a/axios/ScreenSystem/PhysicsGameScreen.cs +++ b/axios/ScreenSystem/PhysicsGameScreen.cs @@ -98,7 +98,7 @@ namespace GameStateManagement public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { - if (!coveredByOtherScreen && !otherScreenHasFocus) + if (!coveredByOtherScreen && !otherScreenHasFocus && ScreenState != GameStateManagement.ScreenState.TransitionOff) { // variable time step but never less then 30 Hz if (UseSecondStep) diff --git a/axios/ScreenSystem/ScreenManager.cs b/axios/ScreenSystem/ScreenManager.cs index 953aedd..c3622bc 100644 --- a/axios/ScreenSystem/ScreenManager.cs +++ b/axios/ScreenSystem/ScreenManager.cs @@ -19,6 +19,7 @@ using System.IO; using System.IO.IsolatedStorage; using System.Xml.Linq; using FarseerPhysics.SamplesFramework; +using Axios.Engine; #endregion namespace GameStateManagement @@ -149,6 +150,18 @@ namespace GameStateManagement spriteBatch = new SpriteBatch(GraphicsDevice); font = content.Load("menufont"); blankTexture = Game.Content.Load("Materials/blank"); + GameServices.AddService(this.Game.GraphicsDevice); + GameServices.AddService(this.Game.Content); + + // It is advised to use one instance of Random + // because Random is seeded with the current time + // initilizing random objects too quickly can cause + // the impression of generating the same value + // http://stackoverflow.com/questions/2727538/random-encounter-not-so-random + GameServices.AddService(new Random()); + AxiosRandom.init(); + + input.LoadContent(); // Tell each of the screens to load their content. foreach (GameScreen screen in screens)