I am using sharpdx and trying to get to work with directwrite.
I initialize it and try to draw text. Everything must be ok, but there is no text at screen at all.
Here is code of initialization and using:
var _device = (SharpDX.Direct3D11.Device)GraphicsDevice;
SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(_device.QueryInterface<SharpDX.DXGI.Device2>());
d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations);
// Specify the properties for the bitmap that we will use as the target of our Direct2D operations.
// We want a 32-bit BGRA surface with premultiplied alpha.
BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw);
// Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target.
//Surface backBuffer = swapChain.GetBackBuffer<Surface>(0);
d2dTarget = new Bitmap1(d2dContext, GraphicsDevice.BackBuffer, properties);
fontFactory = new SharpDX.DirectWrite.Factory();
textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);
textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
textBrush = new SolidColorBrush(d2dContext, Color.Black);
backgroundBrush = new SolidColorBrush(d2dContext, Color.White);
d2dContext.Target = d2dTarget;
And here is drawing code:
protected override void Draw(GameTime gameTime)
{
//Debug.WriteLine("Draw");
if (_modelLoaded)
{
// Clears the screen with the Color.CornflowerBlue
GraphicsDevice.Clear(Color.CornflowerBlue);
// Setup the vertices
GraphicsDevice.SetVertexBuffer(vertices);
GraphicsDevice.SetIndexBuffer(indices, true);
GraphicsDevice.SetVertexInputLayout(inputLayout);
// Apply the basic effect technique and draw the rotating cube
basicEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.SetRasterizerState(currentRasterizerState);
//GraphicsDevice.Draw(PrimitiveType.TriangleList, vertices.ElementCount);
GraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, indices.ElementCount);
// Clear the target.
d2dContext.BeginDraw();
//d2dContext.Clear(Color.CornflowerBlue);
d2dContext.DrawText("????????", textFormat, new RectangleF(0, 0, 400, 200), textBrush);
// Draw a block of text that will be clipped against the specified layout rectangle.
d2dContext.FillRectangle(new RectangleF(50, 50, 200, 200), backgroundBrush);
d2dContext.DrawText("This text is long enough to overflow the designed region but will be clipped to the containing rectangle. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ", textFormat, new RectangleF(50, 50, 200, 200), textBrush, DrawTextOptions.Clip);
// Draw a block of text that will overflow the specified layout rectangle.
d2dContext.FillRectangle(new RectangleF(50, 300, 200, 200), backgroundBrush);
d2dContext.DrawText("However, this other text isn't going to be clipped: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean gravida dui id accumsan dictum.", textFormat, new RectangleF(50, 300, 200, 200), textBrush, DrawTextOptions.None);
// Draw three lines of text with different measuring modes.
d2dContext.FillRectangle(new RectangleF(300, 50, 400, 200), backgroundBrush);
d2dContext.DrawText("MeasuringMode: Natural", textFormat, new RectangleF(300, 50, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.Natural);
d2dContext.DrawText("MeasuringMode: GDI classic", textFormat, new RectangleF(300, 80, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.GdiClassic);
d2dContext.DrawText("MeasuringMode: GDI natural", textFormat, new RectangleF(300, 110, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.GdiNatural);
//float layoutYOffset = (float)Math.Cos(layoutY) * 50.0f;
// Draw moving text.
d2dContext.FillRectangle(new RectangleF(300, 300, 400, 200), backgroundBrush);
//d2dContext.DrawTextLayout(new Vector2(300, 350 + layoutYOffset), textLayout1, textBrush);
// Draw moving text without pixel snapping, thus giving a smoother movement.
d2dContext.FillRectangle(new RectangleF(750, 300, 400, 200), backgroundBrush);
//d2dContext.DrawTextLayout(new Vector2(750, 350 + layoutYOffset), textLayout2, textBrush, DrawTextOptions.NoSnap);
d2dContext.EndDraw();
}
// Handle base.Draw
base.Draw(gameTime);
}
Seems inside d2d there is no exceptions or errors, but still no text. What I am doing wrong here?
P.S. I am using WinRT + Swapchainpanel.