1

等角図でゲームを作成しています。私は現在、2 つのマトリックスを使用して非常に効果的に機能している構造を持っています。ただし、これには追加のメンバーと関数が必要ですが、それらは必要ありません。関数の 1 つはSpriteBatch.Begin、変換行列をパラメーターとして受け入れます。SpriteBatch.Begin2 つの行列 (カメラ変換用とアイソメトリック変換用) を受け入れる新しい関数を書きたいと思います。実際のSpriteBatch.Begin機能がどのように機能するかはわかりませんし、利用可能なソースがあるかどうかもわかりません。誰にもアイデアはありますか?

4

2 に答える 2

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);
    }

ファイルは完全ではありません。終了関数の後に貼り付けませんでしたが、ファイル全体を読みたい場合。リンクは次のとおりです

これがあなたが探していたソース コードであることを願っています。

幸運を!

これは私の他の答えです:

実際に描画できるように、メインの Draw() 関数で spriteBatch.Begin() と spriteBatch.End() を呼び出す必要があります。

次に例を示します。

Game1.cs の Draw() 関数:

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 の Draw() 関数:

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() 関数で初期化されます。

これがお役に立てば幸いです。

于 2013-10-16T17:06:16.477 に答える
0

オブジェクトに対して独自の「Draw」を作成し、「Game1.cs」の「Draw」内から呼び出すことができます。

例:

protected override void Draw(GameTime gameTime){

myObject.Draw(gameTime);

}

この助けを願っています!

于 2013-10-16T15:19:56.230 に答える