Пример #1
0
        /// <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
        }
Пример #2
0
 /// <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>()
         );
 }