Skip to content

Guide: Drawing sprites

Alan Stagner edited this page Jun 16, 2020 · 2 revisions

BNA has a SpriteBatch class which is directly inspired by XNA's SpriteBatch. If you just wanna draw some 2D stuff to the screen, start here.

To start, you need to begin a new batch.

// note: SpriteBatch is a property of the Game class
// this overload by default creates a new batch with alpha blending, back-to-front sorting, and no transform.
SpriteBatch.Begin();

Now you can being drawing sprites:

SpriteBatch.Draw(spriteTexture, Vec2(0, 0), Color.White);

There's many overloads you can use as well, which will let you draw to arbitrary rectangles on the screen, draw from arbitrary source rectangles in the texture, rotate the sprite, etc. Check the SpriteBatch class guide for more details.

Then you just call End to end the batch and submit them to be drawn:

SpriteBatch.End();

As a bit of a performance tip: End() will sort all of the sprites you've drawn based on the sort mode (by default, will try and sort them based on the depth value you passed to Draw or, failing that, the order you drew them in). It will then try and build batches out of those as best it can, each batch being a single draw call to the graphics card. For best results, if you can get multiple sprites drawn in order to all use the same texture (perhaps drawing from a single big texture sheet), SpriteBatch will be able to produce fewer batches and reduce the time spent submitting draw calls.