FontClass.cs 4.7 KB

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