Simple XNA Progress Bar

I recently needed a dead simple progress bar, where I could give it a display rectangle, min, max, and a value and show me something in game, but couldn’t find anything online. I ended up writing it myself, and thought I’d share what I came up with.

My goal was to create a single file I could drop into a project to allow me to draw a progress bar. All the projects I found involved using a texture as a separate file. I knew from a previous project that you could create that texture in-memory using the Texture2D.SetData() function, and that’s what I did.

I also wanted to provide a double border so I could put the progress bar on top of any background without having its border look invisible. If you’ve ever seen closed captioning disappear because of the background color, you know what I mean. But since not everyone needs a double border, I made sure you could turn it off.

Anyways, here’s and example of what you can do with the progress bar. (download the project for this at the bottom).


So the simple way to get a progress bar into your game is this:

1. Get the source code.

2. Add ProgressBar.cs to your XNA project. Note that I wrote this in 3.0, so make sure you’re on that version or higher.

3. Create an instance of a progress bar in your game1 class;

UI.ProgressBar progressBar;

4. In your LoadContent() function, set it to a new progressBar, and provide the rectangle you want it to lie in. You can also set the minimum, maximum, and value member variables here. The defaults are 0, 100, and 0, respectively.

progressBar = new UI.ProgressBar(this, new Rectangle(10, 10, 300, 16));
progressBar.minimum = 0;
progressBar.maximum= 10000;

5. In your Update() function, update the value of your progress bar if necessary, and then call the progress bar’s Update() function, providing the gameTime (gameTime is not currently used in the progress bar code, though).

progressBar.value = character.health;
progressBar.Update(gameTime);

6. In your Draw() function, call progressBar.Draw(), specifying a spriteBatch. Make sure you’re already called SpriteBatch.Begin() before calling the progress bar Draw() function.

progressBar.Draw(spriteBatch);

And that’s it!

Now, I’ve put quite a few customizable pieces in there. They are all commented in the progress bar code, in the “public members” region.

The one I’d like to talk about is the orientation. You can specify that the progress bar be vertical or horizontal, and in which direction it fill. This is specified at creation time in the ProgressBar constructor. See the orientation member variable for more info.

Here’s a list of the public variables in there now:

float minimum – Minimum value of the progress bar. Default is 0.0f.
float maximum – Maximum value of the progress bar. Default is 100.0f.
float value – Current progress value.

Int32 borderThicknessOuter – Outer border thickness. This is drawn within the bounds of the progress bar. Default is 3.
Int32 borderThicknessInner – Inner border thickness. This is drawn within the bounds of the progress bar. Default is 2.

Color borderColorOuter – Outer border color. Default is Gray.
Color borderColorInner – Inner border color. Default is Black
Color fillColor – Color of the progress section of the bar. Default is Dark Blue.
Color backgroundColor – Color of the background (unfilled) section of the progress bar. Default is White.
Orientation orientation – Gets the orientation of this progress bar. Set at creation time.

Note that you should be careful with the border thickness values. I didn’t add any checking to make sure the border isn’t overcrowding the actual progress bar. You could theoretically have a progress bar that is all border if you set the thicknesses too big.

Ok, so here’s a sample project. Hold space bar if you want to pause.

And here’s the bare ProgressBar.cs file (zipped, of course).

6 Responses to “Simple XNA Progress Bar”

  1. Cory F says:

    Haven’t looked at the code, but nicely done. I would have looked at this last week, but I made my own.. =) Nice job tho!

  2. DominicK says:

    Awesome! Works perfectly and easily. I'll pour through the code and see what I learn 😀 Thanks again!

  3. Nathan says:

    I have a coulpe questions about the code – I've used your ProgressBar and it works perfectly – I also used it as a template for a TextBox but some of the properties are non-responsive like setting BorderLineWidth.. its strange, if you email me I'll show you the code I having problems with.

  4. Tahsin says:

    Thank for this code, very helpfull.

  5. August says:

    I’m getting 12 errors when adding the class.
    XNA 4.0 visual 2010

    “The name TextureUsage does not exist in the current context”

    wat to dooo

  6. Jd_Logic says:

    I’m currently working on my first XNA based game and decided to find a progress bar. So I’ve been directed here by Google.

    For those that run into the problem that ‘August’ has replace all the
    “new Texture2D(Game.GameTime, 1, 1, 1, TextureUsage.Linear, SurfaceFormat.Color);”

    with

    “new Texture2D(Game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);”

    The reason why you are running into the TextureUsage error is because as of XNA 4.0(the version I’m currently using) is because this is no longer used.

    the code I posted above will resolve the issue.

Leave a Reply