I am creating a game with an isometric view. I have a structure which is currently working quite effectively, using two matrices. This requires additional members and functions that I would like to do without, though. One of the SpriteBatch.Begin
functions accepts a transformation matrix as a parameter. I would like to write a new SpriteBatch.Begin
function to accept two matrices (one for camera transformation, and one for isometric transformation). I don't know how the actual SpriteBatch.Begin
function works, and I don't know if there is any source available. Does anyone have an idea?
问问题
645 次
2 回答
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
我希望这是您正在寻找的源代码。
祝你好运!
这是我的另一个答案:
您需要在主 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 回答