public static void DrawSprites(int width, int height)
{
// No need to render if we got no sprites this frame
if (sprites.Count == 0)
return;
// Create sprite batch if we have not done it yet.
// Use device from texture to create the sprite batch.
if (spriteBatch == null)
spriteBatch = new SpriteBatch(sprites[0].texture.GraphicsDevice);
// Start rendering sprites
spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.BackToFront, SaveStateMode.None);
// Render all sprites
foreach (SpriteToRender sprite in sprites)
spriteBatch.Draw(sprite.texture,
// Rescale to fit resolution
new Rectangle(
sprite.rect.X * width / 1024,
sprite.rect.Y * height / 768,
sprite.rect.Width * width / 1024,
sprite.rect.Height * height / 768),
sprite.sourceRect, sprite.color);
// We are done, draw everything on screen with help of the end method.
spriteBatch.End();
// Kill list of remembered sprites
sprites.Clear();
} // DrawSprites()
在调用该方法的时候,传递当前窗口分辨率的宽度和高度,并根据当前分辨率对所有sprite进行比例缩放,这对于支持Xbox 360的多分辨率非常重要。方法首先检查是否有东西要渲染,然后确保SpriteBatch类的静态实例(就是这里的spriteBatch变量)是否已被创建,调用Begin方法之后,在当前帧中遍历所有的sprite并重新调整它们的尺寸以适应当前屏幕的大小,最后当把所有sprite画到屏幕上之后再调用End方法。另外还要把sprite列表清空,为下一帧的渲染做准备。可以研究本章最后的Breakout游戏来看看这个类是如何工作的。