Three Slices of Useful Code


While it’s the big problems in game programming that get all the press, there are plenty of small problems that show up day-to-day. Most of these problems can be solved by an equally small bit of code, it’s just a matter of figuring out what that code should be.

With that in mind, I’d like to share a few bits of utility code that I find invaluable in my daily game making.

Word-Wrap
It’s only a matter of time before you have to take a string and inject line breaks in order to fit it into a rectangular window. Sure, you could insert line-breaks by hand, but why do that when you have a function like this ready to be used:

        public static String WrapString(SpriteFont measureFont, String sourceString, float wrapWidth)
        {
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = sourceString.Split(' ');

            foreach (String word in wordArray)
            {
                if (measureFont.MeasureString(line + word).Length() > wrapWidth)
                {
                    returnString += line + "\n";
                    line = String.Empty;
                }
                line += word + ' ';
            }
            returnString += line;
            return returnString;
        }

Rotating Vector2s
I do a lot of 2D games and I constantly find myself needing to figure out the angle of a 2D vector or transform a 2D vector based on some angle. At times like those, this couple of functions come in real handy:

        public static float Vector2ToRadians(Vector2 inVector)
        {
            return (float)Math.Atan2(inVector.Y, inVector.X);
        }

        public static Vector2 RotateVector2(Vector2 sourceVector, float radians)
        {
            float cosRad = (float)Math.Cos(radians);
            float sinRad = (float)Math.Sin(radians);
                
            return new Vector2(cosRad * sourceVector.X - sinRad * sourceVector.Y,
                            sinRad * sourceVector.X + cosRad * sourceVector.Y);
        }

Iterating Through Enums
Using .Net, it’s usually really easy to iterate through Enums using Enum.GetValues(). Unfortunately, that function doesn’t exist on the Xbox 360. That’s where this function suggested on the XBLIG Community Forums, comes to the rescue:

        public static Enum[] GetValues(Type enumType)
        {
            if (enumType.BaseType == typeof(Enum))
            {
                FieldInfo[] info = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
                Enum[] values = new Enum[info.Length];
                for (int i = 0; i < values.Length; ++i)
                {
                    values[i] = (Enum)info[i].GetValue(null);
                }
                return values;
            }
            else
            {
                throw new Exception("Given type is not an Enum type");
            }
        }

There you go. Three little bits of code that should help to make your game development life three little bits easier.

Share this Article:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Print