2013-10-16 55 views
1

我正在創建一個等距視圖的遊戲。我有一個目前正在使用兩個矩陣非常有效的結構。不過,這需要額外的成員和功能,我希望不要這樣做。其中一個SpriteBatch.Begin函數接受一個變換矩陣作爲參數。我想寫一個新的SpriteBatch.Begin函數來接受兩個矩陣(一個用於攝像機變換,另一個用於等距變換)。我不知道實際的SpriteBatch.Begin函數是如何工作的,我不知道是否有可用的源。有人有想法嗎?是否可以編寫新的SpriteBatch.Begin函數?

回答

3

好吧,編輯後我搜索了SpriteBatch.begin() 函數的源代碼。 我發現了monogame的源代碼,它是XNA的開源實現。

所以在這裏,它是:

using System; 
using System.Text; 

namespace Microsoft.Xna.Framework.Graphics 
{ 
    public class SpriteBatch : GraphicsResource 
    { 
     readonly SpriteBatcher _batcher; 

      SpriteSortMode _sortMode; 
      BlendState _blendState; 
      SamplerState _samplerState; 
      DepthStencilState _depthStencilState; 
      RasterizerState _rasterizerState;     
      Effect _effect; 
      bool _beginCalled; 

      Effect _spriteEffect; 
      readonly EffectParameter _matrixTransform; 
      readonly EffectPass _spritePass; 

      Matrix _matrix; 
      Rectangle _tempRect = new Rectangle (0,0,0,0); 
      Vector2 _texCoordTL = new Vector2 (0,0); 
      Vector2 _texCoordBR = new Vector2 (0,0); 

      public SpriteBatch (GraphicsDevice graphicsDevice) 
      { 
       if (graphicsDevice == null) 
       { 
         throw new ArgumentException ("graphicsDevice"); 
       }   

       this.GraphicsDevice = graphicsDevice; 

       // Use a custom SpriteEffect so we can control the transformation matrix 
       _spriteEffect = new Effect(graphicsDevice, SpriteEffect.Bytecode); 
       _matrixTransform = _spriteEffect.Parameters["MatrixTransform"]; 
       _spritePass = _spriteEffect.CurrentTechnique.Passes[0]; 

       _batcher = new SpriteBatcher(graphicsDevice); 

       _beginCalled = false; 
      } 

      public void Begin() 
      { 
       Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Matrix.Identity);   
      } 

      public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) 
      { 
       if (_beginCalled) 
        throw new InvalidOperationException("Begin cannot be called again until End has been successfully called."); 

       // defaults 
       _sortMode = sortMode; 
       _blendState = blendState ?? BlendState.AlphaBlend; 
       _samplerState = samplerState ?? SamplerState.LinearClamp; 
       _depthStencilState = depthStencilState ?? DepthStencilState.None; 
       _rasterizerState = rasterizerState ?? RasterizerState.CullCounterClockwise; 

       _effect = effect; 

       _matrix = transformMatrix; 

       // Setup things now so a user can chage them. 
       if (sortMode == SpriteSortMode.Immediate) 
        Setup(); 

       _beginCalled = true; 
      } 

      public void Begin (SpriteSortMode sortMode, BlendState blendState) 
      { 
       Begin (sortMode, blendState, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Matrix.Identity);       
      } 

      public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) 
      { 
       Begin (sortMode, blendState, samplerState, depthStencilState, rasterizerState, null, Matrix.Identity);   
      } 

      public void Begin (SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) 
      { 
       Begin (sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, Matrix.Identity);       
      } 

      public void End() 
      {   
       _beginCalled = false; 

       if (_sortMode != SpriteSortMode.Immediate) 
         Setup(); 

#if PSM 
     GraphicsDevice.BlendState = _blendState; 
     _blendState.ApplyState(GraphicsDevice); 
#endif 

     _batcher.DrawBatch(_sortMode); 
    } 

的文件不完整,如果你想閱讀整個文件我沒有結束功能後,粘貼但。鏈接是:https://github.com/mono/MonoGame/blob/7ec1ec8a0e924eca60588e770121ed3e2593e74d/MonoGame.Framework/Graphics/SpriteBatch.cs

我希望這是你正在尋找的源代碼。

祝你好運!

這是我的另一個答案:

你需要調用spriteBatch.Begin()和spriteBatch.End()在你的主Draw()函數,所以你可以實際繪製。

下面是一個例子:

的Draw()函數在Game1.cs:

protected override void Draw(GameTime gameTime) 
{ 
    GraphicsDevice.Clear(Color.CornflowerBlue); 

    // Start drawing 
    spriteBatch.Begin(); 

    player.Draw(spriteBatch); 

    // Stop drawing 
    spriteBatch.End(); 

    base.Draw(gameTime); 
} 

的Draw()函數在Player.cs:

public void Draw(SpriteBatch spriteBatch) 
{ 
    spriteBatch.Draw(playerTexture, playerPosition, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f); 
} 

這將繪製玩家在Draw.c()函數之外的屏幕上播放Player.cs。 SpriteBatch在Game1.cs的LoadContent()函數中初始化。

我希望這有助於!

+0

非常感謝!這正是我需要的。快速的問題......從SpriteBatch派生類並隱藏該派生類中的Draw函數,但仍然在這些新函數中調用base.Draw有什麼問題嗎? –

+0

我從來沒有嘗試過類似的東西來創建一個新的spriteBatch.begin()函數,所以我無法幫助你。祝你好運!! – Basecrosser

0

您可以爲對象創建自己的「Draw」,並在「Game1.cs」的「Draw」中調用它們。

例子:

protected override void Draw(GameTime gameTime){ 

myObject.Draw(gameTime); 

} 

希望這有助於!

相關問題