/// <summary>Initializes a new graphics resource keeper for a static mesh</summary> /// <param name="graphicsDevice">Graphics device the mesh lives on</param> /// <param name="vertexCount">Number of vertices that will be required</param> protected StaticMesh( GraphicsDevice graphicsDevice, int vertexCount ) { this.GraphicsDevice = graphicsDevice; #if XNA_4 // Create a new vertex buffer with the requested size this.VertexBuffer = new VertexBuffer( graphicsDevice, typeof(VertexType), vertexCount, BufferUsage.WriteOnly ); #else // Create the vertex declaration this.stride = VertexDeclarationHelper.GetStride <VertexType>(); this.VertexDeclaration = new VertexDeclaration( graphicsDevice, VertexDeclarationHelper.BuildElementList <VertexType>() ); try { this.ownsVertexDeclaration = true; // Create a new vertex buffer with the requested size this.VertexBuffer = new VertexBuffer( graphicsDevice, typeof(VertexType), vertexCount, BufferUsage.WriteOnly ); } catch (Exception) { this.VertexDeclaration.Dispose(); this.VertexDeclaration = null; throw; } #endif }
/// <summary> /// Automatically creates the vertex declaration for the specified type /// </summary> /// <typeparam name="VertexType"> /// Vertex type for which the declaration will be built /// </typeparam> /// <param name="vertexDeclaration"> /// Output parameter that receives the automatically generated vertex declaration /// </param> /// <param name="stride"> /// Output parameter that resizes the size of a particle vertex in bytes /// </param> private void autoCreateVertexDeclaration <VertexType>( out VertexDeclaration vertexDeclaration, out int stride ) where VertexType : struct { // In reverse order so we don't loose the pointer if an exception happens // inside GetStride() stride = VertexDeclarationHelper.GetStride <VertexType>(); vertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexDeclarationHelper.BuildElementList <VertexType>() ); }
public void TestFullConstructor() { MockedGraphicsDeviceService mockGraphicsDeviceService = new MockedGraphicsDeviceService(); using (IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) { VertexElement[] elements = VertexDeclarationHelper.BuildElementList <TestVertex>(); using ( VertexDeclaration declaration = new VertexDeclaration( mockGraphicsDeviceService.GraphicsDevice, elements ) ) { using ( TestStaticMesh test = new TestStaticMesh( mockGraphicsDeviceService.GraphicsDevice, declaration, 4 ) ) { } } } }
public void TestThrowInFullConstructorRollback() { MockedGraphicsDeviceService mockGraphicsDeviceService = new MockedGraphicsDeviceService(); using (IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) { VertexElement[] elements = VertexDeclarationHelper.BuildElementList <TestVertex>(); using ( VertexDeclaration declaration = new VertexDeclaration( mockGraphicsDeviceService.GraphicsDevice, elements ) ) { Assert.Throws <ArgumentOutOfRangeException>( delegate() { using ( TestIndexedStaticMesh test = new TestIndexedStaticMesh( mockGraphicsDeviceService.GraphicsDevice, declaration, 4, -1 ) ) { } } ); } } }
public void Draw(GraphicsDevice dev) { dev.VertexDeclaration = VertexDeclarationHelper.Get(typeof(VertexPositionNormalColored)); dev.DrawUserPrimitives <VertexPositionNormalColored>(PrimitiveType.TriangleFan, _vertexes, 0, _vertexes.Length - 2); }