Skip to content

Instantly share code, notes, and snippets.

@codesmith-fi
Last active December 16, 2015 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codesmith-fi/5488356 to your computer and use it in GitHub Desktop.
Save codesmith-fi/5488356 to your computer and use it in GitHub Desktop.
Bit optimized version of particle draw loop for delta engine
private VertexPositionColorTextured[] _vertices = new VertexPositionColorTextured[4 * 1000];
private short[] _indices = new short[6 * 1000];
public virtual void Draw(Time gameTime, Drawing drawing)
{
int pc = particles.Count;
if (_vertices.Length < (4 * pc) || _indices.Length < (6 * pc))
{
_vertices = new VertexPositionColorTextured[4 * pc];
_indices = new short[6 * pc];
}
Particle p;
var indicesIndex = 0;
var quadIndex = 0;
for (int i = 0; i < pc; i++, quadIndex+=4)
{
p = particles[i];
_vertices[quadIndex] = new VertexPositionColorTextured(
Rotate(p.DrawArea.TopLeft, p), p.Color, Point.Zero);
_vertices[quadIndex + 1] = new VertexPositionColorTextured(
Rotate(p.DrawArea.TopRight, p), p.Color, Point.UnitX);
_vertices[quadIndex + 2] = new VertexPositionColorTextured(
Rotate(p.DrawArea.BottomRight, p), p.Color, Point.One);
_vertices[quadIndex + 3] = new VertexPositionColorTextured(
Rotate(p.DrawArea.BottomLeft, p), p.Color, Point.UnitY);
_indices[indicesIndex++] = (short)quadIndex;
_indices[indicesIndex++] = (short)(quadIndex + 1);
_indices[indicesIndex++] = (short)(quadIndex + 2);
_indices[indicesIndex++] = (short)quadIndex;
_indices[indicesIndex++] = (short)(quadIndex + 2);
_indices[indicesIndex++] = (short)(quadIndex + 3);
}
foreach (Image img in textures)
{
img.Draw(new VertexPositionColorTextured[4]);
}
drawing.SetIndices(_indices, indicesIndex);
drawing.DrawVertices(VerticesMode.Triangles, _vertices);
}
protected Point Rotate(Point point, Particle particle)
{
var rotationCenter = particle.DrawArea.Center;
point -= rotationCenter;
float sin = MathFunctions.Sin(particle.Rotation);
float cos = MathFunctions.Cos(particle.Rotation);
point = new Point(point.X * cos - point.Y * sin, point.X * sin + point.Y * cos);
return rotationCenter + point;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment