0

I need to render a sprite in a texture2d so that this texture can later be render on the screen, but at the same time I need to access the pixels of this modified texture so, if I add let's say a sprite in the texture and I call a get pixel function in a coordinate where the sprite was then it should give me the new pixel values that correspond to the sprite (that has been blended with the texture2d).

I am using xna 4.0 not 3.5 or less.

thanks.

the equivalent of Graphics.FromImage(img).DrawImage(... in GDI

I tried this and failed

public static Texture2D DrawSomething(Texture2D old, int X, int Y, int radius) {
var pp = Res.game.GraphicsDevice.PresentationParameters;
var r = new RenderTarget2D(Res.game.GraphicsDevice, old.Width, old.Height, false, pp.BackBufferFormat, pp.DepthStencilFormat,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
Res.game.GraphicsDevice.SetRenderTarget(r);
var s = new SpriteBatch(r.GraphicsDevice);
s.Begin();
s.Draw(old, new Vector2(0, 0), Color.White);
s.Draw(Res.picture, new Rectangle(X - radius / 2, Y - radius / 2, radius, radius), Color.White);
s.End();
Res.game.GraphicsDevice.SetRenderTarget(null);
return r;
}

Res.game is basically a pointer to the main game form and Res.picture is a random texture2d

2 Answers 2

0

Use a RenderTarget2D: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget2d.aspx

6
  • ya sorry i was pasting it. check again
    – luis
    Mar 3, 2012 at 9:44
  • if you create a new RenderTarget2D every update, call RenderTarget2D.Dispose() after you set the render target to null or put it in a using statement Mar 3, 2012 at 9:46
  • ok i did, but then when I access its pixels later, it says it throws an error 'Cannot access a disposed object.'
    – luis
    Mar 3, 2012 at 9:49
  • If you need to change it later, store it as an instance variable instead of creating one every time. Mar 3, 2012 at 9:50
  • what do u mean? it is stored as a variable after that. can u give me a simple method example of how to do this please
    – luis
    Mar 3, 2012 at 9:58
0

If possible, avoid creating a new render target every time. Create it outside of the method and reuse it for best performance.

Here some pseudo-code:

public Texture2D DrawOnTop(RenderTarget2D target, Texture2D oldTexture, Texture2D picture)
{
    SetRenderTarget(target);

    Draw(oldTexture);
    Draw(picture);

    SetRenderTarget(null);

    return target;
}

If the size changes frequently and you cannot reuse the target, at least dispose the previous one, like annonymously suggested in the comments. Each new target will consume memory, unless you release the resource in time. But dispose it after you used it in a shader or did whatever you wanted to do with it. Once disposed it is gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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