示例#1
0
文件: Effect.cs 项目: zpconn/Gas
        public Effect( Renderer renderer, string filename )
        {
            //if (!File.Exists(filename))
            //    throw new FileNotFoundException(filename);

            if ( renderer == null )
                throw new ArgumentNullException( "renderer",
                    "Can't create an Effect without a valid renderer." );

            string compilationErrors = "";

            try
            {
                effect = Direct3D.Effect.FromFile( renderer.Device, filename, null, null,
                    null, ShaderFlags.None, null, out compilationErrors );

            }
            catch
            {
                Log.Write( "Unable to create effect " + filename + ". The compilation errors were: \n\n" +
                    compilationErrors );
            }

            renderer.AddGraphicsObject( this );
        }
示例#2
0
文件: Texture.cs 项目: zpconn/Gas
        /// <summary>
        /// Creates the texture from a file.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        /// <param name="filename">The name of the texture file to load.</param>
        public Texture( Renderer renderer, string filename )
        {
            if ( renderer == null )
                throw new ArgumentNullException( "renderer",
                    "Unable to create texture without a valid renderer reference." );

            if ( String.IsNullOrEmpty( filename ) )
                throw new ArgumentNullException( "filename",
                    "Unable to create texture without valid filename." );

            this.renderer = renderer;
            texFilename = filename;

            // Try to load the texture
            try
            {
                if ( File.Exists( filename ) == false )
                    throw new FileNotFoundException( filename );

                ImageInformation imageInfo = TextureLoader.ImageInformationFromFile( filename );
                size = new Size( imageInfo.Width, imageInfo.Height );

                if ( size.Width == 0 || size.Height == 0 )
                    throw new InvalidOperationException(
                        "Image size=" + size + " is invalid, unable to create texture." );

                hasAlpha = imageInfo.Format == Format.Dxt5 ||
                    imageInfo.Format == Format.Dxt3 ||
                    imageInfo.Format.ToString().StartsWith( "A" );

                d3dTexture = TextureLoader.FromFile( this.renderer.Device, filename );

                loaded = true;

                renderer.AddGraphicsObject( this );
            }
            catch ( Exception ex )
            {
                loaded = false;
                Log.Write( "Failed to load texture " + filename +
                    ", will use empty texture! Error: " + ex.ToString() );
            }
        }
示例#3
0
文件: Font.cs 项目: zpconn/Gas
        /// <summary>
        /// Constructs the font object using the desired font family. If the desired family
        /// is not supported, the fallback family defined in the Settings file is used.
        /// </summary>
        public Font( Renderer renderer, string familyName, int height )
        {
            this.renderer = renderer;
            this.familyName = familyName;
            this.height = height + 5;

            // Attempt to create the Windows font object
            try
            {
                windowsFont = new System.Drawing.Font( familyName, this.height,
                    System.Drawing.FontStyle.Regular );
            }
            catch
            {
                // Attempt to create the font using the "fallback" font family
                // defined in the Settings file
                Log.Write( "The desired font family was not available." );
            }

            d3dFont = new Direct3D.Font( renderer.Device, windowsFont );
            textSprite = new Direct3D.Sprite( renderer.Device );

            renderer.AddGraphicsObject( this );
        }
示例#4
0
文件: Mesh.cs 项目: zpconn/Gas
        /// <summary>
        /// Creates the mesh and allocates all the memory it will need.
        /// </summary>
        public Mesh( Renderer renderer, int numVertices, int numTriangles )
        {
            if ( renderer == null )
                throw new ArgumentNullException( "renderer", "Can't create mesh with an invalid renderer." );

            if ( numVertices <= 0 )
                throw new ArgumentOutOfRangeException( "numVertices", numVertices, "Can't create mesh with zero or fewer " +
                    "vertices." );

            if ( numTriangles <= 0 )
                throw new ArgumentOutOfRangeException( "numTriangles", numTriangles, "Can't create mesh with zero or fewer " +
                    "triangles." );

            this.renderer = renderer;
            this.numVertices = numVertices;
            this.numTriangles = numTriangles;

            vertices = new CustomVertex.PositionColoredTextured[ numVertices ];
            if ( vertices == null )
                throw new OutOfMemoryException( "Unable to allocate vertex array for Mesh." );

            triangles = new Triangle[ numTriangles ];
            if ( triangles == null )
                throw new OutOfMemoryException( "Unable to allocate triangle array for Mesh." );

            vertexBuffer = new VertexBuffer( typeof( CustomVertex.PositionColoredTextured ), numVertices,
                renderer.Device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColoredTextured.Format,
                Pool.Default );

            if ( vertexBuffer == null )
                throw new DirectXException( "Unable to create vertex buffer for Mesh." );

            indexBuffer = new IndexBuffer( typeof( short ), 3 * numTriangles, renderer.Device,
                Usage.WriteOnly, Pool.Default );

            if ( indexBuffer == null )
                throw new Direct3DXException( "Unable to create index buffer for Mesh." );

            renderer.AddGraphicsObject( this );
        }
示例#5
0
文件: Texture.cs 项目: zpconn/Gas
        /// <summary>
        /// Creates the texture as a render target.
        /// </summary>
        public Texture( Renderer renderer, int width, int height, bool alpha )
        {
            if ( renderer == null )
                throw new ArgumentNullException( "renderer",
                    "Unable to create texture without a valid renderer reference." );

            this.renderer = renderer;

            try
            {
                d3dTexture = new Microsoft.DirectX.Direct3D.Texture( renderer.Device,
                    width, height, 1, Usage.RenderTarget, alpha ? Format.A8R8G8B8 : Format.X8R8G8B8,
                    Pool.Default );

                d3dSurface = d3dTexture.GetSurfaceLevel( 0 );

                this.size = new Size( width, height );
                this.hasAlpha = alpha;

                loaded = true;

                renderer.AddGraphicsObject( this );
            }
            catch ( Exception ex )
            {
                loaded = false;
                Log.Write( "Failed to create texture as render target, will use empty texture!" +
                    " Error: " + ex.ToString() );
            }
        }