What is Snowball and what do I want to do with it?

October 16, 2011 2d, C#, CodePlex, Open Source, SlimDX, Xna

I originally got the idea for Snowball after working with the Xna Framework. The Xna Framework is a good piece of software for what it is but there are some things about which I just do not agree with:

  • The content pipeline only works with content in the serialized .xnb format.
  • There are certain content types which can only be loaded via the content pipeline.
  • Certain features don't exist on the PC because they don't exist on the XBox or Windows Phone 7.

Xna was designed as an abstraction layer for all the 3 platforms mentioned in the last point, so that one is somewhat understandable. I don't want to write games for my XBox right now though, so why should things like drawing lines not be available to me?

With these points in mind I started working on Snowball. It's designed to be an Xna like framework for making 2D games. It uses SlimDX on the backend, but that is completely abstracted away from consumers of the framework. What I want to do is design the API so that the backend can be swapped out somewhat painlessly.

I still have a ways to go before I will consider it a version 1.0 release. As of this writing, I'm transitioning to more of a ContentLoader class style for loading your game's content. Any resource type from within the framework can be loaded by hand if you want, the ContentLoader class will just make it easier. After that I have a few other features like GamePad and Music which I would like to implement before saying I have a Beta type release.

The future after that is up in the air. I would love to try and have different implementations of the API for Xna and/or OpenTK.

I recommend for anyone who is interested as to why an API designer choose to implement the API in the way they did to try it for themselves. I have learned many things from this project including why certain design decisions were made by the Xna Framework team.

500 Downloads of the Same Game

June 09, 2010 .NET, CodePlex, Same Game Xna, WinForms, Xna

My little Xna game that I wrote nearly 2 years ago reached the 500 downloads mark (binaries and source) the other day. With that said, I'd like to say that I'm working on version 2.0.

In version 2.0 I'm going to make the code more event driven. The old code uses the Xna Game class and in the new version I'll be making it WinForms based. Almost a complete rewrite.

My work so far is available through SVN on the project's Codeplex page.

Drawing Rectangles with SpriteBatch

March 29, 2010 C#, Code Snippets, Xna

Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:

public static class SpriteBatchHelper { static Texture2D pixel; private static void LoadPixel(GraphicsDevice graphicsDevice) { if(pixel == null) { pixel = new Texture2D(graphicsDevice, 1, 1); pixel.SetData<Color>(new Color[] { Color.White }); } } public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color) { LoadPixel(spriteBatch.GraphicsDevice); spriteBatch.Draw(pixel, rectangle, color); } }

Calculating an angle from a Vector2

February 28, 2009 .net, vectors, xna

When you need to calculate an angle from a Vector2 structure, you can use this piece of code:

public static class Vector2Helper { public static float CalculateAngle(Vector2 v) { float angle = 0.0f; if(v != Vector2.Zero) { v.Normalize(); angle = (float)Math.Acos(v.Y); if(v.X < 0.0f) angle = -angle; } return angle; } }

I used this to calculate an angle from the Vector2 of the Left Stick.

The original credit for this source code comes from here.

SpriteSheet Class

January 09, 2009 .NET, SpriteSheet, XNA

I've been talking with a guy on the creator forums lately about SpriteSheets and so I decided it might be a good idea to post my SpriteSheet class.

It's very simple. Only reads sprites from left to right and assumes all Sprites are the same width and height.

#region Usingusing System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #endregion namespace Snow.Xna.Graphics { /// Spritesheet class. /// public class SpriteSheet { #region Fields string name; Texture2D texture; Rectangle[] rectangles; int spriteWidth, spriteHeight; #endregion #region Properties /// The name of this SpriteSheet. /// public string Name { get { return name; } } /// The texture for this SpriteSheet. /// public Texture2D Texture { get { return texture; } } /// Returns a rectangle for a sprite in the SpriteSheet. /// /// index /// public Rectangle this[int i] { get { return rectangles[i]; } } /// The number of sprites in this SpriteSheet. /// public int Count { get { return rectangles.Length; } } /// The width of the texture. /// public int Width { get { return texture.Width; } } /// The width of each sprite in the SpriteSheet. /// public int SpriteWidth { get { return spriteWidth; } } /// The height of the texture. /// public int Height { get { return texture.Height; } } /// The height of each sprite in the SpriteSheet. /// public int SpriteHeight { get { return spriteHeight; } } #endregion /// Create a new SpriteSheet and determine the number of sprites in the sheet. /// /// /// Width of each sprite. /// Height of each sprite. public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight) : this(name, texture, spriteWidth, spriteHeight, 0) { } /// Create a new SpriteSheet. /// /// /// Width of each sprite. /// Height of each sprite. /// The number of sprites in the sheet. public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight, int count) { this.name = name; this.texture = texture; this.spriteWidth = spriteWidth; this.spriteHeight = spriteHeight; if(count == 0) { int numX = texture.Width / spriteWidth; int numY = texture.Height / spriteHeight; rectangles = new Rectangle[numX * numY]; } else { rectangles = new Rectangle[count]; } int x = 0, y = 0; for(int i = 0; i < rectangles.Length; i++) { rectangles[i] = new Rectangle(x, y, spriteWidth, spriteHeight); x += spriteWidth; if(x >= texture.Width) { x = 0; y += spriteHeight; } } } public static implicit operator Texture2D(SpriteSheet spriteSheet) { return spriteSheet.Texture; } } }

You can create a new SpriteSheet and use it like this:

`

SpriteSheet spriteSheet = new SpriteSheet("tiles", Content.Load<texture2D>("tiles"), 64, 64); spriteBatch.Begin(); spriteBatch.Draw(spriteSheet, new Rectangle(0, 0, spriteSheet.SpriteWidth, spriteSheet.SpriteHeight), spriteSheet[0], Color.White); spriteBatch.End();

`

Which loads a spritesheet with sprites of size 64x64. It then draws the first Sprite in the SpriteSheet. You of course wouldn't want to load the spritesheet every frame as well.

Feel free to use this code without restriction.

Edit: I copied and pasted the second piece of code from somewhere else so I fixed two typos.

Colors and Hex

December 21, 2008 .NET, XNA

I recently needed to write out Color(s) as an xml attribute. I wrote 2 methods to read and write the Color(s) as Hex strings. Here ya go:

Read More