Jypeli 10
The simple game programming library
ImageBatch.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Diagnostics;
3using System.Collections.Generic;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Graphics;
6
7#if !DISABLE_EFFECTS
8using Jypeli.Effects;
9#endif
10
11namespace Jypeli
12{
13 internal class TextureCoordinates
14 {
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}
System.Numerics.Vector2 Vector2
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Contains graphics resources.
Definition: Graphics.cs:36
static void SetSamplerState()
Definition: Graphics.cs:130
static void ResetSamplerState()
Definition: Graphics.cs:136
static Effect GetTextureEffect(ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled)
Definition: Graphics.cs:146
Draws images efficiently. Draw() calls should be made only between Begin() and End() calls....
Definition: ImageBatch.cs:32
void Begin(ref Matrix matrix, Texture2D texture)
Definition: ImageBatch.cs:74
Texture2D texture
Definition: ImageBatch.cs:51
void Draw(TextureCoordinates c, Vector2 position, Vector2 size, float angle)
Definition: ImageBatch.cs:119
VertexPositionTexture[] vertexBuffer
Definition: ImageBatch.cs:53
static readonly int VerticesPerTexture
Definition: ImageBatch.cs:48
static readonly Vector3[] Vertices
Definition: ImageBatch.cs:35
const int DefaultBufferSize
Definition: ImageBatch.cs:33
bool beginHasBeenCalled
Definition: ImageBatch.cs:56
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36