private bool InitializeSentence(out Sentence sentence, int maxLength, Device device) { // Create a new sentence object. sentence = new Sentence(); // Initialize the sentence buffers to null; sentence.VertexBuffer = null; sentence.IndexBuffer = null; // Set the maximum length of the sentence. sentence.MaxLength = maxLength; // Set the number of vertices in vertex array. sentence.VertexCount = 6 * maxLength; // Set the number of vertices in the vertex array. sentence.IndexCount = sentence.VertexCount; // Create the vertex array. var vertices = new TextureShader.Vertex[sentence.VertexCount]; // Create the index array. var indices = new int[sentence.IndexCount]; // Initialize vertex array to zeros at first. foreach (var vertex in vertices) vertex.SetToZero(); // Initialize the index array. for (var i = 0; i < sentence.IndexCount; i++) indices[i] = i; // Set up the description of the dynamic vertex buffer. var vertexBufferDesc = new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = Utilities.SizeOf<TextureShader.Vertex>() * sentence.VertexCount, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }; // Create the vertex buffer. sentence.VertexBuffer = Buffer.Create(device, vertices, vertexBufferDesc); // Create the index buffer. sentence.IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices); return true; }
private bool InitializeBuffers(Device device) { try { // Set the number of the vertices and indices in the vertex and index array, accordingly. VertexCount = 6; IndexCount = 6; // Create the vertex array. var vertices = new TextureShader.Vertex[VertexCount]; // Initialize vertex array to zeroes at first. foreach (var vertex in vertices) vertex.SetToZero(); // Create the index array. var indices = new int[IndexCount]; // Load the index array with data. for (var i = 0; i < IndexCount; i++) indices[i] = i; // Set up the description of the static vertex buffer. var vertexBufferDesc = new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = Utilities.SizeOf<TextureShader.Vertex>() * VertexCount, BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }; // Create the vertex buffer. VertexBuffer = Buffer.Create(device, vertices, vertexBufferDesc); // Create the index buffer. IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices); return true; } catch { return false; } }
private bool UpdateSentence(ref Sentence sentence, string text, int positionX, int positionY, float red, float green, float blue, DeviceContext deviceContext) { // Store the color of the sentence. sentence.red = red; sentence.green = green; sentence.blue = blue; // Get the number of the letter in the sentence. var numLetters = sentence.MaxLength; // Check for possible buffer overflow. if (numLetters > sentence.MaxLength) return false; //// Adjust text to show on the screen. //for (var i = 0; i < sentence.MaxLength - 1; i++) // text += " "; //text = text.Substring(0, sentence.MaxLength); // Calculate the X and Y pixel position on screen to start drawing to. var drawX = -(ScreenWidth >> 1) + positionX; var drawY = (ScreenHeight >> 1) - positionY; // Use the font class to build the vertex array from the sentence text and sentence draw location. List<TextureShader.Vertex> vertices; Font.BuildVertexArray(out vertices, text, drawX, drawY); // Empty all remaining vertices for (int i = text.Length; i < sentence.MaxLength; i++) { var emptyVertex = new TextureShader.Vertex(); emptyVertex.SetToZero(); vertices.Add(emptyVertex); } DataStream mappedResource; #region Vertex Buffer // Lock the vertex buffer so it can be written to. deviceContext.MapSubresource(sentence.VertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the data into the vertex buffer. mappedResource.WriteRange<TextureShader.Vertex>(vertices.ToArray()); // Unlock the vertex buffer. deviceContext.UnmapSubresource(sentence.VertexBuffer, 0); #endregion return true; }