C# 在XNA中更改分辨率-之后屏幕变黑

C# 在XNA中更改分辨率-之后屏幕变黑,c#,graphics,xna,C#,Graphics,Xna,我正在用最新的XNA(C#)编写一个2D游戏。我正在对旧代码进行一次彻底的修改,并且刚刚添加了一些按钮来更改分辨率。然而,在切换全屏模式或更改窗口大小/分辨率后,屏幕将永远是黑色(我的清晰屏幕调用的颜色),尽管游戏仍在运行,因为我可以通过按键退出。 这在我以前的版本中并没有发生过——一切都很好——但在我能找到的相关代码中并没有区别。但是我重新加载了我的图形,用我自己的纹理加载器而不是内容加载器-这会是问题吗 如果没有其他选项,是否有一个简单的方法使游戏重新启动,因为图形正常,并在重新启动后处于选

我正在用最新的XNA(C#)编写一个2D游戏。我正在对旧代码进行一次彻底的修改,并且刚刚添加了一些按钮来更改分辨率。然而,在切换全屏模式或更改窗口大小/分辨率后,屏幕将永远是黑色(我的清晰屏幕调用的颜色),尽管游戏仍在运行,因为我可以通过按键退出。 这在我以前的版本中并没有发生过——一切都很好——但在我能找到的相关代码中并没有区别。但是我重新加载了我的图形,用我自己的纹理加载器而不是内容加载器-这会是问题吗

如果没有其他选项,是否有一个简单的方法使游戏重新启动,因为图形正常,并在重新启动后处于选定的格式

我的代码是:

 public override void Click()
    {
        base.Click();
        Settings.Default.screenWidth = resolution[0];
        Settings.Default.screenHeight = resolution[1];
        Settings.Default.Save();
        Variables.screenWidth = Settings.Default.screenWidth;
        Variables.screenHeight = Settings.Default.screenHeight;

        Game1.graphics.PreferredBackBufferWidth = Variables.screenWidth;
        Game1.graphics.PreferredBackBufferHeight = Variables.screenHeight;
        Game1.graphics.ApplyChanges();
    }
谢谢

编辑:My texture loadign code-将枚举中包含名称的所有文件作为Texture2D加载

public static void LoadAll(string subFolder)
    {
        List<string> s = Directory.GetFiles(path + "\\" + subFolder, "*.png", SearchOption.AllDirectories).ToList<string>();
        foreach (string S in s)
        {
            if (Enum.IsDefined(typeof(T), Path.GetFileNameWithoutExtension(S)))
            {
                FileStream stream = new FileStream(S, FileMode.Open);
                Texture2D t = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
                RenderTarget2D result = null;

                //Setup a render target to hold our final texture which will have premulitplied alpha values
                result = new RenderTarget2D(Game1.graphics.GraphicsDevice, t.Width, t.Height);

                Game1.graphics.GraphicsDevice.SetRenderTarget(result);
                Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);

                //Multiply each color by the source alpha, and write in just the color values into the final texture
                BlendState blendColor = new BlendState();
                blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;

                blendColor.AlphaDestinationBlend = Blend.Zero;
                blendColor.ColorDestinationBlend = Blend.Zero;

                blendColor.AlphaSourceBlend = Blend.SourceAlpha;
                blendColor.ColorSourceBlend = Blend.SourceAlpha;


                Game1.spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
                Game1.spriteBatch.Draw(t, t.Bounds, Microsoft.Xna.Framework.Color.White);
                Game1.spriteBatch.End();

                //Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
                BlendState blendAlpha = new BlendState();
                blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;

                blendAlpha.AlphaDestinationBlend = Blend.Zero;
                blendAlpha.ColorDestinationBlend = Blend.Zero;

                blendAlpha.AlphaSourceBlend = Blend.One;
                blendAlpha.ColorSourceBlend = Blend.One;

                Game1.spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
                Game1.spriteBatch.Draw(t, t.Bounds, Microsoft.Xna.Framework.Color.White);
                Game1.spriteBatch.End();

                //Release the GPU back to drawing to the screen

                Game1.graphics.GraphicsDevice.SetRenderTarget(null);


                t = result;

                textureDictionary.Add(Path.GetFileNameWithoutExtension(S), t);
            }
           // else
            //  Console.WriteLine("Did not load -- " + Path.GetFileNameWithoutExtension(S) + " -- (add to enum to enable loading)");
        }
    }
publicstaticvoidloadall(字符串子文件夹)
{
List s=Directory.GetFiles(路径+“\\”+子文件夹“*.png”,SearchOption.AllDirectories).ToList();
foreach(S中的字符串S)
{
if(枚举已定义(typeof(T),Path.GetFileNameWithoutExtension(S)))
{
FileStream stream=新的FileStream(S,FileMode.Open);
Texture2D t=Texture2D.FromStream(Game1.graphics.GraphicsDevice,stream);
RenderTarget2D结果=空;
//设置渲染目标以保留最终纹理,该纹理将具有预纹理应用的alpha值
结果=新的RenderTarget2D(Game1.graphics.GraphicsDevice,t.宽度,t.高度);
Game1.graphics.GraphicsDevice.SetRenderTarget(结果);
Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
//将每种颜色乘以源alpha,并将颜色值写入最终纹理
BlendState blendColor=新的BlendState();
blendColor.ColorWriteChannels=ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend=Blend.Zero;
blendColor.ColorDestinationBlend=Blend.Zero;
blendColor.AlphaSourceBlend=Blend.SourceAlpha;
blendColor.ColorSourceBlend=Blend.SourceAlpha;
游戏1.spriteBatch.Begin(SpriteSortMode.Immediate,blendColor);
游戏1.spriteBatch.Draw(t,t.Bounds,Microsoft.Xna.Framework.Color.White);
Game1.spriteBatch.End();
//现在,将alpha值从PNG源纹理复制到最终纹理,而不将它们相乘
BlendState blendAlpha=新的BlendState();
blendAlpha.ColorWriteChannels=ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend=Blend.Zero;
blendAlpha.ColorDestinationBlend=Blend.Zero;
blendAlpha.AlphaSourceBlend=Blend.One;
blendAlpha.ColorSourceBlend=Blend.One;
游戏1.spriteBatch.Begin(SpriteSortMode.Immediate,blendAlpha);
游戏1.spriteBatch.Draw(t,t.Bounds,Microsoft.Xna.Framework.Color.White);
Game1.spriteBatch.End();
//将GPU释放回屏幕以绘制图形
Game1.graphics.GraphicsDevice.SetRenderTarget(null);
t=结果;
添加(路径.getFileName不带outExtension,t);
}
//否则
//Console.WriteLine(“未加载--”+Path.GetFileNameWithoutExtension+“--(添加到枚举以启用加载)”;
}
}
编辑:以下建议中的工作代码-可能不是最有效的,但它可以工作

public static void LoadAll(string subFolder)
    {
        List<string> s = Directory.GetFiles(path + "\\" + subFolder, "*.png", SearchOption.AllDirectories).ToList<string>();
        foreach (string S in s)
        {
            if (Enum.IsDefined(typeof(T), Path.GetFileNameWithoutExtension(S)))
            {
                FileStream stream = new FileStream(S, FileMode.Open);
                Texture2D t = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
                RenderTarget2D result = null;
                Texture2D resultTexture;

                //Setup a render target to hold our final texture which will have premulitplied alpha values
                result = new RenderTarget2D(Game1.graphics.GraphicsDevice, t.Width, t.Height);

                Game1.graphics.GraphicsDevice.SetRenderTarget(result);
                Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);

                //Multiply each color by the source alpha, and write in just the color values into the final texture
                BlendState blendColor = new BlendState();
                blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;

                blendColor.AlphaDestinationBlend = Blend.Zero;
                blendColor.ColorDestinationBlend = Blend.Zero;

                blendColor.AlphaSourceBlend = Blend.SourceAlpha;
                blendColor.ColorSourceBlend = Blend.SourceAlpha;


                Game1.spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
                Game1.spriteBatch.Draw(t, t.Bounds, Microsoft.Xna.Framework.Color.White);
                Game1.spriteBatch.End();

                //Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
                BlendState blendAlpha = new BlendState();
                blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;

                blendAlpha.AlphaDestinationBlend = Blend.Zero;
                blendAlpha.ColorDestinationBlend = Blend.Zero;

                blendAlpha.AlphaSourceBlend = Blend.One;
                blendAlpha.ColorSourceBlend = Blend.One;

                Game1.spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
                Game1.spriteBatch.Draw(t, t.Bounds, Microsoft.Xna.Framework.Color.White);
                Game1.spriteBatch.End();

                //Release the GPU back to drawing to the screen

                Game1.graphics.GraphicsDevice.SetRenderTarget(null);

                resultTexture = new Texture2D(Game1.graphics.GraphicsDevice, result.Width, result.Height);
                Color[] data = new Color[result.Height * result.Width];
                Color[] textureColor = new Color[result.Height * result.Width];

                result.GetData<Color>(textureColor);

                resultTexture.SetData(textureColor);

                textureDictionary.Add(Path.GetFileNameWithoutExtension(S), resultTexture);
            }
           // else
            //  Console.WriteLine("Did not load -- " + Path.GetFileNameWithoutExtension(S) + " -- (add to enum to enable loading)");
        }
    }
publicstaticvoidloadall(字符串子文件夹)
{
List s=Directory.GetFiles(路径+“\\”+子文件夹“*.png”,SearchOption.AllDirectories).ToList();
foreach(S中的字符串S)
{
if(枚举已定义(typeof(T),Path.GetFileNameWithoutExtension(S)))
{
FileStream stream=新的FileStream(S,FileMode.Open);
Texture2D t=Texture2D.FromStream(Game1.graphics.GraphicsDevice,stream);
RenderTarget2D结果=空;
结构2D结果结构;
//设置渲染目标以保留最终纹理,该纹理将具有预纹理应用的alpha值
结果=新的RenderTarget2D(Game1.graphics.GraphicsDevice,t.宽度,t.高度);
Game1.graphics.GraphicsDevice.SetRenderTarget(结果);
Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
//将每种颜色乘以源alpha,并将颜色值写入最终纹理
BlendState blendColor=新的BlendState();
blendColor.ColorWriteChannels=ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend=Blend.Zero;
blendColor.ColorDestinationBlend=Blend.Zero;
blendColor.AlphaSourceBlend=Blend.SourceAlpha;
blendColor.ColorSourceBlend=Blend.SourceAlpha;
游戏1.spriteBatch.Begin(SpriteSortMode.Immediate,blendColor);
游戏1.spriteBatch.Draw(t,t.Bounds,Microsoft.Xna.Framework.Color.White);
Game1.spriteBatch.End();
//现在,将alpha值从PNG源纹理复制到最终纹理,而不将它们相乘
BlendState blendAlpha=新的BlendState();
blendAlpha.ColorWriteChannels=ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend=Blend.Zero;
blendAlpha.ColorDestinationBlend=Blend.Zero;
blendAlpha.AlphaSourceBlend=Blend.One;
白花蛇舌草色源