Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Let's say I have a background and a set of entities.
I have managed to draw the background into a Texture2D using a RenderTarget
I have also managed to draw all my entities into another RenderTarget

Now I want to take these two Textures and render them on the screen as so:

// Top-level rendering
entitiesSystem.Process();
background.Process();
Texture2D entitiesTexture = entitiesSystem.EntitiesTexture;
Texture2D backgroundTexture = background.BackgroundTexture;

graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(
    backgroundTexture,
    LevelManager.LEVEL_RECTANGLE,
    null,
    Color.White,
    0.0f,
    Vector2.Zero,
    SpriteEffects.None,
    1
);
spriteBatch.Draw(
    entitiesTexture,
    LevelManager.LEVEL_RECTANGLE,
    null,
    Color.White,
    0.0f,
    Vector2.Zero,
    SpriteEffects.None,
    0
);
spriteBatch.End();

Here is how the background system renders the background:

graphicsDevice.SetRenderTarget(BackgroundTexture);
spriteBatch.Begin();
for (int i = 0; i < 2; i++)
    spriteBatch.Draw(textures[i], positions[i], Color.White);
spriteBatch.End();

Almost the same thing happens for the entities: I set the render target to the relevant one (not the same as the one for the background), start the spritebatch (again, not the same one), draw the entities and end the spritebatch.

If I set BlendMode to additive, the result is correct (they blend together). However, if I set to to AlphaBlend, then I can only render one of them (depending on the SpriteSortMode).

I have come as far as understanding that the problem lies in not preserving... something, but I am not sure how to preserve it :P

By the way, I read that multiple render targets are not supported for the XBOX... Soooooooo... what is the alternative of doing what I am doing now?

share|improve this question
    
OK, multiple render targets are supported for XBOX, so forget about that part of the question (source: msdn.microsoft.com/en-us/library/… ) –  pek Aug 19 '11 at 20:41

3 Answers 3

up vote 1 down vote accepted

Is your entities texture created with a format that has an alpha channel, and cleared to transparent before use?

share|improve this answer
    
My texture images are PNG files. So I guess they do have alpha channels. As for clearing the transparency, I don't know what you mean. I don't think I do something explicitly; I just draw the textures. –  pek Aug 21 '11 at 1:47
    
@pek Sorry, when I wrote 'Texture', what I meant to say was 'Render Target' –  Adam Aug 27 '11 at 1:37
    
The Format attribute of the RenderTarget was Color, but I wasn't clearing to Color.Transparent (I didn't even know there was such a color!). So once I did that it worked like a charm! –  pek Aug 27 '11 at 6:02

In my 2D XNA-based game, I have multiple layers like this too. I just draw the background, then draw the foreground. Using multiple render targets is often reserved for special effects and taking screenshots. Here is some doc on multiple render targets: http://msdn.microsoft.com/en-us/library/bb975911%28XNAGameStudio.31%29.aspx#ID4E4F

share|improve this answer

Setting a render target will clear it by default, as this is the only fast path on Xbox 360.

You can adjust this by setting the appropriate RenderTargetUsage.

See this blog post for details.

share|improve this answer
    
I added a method for the DeviceCreated event and tested setting all the different options of RenderTargetUsage with no luck. :/ –  pek Aug 20 '11 at 2:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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