Browse Source

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: 0640cced78
master
Colin McMillen 4 years ago
parent
commit
026623ac82
  1. 11
      Shared/LinesOfSight.cs
  2. 1
      Shared/Player.cs
  3. 16
      Shared/Scene.cs
  4. 4
      Shared/SneakGame.cs

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

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

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

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

Loading…
Cancel
Save