diff --git a/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj b/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj index 80e6e469..4785eb59 100644 --- a/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj +++ b/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj @@ -64,6 +64,7 @@ + diff --git a/ANX.Framework.TestCenter/AssertHelper.cs b/ANX.Framework.TestCenter/AssertHelper.cs index fb7ab827..783beab5 100644 --- a/ANX.Framework.TestCenter/AssertHelper.cs +++ b/ANX.Framework.TestCenter/AssertHelper.cs @@ -81,10 +81,25 @@ using ANXPlane = ANX.Framework.Plane; using XNARect = Microsoft.Xna.Framework.Rectangle; using ANXRect = ANX.Framework.Rectangle; +using XNABgr565 = Microsoft.Xna.Framework.Graphics.PackedVector.Bgr565; +using ANXBgr565 = ANX.Framework.Graphics.PackedVector.Bgr565; + namespace ANX.Framework.TestCenter { class AssertHelper { + public static void ConvertEquals(XNABgr565 lhs, ANXBgr565 rhs, String test) + { + if (lhs.PackedValue == rhs.PackedValue) + { + Assert.Pass(test + " passed"); + } + else + { + Assert.Fail(String.Format("{0] failed: Bgr565 XNA: ({1}) Bgr565 ANX: ({2})", test, lhs, rhs)); + } + } + public static void ConvertEquals(byte a, byte b, String test) { if (a == b) diff --git a/ANX.Framework.TestCenter/Strukturen/PackedVector/Bgr565Test.cs b/ANX.Framework.TestCenter/Strukturen/PackedVector/Bgr565Test.cs new file mode 100644 index 00000000..02118f10 --- /dev/null +++ b/ANX.Framework.TestCenter/Strukturen/PackedVector/Bgr565Test.cs @@ -0,0 +1,111 @@ +#region Using Statements + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; + +using XNABgr565 = Microsoft.Xna.Framework.Graphics.PackedVector.Bgr565; +using ANXBgr565 = ANX.Framework.Graphics.PackedVector.Bgr565; + +using XNAVector3 = Microsoft.Xna.Framework.Vector3; +using ANXVector3 = ANX.Framework.Vector3; + +#endregion // Using Statements + +#region License + +// +// This file is part of the ANX.Framework created by the "ANX.Framework developer group". +// +// This file is released under the Ms-PL license. +// +// +// +// Microsoft Public License (Ms-PL) +// +// This license governs use of the accompanying software. If you use the software, you accept this license. +// If you do not accept the license, do not use the software. +// +// 1.Definitions +// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning +// here as under U.S. copyright law. +// A "contribution" is the original software, or any additions or changes to the software. +// A "contributor" is any person that distributes its contribution under this license. +// "Licensed patents" are a contributor's patent claims that read directly on its contribution. +// +// 2.Grant of Rights +// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations +// in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to +// reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution +// or any derivative works that you create. +// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in +// section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed +// patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution +// in the software or derivative works of the contribution in the software. +// +// 3.Conditions and Limitations +// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. +// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your +// patent license from such contributor to the software ends automatically. +// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution +// notices that are present in the software. +// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including +// a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or +// object code form, you may only do so under a license that complies with this license. +// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees, +// or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the +// extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a +// particular purpose and non-infringement. + +#endregion // License + +namespace ANX.Framework.TestCenter.Strukturen.PackedVector +{ + [TestFixture] + class Bgr565Test + { + #region Testdata + + static object[] threefloats = + { + new object[] {DataFactory.RandomValue, DataFactory.RandomValue, DataFactory.RandomValue }, + new object[] {DataFactory.RandomValue, DataFactory.RandomValue, DataFactory.RandomValue }, + new object[] {DataFactory.RandomValue, DataFactory.RandomValue, DataFactory.RandomValue }, + new object[] {DataFactory.RandomValue, DataFactory.RandomValue, DataFactory.RandomValue }, + new object[] {DataFactory.RandomValue, DataFactory.RandomValue, DataFactory.RandomValue } + }; + + #endregion + + [Test, TestCaseSource("threefloats")] + public void contructor1(float r, float g, float b) + { + XNABgr565 xnaVal = new XNABgr565(r, g, b); + ANXBgr565 anxVal = new ANXBgr565(r, g, b); + + AssertHelper.ConvertEquals(xnaVal, anxVal, "Constructor1"); + } + + [Test, TestCaseSource("threefloats")] + public void contructor2(float r, float g, float b) + { + + XNABgr565 xnaVal = new XNABgr565(new XNAVector3(r, g, b)); + ANXBgr565 anxVal = new ANXBgr565(new ANXVector3(r, g, b)); + + AssertHelper.ConvertEquals(xnaVal, anxVal, "Constructor2"); + } + + [Test, TestCaseSource("threefloats")] + public void ToVector3(float r, float g, float b) + { + XNABgr565 xnaVal = new XNABgr565(r, g, b); + ANXBgr565 anxVal = new ANXBgr565(r, g, b); + + AssertHelper.ConvertEquals(xnaVal.ToVector3(), anxVal.ToVector3(), "ToVector3"); + } + + } +} diff --git a/ANX.Framework/Graphics/PackedVector/Bgr565.cs b/ANX.Framework/Graphics/PackedVector/Bgr565.cs index 73c46dc8..142e5f82 100644 --- a/ANX.Framework/Graphics/PackedVector/Bgr565.cs +++ b/ANX.Framework/Graphics/PackedVector/Bgr565.cs @@ -54,31 +54,92 @@ namespace ANX.Framework.Graphics.PackedVector { public struct Bgr565 : IPackedVector, IEquatable, IPackedVector { + private UInt16 packedValue; + + public Bgr565(float x, float y, float z) + { + uint r = (uint)(MathHelper.Clamp(x, 0f, 1f) * 31.0f) << 11; + uint g = (uint)(MathHelper.Clamp(y, 0f, 1f) * 63.0f) << 5; + uint b = (uint)(MathHelper.Clamp(z, 0f, 1f) * 31.0f); + + this.packedValue = (ushort)((r | g) | b); + } + + public Bgr565(Vector3 vector) + { + uint r = (uint)(MathHelper.Clamp(vector.X, 0f, 1f) * 31.0f) << 11; + uint g = (uint)(MathHelper.Clamp(vector.Y, 0f, 1f) * 63.0f) << 5; + uint b = (uint)(MathHelper.Clamp(vector.Z, 0f, 1f) * 31.0f); + + this.packedValue = (ushort)((r | g) | b); + } + public ushort PackedValue { get { - throw new NotImplementedException(); + return this.packedValue; } set { - throw new NotImplementedException(); + this.packedValue = value; } } - public void PackFromVector4(Vector4 vector) + public Vector3 ToVector3() { - throw new NotImplementedException(); + return new Vector3( ((packedValue >> 11) & 31) / 31.0f, + ((packedValue >> 5) & 63) / 63.0f, + ((packedValue) & 31) / 31.0f); } - public Vector4 ToVector4() + void IPackedVector.PackFromVector4(Vector4 vector) { - throw new NotImplementedException(); + uint r = (uint)(MathHelper.Clamp(vector.X, 0f, 1f) * 31.0f) << 11; + uint g = (uint)(MathHelper.Clamp(vector.Y, 0f, 1f) * 63.0f) << 5; + uint b = (uint)(MathHelper.Clamp(vector.Z, 0f, 1f) * 31.0f); + + this.packedValue = (ushort)((r | g) | b); + } + + Vector4 IPackedVector.ToVector4() + { + return new Vector4(this.ToVector3(), 1f); + } + + public override bool Equals(object obj) + { + if (obj != null && obj.GetType() == this.GetType()) + { + return this == (Bgr565)obj; + } + + return false; } public bool Equals(Bgr565 other) { - throw new NotImplementedException(); + return this.packedValue == other.packedValue; + } + + public override string ToString() + { + return this.packedValue.ToString("X4"); + } + + public override int GetHashCode() + { + return this.packedValue.GetHashCode(); + } + + public static bool operator ==(Bgr565 lhs, Bgr565 rhs) + { + return lhs.packedValue == rhs.packedValue; + } + + public static bool operator !=(Bgr565 lhs, Bgr565 rhs) + { + return lhs.packedValue != rhs.packedValue; } } }