Recently, I implemented a Label class in my game. It's a floating quad displaying a texture that only has text on it. To accomplish this, I draw the text to an appropriately sized rendertarget and get the image back. Then pass it to the quad for display when drawn.
The problem is that every once in a while (I really can detect no trend), one of the labels will come up with a blue/purple blank texture instead of the text. This color corresponds to one that I have seen when clearing rendertargets incorrectly in the past.
The code to generate the texture is as follows:
| q = new Quad(graphicsDevice); |
| imageSize = FontXLarge.MeasureString(Message); |
| |
| RenderTarget2D rt = new RenderTarget2D(graphicsDevice, (int)imageSize.X, (int)imageSize.Y, 1, graphicsDevice.PresentationParameters.BackBufferFormat); |
| |
| graphicsDevice.SetRenderTarget(0, rt); |
| graphicsDevice.Clear(Color.TransparentWhite); |
| |
| spriteBatch.Begin(); |
| spriteBatch.DrawString(FontXLarge, Message, Vector2.Zero, Color.White); |
| spriteBatch.End(); |
| graphicsDevice.SetRenderTarget(0, null); |
| |
| q.Texture = rt.GetTexture(); |
Am I doing something wrong here? What could cause the texture to come out blank (but only some of the time)?
I have a feeling that somewhere I'm missing setting up the render state correctly, but I am not knowledgeable enough to know what that could be.
Any tips would be much appreciated.