Jypeli  9
The simple game programming library
ImageBatch.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Diagnostics;
3 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Graphics;
6 
7 #if !DISABLE_EFFECTS
8 using Jypeli.Effects;
9 #endif
10 
11 namespace Jypeli
12 {
13  internal class TextureCoordinates
14  {
15  public Vector2 TopLeft;
16  public Vector2 TopRight;
17  public Vector2 BottomLeft;
18  public Vector2 BottomRight;
19  }
20 
31  internal class ImageBatch
32  {
33  private const int DefaultBufferSize = 512;
34 
35  static readonly Vector3[] Vertices = new Vector3[]
36  {
37  // Triangle 1
38  new Vector3(-0.5f, 0.5f, 0),
39  new Vector3(-0.5f, -0.5f, 0),
40  new Vector3(0.5f, 0.5f, 0),
41 
42  // Triangle 2
43  new Vector3(-0.5f, -0.5f, 0),
44  new Vector3(0.5f, -0.5f, 0),
45  new Vector3(0.5f, 0.5f, 0),
46  };
47 
48  static readonly int VerticesPerTexture = Vertices.Length;
49 
51  Texture2D texture;
52  Effect effect;
53  VertexPositionTexture[] vertexBuffer;
55  int iTexture = 0;
56  bool beginHasBeenCalled = false;
57 
58  public bool LightingEnabled = true;
59 
60 
61  public ImageBatch()
62  {
63  }
64 
65  public void Initialize()
66  {
67  //var capabilities = Game.GraphicsDevice.GraphicsDeviceCapabilities;
68  // Capabilities no longer supported in XNA 4.0
69  // GraphicsProfile.Reach maximum primitive count = 65535
70  this.BufferSize = Math.Min( DefaultBufferSize, 65535 / 2 );
71  vertexBuffer = new VertexPositionTexture[BufferSize * VerticesPerTexture];
72  }
73 
74  public void Begin( ref Matrix matrix, Texture2D texture )
75  {
76  Debug.Assert( !beginHasBeenCalled );
77  beginHasBeenCalled = true;
78 
79  this.matrix = matrix;
80  this.texture = texture;
81  iTexture = 0;
82  }
83 
84  public void End()
85  {
86  Debug.Assert( beginHasBeenCalled );
87 
88  Flush();
89 
90  int textureCount = iTexture;
91  beginHasBeenCalled = false;
92  }
93 
94  private void Flush()
95  {
96  if ( iTexture > 0 )
97  {
98  var device = Game.GraphicsDevice;
99  device.RasterizerState = RasterizerState.CullClockwise;
100  device.BlendState = BlendState.AlphaBlend;
101 
104 
105  for ( int i = 0; i < effect.CurrentTechnique.Passes.Count; i++ )
106  effect.CurrentTechnique.Passes[i].Apply();
107 
108  device.DrawUserPrimitives<VertexPositionTexture>(
109  PrimitiveType.TriangleList,
110  vertexBuffer, 0,
111  iTexture * 2 );
112 
114  }
115 
116  iTexture = 0;
117  }
118 
119  public void Draw( TextureCoordinates c, Vector2 position, Vector2 size, float angle )
120  {
121  Debug.Assert( beginHasBeenCalled );
122 
123  if ( iTexture >= BufferSize )
124  Flush();
125 
126  Matrix matrix =
127  Matrix.CreateScale( size.X, size.Y, 1f )
128  * Matrix.CreateRotationZ( angle )
129  * Matrix.CreateTranslation( position.X, position.Y, 0 )
130  ;
131  Vector3[] transformedPoints = new Vector3[VerticesPerTexture];
132  Vector3.Transform( Vertices, ref matrix, transformedPoints );
133 
134  int startIndex = ( iTexture * VerticesPerTexture );
135 
136  for ( int i = 0; i < VerticesPerTexture; i++ )
137  {
138  int bi = ( iTexture * VerticesPerTexture ) + i;
139  vertexBuffer[bi].Position = transformedPoints[i];
140  }
141 
142  // Triangle 1
143  vertexBuffer[startIndex + 0].TextureCoordinate = c.TopLeft;
144  vertexBuffer[startIndex + 1].TextureCoordinate = c.BottomLeft;
145  vertexBuffer[startIndex + 2].TextureCoordinate = c.TopRight;
146 
147  // Triangle 2
148  vertexBuffer[startIndex + 3].TextureCoordinate = c.BottomLeft;
149  vertexBuffer[startIndex + 4].TextureCoordinate = c.BottomRight;
150  vertexBuffer[startIndex + 5].TextureCoordinate = c.TopRight;
151 
152  iTexture++;
153  }
154  }
155 }
Jypeli.ImageBatch.iTexture
int iTexture
Definition: ImageBatch.cs:55
Jypeli.ImageBatch.ImageBatch
ImageBatch()
Definition: ImageBatch.cs:61
Jypeli.ImageBatch.beginHasBeenCalled
bool beginHasBeenCalled
Definition: ImageBatch.cs:56
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.TextureCoordinates.BottomLeft
Vector2 BottomLeft
Definition: ImageBatch.cs:17
Jypeli.ImageBatch.vertexBuffer
VertexPositionTexture[] vertexBuffer
Definition: ImageBatch.cs:53
Jypeli.ImageBatch.Begin
void Begin(ref Matrix matrix, Texture2D texture)
Definition: ImageBatch.cs:74
Jypeli
Definition: Automobile.cs:5
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.ImageBatch.matrix
Matrix matrix
Definition: ImageBatch.cs:50
Jypeli.ImageBatch.Initialize
void Initialize()
Definition: ImageBatch.cs:65
Jypeli.ImageBatch.effect
Effect effect
Definition: ImageBatch.cs:52
Jypeli.TextureCoordinates.BottomRight
Vector2 BottomRight
Definition: ImageBatch.cs:18
Jypeli.TextureCoordinates.TopRight
Vector2 TopRight
Definition: ImageBatch.cs:16
Jypeli.TextureCoordinates
Definition: ImageBatch.cs:14
Jypeli.Graphics.GetTextureEffect
static Effect GetTextureEffect(ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled)
Definition: Graphics.cs:143
Jypeli.ImageBatch.Flush
void Flush()
Definition: ImageBatch.cs:94
Jypeli.ImageBatch.VerticesPerTexture
static readonly int VerticesPerTexture
Definition: ImageBatch.cs:48
Jypeli.ImageBatch.BufferSize
int BufferSize
Definition: ImageBatch.cs:54
Jypeli.ImageBatch.Draw
void Draw(TextureCoordinates c, Vector2 position, Vector2 size, float angle)
Definition: ImageBatch.cs:119
Jypeli.ImageBatch.Vertices
static readonly Vector3[] Vertices
Definition: ImageBatch.cs:35
Jypeli.Graphics
Contains graphics resources.
Definition: Graphics.cs:36
System
Definition: CFFauxAttributes.cs:29
Jypeli.Effects
Definition: ExplosionSystem.cs:4
Jypeli.TextureCoordinates.TopLeft
Vector2 TopLeft
Definition: ImageBatch.cs:15
Jypeli.Graphics.ResetSamplerState
static void ResetSamplerState()
Definition: Graphics.cs:133
Jypeli.ImageBatch.LightingEnabled
bool LightingEnabled
Definition: ImageBatch.cs:58
Jypeli.Game.GraphicsDevice
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Jypeli.ImageBatch
Draws images efficiently. Draw() calls should be made only between Begin() and End() calls....
Definition: ImageBatch.cs:32
Jypeli.Game
Definition: Content.cs:46
Jypeli.ImageBatch.texture
Texture2D texture
Definition: ImageBatch.cs:51
Jypeli.Graphics.SetSamplerState
static void SetSamplerState()
Definition: Graphics.cs:127
Jypeli.ImageBatch.End
void End()
Definition: ImageBatch.cs:84
Jypeli.ImageBatch.DefaultBufferSize
const int DefaultBufferSize
Definition: ImageBatch.cs:33