Browse Source

Add ProfilingList & use it in Player.

master
Colin McMillen 2 years ago
parent
commit
a6c297196a
  1. 4
      Shared/Line.cs
  2. 4
      Shared/Player.cs
  3. 81
      Shared/ProfilingList.cs
  4. 1
      Shared/Shared.projitems

4
Shared/Line.cs

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace SemiColinGames {
public static class Line {
public static void Rasterize(Point p1, Point p2, List<Point> result) {
public static void Rasterize(Point p1, Point p2, IList<Point> result) {
Line.Rasterize(p1.X, p1.Y, p2.X, p2.Y, result);
}
@ -14,7 +14,7 @@ namespace SemiColinGames {
// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
// http://members.chello.at/~easyfilter/bresenham.html
// http://members.chello.at/~easyfilter/Bresenham.pdf (section 1.6)
public static void Rasterize(int x1, int y1, int x2, int y2, List<Point> result) {
public static void Rasterize(int x1, int y1, int x2, int y2, IList<Point> result) {
int dx = Math.Abs(x2 - x1);
int stepX = x1 < x2 ? 1 : -1;
int dy = -Math.Abs(y2 - y1);

4
Shared/Player.cs

@ -36,9 +36,9 @@ namespace SemiColinGames {
private float invincibilityTime = 0;
// For passing into Line.Rasterize() during movement updates.
private readonly List<Point> movePoints = new List<Point>(100);
private readonly IList<Point> movePoints = new ProfilingList<Point>(32, "Player.movePoints");
// Possible hitboxes for player <-> obstacles.
private readonly List<AABB> candidates = new List<AABB>(10);
private readonly IList<AABB> candidates = new ProfilingList<AABB>(8, "Player.candidates");
public Player(Vector2 position, int facing) {
this.position = position;

81
Shared/ProfilingList.cs

@ -0,0 +1,81 @@
using System.Collections;
using System.Collections.Generic;
namespace SemiColinGames {
// An IList<T>, backed by a List<T>, that prints out a debug message any time that the Capacity
// of the underlying List changes.
public class ProfilingList<T> : IList<T> {
private List<T> items;
private int previousCapacity;
private string name;
public ProfilingList(int capacity, string name) {
items = new List<T>(capacity);
previousCapacity = capacity;
this.name = name;
}
public void Add(T item) {
items.Add(item);
if (items.Capacity != previousCapacity) {
Debug.WriteLine($"{name} capacity: {previousCapacity} -> {items.Capacity}");
previousCapacity = items.Capacity;
}
}
public void Insert(int index, T item) {
items.Insert(index, item);
if (items.Capacity != previousCapacity) {
Debug.WriteLine($"{name} capacity: {previousCapacity} -> {items.Capacity}");
previousCapacity = items.Capacity;
}
}
public bool IsReadOnly {
get { return false; }
}
// Everything below this point is boilerplate delegation to the underlying list.
public void Clear() {
items.Clear();
}
public bool Contains(T item) {
return items.Contains(item);
}
public int Count {
get => items.Count;
}
public int IndexOf(T item) {
return items.IndexOf(item);
}
public bool Remove(T item) {
return items.Remove(item);
}
public void CopyTo(T[] array, int arrayIndex) {
items.CopyTo(array, arrayIndex);
}
public void RemoveAt(int index) {
items.RemoveAt(index);
}
public T this[int index] {
get { return items[index]; }
set { items[index] = value; }
}
public IEnumerator<T> GetEnumerator() {
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return items.GetEnumerator();
}
}
}

1
Shared/Shared.projitems

@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FSM.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NPC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ProfilingList.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SoundEffects.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Sprites.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Strings.cs" />

Loading…
Cancel
Save