| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using GDX.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace GDX.Experimental |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// A debug draw system useful in both the editor and at runtime. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class DebugDraw |
| | | 13 | | { |
| | | 14 | | // /// <summary> |
| | | 15 | | // /// A dictionary of known <see cref="DebugDrawBuffer"/> and their ID which are automatically executed. |
| | | 16 | | // /// </summary> |
| | | 17 | | // static IntKeyDictionary<DebugDrawBuffer> s_ManagedBuffers = new IntKeyDictionary<DebugDrawBuffer>(10); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// A dictionary of known <see cref="DebugDrawBuffer" /> and their ID which are not automatically executed. |
| | | 21 | | /// </summary> |
| | 1 | 22 | | static IntKeyDictionary<DebugDrawBuffer> s_UnmanagedBuffers = new IntKeyDictionary<DebugDrawBuffer>(10); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Get an instance of <see cref="DebugDrawBuffer" /> based on the provided <paramref name="key" /> that is |
| | | 26 | | /// managed. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <example> |
| | | 29 | | /// By checking the <see cref="DebugDrawBuffer.Finalized" /> property we can skip over the expensive buildin |
| | | 30 | | /// <code> |
| | | 31 | | /// DebugDrawBuffer buffer = DebugDraw.GetUnmanagedBuffer(gameObjet.GetInstanceID()); |
| | | 32 | | /// if (!buffer.Finalized) |
| | | 33 | | /// { |
| | | 34 | | /// /// Draw lots of stuff ... |
| | | 35 | | /// buffer.DrawWireCube(Color.white, worldPosition, size); |
| | | 36 | | /// } |
| | | 37 | | /// buffer.Execute(); |
| | | 38 | | /// </code> |
| | | 39 | | /// </example> |
| | | 40 | | /// <param name="key"> |
| | | 41 | | /// A value based key used to reference a <see cref="DebugDrawBuffer" /> in a |
| | | 42 | | /// <see cref="IntKeyDictionary{TValue}" />. |
| | | 43 | | /// </param> |
| | | 44 | | /// <param name="initialColorCount">Initial number of internal materials to allocate (x2).</param> |
| | | 45 | | /// <param name="verticesPerMesh">The number of vertices to split batched meshes on.</param> |
| | | 46 | | /// <returns> |
| | | 47 | | /// A newly created <see cref="DebugDrawBuffer" /> if the provided key is not found, or the previously |
| | | 48 | | /// created <see cref="DebugDrawBuffer" /> identified by the <paramref name="key" />. |
| | | 49 | | /// </returns> |
| | | 50 | | public static DebugDrawBuffer GetUnmanagedBuffer(int key, int initialColorCount = 5, |
| | | 51 | | int verticesPerMesh = DebugDrawBuffer.DefaultMaximumVerticesPerMesh) |
| | 5 | 52 | | { |
| | 5 | 53 | | if (s_UnmanagedBuffers.ContainsKey(key)) |
| | 1 | 54 | | { |
| | 1 | 55 | | return s_UnmanagedBuffers[key]; |
| | | 56 | | } |
| | | 57 | | |
| | 4 | 58 | | DebugDrawBuffer newBuffer = new DebugDrawBuffer(key, initialColorCount, verticesPerMesh); |
| | 4 | 59 | | s_UnmanagedBuffers.AddWithExpandCheck(key, newBuffer); |
| | 4 | 60 | | return newBuffer; |
| | 5 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Returns if the provided key has an unmanaged <see cref="DebugDrawBuffer" /> referenced. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="key"> |
| | | 67 | | /// The key used to reference the <see cref="DebugDrawBuffer" />. |
| | | 68 | | /// </param> |
| | | 69 | | /// <returns>true/false if the key has a <see cref="DebugDrawBuffer" /> associated with it.</returns> |
| | | 70 | | public static bool HasUnmanagedBuffer(int key) |
| | 2 | 71 | | { |
| | 2 | 72 | | return s_UnmanagedBuffers.ContainsKey(key); |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Dereference the indicated unmanaged <see cref="DebugDrawBuffer" /> from the provider. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="key"> |
| | | 79 | | /// The key used to reference the <see cref="DebugDrawBuffer" />. |
| | | 80 | | /// </param> |
| | | 81 | | public static void RemoveUnmanagedBuffer(int key) |
| | 4 | 82 | | { |
| | 4 | 83 | | s_UnmanagedBuffers.TryRemove(key); |
| | 4 | 84 | | } |
| | | 85 | | |
| | | 86 | | // public static void Render() |
| | | 87 | | // { |
| | | 88 | | // int currentIndex = 0; |
| | | 89 | | // while (s_ManagedBuffers.MoveNext(ref currentIndex)) |
| | | 90 | | // { |
| | | 91 | | // DebugDrawBuffer buffer = s_ManagedBuffers.Entries[currentIndex - 1].Value; |
| | | 92 | | // buffer.Execute(); |
| | | 93 | | // } |
| | | 94 | | // } |
| | | 95 | | } |
| | | 96 | | } |