XNA 4: RenderTarget2D textures getting transparent on fullscreen
- by Shashwat
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?