Guest User

Untitled

a guest
Aug 10th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. public class DrawModule <T>
  2.     {
  3.         public GraphicsDevice Device;
  4.         public VertexDeclaration VertexDeclaration;
  5.         private VertexBuffer _vertexBuffer;
  6.         private IndexBuffer _indexBuffer;
  7.         private List<T> _vertices;
  8.         private List<ushort> _indices;
  9.         private const ushort MaxPrimitives = 65535;
  10.  
  11.         private ushort _primitiveCount;
  12.         private bool _readyToDraw;
  13.  
  14.         public DrawModule(GraphicsDevice device, VertexDeclaration vertexDeclaration)
  15.         {
  16.             VertexDeclaration = vertexDeclaration;
  17.             Device = device;
  18.  
  19.             _primitiveCount = 0;
  20.             _readyToDraw = false;
  21.  
  22.            _vertices = new List<T>();
  23.            _indices = new List<ushort>();
  24.         }
  25.  
  26.         public bool HasSpace(List<ushort> indices)
  27.         {
  28.             return ((indices.Count + _indices.Count)/3 < MaxPrimitives);
  29.         }
  30.  
  31.         public bool AddData(List<T> vertices, List<ushort> indices)
  32.         {
  33.             if (HasSpace(indices))
  34.             {
  35.                 ushort offset = (ushort)_vertices.Count();
  36.  
  37.                 _vertices.AddRange(vertices);
  38.  
  39.                 foreach (ushort index in indices)
  40.                 {
  41.                     _indices.Add((ushort)(index + offset));
  42.                 }
  43.             }
  44.  
  45.             return false;
  46.         }
  47.  
  48.         public void PrepareToDraw()
  49.         {
  50.             _vertexBuffer = new VertexBuffer(Device, VertexDeclaration, _vertices.Count, BufferUsage.WriteOnly);
  51.             _vertexBuffer.SetData(_vertices.ToArray());
  52.  
  53.             _indexBuffer = new IndexBuffer(Device, typeof(ushort), _indices.Count, BufferUsage.WriteOnly);
  54.         }
  55.  
  56.         public void Draw()
  57.         {
  58.             if (_readyToDraw)
  59.             {
  60.                 // do stuffs
  61.             }
Advertisement
Add Comment
Please, Sign In to add comment