Jypeli 10
The simple game programming library
ShapeBatch.cs
Siirry tämän tiedoston dokumentaatioon.
1
2using System;
3using System.Diagnostics;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Graphics;
6
7namespace Jypeli
8{
19 internal class ShapeBatch
20 {
21 const int DefaultBufferSize = 512;
22
23 VertexPositionColor[] vertexBuffer;
24
34 Int16[] indexBuffer;
35
36 Effect effect;
38
39 SamplerState samplerState;
40
42 int iIndexBuffer = 0;
43 bool beginHasBeenCalled = false;
44
45 public bool LightingEnabled = true;
46
47
48 public void Initialize()
49 {
50 samplerState = new SamplerState
51 {
52 AddressU = TextureAddressMode.Clamp,
53 AddressW = TextureAddressMode.Clamp,
54 AddressV = TextureAddressMode.Clamp,
55 };
56 //var capabilities = Game.GraphicsDevice.GraphicsDeviceCapabilities;
57 // Capabilities no longer supported in XNA 4.0
58 // GraphicsProfile.Reach maximum primitive count = 65535
59 int vertexBufferSize = Math.Min( DefaultBufferSize, 65535 * 3 );
60
61 vertexBuffer = new VertexPositionColor[vertexBufferSize];
62 indexBuffer = new Int16[vertexBufferSize * 2];
63 }
64
65 public void Begin( ref Matrix matrix )
66 {
67 Debug.Assert( !beginHasBeenCalled );
68 beginHasBeenCalled = true;
69
70 this.matrix = matrix;
71 iVertexBuffer = 0;
72 iIndexBuffer = 0;
73 }
74
75 public void End()
76 {
77 Debug.Assert( beginHasBeenCalled );
78 Flush();
79 beginHasBeenCalled = false;
80 }
81
82 private void Flush()
83 {
84 if ( iIndexBuffer != 0 )
85 {
86 Game.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
87 Game.GraphicsDevice.SamplerStates[0] = samplerState;
88
90 for ( int i = 0; i < effect.CurrentTechnique.Passes.Count; i++ )
91 effect.CurrentTechnique.Passes[i].Apply();
92
93 Game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
94 PrimitiveType.TriangleList,
96 indexBuffer, 0,
97 iIndexBuffer / 3 );
98 }
99
100 iVertexBuffer = 0;
101 iIndexBuffer = 0;
102 }
103
104 public void Draw( Vector[] vertices, Int16[] indices, Color color, Vector2 position, Vector2 size, float angle )
105 {
106 if ( ( iVertexBuffer + vertices.Length ) > vertexBuffer.Length ||
107 ( iIndexBuffer + indices.Length ) > indexBuffer.Length )
108 {
109 Flush();
110 }
111
112 Matrix matrix =
113 Matrix.CreateScale( size.X, size.Y, 1f )
114 * Matrix.CreateRotationZ( angle )
115 * Matrix.CreateTranslation( position.X, position.Y, 0 )
116 ;
117
118 int startIndex = iVertexBuffer;
119
120 for ( int i = 0; i < vertices.Length; i++ )
121 {
122 Vector p = vertices[i];
123 Vector3 p3 = new Vector3( (float)p.X, (float)p.Y, 0f );
124 vertexBuffer[iVertexBuffer].Position = Vector3.Transform( p3, matrix );
125 vertexBuffer[iVertexBuffer].Color = color.AsXnaColor();
127 }
128
129 for ( int i = 0; i < indices.Length; i++ )
130 {
131 indexBuffer[iIndexBuffer] = (Int16)( startIndex + indices[i] );
132 iIndexBuffer++;
133 }
134 }
135 }
136}
System.Numerics.Vector2 Vector2
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Contains graphics resources.
Definition: Graphics.cs:36
static Effect GetColorEffect(ref Matrix worldMatrix, bool lightingEnabled)
Definition: Graphics.cs:167
Draws simple shapes efficiently. Draw() calls should be made only between Begin() and End() calls....
Definition: ShapeBatch.cs:20
SamplerState samplerState
Definition: ShapeBatch.cs:39
bool beginHasBeenCalled
Definition: ShapeBatch.cs:43
const int DefaultBufferSize
Definition: ShapeBatch.cs:21
void Begin(ref Matrix matrix)
Definition: ShapeBatch.cs:65
void Draw(Vector[] vertices, Int16[] indices, Color color, Vector2 position, Vector2 size, float angle)
Definition: ShapeBatch.cs:104
Int16[] indexBuffer
Buffer for index values to the vertex buffer.
Definition: ShapeBatch.cs:34
VertexPositionColor[] vertexBuffer
Definition: ShapeBatch.cs:23
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Väri.
Definition: Color.cs:13
XnaColor AsXnaColor()
Definition: Color.cs:46
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334