From a6c297196acbb16763a8cd15a7921c4832b19f74 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 20 Mar 2020 13:12:12 -0400 Subject: [PATCH] Add ProfilingList & use it in Player. --- Shared/Line.cs | 4 +- Shared/Player.cs | 4 +- Shared/ProfilingList.cs | 81 +++++++++++++++++++++++++++++++++++++++++ Shared/Shared.projitems | 1 + 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 Shared/ProfilingList.cs diff --git a/Shared/Line.cs b/Shared/Line.cs index bd6bca6..454309d 100644 --- a/Shared/Line.cs +++ b/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 result) { + public static void Rasterize(Point p1, Point p2, IList 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 result) { + public static void Rasterize(int x1, int y1, int x2, int y2, IList result) { int dx = Math.Abs(x2 - x1); int stepX = x1 < x2 ? 1 : -1; int dy = -Math.Abs(y2 - y1); diff --git a/Shared/Player.cs b/Shared/Player.cs index 3484fe0..d6417c6 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -36,9 +36,9 @@ namespace SemiColinGames { private float invincibilityTime = 0; // For passing into Line.Rasterize() during movement updates. - private readonly List movePoints = new List(100); + private readonly IList movePoints = new ProfilingList(32, "Player.movePoints"); // Possible hitboxes for player <-> obstacles. - private readonly List candidates = new List(10); + private readonly IList candidates = new ProfilingList(8, "Player.candidates"); public Player(Vector2 position, int facing) { this.position = position; diff --git a/Shared/ProfilingList.cs b/Shared/ProfilingList.cs new file mode 100644 index 0000000..0e00723 --- /dev/null +++ b/Shared/ProfilingList.cs @@ -0,0 +1,81 @@ +using System.Collections; +using System.Collections.Generic; + +namespace SemiColinGames { + // An IList, backed by a List, that prints out a debug message any time that the Capacity + // of the underlying List changes. + public class ProfilingList : IList { + private List items; + private int previousCapacity; + private string name; + + public ProfilingList(int capacity, string name) { + items = new List(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 GetEnumerator() { + return items.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() { + return items.GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index 73e9af0..7e6c612 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -13,6 +13,7 @@ +