1
\$\begingroup\$

Simple static class that will write out a message that is passed in. The SpriteSheet is 256x256 and the A-Z starts at line 240 and the 0-9 starts at 248. Each character is 8x8. I hate the if (ix >= 32) and wondered if there is a tidier way of doing this? I'm pleased it's only one Draw call, but it looks ugly.

public static class Font
{
    private static String chars = "" + //
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ      " + //
        "0123456789.,!?'\"-+=/\\%()<>:;     " + //
        "";

    public static void Draw(string msg, SpriteSheet sprites, SpriteBatch spriteBatch, int x, int y, Color color)
    {
        msg = msg.ToUpper();
        for (int i = 0; i < msg.Length; i++)
        {
            int ix = chars.IndexOf(msg[i]);
            int w = 0, h = 240;
            if (ix >= 0)
            {
                if (ix >= 32)
                {
                    w = 32;
                    h = 248;
                }
                spriteBatch.Draw(sprites.Sheet, new Rectangle(x + i * 8, y, 8, 8), new Rectangle((ix - w) * 8, h, 8, 8), color);
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Do the sprites (SpriteSheet) ever change? \$\endgroup\$ – ANeves Feb 3 '12 at 18:11
  • \$\begingroup\$ No - they spritesheet stays the same. \$\endgroup\$ – Neil Knight Feb 4 '12 at 8:50
1
\$\begingroup\$

It is hard to suggest improvements because the variable names are not very meaningful and, thus, it is hard to understand what they are for and which is the intention behind the code.
Lesson to learn: using meaningful variable names is important.

Here is an attempt.
"Opening brackets not taking a new line" is a matter of style, disregard it.

Could you address the code comments I left?

public static class Font
{
    private const string chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ      " + // I assume the spaces are padding to 32 chars.
        "0123456789.,!?'\"-+=/\\%()<>:;     " // 35chars?!? What is the use of the 5 spaces?
        ;
    // Since the sprites never change, you can have them as a static property.
    private static SpriteSheet _Sprites = ...;

    public static void Draw(string msg, SpriteBatch spriteBatch, int xOffset, int yOffset, Color color) {
        msg = msg.ToUpper();
        for (int x = 0; x < msg.Length; x++) {
            int spriteX = chars.IndexOf(msg[x]);
            if (spriteX < 0) {
                continue; // Skipping char.
            }
            int spriteY = 240;
            if (spriteX >= 32) {
                // I really do not understand the justification of this...
                // Drawing a 1 is supposed to get a B, right? Strange.
                spriteX -= 32;
                spriteY = 248;
            }
            Rectangle charMask = new Rectangle(xOffset + x * 8, yOffset, 8, 8);
            Rectangle spriteMask = new Rectangle(spriteX * 8, spriteY, 8, 8);
            // XNA method: http://msdn.microsoft.com/en-us/library/ff433987.aspx
            spriteBatch.Draw(Sprites.Sheet, charMask, spriteMask, color);
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.