XNA 3.1 到 4.0 需要不断重绘或将显示紫色屏幕 [英] XNA 3.1 to 4.0 requires constant redraw or will display a purple screen

查看:28
本文介绍了XNA 3.1 到 4.0 需要不断重绘或将显示紫色屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于游戏中的菜单,我将它们绘制到屏幕上一次,然后仅在它们被认为脏时才重新绘制.每当用户执行应该导致重绘的操作时,这是通过设置为 true 的布尔值来处理的,然后绘制循环将在绘制菜单之前检查该值.此逻辑在 3.1 中运行良好,但在 4.0 中菜单会闪烁(绘制 1 帧)然后显示紫色屏幕,直到再次绘制.

For menus in my game, I draw them once to the screen, then only redraw if they've been deemed dirty. This is handled through a boolean set to true whenever the user performs an action that should cause a redraw, and then the draw loop will check that value before drawing the menu. This logic worked perfectly in 3.1, but in 4.0 the menu will flicker (drawn for 1 frame) then show a purple screen until drawn again.

我在 4.0 中创建了一个非常简单的测试游戏来演示下面显示的问题.您会注意到屏幕看起来是紫色的.如果将行设置 _isDirty 删除为 false,您将看到矢车菊蓝色背景.

I've created a very simple test game in 4.0 to demonstrate the issue shown below. You will notice that the screen just looks purple. If you remove the line setting _isDirty to false, you will see the cornflower blue background.

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    bool _isDirty = true;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
    }
    protected override void Draw(GameTime gameTime)
    {
        if (_isDirty)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            _isDirty = false;
        }
        base.Draw(gameTime);
    }
}

我将如何从 XNA 3.1 获取行为?我看到好几个人提到了 PreserveContents,但这在 4.0 中似乎没有任何影响,除非我应用不当.

How would I go about getting the behavior from XNA 3.1? I've seen several people mention PreserveContents, but that doesn't seem to have any effect in 4.0 unless I'm applying it incorrectly.

推荐答案

以下是 XNA 游戏中调用的粗略概述:

Here is a rough overview of what gets called in an XNA game:

Update
BeginDraw
Draw
EndDraw

EndDraw 的默认实现最终会调用 GraphicsDevice.Present.打开双缓冲后,这会交换前后缓冲区.

The default implementation of EndDraw ultimately calls GraphicsDevice.Present. With double-buffering turned on, this swaps the back and front buffers.

所以发生的事情是:

  • 第一次:您将场景绘制到后台缓冲区,然后让 XNA 将其交换到前面.

  • First time around: you're drawing your scene to the back buffer and then letting XNA swap it to the front.

第二次:你什么也没画,让表面充满了 DirectX 初始化这些表面的紫色,然后将那个交换到前面!

Second time around: you're drawing nothing, leaving the surface filled with the purple colour that DirectX initialises these surfaces to, and swapping that to the front!

随后的时间:您什么也没画,所以您会看到这两个表面之间的显示闪烁.

Subsequent times: you're drawing nothing, so you'll see the display flicker between these two surfaces.

有几种方法可以在 XNA 中抑制绘图.我在这个对类似问题的回答中详细介绍了它们.在那个答案中,我建议您覆盖 BeginDraw,如下所示:

There are several ways to suppress drawing in XNA. I go over them in this answer to a similar question. As in that answer, I recommend you override BeginDraw, like so:

protected override bool BeginDraw()
{
    if(_isDirty)
        return base.BeginDraw();
    else
        return false;
}

BeginDraw 返回 false 时,DrawEndDraw(因此 Present)将不会被调用该帧.什么都不会画,前/后缓冲区不会交换.

When BeginDraw returns false, Draw and EndDraw (and so Present) will not be called that frame. Nothing will draw and the front/back buffers won't swap.

这篇关于XNA 3.1 到 4.0 需要不断重绘或将显示紫色屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆