0
因此,我正在爲XNA開發一個GUI庫,並且遇到了一個我找不到解決方案的問題。我正在使用rendertargets很多,這本身很煩人,但我有一個非常奇怪和具體的問題。當遊戲最小化時RenderTarget內容發生變化
在我的ProgressBar類中,當元素是Validated且對象大小已更改時,我繪製了各個組件。如果對象是有效的,但對象大小沒有改變,我使用填充的rendertargets作爲紋理將最終產品繪製到緩衝區紋理(也是rendertarget)上。現在,無論何時我最小化應用程序並再次選項卡,進度條的背景層都會在其上方具有條紋紋理的印記。 rendertargets被設置爲保留內容,我確信設置了正確的rendertargets。也清除graphicsDevice(在「實際繪圖」行下面)不會執行任何操作。爲什麼?
所以基本上游戲會創建該區域的截圖,並將其繪製到我的紋理中。我勒個去?
下面是進度代碼:
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);
}
}
}