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); }
// Methods public bool Initialize(SharpDX.Direct3D11.Device device, DeviceContext deviceContext, IntPtr windowHandle, int screanWidth, int screenHeight, Matrix baseViewMatrix) { // Store the screen width and height. ScreenWidth = screanWidth; ScreenHeight = screenHeight; // Store the base view matrix. BaseViewMatrix = baseViewMatrix; // Create the font object. Font = new DFont(); // Initialize the font object. if (!Font.Initialize(device, "fontdata.txt", "font.bmp")) { return(false); } // Initialize the first sentence. if (!InitializeSentence(out sentences[0], 150, device)) { return(false); } // Initialize the second sentence. if (!InitializeSentence(out sentences[1], 32, device)) { return(false); } // Initialize the third sentence. if (!InitializeSentence(out sentences[2], 150, device)) { return(false); } // Initialize the fourth sentence. if (!InitializeSentence(out sentences[3], 150, device)) { return(false); } // Initialize the Fifth sentence. if (!InitializeSentence(out sentences[4], 150, device)) { return(false); } // Initialize the Sixth sentence. if (!InitializeSentence(out sentences[5], 150, device)) { return(false); } return(true); }
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); }
// Methods public bool Initialize(SharpDX.Direct3D11.Device device, DeviceContext deviceContext, IntPtr windowHandle, int screanWidth, int screenHeight, Matrix baseViewMatrix) { // Store the screen width and height. ScreenWidth = screanWidth; ScreenHeight = screenHeight; // Store the base view matrix. BaseViewMatrix = baseViewMatrix; // Create the font object. Font = new DFont(); // Initialize the font object. if (!Font.Initialize(device, "fontdata.txt", "font.bmp")) { return(false); } // Create the font shader object. FontShader = new DFontShader(); // Initialize the font shader object. if (!FontShader.Initialize(device, windowHandle)) { return(false); } // Initialize the first sentence. if (!InitializeSentence(out sentences[0], 16, device)) { return(false); } // Now update the sentence vertex buffer with the new string information. if (!UpdateSentece(ref sentences[0], "FPS: ", 20, 20, 1, 1, 1, deviceContext)) { return(false); } // Initialize the second sentence. if (!InitializeSentence(out sentences[1], 16, device)) { return(false); } // Now update the sentence vertex buffer with the new string information. if (!UpdateSentece(ref sentences[1], "CPU: ", 20, 40, 1, 1, 0, deviceContext)) { return(false); } return(true); }
public void Shutdown() { // Release all sentances however many there may be. foreach (DSentence sentance in sentences) { ReleaseSentences(sentance); } sentences = null; // Release the font object. Font?.Shutdown(); Font = null; }
// Methods public bool Initialize(SharpDX.Direct3D11.Device device, DFont font, DSystemConfiguration configuration, int maxLength, string text, int positionX, int positionY, float red, float green, float blue, bool shadowed, SharpDX.Direct3D11.DeviceContext deviceContext) { // Store the screen width and height. ScreenWidth = configuration.Width; ScreenHeight = configuration.Height; // Store the maximum length of the sentence. MaxLength = maxLength; // Initalize the sentence. if (!InitializeSentence(device, font, text, positionX, positionY, red, green, blue, deviceContext)) { return(false); } return(true); }