public Text(Device device, FontShader shader, int screenWidth, int screenHeight, Font font, Int32 maxLength, Vector4 color) { Font = font; MaxLength = maxLength; Color = color; _shader = shader; _screenHeight = screenHeight; _screenWidth = screenWidth; // The index buffer is static and do not change when the text changes UInt32[] indices = new UInt32[maxLength * 6]; // 6 indices per character for (UInt32 i = 0; i < maxLength; i++) { indices[i * 6] = i * 4; indices[i * 6 + 1] = i * 4 + 1; indices[i * 6 + 2] = i * 4 + 2; indices[i * 6 + 3] = i * 4; indices[i * 6 + 4] = i * 4 + 3; indices[i * 6 + 5] = i * 4 + 1; } IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices); // The vertex buffer is initialized empty _vertices = new FontShader.Vertex[maxLength * 4]; // 4 vertices per character var vertexBufferDesc = new BufferDescription { Usage = ResourceUsage.Dynamic, SizeInBytes = Utilities.SizeOf <FontShader.Vertex>() * maxLength * 4, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }; // Create the vertex buffer. VertexBuffer = Buffer.Create(device, _vertices, vertexBufferDesc); }
public void BuildVertexArray(String text, int positionX, int positionY, ref FontShader.Vertex[] vertices, out int width, out int height) { width = 0; height = Characters.First().Value.height; for (int i = 0; i < text.Length; i++) { Character c = Characters[text[i]]; vertices[i * 4] = new FontShader.Vertex { position = new Vector3(positionX, positionY, 0.0f), texture = new Vector2(c.uLeft, c.vTop) }; //Top left vertices[i * 4 + 1] = new FontShader.Vertex { position = new Vector3(positionX + c.width, positionY - c.height, 0.0f), texture = new Vector2(c.uRight, c.vBottom) }; //Right bottom vertices[i * 4 + 2] = new FontShader.Vertex { position = new Vector3(positionX, positionY - c.height, 0.0f), texture = new Vector2(c.uLeft, c.vBottom) }; //Left bottom vertices[i * 4 + 3] = new FontShader.Vertex { position = new Vector3(positionX + c.width, positionY, 0.0f), texture = new Vector2(c.uRight, c.vTop) }; //Top right positionX += c.width + 1; width += c.width + 1; } }