2013-10-16 1 views
1

등각보기로 게임을 만들었습니다. 나는 현재 두 개의 행렬을 사용하여 매우 효과적으로 작업하고있는 구조를 가지고있다. 그러나이 작업을 수행하려면 추가 구성원과 기능이 필요합니다. SpriteBatch.Begin 함수 중 하나는 변환 행렬을 매개 변수로 허용합니다. 나는 새로운 SpriteBatch.Begin 함수를 작성하여 두 개의 행렬 (카메라 변형 용과 등각 투영 중 하나)을 허용합니다. 어떻게 실제 SpriteBatch.Begin 함수가 작동하는지 모르겠다. 그리고 나는 어떤 소스가 있는지 모른다. 누구나 아이디어가 있습니까?새로운 SpriteBatch.Begin 함수를 작성할 수 있습니까?

답변

3

좋아, 편집 후 SpriteBatch.begin() 함수의 소스 코드를 검색했습니다. XNA의 오픈 소스 구현 인 monogame의 소스 코드를 발견했습니다.

그래서 여기있다 :

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()를 호출 할 필요가있다.

연신() 기능은 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); 
} 

연신() 함수에 Player.cs :

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

것은이 그리는 것 여기

은 일례이며 Player.cs에서 Draw() 함수가있는 화면의 플레이어. spriteBatch는 Game1.cs의 LoadContent() 함수에서 초기화됩니다.

이 도움이 되었기를 바랍니다.

+0

감사합니다. 이것은 내가 필요한 것입니다. 빠른 질문 ... SpriteBatch에서 클래스를 파생시키고 해당 파생 클래스에 Draw 함수를 숨기고 있지만 여전히이 새 함수에서 base.Draw를 호출하는 데 문제가 있습니까? –

+0

새로운 spriteBatch.begin() 함수를 작성하는 것과 비슷한 것을 시도한 적이 없으므로 미안하다는 점을 알려 드릴 수 없습니다. 행운을 빕니다!! – Basecrosser

0

개체에 대한 "그리기"를 직접 만들고 "Game1.cs"의 "그리기"내에서 개체를 호출 할 수 있습니다.

예 :

protected override void Draw(GameTime gameTime){ 

myObject.Draw(gameTime); 

} 

희망이 도움이!

관련 문제