C# 渲染目标内容在游戏最小化时发生更改
所以我正在为XNA开发GUI库,遇到了一个我找不到解决方案的问题。我经常使用rendertargets,这本身就很烦人,但我有一个非常奇怪和具体的问题 在ProgressBar类中,当元素被验证且对象大小发生更改时,我正在绘制各个组件。如果对象已验证,但对象大小未更改,我将使用填充的渲染目标作为纹理将最终产品绘制到缓冲区纹理(同样是渲染目标)上。现在,每当我最小化应用程序并再次使用tab键时,progressbar的背景层将在其上方有条纹纹理的印记。渲染目标设置为保留内容,我确保设置了正确的渲染目标。此外,清除graphicsDevice(仅在“实际绘图”线下方)也没有任何作用。为什么? 所以基本上游戏会创建一个该区域的屏幕截图,并将其绘制到我的纹理中。怎么回事 以下是ProgressBar的代码:C# 渲染目标内容在游戏最小化时发生更改,c#,xna,rendertarget,C#,Xna,Rendertarget,所以我正在为XNA开发GUI库,遇到了一个我找不到解决方案的问题。我经常使用rendertargets,这本身就很烦人,但我有一个非常奇怪和具体的问题 在ProgressBar类中,当元素被验证且对象大小发生更改时,我正在绘制各个组件。如果对象已验证,但对象大小未更改,我将使用填充的渲染目标作为纹理将最终产品绘制到缓冲区纹理(同样是渲染目标)上。现在,每当我最小化应用程序并再次使用tab键时,progressbar的背景层将在其上方有条纹纹理的印记。渲染目标设置为保留内容,我确保设置了正确的渲染
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace PixLib
{
public class ProgressBar : UIElement
{
private float stripeThickness = 5;
private float stripeGapFactor = 3f;
private float animationSpeed = 1f;
private float at = 0;
private RenderTarget2D stripesBg, stripes;
private Coordinate2 lastSize;
protected override Coordinate2 InnerArea
{
get { return Coordinate2.Zero; }
}
protected Color color;
public Color Color
{
get
{
return color;
}
set
{
color = value;
Invalidate();
}
}
protected float value;
public float Value
{
get
{
return value;
}
set
{
float t = Math.Min(1, Math.Max(0, value));
if (t != this.value)
{
this.value = t;
Invalidate();
}
}
}
public ProgressBar(string name,Color color, Rectangle elementRect, bool localPos = true)
: base(name, elementRect, localPos)
{
lastSize = new Coordinate2();
this.color = color;
value = 0;
at = 0;
}
protected override void OnUpdate(MouseState mouseState, KeyboardState keyboardState)
{
}
protected override void OnInit()
{
}
protected override void Redraw(SpriteBatch spriteBatch)
{
if (lastSize.X != Width || lastSize.Y != Height)
{
Console.WriteLine("Redrawing Progressbar");
//redraw textures!
stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
spriteBatch.Begin();
spriteBatch.Draw(pixel, new Rectangle(0, 0, Width, Height), Color.White);
spriteBatch.End();
SetRenderTarget(stripesBg);
spriteBatch.Begin();
spriteBatch.Draw(pixel, new Rectangle(0, 0, Width, Height), Color.White);
spriteBatch.End();
/*SetRenderTarget(border);
spriteBatch.Begin();
int si = (int)(stripeThickness*0.5f + 0.5f);
DrawLine(new Coordinate2(si, 0), new Coordinate2(Width, 0), spriteBatch, Color.White, si);
DrawLine(new Coordinate2(si, Height - si), new Coordinate2(Width, Height - si), spriteBatch, Color.White, si);
DrawLine(new Coordinate2(si, 0), new Coordinate2(si, Height), spriteBatch, Color.White, si);
DrawLine(new Coordinate2(Width, 0), new Coordinate2(Width, Height - si), spriteBatch, Color.White, si);
spriteBatch.End();*/
SetRenderTarget(stripes);
spriteBatch.Begin();
int fy = (int)(stripeThickness +0.5f);
int s = (Height + fy) * 2;
int fx = -s;
at = 0;
while (fx < Width + stripeThickness * stripeGapFactor)
{
DrawLine(new Coordinate2(fx, -fy), new Coordinate2(fx+s, s-fy), spriteBatch, Color.White, stripeThickness);
fx += (int)(stripeThickness * stripeGapFactor);
}
spriteBatch.End();
SetRenderTarget(null);
lastSize.X = Width;
lastSize.Y = Height;
}
//actual drawing
spriteBatch.Begin();
spriteBatch.Draw(pixel, SizeRect, Darken(color, 2));
int cv = (int)(Value * Width + 0.5f);
spriteBatch.Draw(stripesBg, new Rectangle(cv - Width, 0, Width, Height), Darken(Color));
graphicsManager.GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Draw(stripes, new Rectangle(cv - Width, -(int)(at + 0.5f), Width, Height * 2), color);
spriteBatch.End();
at += animationSpeed;
if (at >= Height)
at -= Height - (int)(stripeThickness + .5f);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
名称空间PixLib
{
公共类ProgressBar:UIElement
{
私有浮条厚度=5;
私有浮动条带gapfactor=3f;
私有浮动动画速度=1f;
私人浮动=0;
私有渲染目标2D条纹BG,条纹;
私人协调机构的规模;
受保护的覆盖协调2内部区域
{
获取{return Coordinate2.Zero;}
}
保护色;
公共颜色
{
得到
{
返回颜色;
}
设置
{
颜色=值;
使无效();
}
}
保护浮点数;
公众浮动价值
{
得到
{
返回值;
}
设置
{
float t t=Math.Min(1,Math.Max(0,值));
如果(t!=此.value)
{
该值=t;
使无效();
}
}
}
公共进度条(字符串名称、颜色、矩形元素rect、bool localPos=true)
:base(名称、elementRect、localPos)
{
lastSize=新坐标2();
这个颜色=颜色;
数值=0;
at=0;
}
受保护的覆盖无效OnUpdate(MouseState MouseState、KeyboardState KeyboardState)
{
}
受保护的覆盖void OnInit()
{
}
受保护覆盖无效重绘(SpriteBatch SpriteBatch)
{
if(lastSize.X!=宽度| | lastSize.Y!=高度)
{
Console.WriteLine(“重画进度条”);
//重新绘制纹理!
stripesBg=new RenderTarget2D(graphicsManager.GraphicsDevice,宽度,高度,false,graphicsManager.GraphicsDevice.DisplayMode.Format,DepthFormat.Depth24,1,RenderTargetUsage.PreserveContents);
stripes=new RenderTarget2D(graphicsManager.GraphicsDevice,宽度,高度*2,false,graphicsManager.GraphicsDevice.DisplayMode.Format,DepthFormat.Depth24,1,RenderTargetUsage.PreserveContents);
spriteBatch.Begin();
绘制(像素,新矩形(0,0,宽度,高度),颜色,白色);
spriteBatch.End();
SetRenderTarget(stripesBg);
spriteBatch.Begin();
绘制(像素,新矩形(0,0,宽度,高度),颜色,白色);
spriteBatch.End();
/*SetRenderTarget(边框);
spriteBatch.Begin();
int si=(int)(条纹厚度*0.5f+0.5f);
抽绳(新坐标2(si,0),新坐标2(宽度,0),雪碧色,白色,si);
抽绳(新坐标2(si,高度-si),新坐标2(宽度,高度-si),雪碧,彩色,白色,si);
抽绳(新坐标2(si,0),新坐标2(si,高度),spriteBatch,彩色,白色,si);
抽绳(新坐标2(宽度,0),新坐标2(宽度,高度-si),雪碧色,白色,si);
spriteBatch.End()*/
SetRenderTarget(条纹);
spriteBatch.Begin();
int fy=(int)(条纹厚度+0.5f);
整数s=(高度+财年)*2;
int fx=-s;
at=0;
而(fx=高度处)
at-=高度-(int)(条纹厚度+0.5f);
}
}
}
好吧,看来这个问题的解决方案是让渲染目标不保留其内容,并在任何时候重新绘制整个该死的东西
if (lastSize.X != Width || lastSize.Y != Height || stripesBg.IsContentLost || stripes.IsContentLost)
{
Console.WriteLine("Redrawing Progressbar");
//redraw textures!
//stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
//stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height);
stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2);
[...]