Jypeli  5
The simple game programming library
LineBatch.cs
Siirry tämän tiedoston dokumentaatioon.
1 
2 using System;
3 using System.Diagnostics;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 
7 namespace Jypeli
8 {
9  internal class LineBatch
10  {
11  VertexPositionColor[] vertexBuffer = new VertexPositionColor[512];
12  Effect effect;
13  Matrix matrix;
14  int iVertexBuffer = 0;
15  bool beginHasBeenCalled = false;
16  public bool LightingEnabled = true;
17 
18 
19  public void Begin( ref Matrix matrix )
20  {
21  Debug.Assert( !beginHasBeenCalled );
22  beginHasBeenCalled = true;
23 
24  this.matrix = matrix;
25  iVertexBuffer = 0;
26  }
27 
28  public void End()
29  {
30  Debug.Assert( beginHasBeenCalled );
31  Flush();
32  beginHasBeenCalled = false;
33  }
34 
35  private void Flush()
36  {
37  if ( iVertexBuffer > 0 )
38  {
39  effect = Graphics.GetColorEffect(ref matrix, LightingEnabled);
40  effect.CurrentTechnique.Passes[0].Apply();
41 
42  Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
43  PrimitiveType.LineList, vertexBuffer, 0, iVertexBuffer / 2);
44  }
45 
46  iVertexBuffer = 0;
47  }
48 
49  public void Draw(Vector startPoint, Vector endPoint, Color color)
50  {
51  if ((iVertexBuffer + 2) > vertexBuffer.Length)
52  {
53  Flush();
54  }
55 
56  vertexBuffer[iVertexBuffer++] = new VertexPositionColor(
57  new Vector3((float)startPoint.X, (float)startPoint.Y, 0f),
58  color.AsXnaColor());
59  vertexBuffer[iVertexBuffer++] = new VertexPositionColor(
60  new Vector3((float)endPoint.X, (float)endPoint.Y, 0f),
61  color.AsXnaColor());
62  }
63  }
64 }