FontClass.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using SlimDX;
  7. using SlimDX.D3DCompiler;
  8. using SlimDX.Direct3D11;
  9. using SlimDX.DXGI;
  10. using SlimDX.Windows;
  11. using Device = SlimDX.Direct3D11.Device;
  12. using Resource = SlimDX.Direct3D11.Resource;
  13. using Buffer = SlimDX.Direct3D11.Buffer;
  14. namespace EQ2ModelViewer
  15. {
  16. public class FontClass
  17. {
  18. private struct FontType
  19. {
  20. public float left;
  21. public float right;
  22. public int size;
  23. }
  24. private FontType[] m_font;
  25. private TextureClass m_texture;
  26. public bool Initialize(Device device, string fontFile, string textureFile)
  27. {
  28. if (!LoadFontData(fontFile))
  29. return false;
  30. if (!LoadTexture(device, textureFile))
  31. return false;
  32. return true;
  33. }
  34. public void ShutDown()
  35. {
  36. ReleaseTexture();
  37. }
  38. public ShaderResourceView GetTexture()
  39. {
  40. return m_texture.GetTexture();
  41. }
  42. public void BuildVertexArray(string sentence, float drawX, float drawY, ref TextClass.VertexType[] vertices)
  43. {
  44. int numLetters;
  45. int index;
  46. int i;
  47. int letter;
  48. numLetters = sentence.Length;
  49. index = 0;
  50. for (i = 0; i < numLetters; i++)
  51. {
  52. letter = ((int)sentence[i]) - 32;
  53. if (letter == 0)
  54. {
  55. drawX += 3.0f;
  56. }
  57. else
  58. {
  59. // First Triangle
  60. // Top Left
  61. vertices[index].position = new Vector3(drawX, drawY, 0.0f);
  62. vertices[index].texture = new Vector2(m_font[letter].left, 0.0f);
  63. index++;
  64. // Bottom Right
  65. vertices[index].position = new Vector3(drawX + m_font[letter].size, (drawY - 16), 0.0f);
  66. vertices[index].texture = new Vector2(m_font[letter].right, 1.0f);
  67. index++;
  68. // Bottom Left
  69. vertices[index].position = new Vector3(drawX, (drawY - 16), 0.0f);
  70. vertices[index].texture = new Vector2(m_font[letter].left, 1.0f);
  71. index++;
  72. // Second Triangle
  73. // Top Left
  74. vertices[index].position = new Vector3(drawX, drawY, 0.0f);
  75. vertices[index].texture = new Vector2(m_font[letter].left, 0.0f);
  76. index++;
  77. // Top Right
  78. vertices[index].position = new Vector3(drawX + m_font[letter].size, drawY, 0.0f);
  79. vertices[index].texture = new Vector2(m_font[letter].right, 0.0f);
  80. index++;
  81. // Bottom right
  82. vertices[index].position = new Vector3((drawX + m_font[letter].size), (drawY - 16), 0.0f);
  83. vertices[index].texture = new Vector2(m_font[letter].right, 1.0f);
  84. index++;
  85. // Update the x location for drawing by the size of the letter and one pixel
  86. drawX += m_font[letter].size + 1.0f;
  87. }
  88. }
  89. }
  90. private bool LoadFontData(string fontFile)
  91. {
  92. string line;
  93. byte i = 0;
  94. m_font = new FontType[95];
  95. StreamReader reader = new StreamReader(File.Open(fontFile, FileMode.Open));
  96. Regex trimmer = new Regex(@"([0-9]+)\s.{1}\s([0-9\.]+)\s+([0-9\.]+)\s+([0-9\.]+)");
  97. while (i < 95)
  98. {
  99. line = reader.ReadLine();
  100. Match out_ = trimmer.Match(line);
  101. if (!out_.Success)
  102. continue;
  103. m_font[i].left = float.Parse(out_.Groups[2].Value);
  104. m_font[i].right = float.Parse(out_.Groups[3].Value);
  105. m_font[i].size = int.Parse(out_.Groups[4].Value);
  106. i++;
  107. }
  108. reader.Close();
  109. return true;
  110. }
  111. private bool LoadTexture(Device device, string textureFile)
  112. {
  113. m_texture = new TextureClass();
  114. m_texture.Initialize(device, textureFile);
  115. return true;
  116. }
  117. private void ReleaseTexture()
  118. {
  119. if (m_texture != null)
  120. m_texture.ShutDown();
  121. }
  122. }
  123. }