private bool RenderSentence(DeviceContext deviceContext, DSentence sentence, Matrix worldMatrix, Matrix orthoMatrix) { // Set vertex buffer stride and offset. var stride = Utilities.SizeOf <DTextClass.DVertex>(); var offset = 0; // Set the vertex buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(sentence.VertexBuffer, stride, offset)); // Set the index buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetIndexBuffer(sentence.IndexBuffer, Format.R32_UInt, 0); // Set the type of the primitive that should be rendered from this vertex buffer, in this case triangles. deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; // Create a pixel color vector with the input sentence color. var pixelColor = new Vector4(sentence.red, sentence.green, sentence.blue, 1); // Render the text using the font shader. if (!FontShader.Render(deviceContext, sentence.IndexCount, worldMatrix, BaseViewMatrix, orthoMatrix, Font.Texture.TextureResource, pixelColor)) { return(false); } return(true); }
private void ReleaseSentences(DSentence sentence) { // Release the sentence vertex buffer. sentence.VertexBuffer?.Dispose(); sentence.VertexBuffer = null; // Release the sentence index buffer. sentence.IndexBuffer?.Dispose(); sentence.IndexBuffer = null; }
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); }
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); }