c# - XNA 框架 : Manipulating screen brightness

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

我正在使用 XNA Framework 开发一个游戏项目。

我正在使用 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?
            }
        }
    }
}

非常感谢您对此的调查:)

最佳答案

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

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

第一个选项是最简单的,但有一些缺点(设置时图形效果丑陋 - 大多数情况下只影响选项菜单),并且并不总是受支持。

你可以使用 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;

关于c# - XNA 框架 : Manipulating screen brightness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8372881/

相关文章:

c# - 在 Azure Eventhub 中发布数据时出现未经授权的错误

c# - C#图像转灰度时如何解决循环错误

c# - 如何从对象中获取值?

windows-phone-7 - 播放WP7中的声音循环

c# - 我应该多快关闭 using block ?

c# - Oracle 客户端与基于任务的异步模式(异步/等待)

c# - .Net 中的 PostgreSQL 连接错误

c# - 代码优先迁移是否自动在服务器上运行?

Silverlight Windows Phone 7 - 使用硬件后退按钮后按钮单击事件丢失

html - Windows 7手机Web开发问题