Skip to content

Using NvgSharp in MonoGame or FNA

Roman Shapiro edited this page Oct 31, 2022 · 16 revisions

Adding Reference

There are two ways of referencing NvgSharp in the project:

  1. Through nuget(works only for MonoGame):

  2. As source code(works for all supported engines):

    a. Clone this repo.

    b. Add src/XNA/NvgSharp.{Engine}.csproj to the solution

    c. Optionally add src/NvgSharp.Text/NvgSharp.Text.{Engine}.csproj to the solution, if you want the text rendering.

    d. If FNA was used and the NvgSharp.Text.FNA.csproj was referenced, then FontStashSharp should be cloned too. The overall folder structure is expected to be following:

Stencil

NvgSharp requires Game to be created with stencil. That could be archived by setting PreferredDepthStencilFormat to DepthFormat.Depth24Stencil8 in the GraphicsDeviceManager creation.

I.e.

public Game1()
{
	_graphics = new GraphicsDeviceManager(this)
	{
		PreferredBackBufferWidth = 1000,
		PreferredBackBufferHeight = 600,
		PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8
	};
}

Context Creation

Add following field to the Game class:

	private NvgContext _context;

And following code to the LoadContent method:

	_context = new NvgContext(GraphicsDevice);

Drawing

Add following code to Draw method to render a red circle:

	_context.FillColor(Color.Red);
	_context.Circle(100, 100, 20);
	_context.Fill();

	_context.Flush();

Sample

https://github.com/rds1983/NvgSharp/tree/master/samples/NvgSharp.Samples.Demo

Clone this wiki locally