Skip to content

Instantly share code, notes, and snippets.

@kot-behemoth
Created November 2, 2012 15:02
Show Gist options
  • Save kot-behemoth/4001872 to your computer and use it in GitHub Desktop.
Save kot-behemoth/4001872 to your computer and use it in GitHub Desktop.
LinkBatch
using System;
using Microsoft.Xna.Framework;
namespace LineBatch
{
public class LineBatchTest : Game
{
GraphicsDeviceManager graphics;
LineBatch lineBatch;
public LineBatchTest()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
lineBatch = new LineBatch(graphics.GraphicsDevice, new Vector2(0, 300));
base.Initialize();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
float scale = 100;
float step = 1;
graphics.GraphicsDevice.Clear(Color.DarkOrange);
lineBatch.Begin();
for (float x = 0; x < 800; x+=step)
{
float delta = x + step;
lineBatch.Batch(new Vector2(x, (float) Math.Sin(MathHelper.ToRadians(x)) * scale),
new Vector2(delta, (float) Math.Sin(MathHelper.ToRadians(delta)) * scale),
Color.DarkSlateGray,
0);
}
lineBatch.End();
base.Draw(gameTime);
}
}
}
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace LineBatch
{
// Simple Line Batch class. Based on
// http://www.gamedev.net/topic/511030-xna-basic-2d-line-and-point-batch-source/
public sealed class LineBatch
{
private GraphicsDevice graphicsDevice;
private List<VertexPositionColor> points = new List<VertexPositionColor>();
private List<short> indices = new List<short>();
private BasicEffect basicEffect;
private RasterizerState rasterizerState;
public Vector2 Origin;
public LineBatch(GraphicsDevice graphicsDevice, Vector2 origin, float alpha = 1.0f)
{
this.graphicsDevice = graphicsDevice;
basicEffect = new BasicEffect(graphicsDevice)
{
VertexColorEnabled = true,
Alpha = alpha,
Projection = Matrix.CreateOrthographicOffCenter(0.0F,
graphicsDevice.Viewport.Width,
graphicsDevice.Viewport.Height,
0.0F, 0.0F, -1.0F),
View = Matrix.Identity,
World = Matrix.Identity
};
rasterizerState = new RasterizerState
{
FillMode = FillMode.Solid
};
this.graphicsDevice.RasterizerState = rasterizerState;
Origin = origin;
}
public void Begin()
{
points.Clear();
indices.Clear();
}
// Batch a single-colour line
public void Batch(Vector2 startPoint, Vector2 endPoint, Color color, float layerDepth)
{
Batch(startPoint, color, layerDepth);
Batch(endPoint, color, layerDepth);
}
/// <summary>
/// Batch a multicolour line
/// </summary>
/// <param name="startPoint"></param>
/// <param name="startColor"></param>
/// <param name="endPoint"></param>
/// <param name="endColor"></param>
/// <param name="layerDepth"></param>
public void Batch(Vector2 startPoint, Color startColor, Vector2 endPoint, Color endColor, float layerDepth)
{
Batch(startPoint, startColor, layerDepth);
Batch(endPoint, endColor, layerDepth);
}
/// <summary>
/// Batch a single point - used by other batches
/// </summary>
/// <param name="point"></param>
/// <param name="color"></param>
/// <param name="layerDepth"></param>
public void Batch(Vector2 point, Color color, float layerDepth)
{
VertexPositionColor batchPoint = new VertexPositionColor(new Vector3(Origin + point, layerDepth), color);
points.Add(batchPoint);
indices.Add((short)indices.Count);
}
/// <summary>
/// Finish batching and actually draw the line
/// </summary>
public void End()
{
if (points.Count <= 0) return;
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
effectPass.Apply();
graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.LineList, points.ToArray(), 0, points.Count,
indices.ToArray(), 0, points.Count/2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment