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