XNA Quick Tip – XNA SpriteBatch and Garbage

The XNA SpriteBatch class maintains an internal array to hold sprites to be drawn between calls to Begin() and End() if a sort mode is used. This array will grow as necessary, however, the reallocation will inevitably result in extra garbage that will eventually need to be collected.  On the XBox 360 this is quite expensive and so in general it is recommended to limit garbage wherever possible.

If you know the upper bound on the number of sprites to be drawn in a single batch, you can force the draw before the game begins.  This is pretty easy to do, however to make it even easier when you have multiple SpriteBatch instances for different purposes I use the following simple wrapper class in Solaroids that has a new constructor that takes an extra argument for the expected number of active sprites:

/// <summary>
/// Wrapper around a SpriteBatch that pre-allocates internal sorting buffers by drawing a set of sprites thus forcing the buffers to be expanded.
/// </summary>
public class PreallocatingSpriteBatch : SpriteBatch
{
  /// <summary>
  /// Initializes a new instance of the <see cref="PreallocatingSpriteBatch"/> class.
  /// </summary>
  /// <param name="graphicsDevice">The graphics device.</param>
  /// <param name="expectedActiveSprites">The expected number of active sprites.</param>
  public PreallocatingSpriteBatch(GraphicsDevice graphicsDevice, int expectedActiveSprites)
    : base(graphicsDevice)
  {
    ForceBufferAllocation(expectedActiveSprites);
  }

  /// <summary>
  /// Forces the buffer allocation.
  /// </summary>
  /// <param name="expectedActiveSprites">The expected number of active sprites.</param>
  private void ForceBufferAllocation(int expectedActiveSprites)
  {
    Texture2D tex = new Texture2D(GraphicsDevice, 16, 16);

    Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

    for (int i = 0; i < expectedActiveSprites; i++)
    {
      Draw(tex, Vector2.Zero, Color.White);
    }

    End();
  }
}

Audio positioning and a new boss

Wow it’s been awhile.  I haven’t had any free time to work on Solaroids since last June.  With a big work commitment out of the way and a much needed holiday break, I had a chance to work on it for a few days.  The results are a new boss ship, some newly done sound effects, and positional audio complete with Doppler effects on the missiles.  XNA sure makes it easy to put this stuff together, and after this update I’m a lot more confident with the XACT audio software.

Here’s a couple of screen shots and a video showing off the new features.

Solaroids 2010-01-07 22-54-35-33

Mean enough?

Coming in for the kill

Coming in for the kill

Surprise!

Surprise!


A few small updates

With spring here, development has slowed down quite a bit.  There are just to many yard projects that have to get done!

Recently, I did manage to spend a little time adding a few new features.  I adapted the XNA lens flare sample designed for 3D games to add the effect to my sun.  It worked out pretty nicely (and cheap to implement).  I swapped out the occlusion queries with a simple 2d  query to calculate the percentage of the sun that is on screen which then controls the visibility and intensity of the effect.  This technique doesn’t allow sprites to block the flare effect though, but it sufficient for what I wanted to achieve.

New lens flare effect

New lens flare effect

I while back while I was re-drawing my stand in graphics in Inkscape, I drew some pieces to build up a snake like enemy.  Combining that with some prototype serpent code I wrote quite a while ago (I can’t believe it was still functioning with all my engine changes) results in the following.

New serpent enemy

New serpent enemy

Right now each segment can take a hit, and that hit results in a cascading set of mini explosions down to the tail, which then explodes leaving the serpent a little shorter.  I plan on changing things around to show the energy being directed down to the tail – maybe the typical arcade shooter flashing sprite effect. Another thought is to have the serpent break into multiple pieces that then form a head and continue to pursue.

Here’s a video showing the two new features.  If you watch closely as the serpent ship comes on the screen you can actually see the segments being added which is a current bug.  The ship builds itself over time after it has been added to the scene.  As I use a queuing mechanism to record the orientation of the segments in the kinematics chain based on time delays.  Final code will build the entire chain at construction time.


Inkscape for Games

I’ve been using Inkscape, a freely available open source 2d vector drawing package, to do my 2d art for Solaroids and it has been working out quite nicely.  The limitations of vector art have given Solaroids a distinctively smooth style that I kind of like for a game such as this.  It has a certain modernized retro-feel to it that fits perfectly considering its graphics originated many many moons ago in a different computer era.

playershiplarge2

Redux

playershiplarge

Original

Here’s a comparison of the original player ship done in Deluxe Paint III on the Commodore Amiga vs. the new version done in Inkscape. Of course the original is being linearly interpolated by the web browser, and actually looked pretty good at it’s designed for resolution of a whopping 320x200x5bit, but the Inkscape version looks crisp and beautiful. Due to the nature of Inkscape and svg, the new version is actually resolution independent, so I can export it at whatever resolution is required.  Solaroids supports 1080p and so my graphics are exported large enough to support full detail at 1920×1080.

A couple of tips when using Inkscape for game development.  I export my images as png files, and unless you go into the Document Preferences and set the background color to something suitable (black instead of white in my case) you’ll get sub-pixel halos around your images when positioning, scaling, and rotating them in XNA.  This appears to be due to the graphics hardware using the neighboring (fully transparent) pixel color during interpolation.  It may also have something to do with the sprite sheet content processor I’m using as well.

Another cool thing about Inkscape is it remembers the export filename based on the set of shapes selected, so if you have multiple sprites in your document, you can repeatedly re-select various ones and it will remember your last filename for the selection as well as other export settings such as dpi etc.  Very nice.


New video of gameplay showing new effects

I posted a new video at youtube to show some of the new additions in action.  New sun and planets.  New middle ground stars to give it a little more dimension.  I fixed the background stars as it had a funny artifact that was visible at certain gamma levels.  I also changed the explosion particle system to have a red/orange tint to give it a little more color and increased the size of icons in the player HUD and made them resolution independent.  This one also has a different more mellow audio track I composed, great for cruising around in space and clearing space debris.

There is also a SD version available if your computer can’t handle the HD version.


Current Screenshots

Here are some screen shots from the current game.  I currently have the player information hidden as I work through some garbage collection issues on the 360.

solaroids-2009-04-27-21-48-28-14

Just some basic destroy the asteroid style gameplay.

solaroids-2009-04-27-21-49-52-70

I guess I got a little too close.

Missle Systems activated

Missile systems activated

Smartbombs!

Smart bombs!

Being stalked

Being stalked

Let's try this again.

Let’s try this again.

I owned you!

I owned you!


Welcome!

Welcome to the Solaroids blog.  Solaroids© is the title of my XBox Live community game currently in development.  It is written in C# and uses the Microsoft XNA platform which allows it to run both on Windows and on the XBox 360.

It could be said that it is an Asteroids© clone, and therefore also a Stardust© or Maelstrom© clone, but as I’ve only ever played the Atari’s Asteroids, any similarities to the later are merely coincidence.  Solaroids is Asteroids as I always envisioned it could be.  The concept was born in the early 1990’s when I was creating video game software on the Commodore Amiga line of personal computers.

This blog will track the ongoing development of both the game and the engine as well as various tutorials as I have time to create them.