XNA Framework:操作屏幕亮度

时间:2011-12-04 02:51:44

标签: c# .net windows-phone-7 xna xna-4.0

我正在开发一个使用XNA Framework的Game项目。

我正在使用OptionsMenuScreen课程向用户显示不同的游戏选项/设置。

我在屏幕上添加了两个UPDOWN按钮(Texture2D),这会增加或降低屏幕的亮度。

我无法找到操作屏幕亮度所需的逻辑。

如果你能指出我正确的方向,我真的很感激。

以下是相关代码:


class OptionsMenuScreen : MenuScreen
{
    SpriteBatch spriteBatch;

    MenuEntry brightness;
    Texture2D brightnessUp;
    Texture2D brightnessDown;

    ContentManager contentManager;

    public OptionsMenuScreen() : base("Options")
    {
        brightness = new MenuEntry("Brightness");
        MenuEntries.Add(brightness);
    }

    public override void LoadContent()
    {
        if (contentManager == null)
        {
            contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
        }

        brightnessUp = contentManager.Load<Texture2D>("handup");
        brightnessDown = contentManager.Load<Texture2D>("handdown");
    }

    public override void Draw(GameTime gameTime)
    {
        spriteBatch = ScreenManager.SpriteBatch;

        var brightnessDownPosition =
            new Vector2(brightness.Position.X - 50, brightness.Position.Y - 12);

        var brightnessUpPosition =
            new Vector2(brightness.Position.X 
                + brightness.GetWidth(this) + 8, brightness.Position.Y - 12);

        spriteBatch.Begin();

        spriteBatch.Draw(brightnessDown,
            new Rectangle((int)brightnessDownPosition.X, (int)brightnessDownPosition.Y,
                30, 30), Color.White);

        spriteBatch.Draw(brightnessUp,
            new Rectangle((int)brightnessUpPosition.X, (int)brightnessUpPosition.Y,
                30, 30), Color.White);

        spriteBatch.End();

        base.Draw(gameTime);
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        foreach (TouchLocation t in TouchPanel.GetState())
        {
            if (t.Position.X >= brightnessDown.Bounds.Left
                && t.Position.X <= brightnessDown.Bounds.Right
                && t.Position.Y >= brightnessDown.Bounds.Top
                && t.Position.X <= brightnessDown.Bounds.Bottom)
            {
                // What should I do here?
            }
            else if (t.Position.X >= brightnessUp.Bounds.Left
                && t.Position.X <= brightnessUp.Bounds.Right
                && t.Position.Y >= brightnessUp.Bounds.Top
                && t.Position.X <= brightnessUp.Bounds.Bottom)
            {
                // What should I do here?
            }
        }
    }
}

非常感谢您对此进行调查:)

1 个答案:

答案 0 :(得分:3)

According to this thread,有几种方法可以解决这个问题:

  • 使用GraphicsDevice.SetGammaRamp(...)
  • 使用黑色纹理和Alpha透明度
  • 调整场景中所有对象的纹理上的材质和/或灯光
  • 使用像素着色器的每像素颜色重新计算

第一个选项是最简单的,但有一些缺点(在设置时会产生难看的图形效果 - 主要只影响选项菜单),而且并不总是支持。

您可以使用SupportsFullScreenGammaCanCalibrateGamma来确定它是否受支持,如果不支持,则可以使用其他方法。

修改

在Windows Phone 7上,some of these options might not be available to you

该网站(目前已损坏,但如果你谷歌它可以找到缓存副本)建议你使用带有alpha混合方法的全屏四边形,以及这些特定的混合模式:

Brightness: source = ZERO, dest = SOURCECOLOR
Contrast: source = DESTCOLOR, dest = SOURCECOLOR

这是一些代码(从那篇文章中偷来的):

// In your game class:

Texture2D whiteTexture;

// In LoadContent:

whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
whiteTexture.SetData<Color>(new Color[] { Color.White });

// In the appropriate class (the game class? not sure...):

int brightness;
int contrast;

BlendState brightnessBlend;
BlendState contrastBlend;

// In Initialize:

brightness = 255;
contrast = 128;

brightnessBlend = new BlendState();
brightnessBlend.ColorSourceBlend = brightnessBlend.AlphaSourceBlend =  Blend.Zero;
brightnessBlend.ColorDestinationBlend = brightnessBlend.AlphaDestinationBlend = Blend.SourceColor;

contrastBlend = new BlendState();
contrastBlend.ColorSourceBlend = contrastBlend.AlphaSourceBlend = Blend.DestinationColor;
contrastBlend.ColorDestinationBlend = contrastBlend.AlphaDestinationBlend = Blend.SourceColor;

// In Draw:

spriteBatch.Begin(SpriteSortMode.Immediate, brightnessBlend);
spriteBatch.Draw(whiteTexture, new Rectangle(0, 0, 480, 800), new Color (brightness, brightness, brightness, 255));
spriteBatch.End();

spriteBatch.Begin(SpriteSortMode.Immediate, contrastBlend);
spriteBatch.Draw(whiteTexture, new Rectangle(0, 0, 480, 800), new Color(contrast, contrast, contrast, 255));
spriteBatch.End();

GraphicsDevice.BlendState = BlendState.Opaque;