XNA与Winform,视口错误

时间:2012-03-11 17:08:54

标签: c# winforms xna viewport

我有一个winforms应用程序,它使用xna作为各种编辑器。我基本上有一个自定义控件,它包装一个新创建的GraphicsDevice(使用面板句柄和宽度/高度来创建视口)和绘制循环。

现在,每当我尝试使用此控件时,我都会异常地说:

The viewport is invalid. The viewport cannot be larger than or outside the bounds of the current render target.The MinDepth and MaxDepth but be between 0 and 1

我有点困惑,因为我正在创建一个像这样的GraphicsDevice:

public class GraphicsDeviceBuilder
{
    private Viewport viewport;
    private PresentationParameters presentationParameters;
    private GraphicsProfile graphicsProfile;
    private GraphicsAdapter graphicsAdapter;

    private void ResetBuilder()
    {
        viewport = new Viewport(0, 0, 128, 128);
        presentationParameters = new PresentationParameters();
        presentationParameters.BackBufferFormat = SurfaceFormat.Color;
        presentationParameters.DepthStencilFormat = DepthFormat.Depth24;
        presentationParameters.PresentationInterval = PresentInterval.Immediate;
        presentationParameters.IsFullScreen = false;
        graphicsProfile = GraphicsProfile.Reach;
        graphicsAdapter = GraphicsAdapter.DefaultAdapter;
    }

    public GraphicsDeviceBuilder Create()
    {
        ResetBuilder();
        return this;
    }

    public GraphicsDeviceBuilder WithViewport(Viewport viewport)
    {
        this.viewport = viewport;
        return this;
    }

    public GraphicsDeviceBuilder WithPresentationParameters(PresentationParameters presentationParameters)
    {
        this.presentationParameters = presentationParameters;
        return this;
    }

    public GraphicsDeviceBuilder WithGraphicsProfile(GraphicsProfile graphicsProfile)
    {
        this.graphicsProfile = graphicsProfile;
        return this;
    }

    public GraphicsDeviceBuilder WithGraphicsAdapter(GraphicsAdapter graphicsAdapter)
    {
        this.graphicsAdapter = graphicsAdapter;
        return this;
    }

    public GraphicsDevice Build(IntPtr handle)
    {
        presentationParameters.DeviceWindowHandle = handle;
        presentationParameters.BackBufferWidth = viewport.Width;
        presentationParameters.BackBufferHeight = viewport.Height;
        return new GraphicsDevice(graphicsAdapter, graphicsProfile, presentationParameters)
        {
            Viewport = viewport
        };
    }
}

然后在封装这个创建的设备的面板中:

private void SetupGraphics(GraphicsDeviceBuilder graphicsDeviceBuilder)
{
    var viewport = new Viewport()
    {
        X = 0,
        Y = 0,
        Width = Math.Max(Width, 1),
        Height = Math.Max(Height, 1),
        MinDepth = 0.0f,
        MaxDepth = 1.0f
    };
    GraphicsDevice = graphicsDeviceBuilder.Create().WithViewport(viewport).Build(Handle);
}

我还有一个方法,它会在调整窗体大小时重置()GraphicsDevice,如果需要,它还会更新高度/宽度,如下所示:

private void ResetDevice()
{
    if (GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost)
    { throw new DeviceLostException("Cannot regain access to video hardware"); }

    var safeWidth = Math.Max(Width, 1);
    var safeHeight = Math.Max(Height, 1);
    var newViewport = new Viewport(0, 0, safeWidth, safeHeight) { MinDepth = 0.0f, MaxDepth = 1.0f };

    GraphicsDevice.Viewport = newViewport;
    GraphicsDevice.PresentationParameters.BackBufferWidth = newViewport.Width;
    GraphicsDevice.PresentationParameters.BackBufferHeight = newViewport.Height;
    GraphicsDevice.PresentationParameters.DeviceWindowHandle = Handle;
    GraphicsDevice.Reset();
}

错误意味着视口的大小无效(在调试时看起来是错误的,因为它与面板的大小相匹配)并且深度也是无效的,但正如您所看到的那样,它被设置为0和1,使其在范围(调试也证明这是真的)。​​

在运行时运行时,我没有收到像设计师那样的错误,但我只是在面板上遇到红色交叉,让我觉得它在那里也不起作用。

1 个答案:

答案 0 :(得分:1)

我已经改变了一些东西,但是我的问题似乎围绕着我在重置之前设置视口的事实,我还需要将PresentationParameters传递给reset方法或者它们没有效果,所以像这样:

private void ResetDevice()
{
    var safeWidth = Math.Max(Width, 1);
    var safeHeight = Math.Max(Height, 1);
    var newViewport = new Viewport(0, 0, safeWidth, safeHeight) { MinDepth = 0.0f, MaxDepth = 1.0f };

    var presentationParams = GraphicsDevice.PresentationParameters;
    presentationParams.BackBufferWidth = safeWidth;
    presentationParams.BackBufferHeight = safeHeight;
    presentationParams.DeviceWindowHandle = Handle;
    GraphicsDevice.Reset(presentationParams);

    GraphicsDevice.Viewport = newViewport;

    if (GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost)
    { throw new DeviceLostException("Cannot regain access to video hardware"); }
}