diff options
author | sanine <sanine.not@pm.me> | 2023-02-14 00:53:26 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-14 00:53:26 -0600 |
commit | cb9764cddf0ed8186e78c418f066b31921fbda40 (patch) | |
tree | 80f58cead45f4f855e910ac4cdb5aaf461797daf /src/vector/vector.c | |
parent | f1fe73d1909a2448a004a88362a1a532d0d4f7c3 (diff) |
add basic nanovg bindings
Diffstat (limited to 'src/vector/vector.c')
-rw-r--r-- | src/vector/vector.c | 83 |
1 files changed, 83 insertions, 0 deletions
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"); +} |