XNA 4: RenderTarget2D textures getting transparent on fullscreen

Posted by Shashwat on Game Development See other posts from Game Development or by Shashwat
Published on 2014-06-11T18:25:56Z Indexed on 2014/06/11 21:42 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I'm generating a Texture2D object using RenderTarget2D as in the following code

public static Texture2D GetTextTexture(string text, Vector2 position, SpriteFont font, Color foreColor, Color backColor, Texture2D background=null)
{
            int width = (int)font.MeasureString(text).X;
            int height = (int)font.MeasureString(text).Y;

            GraphicsDevice device = Settings.game.GraphicsDevice;
            SpriteBatch spriteBatch = Settings.game.spriteBatch;

            RenderTarget2D renderTarget = new RenderTarget2D(device, width,
            height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8,
            device.PresentationParameters.MultiSampleCount, RenderTargetUsage.DiscardContents);

            device.SetRenderTarget(renderTarget);
                device.Clear(backColor);
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
                if (background != null) spriteBatch.Draw(background, new Rectangle(0, 0, 70, 70), Color.White);
                spriteBatch.End();
                spriteBatch.Begin();
                spriteBatch.DrawString(font, text, position, foreColor, 0, new Vector2(0), 0.8f, SpriteEffects.None, 0);
                spriteBatch.End();
            device.SetRenderTarget(null);
            ResetGraphicsDeviceSettings();
            return (Texture2D)renderTarget;
}

It's working all fine. But when I ToggleFullScreen() (and vice-versa), the previous textures are getting transparent. However, the new textures after that are being generated correctly.

What can be the reason for this?

© Game Development or respective owner

Related posts about c#

Related posts about textures