我对 XNA 还很陌生,并且遇到了一些问题。每当我最小化游戏时,当我再次打开它时屏幕会变黑。这可能是什么原因以及如何解决?
这是我的图像类:
public class Image
{
public float Alpha;
public string Text, FontName, Path;
public Vector2 Position, Scale;
public Rectangle SourceRect;
public bool IsActive;
public bool Logo;
public Texture2D Texture;
Vector2 origin;
ContentManager content;
RenderTarget2D renderTarget;
SpriteFont font;
Dictionary<string, ImageEffect> effectList;
public string Effects;
public FadeEffect FadeEffect;
void SetEffect<T>(ref T effect)
{
if (effect == null)
effect = (T)Activator.CreateInstance(typeof(T));
else
{
(effect as ImageEffect).IsActive = true;
var obj = this;
(effect as ImageEffect).LoadContent(ref obj);
}
effectList.Add(effect.GetType().ToString().Replace("RPG.", ""), (effect as ImageEffect));
}
public void ActivateEffect(string effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = true;
var obj = this;
effectList[effect].LoadContent(ref obj);
}
}
public void DeactivateEffect(string effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = false;
effectList[effect].UnloadContent();
}
}
public void StoreEffects()
{
Effects = String.Empty;
foreach (var effect in effectList)
{
if (effect.Value.IsActive)
Effects += effect.Key + ":";
}
if(Effects != String.Empty)
Effects.Remove(Effects.Length - 1);
}
public void RestoreEffects()
{
foreach (var effect in effectList)
DeactivateEffect(effect.Key);
string[] split = Effects.Split(':');
foreach (string s in split)
ActivateEffect(s);
}
public Image()
{
Path = Text = Effects = String.Empty;
FontName = "Fonts/FixedSys Ex";
Position = Vector2.Zero;
Scale = Vector2.One;
Alpha = 1.0f;
SourceRect = Rectangle.Empty;
effectList = new Dictionary<string, ImageEffect>();
}
public void LoadContent()
{
content = new ContentManager(ScreenManager.Instance.Content.ServiceProvider, "Content");
if (Path != String.Empty)
Texture = content.Load<Texture2D>(Path);
font = content.Load<SpriteFont>(FontName);
Vector2 dimensions = Vector2.Zero;
if (Texture != null)
dimensions.X += Texture.Width;
dimensions.X += font.MeasureString(Text).X;
if (Texture != null)
dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
else
dimensions.Y = font.MeasureString(Text).Y;
if (SourceRect == Rectangle.Empty)
SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);
renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice, (int)dimensions.X, (int)dimensions.Y);
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
ScreenManager.Instance.SpriteBatch.Begin();
if (Texture != null)
ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.End();
Texture = renderTarget;
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
SetEffect<FadeEffect>(ref FadeEffect);
if (Effects != string.Empty)
{
string[] split = Effects.Split(':');
foreach (string item in split)
ActivateEffect(item);
}
}
public void UnloadContent()
{
content.Unload();
foreach (var effect in effectList)
{
DeactivateEffect(effect.Key);
}
}
public void Update(GameTime gameTime)
{
foreach (var effect in effectList)
{
if(effect.Value.IsActive)
effect.Value.Update(gameTime);
}
}
public void Draw(SpriteBatch spriteBatch)
{
origin = new Vector2(SourceRect.Width / 2, SourceRect.Height / 2);
spriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White * Alpha, 0.0f, origin, Scale, SpriteEffects.None, 0.0f);
}
}
编辑:
我终于设法让它正常工作!
固定代码:
public class Image
{
public float Alpha;
public string Text, FontName, Path;
public Vector2 Position, Scale;
public Rectangle SourceRect;
public bool IsActive;
public bool Logo;
public Texture2D Texture;
Vector2 origin;
Vector2 dimensions;
ContentManager content;
RenderTarget2D renderTarget;
SpriteFont font;
Dictionary<string, ImageEffect> effectList;
public string Effects;
public FadeEffect FadeEffect;
void SetEffect<T>(ref T effect)
{
if (effect == null)
effect = (T)Activator.CreateInstance(typeof(T));
else
{
(effect as ImageEffect).IsActive = true;
var obj = this;
(effect as ImageEffect).LoadContent(ref obj);
}
effectList.Add(effect.GetType().ToString().Replace("RPG.", ""), (effect as ImageEffect));
}
public void ActivateEffect(string effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = true;
var obj = this;
effectList[effect].LoadContent(ref obj);
}
}
public void DeactivateEffect(string effect)
{
if (effectList.ContainsKey(effect))
{
effectList[effect].IsActive = false;
effectList[effect].UnloadContent();
}
}
public void StoreEffects()
{
Effects = String.Empty;
foreach (var effect in effectList)
{
if (effect.Value.IsActive)
Effects += effect.Key + ":";
}
if(Effects != String.Empty)
Effects.Remove(Effects.Length - 1);
}
public void RestoreEffects()
{
foreach (var effect in effectList)
DeactivateEffect(effect.Key);
string[] split = Effects.Split(':');
foreach (string s in split)
ActivateEffect(s);
}
public Image()
{
Path = Text = Effects = String.Empty;
FontName = "Fonts/FixedSys Ex";
Position = Vector2.Zero;
Scale = Vector2.One;
Alpha = 1.0f;
SourceRect = Rectangle.Empty;
effectList = new Dictionary<string, ImageEffect>();
}
public void LoadContent()
{
content = new ContentManager(ScreenManager.Instance.Content.ServiceProvider, "Content");
if (Path != String.Empty)
Texture = content.Load<Texture2D>(Path);
font = content.Load<SpriteFont>(FontName);
dimensions = Vector2.Zero;
if (Texture != null)
dimensions.X += Texture.Width;
dimensions.X += font.MeasureString(Text).X;
if (Texture != null)
dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
else
dimensions.Y = font.MeasureString(Text).Y;
if (SourceRect == Rectangle.Empty)
SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);
SetEffect<FadeEffect>(ref FadeEffect);
LoadDevice();
if (Effects != string.Empty)
{
string[] split = Effects.Split(':');
foreach (string item in split)
ActivateEffect(item);
}
}
public void LoadDevice()
{
if (Path != String.Empty)
Texture = content.Load<Texture2D>(Path);
font = content.Load<SpriteFont>(FontName);
renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice, (int)dimensions.X, (int)dimensions.Y);
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
ScreenManager.Instance.SpriteBatch.Begin();
if (Texture != null)
ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
ScreenManager.Instance.SpriteBatch.End();
Texture = renderTarget;
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
}
public void UnloadContent()
{
content.Unload();
foreach (var effect in effectList)
{
DeactivateEffect(effect.Key);
}
}
public void Update(GameTime gameTime)
{
foreach (var effect in effectList)
{
if(effect.Value.IsActive)
effect.Value.Update(gameTime);
}
}
public void Draw(SpriteBatch spriteBatch)
{
origin = new Vector2(SourceRect.Width / 2, SourceRect.Height / 2);
spriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White * Alpha, 0.0f, origin, Scale, SpriteEffects.None, 0.0f);
if (renderTarget.IsContentLost)
{
ScreenManager.Instance.SpriteBatch.End();
LoadDevice();
ScreenManager.Instance.SpriteBatch.Begin();
}
}
}
renderTarget 的内容现在在一个单独的方法中绘制,并且被调用LoadContent()
一次,并且在Draw()
内容丢失时在该方法中调用。