-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathMonoGameCompat.cs
141 lines (111 loc) · 3.46 KB
/
MonoGameCompat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#if FNA
using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Nez
{
/// <summary>
/// implements some methods available in MonoGame that do not exist in FNA/XNA to make transitioning a codebase from MG/XNA to FNA
/// a little bit easier.
/// </summary>
public static class MonoGameCompat
{
#region GraphicsDevice
public static void DrawIndexedPrimitives( this GraphicsDevice self, PrimitiveType primitiveType, int baseVertex, int startIndex, int primitiveCount )
{
self.DrawIndexedPrimitives( primitiveType, baseVertex, 0, primitiveCount * 2, startIndex, primitiveCount );
}
/// <summary>
/// it is recommended to use GetRenderTargets() to avoid the extra Array.Copy when using FNA
/// </summary>
/// <returns>The render targets.</returns>
/// <param name="self">Self.</param>
/// <param name="outTargets">Out targets.</param>
public static void GetRenderTargets( this GraphicsDevice self, RenderTargetBinding[] outTargets )
{
var currentRenderTargets = self.GetRenderTargets();
System.Diagnostics.Debug.Assert( outTargets.Length == currentRenderTargets.Length, "Invalid outTargets array length!" );
Array.Copy( currentRenderTargets, outTargets, currentRenderTargets.Length );
}
#endregion
#region Point
/// <summary>
/// Gets a <see cref="Vector2"/> representation for this object.
/// </summary>
/// <returns>A <see cref="Vector2"/> representation for this object.</returns>
public static Vector2 ToVector2( this Point self )
{
return new Vector2( (float)self.X, (float)self.Y );
}
#endregion
#region Vector2
/// <summary>
/// Gets a <see cref="Point"/> representation for this object.
/// </summary>
/// <returns>A <see cref="Point"/> representation for this object.</returns>
public static Point ToPoint( this Vector2 self )
{
return new Point( (int)self.X, (int)self.Y );
}
#endregion
#region Rectangle
public static bool Contains( this Rectangle rect, Vector2 value )
{
return ( ( ( ( rect.X <= value.X ) && ( value.X < ( rect.X + rect.Width ) ) ) && ( rect.Y <= value.Y ) ) && ( value.Y < ( rect.Y + rect.Height ) ) );
}
#endregion
}
}
namespace Microsoft.Xna.Framework.Graphics
{
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
public struct VertexPosition : IVertexType
{
public Vector3 Position;
public static readonly VertexDeclaration VertexDeclaration;
public VertexPosition( Vector3 position )
{
Position = position;
}
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
public override int GetHashCode()
{
return Position.GetHashCode();
}
public override string ToString()
{
return "{{Position:" + Position + "}}";
}
public static bool operator ==( VertexPosition left, VertexPosition right )
{
return left.Position == right.Position;
}
public static bool operator !=( VertexPosition left, VertexPosition right )
{
return !( left == right );
}
public override bool Equals( object obj )
{
if( obj == null )
{
return false;
}
if( obj.GetType() != GetType() )
{
return false;
}
return this == (VertexPosition)obj;
}
static VertexPosition()
{
VertexElement[] elements = { new VertexElement( 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ) };
VertexDeclaration declaration = new VertexDeclaration( elements );
VertexDeclaration = declaration;
}
}
}
#endif