Is it possible to use MonoGame & SharpDX at the same time?

Since XNA & MonoGame only use SpriteFont, it’s not very suitable for Chinese/Japanese fonts which has thousands of characters. So I want to use SharpDX directly to render texts while still use MonoGame for other rendering. Here is my code:

//in Initialize()
        width = Window.ClientBounds.Width;
        height = Window.ClientBounds.Height;
        
        device = new Device(new Direct3D(), 0, DeviceType.Hardware, Window.Handle,CreateFlags.HardwareVertexProcessing,
            new PresentParameters(width, height) { PresentationInterval = PresentInterval.One });

 protected override void Draw(GameTime gameTime)
 {            
        GraphicsDevice.Clear(Color.Black);
        device.Clear(ClearFlags.Target, SharpDX.Color.Black, 1.0f, 0);
        
        // Draw a texture in MonoGame
        spriteBatch.Begin();
        spriteBatch.Draw(texture,new Microsoft.Xna.Framework.Vector2(0,0));
        spriteBatch.End();

        // Draw a text in SharpDX.Direct3D9

        device.BeginScene();
        font.DrawText(null, displayText, fontDimension, FontDrawFlags.Center | FontDrawFlags.VerticalCenter, SharpDX.Color.White);
        device.EndScene();
        device.Present();

        base.Draw(gameTime);
 }

This code works,but seems they are drawing in turn. In some frames there are only the texture drawn by MonoGame, other frames there are only the text.
Is it possible to use them at the same time? Thanks.

Hmm, seems GraphicsDevice.Handle can be converted to a SharpDX.Direct3D11.Device. But what if I want to use Direct3D9 ?

That would be difficult since MonoGame uses DX11 under the hood. It would be a massive rewrite to revert it back to DX9.

You can’t use DirectX9 with MonoGame on Windows.

Another option is to use FreeType (with the SharpFont C# bindings) or TrueTypeSharp (a C# TTF rasterizer) to render the Asian glyphs you require at runtime into a byte buffer and then use Texture2D.SetData() to get those glyphs into a texture. Then render that texture using SpriteBatch as you normally would.

1 Like

Thank you. That would be a useful way.

and are there any ways to make a transparent window in monogame( or SharpDX)? Just like this:

Thanks again ^ ^