Frage

Hi guys i have a little problem.

I have this 2 DrawableGameComponets (bigApple, smallApple) in both I'm drawing into a RenderTarget and then draw the RenderTarget in the backbuffer but this happens independently in each DrawableGameComponent.

The thing I want to achieve is that both DrawableGameComponents draw properly one in top of another.

Something like this:
This it's the screen with both drawableComponent with no rendertargets in each component. enter image description here

But instead of that I get this:
This it's the screen with both drawableComponent with rendertargets in each component. enter image description here

This is for a little game I'm working on. I'm planning to display in one drawable component and image from the camera and in the other drawable gamecomponent the game itself. But once I add another GameComponent to the Componets List, the one above the last added can't be seen.

This is the Code from each drawable Component.

SmallApple:

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);
    }

}

-- And the BigApple Class

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);
    }
}

The class DrawableComponent2D is the one that contains the heritage from drawablegameComponent and has some variables to work with.

War es hilfreich?

Lösung 2

Solved!!. i have to add a method to the delegate:

graphics.PreparingDeviceSettings;

so this is the method:

private void GraphicsDevicePreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e) {

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

}


And to add it to the graphicsDeviceManager is just one line:

graphics. PreparingDeviceSettings += GraphicsDevicePreparingDeviceSettings;

voilà!!! Thanks for your support

Andere Tipps

What is your XNA version?
If you use XNA 3.1 maybe your problem is here GraphicsDeviceCapabilities.MaxSimultaneousRenderTargets property

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top