1
\$\begingroup\$

I'm having a problem with drawing 2 RenderTargets to the screen.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Please
{
    class Class1
    {
        RenderTarget2D rt;
        Texture2D temp;
        Vector2 v;
        public Class1(Vector2 v,GraphicsDevice g)
        {
            this.v = v;
            rt=new RenderTarget2D(g,800,600);
        }

        public void Draw(SpriteBatch sb,GraphicsDevice gd,ContentManager cm)
        {
            gd.SetRenderTarget(rt);
            sb.Begin();
            sb.Draw(cm.Load<Texture2D>("Untitled.png"),v,Color.White);
            sb.End();
            gd.SetRenderTarget(null);

            temp = (Texture2D)rt;

            sb.Begin();
            sb.Draw(temp, Vector2.Zero, Color.White);
            sb.End();
        }
    }
}

Here's my draw function in the actual "game"

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    base.Draw(gameTime);
    one.Draw(spriteBatch, graphics.GraphicsDevice, Content);
    two.Draw(spriteBatch, graphics.GraphicsDevice, Content);
}

one and two have positions (100,200) and (300,200) respectively, so, they should be drawn 100 pixels apart, however, only two is being drawn with the rest of the screen black

The problem

\$\endgroup\$
3
\$\begingroup\$

Clear both rt's to Transparent before drawing the texture to them.

Since you will be explicitly clearing them to Transparent[Black], you should specify:

 new RenderTarget2D(..., RenderTargetUsage.PreserveContents);

to prevent XNA from clearing rt's rendertarget (to not Transparent[Black]) when you set the current rendertarget to NULL.

When you draw them with AlphaBlending enabled (the default), the not Transparent[Black] overwrites the backbuffer, as though you were using Opaque.

Also:
You should not Load<> the texture more than once and it should be during or after game.LoadContent(). You are currently re-re-re-re-re-loading it every time you render it.

\$\endgroup\$
  • 1
    \$\begingroup\$ I think re-re-re-re-re-loading textures is actually fine, since the ContentManager caches its result causing the texture to be loaded from disk only on first load. \$\endgroup\$ – Jaanus Varus Jun 27 '15 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.