0

大家好,我有一个小问题。

我在两个(bigApple,smallApple)中都有这个 2 DrawableGameComponets(bigApple,smallApple),我正在绘制到 a中RenderTarget,然后RenderTarget在 backbuffer 中绘制,但这在每个DrawableGameComponent.

我想要实现的是两者都DrawableGameComponents正确地绘制在另一个之上。

像这样:
这是两个 drawableComponent 的屏幕,每个组件中都没有渲染目标。 在此处输入图像描述

但我得到的不是这个:
这是每个组件中都有 drawableComponent 和 rendertargets 的屏幕。 在此处输入图像描述

这是我正在开发的一个小游戏。我计划在一个可绘制组件和来自相机的图像中显示游戏本身,并在另一个可绘制游戏组件中显示游戏本身。但是,一旦我将另一个添加GameComponent到组件列表中,就看不到最后添加的那个。

这是每个可绘制组件的代码。

小苹果:

public class SmallApple:DrawableComponent2D
{
    Texture2D apple;

    public SmallApple(Game game)
        : base(game)
    {
        //Do nothing
    }

    protected override void LoadContent()
    {
        apple = Game.Content.Load<Texture2D>("apple");

        this.Size = new Vector2(apple.Width,
            apple.Height);

        renderTarget = new RenderTarget2D(GraphicsDevice,
            (int)Size.X,
            (int)Size.Y,
            false,
            SurfaceFormat.Color,
            DepthFormat.None,
            this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
            RenderTargetUsage.PreserveContents);
        base.LoadContent();
    }

    public override void Initialize()
    {
        base.Initialize();
    }

    public override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);
        GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);

        this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate, null);
        this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
        this.SharedSpriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);

        this.SharedSpriteBatch.Begin();
        this.SharedSpriteBatch.Draw(apple, this.Position,Color.White);
        this.SharedSpriteBatch.End();
        base.Draw(gameTime);
    }

}

——还有大苹果班

public class BigApple:DrawableComponent2D
{

    Texture2D apple;

    public BigApple(Game game)
        : base(game)
    {

    }


    protected override void LoadContent()
    {
        base.LoadContent();

        apple = Game.Content.Load<Texture2D>("apple");

        this.Size = new Vector2(apple.Width, apple.Height);

        renderTarget = new RenderTarget2D(GraphicsDevice, 
            (int)Size.X,
            (int)Size.Y, 
            false, 
            SurfaceFormat.Color, 
            DepthFormat.None,
            this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
            RenderTargetUsage.PreserveContents);

    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);
        GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);

        this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate,null);
        this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
        this.SharedSpriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);

        this.SharedSpriteBatch.Begin();
        this.SharedSpriteBatch.Draw(renderTarget,new Rectangle((int)Position.X, (int)Position.Y, (int)GraphicsDevice.Viewport.Width, (int)GraphicsDevice.Viewport.Height), Color.White);
        this.SharedSpriteBatch.End();

        base.Draw(gameTime);
    }
}

DrawableComponent2D 类包含了 drawablegameComponent 的继承,并且有一些变量可以使用。

4

2 回答 2

0

你的 XNA 版本是多少?
如果您使用 XNA 3.1,您的问题可能出在此处GraphicsDeviceCapabilities.MaxSimultaneousRenderTargets 属性

于 2013-12-11T22:12:01.357 回答
0

解决了!!。我必须向委托添加一个方法:

graphics.PreparingDeviceSettings;

所以这是方法:

私人无效GraphicsDevicePreparingDeviceSettings(对象发送者,PreparingDeviceSettingsEventArgs e){

e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;

}


将其添加到 graphicsDeviceManager 只需一行:

图形。PreparingDeviceSettings += GraphicsDevicePreparingDeviceSettings;

瞧!!!谢谢你的支持

于 2013-12-12T15:40:17.493 回答