Fix memory leaks of graphics resources.

Make Scene & LinesOfSight dispose of the graphics resources they create.

Force GC when a new level is loaded.

GitOrigin-RevId: 0640cced784e6bf5ee15f4edce8bf11d122dac51
This commit is contained in:
Colin McMillen 2020-02-19 16:30:34 -05:00
parent 1eb4d7a7d2
commit 026623ac82
4 changed files with 27 additions and 5 deletions

View File

@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
using System; using System;
namespace SemiColinGames { namespace SemiColinGames {
class LinesOfSight { class LinesOfSight : IDisposable {
const int numEdgeVertices = 60; const int numEdgeVertices = 60;
// coneVertices[0] is the eye position; the rest are the edge vertices. // 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); graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
} }
~LinesOfSight() {
Dispose();
}
public void Dispose() {
vertexBuffer.Dispose();
indexBuffer.Dispose();
}
public void Update(Player player, AABB[] collisionTargets) { public void Update(Player player, AABB[] collisionTargets) {
Vector2 eyePos = player.EyePosition; Vector2 eyePos = player.EyePosition;
float visionRange = player.VisionRange; float visionRange = player.VisionRange;

View File

@ -1,5 +1,4 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,9 +1,9 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System;
namespace SemiColinGames { namespace SemiColinGames {
class Scene { class Scene : IDisposable {
public bool Enabled = false; public bool Enabled = false;
Color backgroundColor = Color.CornflowerBlue; Color backgroundColor = Color.CornflowerBlue;
@ -14,7 +14,6 @@ namespace SemiColinGames {
readonly RenderTarget2D sceneTarget; readonly RenderTarget2D sceneTarget;
readonly RenderTarget2D lightingTarget; readonly RenderTarget2D lightingTarget;
readonly BasicEffect lightingEffect; readonly BasicEffect lightingEffect;
readonly SpriteBatch spriteBatch; readonly SpriteBatch spriteBatch;
public Scene(GraphicsDevice graphics, Camera camera) { public Scene(GraphicsDevice graphics, Camera camera) {
@ -37,6 +36,17 @@ namespace SemiColinGames {
spriteBatch = new SpriteBatch(graphics); 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) { public void Draw(World world, Player player, LinesOfSight linesOfSight) {
graphics.SetRenderTarget(null); graphics.SetRenderTarget(null);
graphics.Clear(backgroundColor); graphics.Clear(backgroundColor);

View File

@ -74,8 +74,12 @@ namespace SemiColinGames {
camera = new Camera(); camera = new Camera();
player = new Player(); player = new Player();
world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]); world = new World(Levels.ALL_LEVELS[levelIdx % Levels.ALL_LEVELS.Length]);
scene?.Dispose();
scene = new Scene(GraphicsDevice, camera); scene = new Scene(GraphicsDevice, camera);
levelIdx++; levelIdx++;
GC.Collect();
GC.WaitForPendingFinalizers();
} }
// Called once per game. Unloads all game content. // Called once per game. Unloads all game content.