123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using SlimDX;
- using SlimDX.D3DCompiler;
- using SlimDX.Direct3D11;
- using SlimDX.DXGI;
- using SlimDX.Windows;
- using Device = SlimDX.Direct3D11.Device;
- using Resource = SlimDX.Direct3D11.Resource;
- using Buffer = SlimDX.Direct3D11.Buffer;
- namespace EQ2ModelViewer
- {
- public class FontClass
- {
- private struct FontType
- {
- public float left;
- public float right;
- public int size;
- }
- private FontType[] m_font;
- private TextureClass m_texture;
- public bool Initialize(Device device, string fontFile, string textureFile)
- {
- if (!LoadFontData(fontFile))
- return false;
- if (!LoadTexture(device, textureFile))
- return false;
- return true;
- }
- public void ShutDown()
- {
- ReleaseTexture();
- }
- public ShaderResourceView GetTexture()
- {
- return m_texture.GetTexture();
- }
- public void BuildVertexArray(string sentence, float drawX, float drawY, ref TextClass.VertexType[] vertices)
- {
- int numLetters;
- int index;
- int i;
- int letter;
- numLetters = sentence.Length;
- index = 0;
- for (i = 0; i < numLetters; i++)
- {
- letter = ((int)sentence[i]) - 32;
- if (letter == 0)
- {
- drawX += 3.0f;
- }
- else
- {
- // First Triangle
- // Top Left
- vertices[index].position = new Vector3(drawX, drawY, 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].left, 0.0f);
- index++;
- // Bottom Right
- vertices[index].position = new Vector3(drawX + m_font[letter].size, (drawY - 16), 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].right, 1.0f);
- index++;
- // Bottom Left
- vertices[index].position = new Vector3(drawX, (drawY - 16), 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].left, 1.0f);
- index++;
- // Second Triangle
- // Top Left
- vertices[index].position = new Vector3(drawX, drawY, 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].left, 0.0f);
- index++;
- // Top Right
- vertices[index].position = new Vector3(drawX + m_font[letter].size, drawY, 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].right, 0.0f);
- index++;
- // Bottom right
- vertices[index].position = new Vector3((drawX + m_font[letter].size), (drawY - 16), 0.0f);
- vertices[index].texture = new Vector2(m_font[letter].right, 1.0f);
- index++;
- // Update the x location for drawing by the size of the letter and one pixel
- drawX += m_font[letter].size + 1.0f;
- }
- }
- }
- private bool LoadFontData(string fontFile)
- {
- string line;
- byte i = 0;
- m_font = new FontType[95];
- StreamReader reader = new StreamReader(File.Open(fontFile, FileMode.Open));
- while (i < 95)
- {
- line = reader.ReadLine();
- string[] data = line.Split(' ');
- /*string line2 = "";
- for (int z = 0; z < data.Length; z++)
- line2 += data[z] + "\n";
- MessageBox.Show(line2);*/
- int y = 2;
- if (data[y] == "")
- y++;
- m_font[i].left = float.Parse(data[y]);
- y++;
- while (data[y] == "")
- y++;
- m_font[i].right = float.Parse(data[y]);
- y++;
- while (data[y] == "")
- y++;
- m_font[i].size = int.Parse(data[y]);
- i++;
- }
- reader.Close();
- return true;
- }
- private bool LoadTexture(Device device, string textureFile)
- {
- m_texture = new TextureClass();
- m_texture.Initialize(device, textureFile);
- return true;
- }
- private void ReleaseTexture()
- {
- if (m_texture != null)
- m_texture.ShutDown();
- }
- }
- }
|