2011-12-04 33 views
3

我正在爲XNA框架的遊戲項目工作。XNA框架:操縱屏幕亮度

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

非常感謝您就這個:)

回答

3

According to this thread,有幾種方法,你可以去了解它:

第一種選擇是調節上的紋理的所有對象的材料和/或燈在場景

  • 每像素顏色重新計算最簡單,但有一些缺點(在設置時會產生醜陋的圖形效果 - 主要影響選項菜單),並不總是受支持。

    您可以使用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; 
    
  • +0

    我找到了相同的代碼,問題是在我的OptionsMenuScreen類中繼承GameScreen類。 GameScreen類沒有與上面粘貼的Game1.cs類模板相同的方法。那麼,如何讓這兩個類一起工作,這兩個類都有我需要使用的方法? – Mash

    +0

    @Mash:對不起,沒有安裝XNA,所以我無法檢查出來。如果您可以從另一個實例訪問一個實例,則可以使用公共屬性/方法輕鬆彌合差距。我想你會公開曝光這些亮度和對比度屬性,並將剩下的內容留在遊戲類中,因爲它不需要直接訪問。如果結果比我所建議的要困難得多,而且你最終找出答案,我建議添加自己的答案,以表明你做了什麼來幫助有同樣問題的人解決問題。 –

    +0

    @Mash:從遊戲類訪問屏幕類的一種方法是將遊戲類傳遞給構造函數。如果你從遊戲類中的某些代碼手動構造屏幕類,這可能只會起作用。再次,我沒有安裝XNA,並且在2年內沒有觸及它,所以我不記得肯定... –