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

View File

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

View File

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

View File

@ -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.