From 36ef5b38b7880b38d62fd122df2fdfb984de8a8f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Aug 2019 19:52:22 +0200 Subject: [PATCH] Ported DirectX12GameEngine.Core project --- .../AsyncDictionary.cs | 62 ++++++++++++++++++ .../DirectX12GameEngine.Core.csproj | 17 ++++- .../DisposableExtensions.cs | 13 ++++ src/DirectX12GameEngine.Core/ICollector.cs | 10 +++ src/DirectX12GameEngine.Core/IIdentifiable.cs | 9 +++ src/DirectX12GameEngine.Core/MemoryHelper.cs | 33 ++++++++++ .../QuaternionExtensions.cs | 63 ++++++++++++++++++ .../WindowsRuntimeExtensions.cs | 64 +++++++++++++++++++ 8 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 src/DirectX12GameEngine.Core/AsyncDictionary.cs create mode 100644 src/DirectX12GameEngine.Core/DisposableExtensions.cs create mode 100644 src/DirectX12GameEngine.Core/ICollector.cs create mode 100644 src/DirectX12GameEngine.Core/IIdentifiable.cs create mode 100644 src/DirectX12GameEngine.Core/MemoryHelper.cs create mode 100644 src/DirectX12GameEngine.Core/QuaternionExtensions.cs create mode 100644 src/DirectX12GameEngine.Core/WindowsRuntimeExtensions.cs diff --git a/src/DirectX12GameEngine.Core/AsyncDictionary.cs b/src/DirectX12GameEngine.Core/AsyncDictionary.cs new file mode 100644 index 00000000..4a872a70 --- /dev/null +++ b/src/DirectX12GameEngine.Core/AsyncDictionary.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DirectX12GameEngine.Core +{ + public class AsyncDictionary + { + private readonly Dictionary dictionary = new Dictionary(); + + public event EventHandler> ItemAdded; + + public void Add(TKey key, TValue value) + { + dictionary.Add(key, value); + ItemAdded?.Invoke(this, new DictionaryChangedEventArgs(key, value)); + } + + public bool ContainsKey(TKey key) => dictionary.ContainsKey(key); + + public bool ContainsValue(TValue value) => dictionary.ContainsValue(value); + + public async Task GetValueAsync(TKey key) + { + if (!dictionary.TryGetValue(key, out TValue value)) + { + TaskCompletionSource tcs = new TaskCompletionSource(); + + void OnItemAdded(object s, DictionaryChangedEventArgs e) + { + if (e.Item.Key.Equals(key)) + { + tcs.TrySetResult(e.Item.Value); + } + } + + ItemAdded += OnItemAdded; + value = await tcs.Task; + ItemAdded -= OnItemAdded; + } + + return value; + } + + public Task this[TKey key] => GetValueAsync(key); + } + + public class DictionaryChangedEventArgs : EventArgs + { + public DictionaryChangedEventArgs(TKey key, TValue value) + : this(new KeyValuePair(key, value)) + { + } + + public DictionaryChangedEventArgs(KeyValuePair item) + { + Item = item; + } + + public KeyValuePair Item { get; } + } +} diff --git a/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj b/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj index 9f5c4f4a..6a9c3d3b 100644 --- a/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj +++ b/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj @@ -1,7 +1,22 @@ - + netstandard2.0 + + true + + + + true + + + + + + + + + diff --git a/src/DirectX12GameEngine.Core/DisposableExtensions.cs b/src/DirectX12GameEngine.Core/DisposableExtensions.cs new file mode 100644 index 00000000..2f1f8e49 --- /dev/null +++ b/src/DirectX12GameEngine.Core/DisposableExtensions.cs @@ -0,0 +1,13 @@ +using System; + +namespace DirectX12GameEngine.Core +{ + public static class DisposableExtensions + { + public static T DisposeBy(this T item, ICollector collector) where T : IDisposable + { + collector.Disposables.Add(item); + return item; + } + } +} diff --git a/src/DirectX12GameEngine.Core/ICollector.cs b/src/DirectX12GameEngine.Core/ICollector.cs new file mode 100644 index 00000000..5c5bebc3 --- /dev/null +++ b/src/DirectX12GameEngine.Core/ICollector.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace DirectX12GameEngine.Core +{ + public interface ICollector + { + ICollection Disposables { get; } + } +} diff --git a/src/DirectX12GameEngine.Core/IIdentifiable.cs b/src/DirectX12GameEngine.Core/IIdentifiable.cs new file mode 100644 index 00000000..3e327b71 --- /dev/null +++ b/src/DirectX12GameEngine.Core/IIdentifiable.cs @@ -0,0 +1,9 @@ +using System; + +namespace DirectX12GameEngine.Core +{ + public interface IIdentifiable + { + Guid Id { get; set; } + } +} diff --git a/src/DirectX12GameEngine.Core/MemoryHelper.cs b/src/DirectX12GameEngine.Core/MemoryHelper.cs new file mode 100644 index 00000000..4fef2426 --- /dev/null +++ b/src/DirectX12GameEngine.Core/MemoryHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Runtime.CompilerServices; + +namespace DirectX12GameEngine.Core +{ + public static class MemoryHelper + { + public static unsafe void Copy(IntPtr source, IntPtr destination, int sizeInBytesToCopy) + { + Copy(new Span(source.ToPointer(), sizeInBytesToCopy), destination); + } + + public static unsafe void Copy(Span source, IntPtr destination) where T : unmanaged + { + source.CopyTo(new Span(destination.ToPointer(), source.Length)); + } + + public static unsafe void Copy(IntPtr source, Span destination) where T : unmanaged + { + new Span(source.ToPointer(), destination.Length).CopyTo(destination); + } + + public static unsafe void Copy(in T source, IntPtr destination) where T : unmanaged + { + Unsafe.Copy(destination.ToPointer(), ref Unsafe.AsRef(source)); + } + + public static unsafe void Copy(IntPtr source, ref T destination) where T : unmanaged + { + Unsafe.Copy(ref destination, source.ToPointer()); + } + } +} diff --git a/src/DirectX12GameEngine.Core/QuaternionExtensions.cs b/src/DirectX12GameEngine.Core/QuaternionExtensions.cs new file mode 100644 index 00000000..e20430f6 --- /dev/null +++ b/src/DirectX12GameEngine.Core/QuaternionExtensions.cs @@ -0,0 +1,63 @@ +using System; +using System.Numerics; + +namespace DirectX12GameEngine.Core +{ + public static class QuaternionExtensions + { + public static Vector3 ToEuler(in Quaternion rotation) + { + Vector3 rotationEuler; + + float xx = rotation.X * rotation.X; + float yy = rotation.Y * rotation.Y; + float zz = rotation.Z * rotation.Z; + float xy = rotation.X * rotation.Y; + float zw = rotation.Z * rotation.W; + float zx = rotation.Z * rotation.X; + float yw = rotation.Y * rotation.W; + float yz = rotation.Y * rotation.Z; + float xw = rotation.X * rotation.W; + + rotationEuler.Y = (float)Math.Asin(2.0f * (yw - zx)); + double test = Math.Cos(rotationEuler.Y); + + if (test > 1e-6f) + { + rotationEuler.Z = (float)Math.Atan2(2.0f * (xy + zw), 1.0f - (2.0f * (yy + zz))); + rotationEuler.X = (float)Math.Atan2(2.0f * (yz + xw), 1.0f - (2.0f * (yy + xx))); + } + else + { + rotationEuler.Z = (float)Math.Atan2(2.0f * (zw - xy), 2.0f * (zx + yw)); + rotationEuler.X = 0.0f; + } + + return rotationEuler; + } + + public static Quaternion ToQuaternion(in Vector3 value) + { + Quaternion rotation; + + var halfAngles = value * 0.5f; + + var fSinX = (float)Math.Sin(halfAngles.X); + var fCosX = (float)Math.Cos(halfAngles.X); + var fSinY = (float)Math.Sin(halfAngles.Y); + var fCosY = (float)Math.Cos(halfAngles.Y); + var fSinZ = (float)Math.Sin(halfAngles.Z); + var fCosZ = (float)Math.Cos(halfAngles.Z); + + var fCosXY = fCosX * fCosY; + var fSinXY = fSinX * fSinY; + + rotation.X = fSinX * fCosY * fCosZ - fSinZ * fSinY * fCosX; + rotation.Y = fSinY * fCosX * fCosZ + fSinZ * fSinX * fCosY; + rotation.Z = fSinZ * fCosXY - fSinXY * fCosZ; + rotation.W = fCosZ * fCosXY + fSinXY * fSinZ; + + return rotation; + } + } +} diff --git a/src/DirectX12GameEngine.Core/WindowsRuntimeExtensions.cs b/src/DirectX12GameEngine.Core/WindowsRuntimeExtensions.cs new file mode 100644 index 00000000..787b5040 --- /dev/null +++ b/src/DirectX12GameEngine.Core/WindowsRuntimeExtensions.cs @@ -0,0 +1,64 @@ +using System.Numerics; +using Windows.Foundation; + +namespace DirectX12GameEngine.Core +{ + public static class PointExtensions + { + public static Vector2 ToVector2(this in Point value) + { + return new Vector2((float)value.X, (float)value.Y); + } + + public static Point ToPoint(this in Vector2 value) + { + return new Point(value.X, value.Y); + } + + public static System.Drawing.PointF ToPointF(this in Point value) + { + return new System.Drawing.PointF((float)value.X, (float)value.Y); + } + + public static Point ToPoint(this in System.Drawing.PointF value) + { + return new Point(value.X, value.Y); + } + } + + public static class RectangleExtensions + { + public static System.Drawing.RectangleF ToRectangleF(this in Rect value) + { + return new System.Drawing.RectangleF((float)value.X, (float)value.Y, (float)value.Width, (float)value.Height); + } + + public static Rect ToRect(this in System.Drawing.RectangleF value) + { + return new Rect(value.X, value.Y, value.Width, value.Height); + } + } + + public static class SizeExtensions + { + public static Vector2 ToVector2(this in Size value) + { + return new Vector2((float)value.Width, (float)value.Height); + } + + public static Size ToSize(this in Vector2 value) + { + return new Size(value.X, value.Y); + } + + public static System.Drawing.SizeF ToSizeF(this in Size value) + { + return new System.Drawing.SizeF((float)value.Width, (float)value.Height); + } + + public static Size ToSize(this in System.Drawing.SizeF value) + { + return new Size(value.Width, value.Height); + } + } +}