首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows Phone中的XNA RenderTarget2D

Windows Phone中的XNA RenderTarget2D
EN

Stack Overflow用户
提问于 2013-02-26 21:45:07
回答 1查看 398关注 0票数 2

我想在我的Windows phone7XNA应用程序中使用RenderTarget2D。然而,我没有成功,因为切换到渲染目标然后切换回来(通过使用SetRenderTarget(null) )会导致我的整个屏幕被绘制为蓝色,从而覆盖切换到渲染目标之前绘制的任何内容。我不确定这是否是预期的行为。

事实上,很容易重现这种行为。只需为Windows Phone 7创建一个简单的XNA游戏,并使用以下代码:

代码语言:javascript
复制
protected override void Draw(GameTime gameTime)
{
   spriteBatch.Begin();
   spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
   spriteBatch.End(); // Testing this block of code confirms that the textureBackground is being drawn

   graphics.GraphicsDevice.SetRenderTarget(textureBuffer);

   spriteBatch.Begin();
   spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
   spriteBatch.End();

   graphics.GraphicsDevice.SetRenderTarget(null); // switch back 

   spriteBatch.Begin();
   spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
   spriteBatch.End(); // at this point, textureBuffer is drawn (the 20x20 pixeles image) in the upper left hand corner but the background of the whole screen is BLUE and so textureBackground texture has been overwritten by this color and is not showing anymore.

   // At this point all I see is a blue background with a 20x20 pixels image in the upper left hand corner.
}

我创建我的渲染目标如下所示:

代码语言:javascript
复制
textureSummer = Content.Load<Texture2D>("summer_picture_icon"); // the 20x20 pixels texture
textureBuffer = new RenderTarget2D(graphics.GraphicsDevice, textureSummer.Width, textureSummer.Height); // the RenderTarget2D object. Note that this RenderTarget is only 20x20.

因此,我做错了什么?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-26 22:19:01

“问题”是你首先绘制背景,然后更改rendertarget,然后渲染那个小方块,然后更改rendertarget,然后再次绘制小方块。按如下顺序排列:

  • Render on Screen stuff
  • Rendertarget
    • Render off-screen stuff
    • Rendertarget Change
    • Render on-screen stuff

rendertarget的每次更改都会清除它。

你应该做什么;

  • Rendertarget Change stuff
  • Rendertarget Change
  • Render
  • Render off-screen stuff
  • Rendertarget Change
  • Render on-screen stuff

如下所示:

代码语言:javascript
复制
GraphicsDevice.SetRenderTarget(textureBuffer);

spriteBatch.Begin();
spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
spriteBatch.End();

GraphicsDevice.SetRenderTarget(null); // switch back 

spriteBatch.Begin();
spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
spriteBatch.End();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15090700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档