コード例 #1
0
        public static Dictionary<char, CharacterData> Parse(string filePath)
        {
            Dictionary<char, CharacterData> charDictionary = new Dictionary<char, CharacterData>();

            string[] lines = File.ReadAllLines(filePath);

            for (int i = HeaderSize; i < lines.Length; i += 1)
            {
                string firstLine = lines[i];
                string[] typesAndValues = firstLine.Split(" ".ToCharArray(),
                    StringSplitOptions.RemoveEmptyEntries);

                // All the data comes in a certain order,
                // used to make the parser shorter
                CharacterData charData = new CharacterData
                {
                    Id = GetValue(typesAndValues[1]),
                    X = GetValue(typesAndValues[2]),
                    Y = GetValue(typesAndValues[3]),
                    Width = GetValue(typesAndValues[4]),
                    Height = GetValue(typesAndValues[5]),
                    XOffset = GetValue(typesAndValues[6]),
                    YOffset = GetValue(typesAndValues[7]),
                    XAdvance = GetValue(typesAndValues[8])
                };
                charDictionary.Add((char)charData.Id, charData);
            }
            return charDictionary;
        }
コード例 #2
0
        /// <summary>
        /// Create a font object from a .fnt file (and associated textures)
        /// </summary>
        /// <param name="path">Path to the .fnt file.</param>
        /// <param name="textureManager">Texture manager to load font textures</param>
        /// <returns>Font as described by the .fnt file.</returns>
        public static Font CreateFont(string path, TextureManager textureManager)
        {
            List<Texture> _texturePages = new List<Texture>();
            Dictionary<KernKey, int> kernDictionary = new Dictionary<KernKey, int>();
            Dictionary<char, CharacterData> charDictionary = new Dictionary<char, CharacterData>();

            string[] lines = File.ReadAllLines(path);

            int texturePageInfo = HeaderSize;
            while (lines[texturePageInfo].StartsWith("page"))
            {
                string line = lines[texturePageInfo];
                string[] typesAndValues = line.Split(" ".ToCharArray(),
                  StringSplitOptions.RemoveEmptyEntries);
                string textureString = GetTextValue(typesAndValues[2]).Trim('"');
                string textureId = Path.GetFileNameWithoutExtension(textureString);

                if (textureManager.Exists(textureId))
                {
                    // Really textures should never be loaded twice so it's worth warning the user
                    Console.Error.WriteLine("WARNING: Tried to load a texture that had been already been loaded. "
                        + "[" + textureString + "] when loading font [" + path + "]");
                }
                else
                {
                    // Assume texture files are in the same path as the .fnt file.
                    string directory = Path.GetDirectoryName(path);
                    if (string.IsNullOrEmpty(directory) == false)
                    {
                        directory += "\\";
                    }
                    textureManager.LoadTexture(textureId, directory  + textureString);   
                }

                _texturePages.Add(textureManager.Get(textureId));

                texturePageInfo++;
            }

            texturePageInfo++; // jump over number of characters data.

            for (int i = texturePageInfo; i < lines.Length; i += 1)
            {
                string line = lines[i];
                string[] typesAndValues = line.Split(" ".ToCharArray(),
                    StringSplitOptions.RemoveEmptyEntries);

                // Some fonts have kerning data at the end
                if (line.StartsWith("kernings"))
                {
                    ParseKernData(i + 1, lines, kernDictionary);
                    break;
                }

                // All the data comes in a certain order,
                // used to make the parser shorter
                CharacterData charData = new CharacterData
                {
                    Id = GetValue(typesAndValues[1]),
                    X = GetValue(typesAndValues[2]),
                    Y = GetValue(typesAndValues[3]),
                    Width = GetValue(typesAndValues[4]),
                    Height = GetValue(typesAndValues[5]),
                    XOffset = GetValue(typesAndValues[6]),
                    YOffset = GetValue(typesAndValues[7]),
                    XAdvance = GetValue(typesAndValues[8])
                };
                charDictionary.Add((char)charData.Id, charData);
            }

            return new Font(_texturePages.FirstOrDefault(), charDictionary, kernDictionary);
        }
コード例 #3
0
        /// <summary>
        /// Create a font object from a .fnt file (and associated textures)
        /// </summary>
        /// <param name="path">Path to the .fnt file.</param>
        /// <param name="textureManager">Texture manager to load font textures</param>
        /// <returns>Font as described by the .fnt file.</returns>
        public static Font CreateFont(string path, TextureManager textureManager)
        {
            List <Texture>                   _texturePages  = new List <Texture>();
            Dictionary <KernKey, int>        kernDictionary = new Dictionary <KernKey, int>();
            Dictionary <char, CharacterData> charDictionary = new Dictionary <char, CharacterData>();

            string[] lines = File.ReadAllLines(path);

            int texturePageInfo = HeaderSize;

            while (lines[texturePageInfo].StartsWith("page"))
            {
                string   line           = lines[texturePageInfo];
                string[] typesAndValues = line.Split(" ".ToCharArray(),
                                                     StringSplitOptions.RemoveEmptyEntries);
                string textureString = GetTextValue(typesAndValues[2]).Trim('"');
                string textureId     = Path.GetFileNameWithoutExtension(textureString);

                if (textureManager.Exists(textureId))
                {
                    // Really textures should never be loaded twice so it's worth warning the user
                    Console.Error.WriteLine("WARNING: Tried to load a texture that had been already been loaded. "
                                            + "[" + textureString + "] when loading font [" + path + "]");
                }
                else
                {
                    // Assume texture files are in the same path as the .fnt file.
                    string directory = Path.GetDirectoryName(path);
                    if (string.IsNullOrEmpty(directory) == false)
                    {
                        directory += "\\";
                    }
                    textureManager.LoadTexture(textureId, directory + textureString);
                }

                _texturePages.Add(textureManager.Get(textureId));

                texturePageInfo++;
            }

            texturePageInfo++; // jump over number of characters data.

            for (int i = texturePageInfo; i < lines.Length; i += 1)
            {
                string   line           = lines[i];
                string[] typesAndValues = line.Split(" ".ToCharArray(),
                                                     StringSplitOptions.RemoveEmptyEntries);

                // Some fonts have kerning data at the end
                if (line.StartsWith("kernings"))
                {
                    ParseKernData(i + 1, lines, kernDictionary);
                    break;
                }

                // All the data comes in a certain order,
                // used to make the parser shorter
                CharacterData charData = new CharacterData
                {
                    Id       = GetValue(typesAndValues[1]),
                    X        = GetValue(typesAndValues[2]),
                    Y        = GetValue(typesAndValues[3]),
                    Width    = GetValue(typesAndValues[4]),
                    Height   = GetValue(typesAndValues[5]),
                    XOffset  = GetValue(typesAndValues[6]),
                    YOffset  = GetValue(typesAndValues[7]),
                    XAdvance = GetValue(typesAndValues[8])
                };
                charDictionary.Add((char)charData.Id, charData);
            }

            return(new Font(_texturePages.FirstOrDefault(), charDictionary, kernDictionary));
        }
コード例 #4
0
 public CharacterSprite(Sprite sprite, CharacterData data)
 {
     Data = data;
     Sprite = sprite;
 }
コード例 #5
0
 public CharacterSprite(CharacterData data, Sprite sprite)
 {
     Data   = data;
     Sprite = sprite;
 }