From 71af5331b108d6407c791e3859af41ef2b379483 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 15 Feb 2023 00:35:52 -0600 Subject: implement basic vector bindings --- src/main.c | 4 +- src/vector/CMakeLists.txt | 8 +-- src/vector/vector.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 869a7f5..a01dd49 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,9 @@ #include "image/image.h" #include "import/import.h" #include "logging/logging.h" -#include "options/options.h" #include "ode/ode_bindings.h" +#include "options/options.h" +#include "vector/vector.h" void print_load_error(lua_State *L, const char *script_file, int error_type); @@ -37,6 +38,7 @@ int main(int argc, char **argv) setup_ode(L, honey_index); setup_util(L, honey_index); setup_window(L, honey_index); + setup_nvg(L, honey_index); lua_setglobal(L, "honey"); /* load main script */ diff --git a/src/vector/CMakeLists.txt b/src/vector/CMakeLists.txt index 53032eb..d74b1af 100644 --- a/src/vector/CMakeLists.txt +++ b/src/vector/CMakeLists.txt @@ -1,9 +1,3 @@ project(honey_engine) -target_sources(honey PUBLIC - ${CMAKE_CURRENT_LIST_DIR}/vector.c -) - - -target_sources(test PUBLIC -) +target_sources(honey PUBLIC ${CMAKE_CURRENT_LIST_DIR}/vector.c) diff --git a/src/vector/vector.c b/src/vector/vector.c index 230bfdd..33f80e5 100644 --- a/src/vector/vector.c +++ b/src/vector/vector.c @@ -61,6 +61,146 @@ int nvg_end_frame(lua_State *L) } +/* --===== paths =====-- */ + +int nvg_begin_path(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgBeginPath(*vg); + return 0; +} + + +int nvg_move_to(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + nvgMoveTo(*vg, x, y); + return 0; +} + + +int nvg_line_to(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + nvgLineTo(*vg, x, y); + return 0; +} + + +int nvg_bezier_to(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float c1x = luaL_checknumber(L, 2); + float c1y = luaL_checknumber(L, 3); + float c2x = luaL_checknumber(L, 4); + float c2y = luaL_checknumber(L, 5); + float x = luaL_checknumber(L, 6); + float y = luaL_checknumber(L, 7); + nvgBezierTo(*vg, c1x, c1y, c2x, c2y, x, y); + return 0; +} + + +int nvg_quad_to(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float cx = luaL_checknumber(L, 2); + float cy = luaL_checknumber(L, 3); + float x = luaL_checknumber(L, 4); + float y = luaL_checknumber(L, 5); + nvgQuadTo(*vg, cx, cy, x, y); + return 0; +} + + +int nvg_arc_to(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float x1 = luaL_checknumber(L, 2); + float y1 = luaL_checknumber(L, 3); + float x2 = luaL_checknumber(L, 4); + float y2 = luaL_checknumber(L, 5); + float radius = luaL_checknumber(L, 6); + nvgArcTo(*vg, x1, y1, x2, y2, radius); + return 0; +} + + +int nvg_close_path(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgClosePath(*vg); + return 0; +} + + +int nvg_path_winding(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + int dir = luaL_checkinteger(L, 2); + nvgPathWinding(*vg, dir); + return 0; +} + + +int nvg_arc(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float cx = luaL_checknumber(L, 2); + float cy = luaL_checknumber(L, 3); + float r = luaL_checknumber(L, 4); + float a0 = luaL_checknumber(L, 5); + float a1 = luaL_checknumber(L, 6); + int dir = luaL_checkinteger(L, 7); + nvgArc(*vg, cx, cy, r, a0, a1, dir); + return 0; +} + +/* ... */ + +int nvg_fill(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgFill(*vg); + return 0; +} + + +int nvg_stroke(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgStroke(*vg); + return 0; +} + + +/* --===== render styles =====-- */ + +int nvg_stroke_color(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + NVGcolor color; + color.r = luaL_checknumber(L, 2); + color.g = luaL_checknumber(L, 3); + color.b = luaL_checknumber(L, 4); + color.a = luaL_checknumber(L, 5); + nvgStrokeColor(*vg, color); + return 0; +} + + +int nvg_stroke_width(lua_State *L) +{ + struct NVGcontext **vg = luaL_checkudata(L, 1, nvg_ctx_tname); + float size = luaL_checknumber(L, 2); + nvgStrokeWidth(*vg, size); + return 0; +} + /* --===== complete =====-- */ @@ -77,6 +217,21 @@ void setup_nvg(lua_State *L, int honey_tbl) hs_str_cfunc("BeginFrame", nvg_begin_frame), hs_str_cfunc("CancelFrame", nvg_cancel_frame), hs_str_cfunc("EndFrame", nvg_end_frame), + + hs_str_cfunc("BeginPath", nvg_begin_path), + hs_str_cfunc("MoveTo", nvg_move_to), + hs_str_cfunc("LineTo", nvg_line_to), + hs_str_cfunc("BezierTo", nvg_bezier_to), + hs_str_cfunc("QuadTo", nvg_quad_to), + hs_str_cfunc("ArcTo", nvg_arc_to), + hs_str_cfunc("ClosePath", nvg_close_path), + hs_str_cfunc("PathWinding", nvg_path_winding), + hs_str_cfunc("Arc", nvg_arc), + hs_str_cfunc("Fill", nvg_fill), + hs_str_cfunc("Stroke", nvg_stroke), + + hs_str_cfunc("StrokeColor", nvg_stroke_color), + hs_str_cfunc("StrokeWidth", nvg_stroke_width), ); lua_setfield(L, honey_tbl, "nvg"); -- cgit v1.2.1