Currently working on a game using XNA with MonoGame but have run into a visual error while trying to render the background where it'll appear above sprites when it should always appear behind.
The background render target will occasionally flicker on top of the sprites when it should always appear behind.
I've narrowed it down to something breaking when I use the RenderTarget2D.Dispose() function because when I don't dispose my render targets, it works just fine.
Код: Выделить всё
/* */ spriteBatch.GraphicsDevice.SetRenderTarget(entityRt); spriteBatch.GraphicsDevice.Clear(Color.Transparent); spriteBatch.Begin(SpriteSortMode.BackToFront, samplerState: SamplerState.PointClamp); for (int i = 0; i < entities.Count; i++) entities[i].Draw(); spriteBatch.End(); spriteBatch.GraphicsDevice.SetRenderTarget(null); /* */ /* */ MouseState mouseState = Mouse.GetState(); Point mousePos = mouseState.Position; RenderTarget2D newPrev = new RenderTarget2D(spriteBatch.GraphicsDevice, BackgroundBuffer1.Width, BackgroundBuffer1.Height); spriteBatch.GraphicsDevice.SetRenderTarget(newPrev); spriteBatch.GraphicsDevice.Clear(Color.Transparent); spriteBatch.Begin(); spriteBatch.Draw(BackgroundBuffer1, new Rectangle(0, 0, BackgroundBuffer1.Width, BackgroundBuffer1.Height), Color.White); spriteBatch.Draw(DrawUtils.createTexture(spriteBatch.GraphicsDevice), new Vector2((int)(mousePos.X * PixelateMultiplier), (int)(mousePos.Y * PixelateMultiplier)), Color.White); spriteBatch.End(); spriteBatch.GraphicsDevice.SetRenderTarget(null); (BackgroundBuffer1, newPrev) = (newPrev, BackgroundBuffer1); newPrev.Dispose(); //
Источник: [url]https://stackoverflow.com/questions/78131480/xna-dispose-function-causes-visual-error-when-switching-render-targets[/url]