3

当运行这个 XNA 应用程序时,它应该显示一个从左上角移动到右下角的旋转矩形。

看起来我的 F# 版本明显慢得多。似乎 Draw 方法跳过了很多帧。

我正在使用 VS 2012 RC、XNA 4.0、.NET 4.5、F# 3.0。我正在努力使其尽可能实用。

性能不佳的原因可能是什么?

C#:

class Program
{
    static void Main(string[] args)
    {
        using (var game = new FlockGame())
        {
            game.Run();
        }
    }
}

public class FlockGame : Game
{
    private GraphicsDeviceManager graphics;
    private DrawingManager drawingManager;
    private Vector2 position = Vector2.Zero;

    public FlockGame()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void Initialize()
    {
        drawingManager = new DrawingManager(graphics.GraphicsDevice);
        this.IsFixedTimeStep = false;
    }

    protected override void Update(GameTime gameTime)
    {
        position = new Vector2(position.X + 50.1f * (float)gameTime.ElapsedGameTime.TotalSeconds,
                               position.Y + 50.1f * (float)gameTime.ElapsedGameTime.TotalSeconds);
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        //this.GraphicsDevice.Clear(Color.Lavender)

        drawingManager.DrawRectangle(position, new Vector2(100.0f, 100.0f), 0.7845f, Color.Red);

        base.Draw(gameTime);
    }
}

public class DrawingManager
{
    private GraphicsDevice GraphicsDevice;
    private Effect Effect;

    public DrawingManager(GraphicsDevice graphicsDevice)
    {
        GraphicsDevice = graphicsDevice;
        this.Effect =
            new BasicEffect(this.GraphicsDevice)
                {
                    VertexColorEnabled = true,
                    Projection = Matrix.CreateOrthographicOffCenter(0.0f, this.GraphicsDevice.Viewport.Width,
                                                                    this.GraphicsDevice.Viewport.Height,                                                                         0.0f, 0.0f, 1.0f)
                };
    }

    private VertexPositionColor[] GetRectangleVertices (Vector2 center, Vector2 size, float radians, Color color)
    {
        var halfSize = size/2.0f;
        var topLeft = -halfSize;
        var bottomRight = halfSize;
        var topRight = new Vector2(bottomRight.X, topLeft.Y);
        var bottomLeft = new Vector2(topLeft.X, bottomRight.Y);

        topLeft = Vector2.Transform(topLeft, Matrix.CreateRotationZ(radians)) + center;
        topRight = Vector2.Transform(topRight, Matrix.CreateRotationZ(radians)) + center;
        bottomRight = Vector2.Transform(bottomRight, Matrix.CreateRotationZ(radians)) + center;
        bottomLeft = Vector2.Transform(bottomLeft, Matrix.CreateRotationZ(radians)) + center;

        return new VertexPositionColor[]
        {
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(topRight, 0.0f), color),
            new VertexPositionColor(new Vector3(topRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
        };
    }


    public void DrawRectangle(Vector2 center, Vector2 size, float radians, Color color)
    {
        var vertices = GetRectangleVertices(center, size, radians, color);

        foreach (var pass in this.Effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            this.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, vertices.Length/2);
        }
    }
}

F#:

namespace Flocking

module FlockingProgram = 
    open System
    open Flocking

    [<STAThread>]
    [<EntryPoint>]
    let Main _ =
        use g = new FlockGame()
        g.Run()
        0

//------------------------------------------------ ------------------------------

namespace Flocking

open System
open System.Diagnostics
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input


type public FlockGame() as this =
    inherit Game()
    let mutable graphics = new GraphicsDeviceManager(this)
    let mutable drawingManager = null
    let mutable position = Vector2.Zero

    override Game.LoadContent() =
        drawingManager <- new Rendering.DrawingManager(graphics.GraphicsDevice)
        this.IsFixedTimeStep <- false

    override Game.Update gameTime =
        position <- Vector2(position.X + 50.1f * float32 gameTime.ElapsedGameTime.TotalSeconds,
                            position.Y + 50.1f * float32 gameTime.ElapsedGameTime.TotalSeconds)
        base.Update gameTime

    override Game.Draw gameTime =
        //this.GraphicsDevice.Clear(Color.Lavender)

        Rendering.DrawRectangle(drawingManager, position, Vector2(100.0f, 100.0f), 0.7845f, Color.Red)

        base.Draw gameTime

//------------------------------------------------ ------------------------------

namespace Flocking

open System
open System.Collections.Generic
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input

module Rendering = 

    [<AllowNullLiteral>]
    type DrawingManager (graphicsDevice : GraphicsDevice) = 
        member this.GraphicsDevice = graphicsDevice
        member this.Effect =
            new BasicEffect(this.GraphicsDevice,
                VertexColorEnabled = true,
                Projection = Matrix.CreateOrthographicOffCenter(0.0f,
                    float32 this.GraphicsDevice.Viewport.Width,
                    float32 this.GraphicsDevice.Viewport.Height,
                    0.0f, 0.0f, 1.0f))


    let private GetRectangleVertices (center:Vector2, size:Vector2, radians:float32, color:Color) = 
        let halfSize = size / 2.0f
        let mutable topLeft = -halfSize
        let mutable bottomRight = halfSize
        let mutable topRight = new Vector2(bottomRight.X, topLeft.Y)
        let mutable bottomLeft = new Vector2(topLeft.X, bottomRight.Y)

        topLeft <- Vector2.Transform(topLeft, Matrix.CreateRotationZ(radians)) + center
        topRight <- Vector2.Transform(topRight, Matrix.CreateRotationZ(radians)) + center
        bottomRight <- Vector2.Transform(bottomRight, Matrix.CreateRotationZ(radians)) + center
        bottomLeft <- Vector2.Transform(bottomLeft, Matrix.CreateRotationZ(radians)) + center

        [|
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(topRight, 0.0f), color)
            new VertexPositionColor(new Vector3(topRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
        |]


    let DrawRectangle (drawingManager:DrawingManager, center:Vector2, size:Vector2, radians:float32, color:Color) = 
        let vertices = GetRectangleVertices(center, size, radians, color)

        for pass in drawingManager.Effect.CurrentTechnique.Passes do
            pass.Apply()
            drawingManager.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, vertices.Length/2)
4

1 回答 1

16

这是一个猜测;我对这个域一无所知,但是每次读取该Effect属性时都会创建一个新对象;BasicEffect也许您想let在构造函数中使用 a 初始化该对象一次,然后返回该对象?

于 2012-06-18T21:30:38.313 回答