public bool UpdateSentence2(DFont font, string text, int positionX, int positionY, float red, float green, float blue, DeviceContext deviceContext)
        {
            // Store the color of the sentence.
            PixelColour = new Vector4(red, green, blue, 1.0f);

            // Get the number of letters in the sentence.
            var numLetters = text.Length;

            // Check for possible buffer overflow.
            if (numLetters > MaxLength)
            {
                return(false);
            }

            // 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 <DFont.DVertexType> vertices;

            font.BuildVertexArray(out vertices, text, drawX, drawY);

            // Empty all remaining vertices
            for (int i = text.Length; i < MaxLength; i++)
            {
                var emptyVertex = new DFont.DVertexType();
                emptyVertex.position = Vector3.Zero;
                emptyVertex.texture  = Vector2.Zero;
                vertices.Add(emptyVertex);
            }

            DataStream mappedResource;

            #region Vertex Buffer
            // Lock the vertex buffer so it can be written to.
            deviceContext.MapSubresource(VertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the data into the vertex buffer.
            mappedResource.WriteRange <DFont.DVertexType>(vertices.ToArray());

            // Unlock the vertex buffer.
            deviceContext.UnmapSubresource(VertexBuffer, 0);
            #endregion

            // If shadowed then do the same for the second vertex buffer but offset by one pixel.
            if (Shadowed)
            {
                // Perform same mapping and writings code as above except for the shadows offset position for the effect.
            }

            vertices?.Clear();
            vertices = null;

            return(true);
        }
Beispiel #2
0
        private bool InitializeSentence(SharpDX.Direct3D11.Device device, DFont font, string text, int positionX, int positionY, float red, float green, float blue, SharpDX.Direct3D11.DeviceContext deviceContext)
        {
            // Set the vertex and index count.
            VertexCount = 6 * MaxLength;
            IndexCount  = 6 * MaxLength;

            // Create the vertex array.
            DFont.DVertexType[] vertices = new DFont.DVertexType[VertexCount];
            // Create the index array.
            int[] indices = new int[IndexCount];

            //// Initialize vertex array to zeros at first.
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].position = Vector3.Zero;
                vertices[i].texture  = Vector2.Zero;
            }

            // Initialize the index array.
            for (var i = 0; i < IndexCount; i++)
            {
                indices[i] = i;
            }

            // Set up the description of the dynamic vertex buffer.
            var vertexBufferDesc = new BufferDescription()
            {
                Usage               = ResourceUsage.Dynamic,
                SizeInBytes         = Utilities.SizeOf <DFont.DVertexType>() * VertexCount,
                BindFlags           = BindFlags.VertexBuffer,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.None,
                StructureByteStride = 0
            };

            // Create the vertex buffer.
            VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, vertices, vertexBufferDesc);

            // Create the index buffer.
            IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);

            // Release the vertex array as it is no longer needed.
            // Release the index array as it is no longer needed.
            vertices = null;
            indices  = null;

            // Now add the text data to the sentence buffers.
            if (!UpdateSentence2(font, text, positionX, positionY, red, green, blue, deviceContext))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private bool InitializeSentence(out DSentence sentence, int maxLength, int yPosition, SharpDX.Direct3D11.Device device)
        {
            // Create a new sentence object.
            sentence      = new DSentence();
            sentence.yPos = yPosition;

            // 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 DFont.DVertexType[sentence.VertexCount];

            // Create the index array.
            var indices = new int[sentence.IndexCount];

            // 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 <DFont.DVertexType>() * sentence.VertexCount,
                BindFlags           = BindFlags.VertexBuffer,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.None,
                StructureByteStride = 0
            };

            // Create the vertex buffer.
            sentence.VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, vertices, vertexBufferDesc);

            // Create the index buffer.
            sentence.IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);

            vertices = null;
            indices  = null;

            return(true);
        }
Beispiel #4
0
        private bool UpdateSentence2(ref DSentence 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);
            }

            // 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 <DFont.DVertexType> vertices;

            Font.BuildVertexArray(out vertices, text, drawX, drawY);

            // Empty all remaining vertices
            for (int i = text.Length; i < sentence.MaxLength; i++)
            {
                var emptyVertex = new DFont.DVertexType();
                emptyVertex.position = Vector3.Zero;
                emptyVertex.texture  = Vector2.Zero;
                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 <DFont.DVertexType>(vertices.ToArray());

            // Unlock the vertex buffer.
            deviceContext.UnmapSubresource(sentence.VertexBuffer, 0);
            #endregion

            vertices?.Clear();
            vertices = null;

            return(true);
        }