Giter VIP home page Giter VIP logo

nanovg_dotnet's Introduction

nanovg_dotnet

alt text

.NET binding for nanovg using P/Invoke

Demo application window is created with GLFW.Net and uses the built in OpenGL 3.2 NanoVG context. Custom rendering contexts can be created by implementing a class which inherits from NanoVG.NVGParameters. Source in Test/Program.cs

This binding uses System.Numerics vectors as much as possible, currently for colors and vertices, as they mach with the ones on the C side.

This works on both x86 and x64, it should also work on platforms other than Windows, but i do not have any way to test righ now

TODO

  • All string functions should take a byte array and include an overload which automatically converts to UTF-8
  • Fill in this TODO list

Example

// This sets the native nanovg directory so you can ship both
// x86 and x64 versions of original nanovg library, and only
// one .NET executable
NVG.SetLibraryDirectory();

// This is optional and may not exist in the resulting binary if compiled
// from this solution if NANOVG_DOTNET_COMPILE_GL3 isn't defined
NanoVGContext Ctx = NVG.CreateGL3Glew(3);

// or if you want to implement your own renderer
NanoVGContext Ctx = NVG.CreateContext(new CustomParameters()); // CustomParameters inherits from NanoVG.NVGParameters

// ...

static void DrawWindow(NanoVGContext Ctx, string Title, float X, float Y, float W, float H) {
	float CornerRadius = 3;
	NVGPaint ShadowPaint;
	NVGPaint HeaderPaint;
	NVG.Save(Ctx);

	// Window
	NVG.BeginPath(Ctx);
	NVG.RoundedRect(Ctx, X, Y, W, H, CornerRadius);
	NVG.FillColor(Ctx, NVG.RGBA(28, 30, 34, 192));
	NVG.Fill(Ctx);

	// Drop shadow
	ShadowPaint = NVG.BoxGradient(Ctx, X, Y + 2, W, H, CornerRadius * 2, 10, NVG.RGBA(0, 0, 0, 128), NVG.RGBA(0, 0, 0, 0));
	NVG.BeginPath(Ctx);
	NVG.Rect(Ctx, X - 10, Y - 10, W + 20, H + 30);
	NVG.RoundedRect(Ctx, X, Y, W, H, CornerRadius);
	NVG.PathWinding(Ctx, NVGSolidity.NVG_HOLE);
	NVG.FillPaint(Ctx, ShadowPaint);
	NVG.Fill(Ctx);

	// Header
	HeaderPaint = NVG.LinearGradient(Ctx, X, Y, X, Y + 15, NVG.RGBA(255, 255, 255, 8), NVG.RGBA(0, 0, 0, 16));
	NVG.BeginPath(Ctx);
	NVG.RoundedRect(Ctx, X + 1, Y + 1, W - 2, 30, CornerRadius - 1);
	NVG.FillPaint(Ctx, HeaderPaint);
	NVG.Fill(Ctx);
	NVG.BeginPath(Ctx);
	NVG.MoveTo(Ctx, X + 0.5f, Y + 0.5f + 30);
	NVG.LineTo(Ctx, X + 0.5f + W - 1, Y + 0.5f + 30);
	NVG.StrokeColor(Ctx, NVG.RGBA(0, 0, 0, 32));
	NVG.Stroke(Ctx);

	NVG.FontSize(Ctx, 18.0f);
	NVG.FontFace(Ctx, "sans-bold");
	NVG.TextAlign(Ctx, NVGAlign.NVG_ALIGN_CENTER | NVGAlign.NVG_ALIGN_MIDDLE);

	NVG.FontBlur(Ctx, 2);
	NVG.FillColor(Ctx, NVG.RGBA(0, 0, 0, 128));
	NVG.Text(Ctx, X + W / 2, Y + 16 + 1, Title, null);

	NVG.FontBlur(Ctx, 0);
	NVG.FillColor(Ctx, NVG.RGBA(220, 220, 220, 160));
	NVG.Text(Ctx, X + W / 2, Y + 16, Title, null);

	NVG.Restore(Ctx);
}

All NVG methods are implemented as extension methods, so these are interchargable

NanoVGContext Ctx = NVG.CreateContext(/* ... */);

NVG.BeginPath(Ctx);
NVG.FontBlur(Ctx, 2);

// and

Ctx.BeginPath();
Ctx.FontBlur(2);

nanovg_dotnet's People

Contributors

sbarisic avatar imgbotapp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.