From cb9764cddf0ed8186e78c418f066b31921fbda40 Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Tue, 14 Feb 2023 00:53:26 -0600
Subject: add basic nanovg bindings

---
 src/image/image.c         |  2 +-
 src/vector/CMakeLists.txt |  9 +++++
 src/vector/vector.c       | 83 +++++++++++++++++++++++++++++++++++++++++++++++
 src/vector/vector.h       |  6 ++++
 4 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 src/vector/CMakeLists.txt
 create mode 100644 src/vector/vector.c
 create mode 100644 src/vector/vector.h

(limited to 'src')

diff --git a/src/image/image.c b/src/image/image.c
index 64b7330..aeff333 100644
--- a/src/image/image.c
+++ b/src/image/image.c
@@ -2,7 +2,7 @@
 #include <honeysuckle.h>
 #include <cairo/cairo.h>
 /* assimp provides its own stb_image implementation */
-#define STB_IMAGE_IMPLEMENTATION
+/*#define STB_IMAGE_IMPLEMENTATION*/
 #include "stb_image.h"
 #include "image.h"
 
diff --git a/src/vector/CMakeLists.txt b/src/vector/CMakeLists.txt
new file mode 100644
index 0000000..53032eb
--- /dev/null
+++ b/src/vector/CMakeLists.txt
@@ -0,0 +1,9 @@
+project(honey_engine)
+
+target_sources(honey PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/vector.c
+)
+
+
+target_sources(test PUBLIC
+)
diff --git a/src/vector/vector.c b/src/vector/vector.c
new file mode 100644
index 0000000..230bfdd
--- /dev/null
+++ b/src/vector/vector.c
@@ -0,0 +1,83 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#include "gl/glad/glad.h"
+#include <GLFW/glfw3.h>
+#define NANOVG_GL3_IMPLEMENTATION
+#include <nanovg.h>
+#include <nanovg_gl.h>
+#include "vector.h"
+
+
+static const char *nvg_ctx_tname = "nvg.Context";
+
+
+/* --===== contexts =====-- */
+
+int nvg_context(lua_State *L)
+{
+	struct NVGcontext **vg = lua_newuserdata(L, sizeof(struct NVGcontext *));
+	*vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
+	luaL_getmetatable(L, nvg_ctx_tname);
+	lua_setmetatable(L, -2);
+	return 1;
+}
+
+
+int nvg_delete(lua_State *L)
+{
+	struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname);
+	nvgDeleteGL3(*vg);
+	return 0;
+}
+
+
+/* --===== frames =====-- */
+
+int nvg_begin_frame(lua_State *L)
+{
+	struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname);
+	float width      = luaL_checknumber(L, 2);
+	float height     = luaL_checknumber(L, 3);
+	float pixelRatio = luaL_checknumber(L, 4);
+
+	nvgBeginFrame(*vg, width, height, pixelRatio);
+	return 0;
+}
+
+
+int nvg_cancel_frame(lua_State *L)
+{
+	struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname);
+	nvgCancelFrame(*vg);
+	return 0;
+}
+
+
+int nvg_end_frame(lua_State *L)
+{
+	struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname);
+	nvgEndFrame(*vg);
+	return 0;
+}
+
+
+
+/* --===== complete =====-- */
+
+void setup_nvg(lua_State *L, int honey_tbl)
+{
+	luaL_newmetatable(L, nvg_ctx_tname);
+	lua_pushcfunction(L, nvg_delete);
+	lua_setfield(L, -2, "__gc");
+	lua_pop(L, 1);
+
+	hs_create_table(L,
+		hs_str_cfunc("Context", nvg_context),
+
+		hs_str_cfunc("BeginFrame", nvg_begin_frame),
+		hs_str_cfunc("CancelFrame", nvg_cancel_frame),
+		hs_str_cfunc("EndFrame", nvg_end_frame),
+	);
+
+	lua_setfield(L, honey_tbl, "nvg");
+}
diff --git a/src/vector/vector.h b/src/vector/vector.h
new file mode 100644
index 0000000..1a08e8e
--- /dev/null
+++ b/src/vector/vector.h
@@ -0,0 +1,6 @@
+#ifndef HONEY_VECTOR_H
+#define HONEY_VECTOR_H
+
+void setup_nvg(lua_State *L, int honey_tbl);
+
+#endif
-- 
cgit v1.2.1