我正在开发自己的基于XNA图块的游戏,我遇到了RenderTarget2D的问题。 我尝试在屏幕上绘制背景图像,并在屏幕上绘制我的播放器,它工作正常!但是当我尝试使用RenderTarget2D在运行时向游戏添加块纹理时,我的屏幕开始闪烁紫色。 每当我使用RenderTarget2D创建一个新的块块时就会发生这种情况。
以下是一些代码:
protected override void Draw(GameTime gameTime)
{
this.DrawPlayer(gameTime);
this.DrawWorld(gameTime);
base.Draw(gameTime);
}
private void DrawPlayer(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(Main.PlayerRenderTarget);
GraphicsDevice.Clear(Color.Transparent);
this.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, this.Camera.Transform);
SpriteBatch.Draw(this.Background, new Vector2(this.Screen.X, this.Screen.Y), Color.White);
this.Player.Draw(SpriteBatch);
SpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Transparent);
SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
SpriteBatch.Draw(Main.PlayerRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End();
}
private void DrawWorld(GameTime gameTime)
{
Main.World.Draw(SpriteBatch, this.Screen);
}
世界抽奖:
public void Draw(SpriteBatch SpriteBatch, System.Drawing.RectangleF Screen)
{
foreach (Chunk CurrentChunk in this.WorldChunks.Where(SomeChunk => SomeChunk.Bounds.IntersectsWith(Screen)))
{
CurrentChunk.Draw(SpriteBatch);
}
}
大块抽奖:
public void Draw(SpriteBatch spriteBatch)
{
if (this.ChunkTexture == null)
{
this.CreateChunk(spriteBatch);
}
else
{
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(this.ChunkTexture, this.ChunkPosition, Color.White);
spriteBatch.End();
}
}
public void CreateChunk(SpriteBatch spriteBatch)
{
RenderTarget2D CurrentRenderTarget = new RenderTarget2D(Chunk.GraphicsDevice, (int)this.Bounds.Width, (int)this.Bounds.Height);
Chunk.GraphicsDevice.SetRenderTarget(CurrentRenderTarget);
Chunk.GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
foreach(Block in Chunk)
{
spriteBatch.Draw(BlockTexture, BlockPosition, Color.White);
}
spriteBatch.End();
Chunk.GraphicsDevice.SetRenderTarget(null);
Chunk.GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(CurrentRenderTarget, ChunkPositon, Color.White);
spriteBatch.End();
CurrentRenderTarget.GetData<Color>(Chunk.Colors);
this.Texture = new Texture2D(Chunk.GraphicsDevice, (int)this.Bounds.Width, (int)this.Bounds.Height);
this.Texture.SetData<Color>(Chunk.Colors);
this.IsLoaded = true;
CurrentRenderTarget.Dispose();
}
每当调用“CreateChunk”时,屏幕会以紫色或黑色闪烁约0.1秒,之后会将所有内容都画得很好。
当我使用Chunk.GraphicsDevice.Clear(Color.Transparent)时,屏幕会闪烁黑色,但当我删除该行时,屏幕开始闪烁紫色。
所以我想让那些“闪烁”消失,因为它非常令人不安
请帮我弄清楚为什么会这样。
谢谢,弗拉德。
编辑:
“闪烁”似乎仅适用于背景图像和播放器图像,但不适用于我绘制的块。
答案 0 :(得分:0)
XNA认为您正在尝试创建另一个backbuffer。它会自动翻转后备缓冲区,以便您可以在不打扰当前显示纹理的情况下进行绘制,然后在后台无缝切换。这样可以消除撕裂,但对XNA新手来说会引起很多困惑(我自己也挣扎了一段时间)。
每当调用CreateChunk
时,您正在创建一个新的RenderTarget2D
,它在函数末尾超出范围。虽然RenderTarget2D.Dispose()应该处理分配给纹理的所有资源,但根据我的经验,它不会减少GraphicsDevice中RenderTargets的内部计数。在CreateChunk结束之前,插入一个断点并调用GraphicsDevice.GetRenderTarget(1);
并检查它是否为空。
提示:当您点击该断点时,请检查GraphicsDevice的非公共成员。您将看到设备使用了多少RenderTargets以及大量有用的调试信息。
希望这可以帮助您确定调查目标。