最小化时,C#XNA 4.0纹理将消失
我对XNA还比较陌生,我遇到了一些问题。每当我最小化游戏时,当我再次打开它时,屏幕就会变黑。这可能是什么原因?如何解决 这是我的图像类:最小化时,C#XNA 4.0纹理将消失,c#,xna,textures,xna-4.0,rendertarget,C#,Xna,Textures,Xna 4.0,Rendertarget,我对XNA还比较陌生,我遇到了一些问题。每当我最小化游戏时,当我再次打开它时,屏幕就会变黑。这可能是什么原因?如何解决 这是我的图像类: public class Image { public float Alpha; public string Text, FontName, Path; public Vector2 Position, Scale; public Rectangle SourceRect; public bool IsActive;
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);
}
}
公共类映像
{
公共交通工具;
公共字符串文本、字体名称、路径;
公共向量2位置、规模;
公共矩形SourceRect;
公共福利活动;
公共布尔标志;
公共纹理2D纹理;
矢量2起源;
ContentManager内容;
RenderTarget2D renderTarget;
SpriteFont字体;
词典效应表;
公共字符串效应;
公共FadeEffect FadeEffect;
无效设置效应(参考T效应)
{
if(effect==null)
effect=(T)Activator.CreateInstance(typeof(T));
其他的
{
(效果为ImageEffect)。IsActive=true;
var obj=这个;
(效果与图像效果相同)。载荷内容(参考obj);
}
添加(effect.GetType().ToString().Replace(“RPG.”,“”),(效果为ImageEffect));
}
公共无效激活效果(字符串效果)
{
if(effectList.ContainsKey(effect))
{
effectList[effect].IsActive=true;
var obj=这个;
效应列表[effect].LoadContent(参考obj);
}
}
公共无效停用效果(字符串效果)
{
if(effectList.ContainsKey(effect))
{
效应列表[effect].IsActive=false;
效应列表[效应].UnloadContent();
}
}
公共影响()
{
Effects=String.Empty;
foreach(效应列表中的var效应)
{
if(effect.Value.IsActive)
效果+=效果。键+“:”;
}
if(Effects!=String.Empty)
效果。移除(效果。长度-1);
}
公共无效恢复效果()
{
foreach(效应列表中的var效应)
停用效果(effect.Key);
string[]split=Effects.split(“:”);
foreach(拆分中的字符串s)
激活效应;
}
公众形象()
{
Path=Text=Effects=String.Empty;
FontName=“font/FixedSys Ex”;
位置=矢量2。零;
比例=矢量2.1;
α=1.0f;
SourceRect=Rectangle.Empty;
effectList=新字典();
}
公共void LoadContent()
{
内容=新的ContentManager(ScreenManager.Instance.content.ServiceProvider,“内容”);
if(路径!=String.Empty)
纹理=内容。加载(路径);
font=content.Load(FontName);
Vector2维度=Vector2.0;
如果(纹理!=null)
尺寸.X+=纹理.宽度;
尺寸.X+=字体尺寸(文本).X;
如果(纹理!=null)
dimensions.Y=Math.Max(纹理、高度、字体、度量(文本).Y);
其他的
维度.Y=字体.MeasureString(文本).Y;
if(SourceRect==Rectangle.Empty)
SourceRect=新矩形(0,0,(int)dimensions.X,(int)dimensions.Y);
renderTarget=新的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();
如果(纹理!=null)
ScreenManager.Instance.SpriteBatch.Draw(纹理,Vector2.Zero,Color.White);
ScreenManager.Instance.SpriteBatch.DrawString(字体、文本、矢量2.Zero、颜色、白色);
ScreenManager.Instance.SpriteBatch.End();
纹理=渲染目标;
ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
SetEffect(参考FadeEffect);
if(Effects!=string.Empty)
{
string[]split=Effects.split(“:”);
foreach(拆分中的字符串项)
激活效应(项目);
}
}
公共内容()
{
content.Unload();
foreach(效应列表中的var效应)
{
停用效果(effect.Key);
}
}
公开作废更新(游戏时间游戏时间)
{
foreach(效应列表中的var效应)
{
if(effect.Value.IsActive)
效果。值。更新(游戏时间);
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
原点=新矢量2(SourceRect.Width/2,SourceRect.Height/2);
spriteBatch.Draw(纹理、位置+原点、SourceRect、颜色.White*Alpha、0.0f、原点、比例、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();
}
}
}
公共类映像
{
公共交通工具;
公共字符串文本、字体名称、路径;
公共向量2位置、规模;
公共矩形SourceRect;
公共福利活动;
公共布尔标志;
公共纹理2D纹理;
矢量2起源;
向量2维;
ContentManager内容;
RenderTarget2D renderTarget;
SpriteFont字体;
词典效应表;
公共字符串效应;
公共FadeEffect FadeEffect;
无效设置效应(参考T效应)
{
if(effect==null)
effect=(T)Activator.CreateInstance(typeof(T));
其他的
{
(效果为ImageEffect)。IsActive=true;
var obj=这个;
(效果与图像效果相同)。载荷内容(参考obj);
}
添加(effect.GetType().ToString().Replace(“RPG.”,“”),(效果为ImageEffect));
}
公共无效激活效果(字符串效果)
{
if(effectList.ContainsKey(effect))
{
effectList[effect].IsActive=true;
var obj=这个;
效应列表[effect].LoadContent(参考obj);
}
}
公共无效停用效果(字符串效果)
{
如果(效果列表)。
public void Initialize(GraphicsDevice graphics)
{
Color[] colors = new Color[Width * Height];
RenderTarget2D target = new RenderTarget2D(graphics, Width, Height);
gridTexture = new Texture2D(graphics, Width, Height);
graphics.SetRenderTarget(target);
graphics.Clear(Color.Black);
SpriteBatch.Begin();
// drawing code...
SpriteBatch.End();
graphics.SetRenderTarget(null);
target.GetData<Color>(colors);
gridTexture.SetData<Color>(colors);
}