Example #1
0
        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);
        }