diff --git a/Shared/LinesOfSight.cs b/Shared/LinesOfSight.cs index 5b325d0..6ca2b33 100644 --- a/Shared/LinesOfSight.cs +++ b/Shared/LinesOfSight.cs @@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics; using System; namespace SemiColinGames { - class LinesOfSight { + class LinesOfSight : IDisposable { const int numEdgeVertices = 60; // coneVertices[0] is the eye position; the rest are the edge vertices. @@ -21,6 +21,15 @@ namespace SemiColinGames { graphics, typeof(int), indices.Length, BufferUsage.WriteOnly); } + ~LinesOfSight() { + Dispose(); + } + + public void Dispose() { + vertexBuffer.Dispose(); + indexBuffer.Dispose(); + } + public void Update(Player player, AABB[] collisionTargets) { Vector2 eyePos = player.EyePosition; float visionRange = player.VisionRange; diff --git a/Shared/Player.cs b/Shared/Player.cs index 2cdab96..d621b06 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -1,5 +1,4 @@ using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; diff --git a/Shared/Scene.cs b/Shared/Scene.cs index 4199ee6..33578dd 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -1,9 +1,9 @@ using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using System; namespace SemiColinGames { - class Scene { + class Scene : IDisposable { public bool Enabled = false; Color backgroundColor = Color.CornflowerBlue; @@ -14,7 +14,6 @@ namespace SemiColinGames { readonly RenderTarget2D sceneTarget; readonly RenderTarget2D lightingTarget; readonly BasicEffect lightingEffect; - readonly SpriteBatch spriteBatch; public Scene(GraphicsDevice graphics, Camera camera) { @@ -37,6 +36,17 @@ namespace SemiColinGames { spriteBatch = new SpriteBatch(graphics); } + ~Scene() { + Dispose(); + } + + public void Dispose() { + sceneTarget.Dispose(); + lightingTarget.Dispose(); + lightingEffect.Dispose(); + spriteBatch.Dispose(); + } + public void Draw(World world, Player player, LinesOfSight linesOfSight) { graphics.SetRenderTarget(null); graphics.Clear(backgroundColor); diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 7910c47..8273113 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -74,8 +74,12 @@ namespace SemiColinGames { camera = new Camera(); player = new Player(); world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]); + scene?.Dispose(); scene = new Scene(GraphicsDevice, camera); levelIdx++; + + GC.Collect(); + GC.WaitForPendingFinalizers(); } // Called once per game. Unloads all game content.