diff options
author | sanine <sanine.not@pm.me> | 2023-02-19 19:03:51 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-19 19:03:51 -0600 |
commit | fb0eb6abac31ab1945a5b32e68824c9f9420d71f (patch) | |
tree | b414bb9f93af85755df553d003ed88afc24cef50 | |
parent | bc4af3b47260ee0f79343303b135d1ea32cde4d4 (diff) |
refactor: bind (nearly) all nvg functions
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | demo/fancy/honey.lua | 130 | ||||
-rw-r--r-- | demo/vector/main.lua | 4 | ||||
-rw-r--r-- | src/nvg/CMakeLists.txt | 16 | ||||
-rw-r--r-- | src/nvg/context.c | 3 | ||||
-rw-r--r-- | src/nvg/image.c | 16 | ||||
-rw-r--r-- | src/nvg/nvg.h | 10 | ||||
-rw-r--r-- | src/nvg/paint.c | 77 | ||||
-rw-r--r-- | src/nvg/path.c | 131 | ||||
-rw-r--r-- | src/nvg/render.c | 4 | ||||
-rw-r--r-- | src/nvg/scissor.c | 35 | ||||
-rw-r--r-- | src/nvg/setup.c | 5 | ||||
-rw-r--r-- | src/nvg/text.c | 182 | ||||
-rw-r--r-- | src/nvg/transform.c | 18 | ||||
-rw-r--r-- | util/generate-binding.lua | 62 | ||||
-rw-r--r-- | util/test.lua | 40 |
16 files changed, 604 insertions, 131 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3713c73..e434700 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,4 +80,4 @@ add_subdirectory(${SRC_ROOT}/test) add_subdirectory(${SRC_ROOT}/util) add_subdirectory(${SRC_ROOT}/import) add_subdirectory(${SRC_ROOT}/ode) -add_subdirectory(${SRC_ROOT}/vector) +add_subdirectory(${SRC_ROOT}/nvg) diff --git a/demo/fancy/honey.lua b/demo/fancy/honey.lua index 75f0a99..d5b6ad0 100644 --- a/demo/fancy/honey.lua +++ b/demo/fancy/honey.lua @@ -121,45 +121,45 @@ gl.EnableVertexAttribArray(1) --===== load mesh from file =====-- -local scene = honey.import.importFile('suzanne.dae') -local mesh = scene.meshes[1] -local suzanne = {} -suzanne.vertices = {} -print('mesh.vertices', #mesh.vertices) -for i=1,#mesh.vertices do - local position = mesh.vertices[i] - local uv = mesh.uvs[1][i] - table.insert(suzanne.vertices, position.x) - table.insert(suzanne.vertices, position.y) - table.insert(suzanne.vertices, position.z) - table.insert(suzanne.vertices, uv.x) - table.insert(suzanne.vertices, uv.y) -end -suzanne.indices = {} -for _, face in ipairs(mesh.faces) do - assert(#face == 3) - for _, i in ipairs(face) do - table.insert(suzanne.indices, i) - end -end -print('mesh.faces', #mesh.faces) -print('suzanne.indices', #suzanne.indices) - -suzanne.vertexArr = gl.GenVertexArrays() -suzanne.vertexBuf = gl.GenBuffers() -suzanne.elementBuf = gl.GenBuffers() - -gl.BindVertexArray(suzanne.vertexArr) -gl.BindBuffer(gl.ARRAY_BUFFER, suzanne.vertexBuf) -gl.BufferData(gl.ARRAY_BUFFER, gl.FLOAT, suzanne.vertices, gl.STATIC_DRAW) - -gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, suzanne.elementBuf) -gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.UNSIGNED_INT, suzanne.indices, gl.STATIC_DRAW) - -gl.VertexAttribPointer(0, 3, false, 5, 0) -gl.EnableVertexAttribArray(0) -gl.VertexAttribPointer(1, 2, false, 5, 3) -gl.EnableVertexAttribArray(1) +-- local scene = honey.import.importFile('suzanne.dae') +-- local mesh = scene.meshes[1] +-- local suzanne = {} +-- suzanne.vertices = {} +-- print('mesh.vertices', #mesh.vertices) +-- for i=1,#mesh.vertices do +-- local position = mesh.vertices[i] +-- local uv = mesh.uvs[1][i] +-- table.insert(suzanne.vertices, position.x) +-- table.insert(suzanne.vertices, position.y) +-- table.insert(suzanne.vertices, position.z) +-- table.insert(suzanne.vertices, uv.x) +-- table.insert(suzanne.vertices, uv.y) +-- end +-- suzanne.indices = {} +-- for _, face in ipairs(mesh.faces) do +-- assert(#face == 3) +-- for _, i in ipairs(face) do +-- table.insert(suzanne.indices, i) +-- end +-- end +-- print('mesh.faces', #mesh.faces) +-- print('suzanne.indices', #suzanne.indices) +-- +-- suzanne.vertexArr = gl.GenVertexArrays() +-- suzanne.vertexBuf = gl.GenBuffers() +-- suzanne.elementBuf = gl.GenBuffers() +-- +-- gl.BindVertexArray(suzanne.vertexArr) +-- gl.BindBuffer(gl.ARRAY_BUFFER, suzanne.vertexBuf) +-- gl.BufferData(gl.ARRAY_BUFFER, gl.FLOAT, suzanne.vertices, gl.STATIC_DRAW) +-- +-- gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, suzanne.elementBuf) +-- gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.UNSIGNED_INT, suzanne.indices, gl.STATIC_DRAW) +-- +-- gl.VertexAttribPointer(0, 3, false, 5, 0) +-- gl.EnableVertexAttribArray(0) +-- gl.VertexAttribPointer(1, 2, false, 5, 3) +-- gl.EnableVertexAttribArray(1) --====== load texture ======-- @@ -190,26 +190,26 @@ gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0) --===== generate cairo texture =====-- -local image = honey.image -local surface = image.surface_create(image.FORMAT_ARGB32, 512, 512) -local cr = image.context_create(surface) -image.context_select_font_face(cr, "sans-serif", image.FONT_SLANT_NORMAL, image.FONT_WEIGHT_NORMAL) -image.context_set_font_size(cr, 32) -image.context_set_source_rgb(cr, 1, 0, 0) -image.context_move_to(cr, 100, 100) -image.context_show_text(cr, "hello, world!") -local data = image.surface_get_data(surface) -gl.BindTexture(gl.TEXTURE_2D, texture) -gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_R, gl.BLUE) -gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_B, gl.RED) ---gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_A, gl.RED) -gl.TexImage2D( - gl.TEXTURE_2D, 0, - gl.RGB, width, height, - gl.RGBA, gl.UNSIGNED_BYTE, - data -) -gl.GenerateMipmap(gl.TEXTURE_2D) +-- local image = honey.image +-- local surface = image.surface_create(image.FORMAT_ARGB32, 512, 512) +-- local cr = image.context_create(surface) +-- image.context_select_font_face(cr, "sans-serif", image.FONT_SLANT_NORMAL, image.FONT_WEIGHT_NORMAL) +-- image.context_set_font_size(cr, 32) +-- image.context_set_source_rgb(cr, 1, 0, 0) +-- image.context_move_to(cr, 100, 100) +-- image.context_show_text(cr, "hello, world!") +-- local data = image.surface_get_data(surface) +-- gl.BindTexture(gl.TEXTURE_2D, texture) +-- gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_R, gl.BLUE) +-- gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_B, gl.RED) +-- --gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_SWIZZLE_A, gl.RED) +-- gl.TexImage2D( +-- gl.TEXTURE_2D, 0, +-- gl.RGB, width, height, +-- gl.RGBA, gl.UNSIGNED_BYTE, +-- data +-- ) +-- gl.GenerateMipmap(gl.TEXTURE_2D) --====== matrices ======-- @@ -264,11 +264,11 @@ while not window.shouldClose(w) do gl.UniformMatrix4fv(viewL, false, view) gl.UniformMatrix4fv(projectionL, false, projection) - --gl.BindVertexArray(vertexArray) - --gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) + gl.BindVertexArray(vertexArray) + gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) - gl.BindVertexArray(suzanne.vertexArr) - gl.DrawElements(gl.TRIANGLES, 3*#mesh.faces, gl.UNSIGNED_INT, 0) + --gl.BindVertexArray(suzanne.vertexArr) + --gl.DrawElements(gl.TRIANGLES, 3*#mesh.faces, gl.UNSIGNED_INT, 0) window.swapBuffers(w) window.pollEvents() @@ -279,5 +279,5 @@ end window.destroy(w) gl.Terminate() honey.audio.engine_uninit(engine) -image.surface_destroy(surface) -image.context_destroy(cr) +--image.surface_destroy(surface) +--image.context_destroy(cr) diff --git a/demo/vector/main.lua b/demo/vector/main.lua index 595131b..fda4bc8 100644 --- a/demo/vector/main.lua +++ b/demo/vector/main.lua @@ -7,7 +7,7 @@ local nvg = honey.nvg local vg function honey.init() - vg = nvg.Context() + vg = nvg.CreateContext() end @@ -31,7 +31,7 @@ end function honey.draw() nvg.BeginFrame(vg, 640, 480, 1.0) nvg.StrokeWidth(vg, 20) - nvg.StrokeColor(vg, 1, 0, 0, 1) + nvg.StrokeColor(vg, nvg.RGB(1, 0, 0)) local w = 640 local h = 480 diff --git a/src/nvg/CMakeLists.txt b/src/nvg/CMakeLists.txt index 8003158..9a96328 100644 --- a/src/nvg/CMakeLists.txt +++ b/src/nvg/CMakeLists.txt @@ -1,3 +1,17 @@ project(honey_engine) -target_sources(honey PUBLIC ${CMAKE_CURRENT_LIST_DIR}/nvg.c) +target_sources(honey PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/color.c + ${CMAKE_CURRENT_LIST_DIR}/composite.c + ${CMAKE_CURRENT_LIST_DIR}/context.c + ${CMAKE_CURRENT_LIST_DIR}/frame.c + ${CMAKE_CURRENT_LIST_DIR}/image.c + ${CMAKE_CURRENT_LIST_DIR}/path.c + ${CMAKE_CURRENT_LIST_DIR}/render.c + ${CMAKE_CURRENT_LIST_DIR}/setup.c + ${CMAKE_CURRENT_LIST_DIR}/state.c + ${CMAKE_CURRENT_LIST_DIR}/transform.c + ${CMAKE_CURRENT_LIST_DIR}/text.c + ${CMAKE_CURRENT_LIST_DIR}/paint.c + ${CMAKE_CURRENT_LIST_DIR}/scissor.c +) diff --git a/src/nvg/context.c b/src/nvg/context.c index 48a7282..ee386aa 100644 --- a/src/nvg/context.c +++ b/src/nvg/context.c @@ -1,5 +1,8 @@ #include <lua.h> #include <lauxlib.h> +#include "gl/glad/glad.h" +#include <GLFW/glfw3.h> +#define NANOVG_GL3_IMPLEMENTATION #include <nanovg.h> #include <nanovg_gl.h> #include "nvg.h" diff --git a/src/nvg/image.c b/src/nvg/image.c index e7f39c2..9a1d22c 100644 --- a/src/nvg/image.c +++ b/src/nvg/image.c @@ -5,15 +5,13 @@ #include "nvg.h" -int CreateImage_bind(lua_State *L) +int nvgCreateImage_bind(lua_State *L) { NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); const char *filename = luaL_checkstring(L, 2); int flags = luaL_checkinteger(L, 3); - int *handle = lua_newuserdata(L, sizeof(int)); - luaL_getmetatable(L, nvg_image_tname); - lua_setmetatable(L, -2); - *handle = nvgCreateImage(*ctx, filename, flags); + int handle = nvgCreateImage(*ctx, filename, flags); + lua_pushinteger(L, handle); return 1; } @@ -21,9 +19,9 @@ int CreateImage_bind(lua_State *L) int nvgImageSize_bind(lua_State *L) { NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); - int *handle = luaL_checkudata(L, 2, nvg_image_tname); + int handle = luaL_checkinteger(L, 2); int w, h; - nvgImageSize(*ctx, *handle, &w, &h); + nvgImageSize(*ctx, handle, &w, &h); lua_pushinteger(L, w); lua_pushinteger(L, h); return 2; @@ -33,7 +31,7 @@ int nvgImageSize_bind(lua_State *L) int nvgDeleteImage_bind(lua_State *L) { NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); - int *handle = luaL_checkudata(L, 2, nvg_image_tname); - nvgDeleteImage(*ctx, *handle); + int handle = luaL_checkinteger(L, 2); + nvgDeleteImage(*ctx, handle); return 0; } diff --git a/src/nvg/nvg.h b/src/nvg/nvg.h index 33a2420..9bbb4a9 100644 --- a/src/nvg/nvg.h +++ b/src/nvg/nvg.h @@ -112,8 +112,8 @@ extern const char *nvg_paint_tname; /* text */ \ X("CreateFont", nvgCreateFont_bind) \ X("CreateFontAtIndex", nvgCreateFontAtIndex_bind) \ - X("CreateFontMem", nvgCreateFontMem_bind) \ - X("CreateFontMemAtIndex", nvgCreateFontMemAtIndex_bind) \ + /* X("CreateFontMem", nvgCreateFontMem_bind) */ \ + /* X("CreateFontMemAtIndex", nvgCreateFontMemAtIndex_bind) */ \ X("FindFont", nvgFindFont_bind) \ X("AddFallbackFontId", nvgAddFallbackFontId_bind) \ X("AddFallbackFont", nvgAddFallbackFont_bind) \ @@ -130,9 +130,9 @@ extern const char *nvg_paint_tname; X("TextBox", nvgTextBox_bind) \ X("TextBounds", nvgTextBounds_bind) \ X("TextBoxBounds", nvgTextBoxBounds_bind) \ - X("TextGlyphPositions", nvgTextGlyphPositions_bind) \ - X("TextMetrics", nvgTextMetrics_bind) \ - X("TextBreakLines", nvgTextBreakLines_bind) \ + /* X("TextGlyphPositions", nvgTextGlyphPositions_bind) */ \ + /* X("TextMetrics", nvgTextMetrics_bind) */ \ + /* X("TextBreakLines", nvgTextBreakLines_bind) */ \ /* function declarations */ diff --git a/src/nvg/paint.c b/src/nvg/paint.c new file mode 100644 index 0000000..0bdcae1 --- /dev/null +++ b/src/nvg/paint.c @@ -0,0 +1,77 @@ +#include <lua.h> +#include <lauxlib.h> +#include <nanovg.h> +#include <nanovg_gl.h> +#include "nvg.h" + + +static void create_paint(lua_State *L, NVGpaint paint) +{ + NVGpaint *p = lua_newuserdata(L, sizeof(NVGpaint)); + *p = paint; + luaL_getmetatable(L, nvg_paint_tname); + lua_setmetatable(L, -2); +} + +int nvgLinearGradient_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float sx = luaL_checknumber(L, 2); + float sy = luaL_checknumber(L, 3); + float ex = luaL_checknumber(L, 4); + float ey = luaL_checknumber(L, 5); + NVGcolor *icol = luaL_checkudata(L, 6, nvg_color_tname); + NVGcolor *ocol = luaL_checkudata(L, 7, nvg_color_tname); + create_paint(L, + nvgLinearGradient(*ctx, sx, sy, ex, ey, *icol, *ocol) + ); + return 1; +} + +int nvgBoxGradient_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + float r = luaL_checknumber(L, 6); + float f = luaL_checknumber(L, 7); + NVGcolor *icol = luaL_checkudata(L, 8, nvg_color_tname); + NVGcolor *ocol = luaL_checkudata(L, 9, nvg_color_tname); + create_paint(L, + nvgBoxGradient(*ctx, x, y, w, h, r, f, *icol, *ocol) + ); + return 1; +} + +int nvgRadialGradient_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float cx = luaL_checknumber(L, 2); + float cy = luaL_checknumber(L, 3); + float inr = luaL_checknumber(L, 4); + float outr = luaL_checknumber(L, 5); + NVGcolor *icol = luaL_checkudata(L, 6, nvg_color_tname); + NVGcolor *ocol = luaL_checkudata(L, 7, nvg_color_tname); + create_paint(L, + nvgRadialGradient(*ctx, cx, cy, inr, outr, *icol, *ocol) + ); + return 1; +} + +int nvgImagePattern_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float ox = luaL_checknumber(L, 2); + float oy = luaL_checknumber(L, 3); + float ex = luaL_checknumber(L, 4); + float ey = luaL_checknumber(L, 5); + float angle = luaL_checknumber(L, 6); + int image = luaL_checkinteger(L, 7); + float alpha = luaL_checknumber(L, 8); + create_paint(L, + nvgImagePattern(*ctx, ox, oy, ex, ey, angle, image, alpha) + ); + return 1; +} diff --git a/src/nvg/path.c b/src/nvg/path.c index 1e0f527..7f2c4d0 100644 --- a/src/nvg/path.c +++ b/src/nvg/path.c @@ -1,117 +1,180 @@ +#include <lua.h> +#include <lauxlib.h> +#include <nanovg.h> +#include <nanovg_gl.h> +#include "nvg.h" + + /* --===== paths =====-- */ -int nvg_begin_path(lua_State *L) +int nvgBeginPath_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); - nvgBeginPath(*vg); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgBeginPath(*ctx); return 0; } -int nvg_move_to(lua_State *L) +int nvgMoveTo_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); - nvgMoveTo(*vg, x, y); + nvgMoveTo(*ctx, x, y); return 0; } -int nvg_line_to(lua_State *L) +int nvgLineTo_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); - nvgLineTo(*vg, x, y); + nvgLineTo(*ctx, x, y); return 0; } -int nvg_bezier_to(lua_State *L) +int nvgBezierTo_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = 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); + nvgBezierTo(*ctx, c1x, c1y, c2x, c2y, x, y); return 0; } -int nvg_quad_to(lua_State *L) +int nvgQuadTo_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = 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); + nvgQuadTo(*ctx, cx, cy, x, y); return 0; } -int nvg_arc_to(lua_State *L) +int nvgArcTo_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = 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); + nvgArcTo(*ctx, x1, y1, x2, y2, radius); return 0; } -int nvg_close_path(lua_State *L) +int nvgClosePath_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); - nvgClosePath(*vg); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgClosePath(*ctx); return 0; } -int nvg_path_winding(lua_State *L) +int nvgPathWinding_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); int dir = luaL_checkinteger(L, 2); - nvgPathWinding(*vg, dir); + nvgPathWinding(*ctx, dir); return 0; } -int nvg_arc(lua_State *L) +int nvgArc_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); + struct NVGcontext **ctx = 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); + nvgArc(*ctx, cx, cy, r, a0, a1, dir); + return 0; +} + + +int nvgRect_bind(lua_State *L) +{ + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + nvgRect(*ctx, x, y, w, h); + return 0; +} + +int nvgRoundedRect_bind(lua_State *L) +{ + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + float r = luaL_checknumber(L, 6); + nvgRoundedRect(*ctx, x, y, w, h, r); return 0; } -/* ... */ +int nvgRoundedRectVarying_bind(lua_State *L) +{ + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + float rtl = luaL_checknumber(L, 6); + float rtr = luaL_checknumber(L, 7); + float rbl = luaL_checknumber(L, 8); + float rbr = luaL_checknumber(L, 9); + nvgRoundedRectVarying(*ctx, x, y, w, h, rtl, rtr, rbl, rbr); + return 0; +} -int nvg_fill(lua_State *L) +int nvgEllipse_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); - nvgFill(*vg); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float cx = luaL_checknumber(L, 2); + float cy = luaL_checknumber(L, 3); + float rx = luaL_checknumber(L, 4); + float ry = luaL_checknumber(L, 5); + nvgEllipse(*ctx, cx, cy, rx, ry); return 0; } +int nvgCircle_bind(lua_State *L) +{ + struct NVGcontext **ctx = 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); + nvgCircle(*ctx, cx, cy, r); + return 0; +} -int nvg_stroke(lua_State *L) +int nvgFill_bind(lua_State *L) { - struct NVGcontext **vg = luaL_checkudata(L, 1, ctx_tname); - nvgStroke(*vg); + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgFill(*ctx); return 0; } +int nvgStroke_bind(lua_State *L) +{ + struct NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgStroke(*ctx); + return 0; +} diff --git a/src/nvg/render.c b/src/nvg/render.c index f28da90..87aaba2 100644 --- a/src/nvg/render.c +++ b/src/nvg/render.c @@ -5,10 +5,10 @@ #include "nvg.h" -int ShapeAntiAlias_bind(lua_State *L) +int nvgShapeAntiAlias_bind(lua_State *L) { NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); - int enabled = luaL_toboolean(L, 2); + int enabled = lua_toboolean(L, 2); nvgShapeAntiAlias(*ctx, enabled); return 0; } diff --git a/src/nvg/scissor.c b/src/nvg/scissor.c new file mode 100644 index 0000000..7413c46 --- /dev/null +++ b/src/nvg/scissor.c @@ -0,0 +1,35 @@ +#include <lua.h> +#include <lauxlib.h> +#include <nanovg.h> +#include <nanovg_gl.h> +#include "nvg.h" + + +int nvgScissor_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + nvgScissor(*ctx, x, y, w, h); + return 0; +} + +int nvgIntersectScissor_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float w = luaL_checknumber(L, 4); + float h = luaL_checknumber(L, 5); + nvgIntersectScissor(*ctx, x, y, w, h); + return 0; +} + +int nvgResetScissor_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + nvgResetScissor(*ctx); + return 0; +} diff --git a/src/nvg/setup.c b/src/nvg/setup.c index 16766c0..0404f91 100644 --- a/src/nvg/setup.c +++ b/src/nvg/setup.c @@ -1,8 +1,5 @@ #include <lua.h> #include <lauxlib.h> -#include "gl/glad/glad.h" -#include <GLFW/glfw3.h> -#define NANOVG_GL3_IMPLEMENTATION #include <nanovg.h> #include <nanovg_gl.h> #include "util/util.h" @@ -11,6 +8,8 @@ const char *nvg_ctx_tname = "nvg.Context"; const char *nvg_color_tname = "nvg.Color"; +const char *nvg_image_tname = "nvg.Image"; +const char *nvg_paint_tname = "nvg.Paint"; void setup_nvg(lua_State *L, int honey_tbl) diff --git a/src/nvg/text.c b/src/nvg/text.c new file mode 100644 index 0000000..cd480f6 --- /dev/null +++ b/src/nvg/text.c @@ -0,0 +1,182 @@ +#include <lua.h> +#include <lauxlib.h> +#include <nanovg.h> +#include <nanovg_gl.h> +#include "nvg.h" + + +int nvgCreateFont_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *name = luaL_checkstring(L, 2); + const char *filename = luaL_checkstring(L, 3); + lua_pushinteger(L, + nvgCreateFont(*ctx, name, filename) + ); + return 1; +} + +int nvgCreateFontAtIndex_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *name = luaL_checkstring(L, 2); + const char *filename = luaL_checkstring(L, 3); + int index = luaL_checkinteger(L, 4); + lua_pushinteger(L, + nvgCreateFontAtIndex(*ctx, name, filename, index) + ); + return 1; + +} + +int nvgFindFont_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *name = luaL_checkstring(L, 2); + lua_pushinteger(L, + nvgFindFont(*ctx, name) + ); + return 1; +} + +int nvgAddFallbackFontId_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + int baseFont = luaL_checkinteger(L, 2); + int fallbackFont = luaL_checkinteger(L, 3); + int font = nvgAddFallbackFontId(*ctx, baseFont, fallbackFont); + lua_pushinteger(L, font); + return 1; +} + +int nvgAddFallbackFont_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *baseFont = luaL_checkstring(L, 2); + const char *fallbackFont = luaL_checkstring(L, 3); + int font = nvgAddFallbackFont(*ctx, baseFont, fallbackFont); + lua_pushinteger(L, font); + return 1; +} + +int nvgResetFallbackFontsId_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + int baseFont = luaL_checkinteger(L, 2); + nvgResetFallbackFontsId(*ctx, baseFont); + return 0; +} + +int nvgResetFallbackFonts_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *baseFont = luaL_checkstring(L, 2); + nvgResetFallbackFonts(*ctx, baseFont); + return 0; +} + +int nvgFontSize_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float size = luaL_checknumber(L, 2); + nvgFontSize(*ctx, size); + return 0; +} + +int nvgFontBlur_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float blur = luaL_checknumber(L, 2); + nvgFontBlur(*ctx, blur); + return 0; +} + +int nvgTextLetterSpacing_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float spacing = luaL_checknumber(L, 2); + nvgTextLetterSpacing(*ctx, spacing); + return 0; +} + +int nvgTextLineHeight_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float lineHeight = luaL_checknumber(L, 2); + nvgTextLineHeight(*ctx, lineHeight); + return 0; +} + +int nvgTextAlign_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + int align = luaL_checkinteger(L, 2); + nvgTextAlign(*ctx, align); + return 0; +} + +int nvgFontFaceId_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + int font = luaL_checknumber(L, 2); + nvgFontFaceId(*ctx, font); + return 0; +} + +int nvgFontFace_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + const char *font = luaL_checkstring(L, 2); + nvgFontFace(*ctx, font); + return 0; +} + +int nvgText_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + const char *string = luaL_checkstring(L, 4); + nvgText(*ctx, x, y, string, NULL); + return 0; +} + +int nvgTextBox_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float breakRowWidth = luaL_checknumber(L, 4); + const char *string = luaL_checkstring(L, 5); + nvgTextBox(*ctx, x, y, breakRowWidth, string, NULL); + return 0; +} + +int nvgTextBounds_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + const char *string = luaL_checkstring(L, 4); + float bounds[4]; + nvgTextBounds(*ctx, x, y, string, NULL, bounds); + for (int i=0; i<4; i++) { + lua_pushnumber(L, bounds[i]); + } + return 4; +} + +int nvgTextBoxBounds_bind(lua_State *L) +{ + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + const char *string = luaL_checkstring(L, 4); + float breakRowWidth = luaL_checknumber(L, 5); + float bounds[4]; + nvgTextBoxBounds(*ctx, x, y, breakRowWidth, string, NULL, bounds); + for (int i=0; i<4; i++) { + lua_pushnumber(L, bounds[i]); + } + return 4; +} diff --git a/src/nvg/transform.c b/src/nvg/transform.c index a4ab1eb..6fe0c3f 100644 --- a/src/nvg/transform.c +++ b/src/nvg/transform.c @@ -7,7 +7,7 @@ int nvgResetTransform_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); nvgResetTransform(*ctx); return 0; } @@ -15,21 +15,21 @@ int nvgResetTransform_bind(lua_State *L) int nvgTransform_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float a = luaL_checknumber(L, 2); float b = luaL_checknumber(L, 3); float c = luaL_checknumber(L, 4); float d = luaL_checknumber(L, 5); float e = luaL_checknumber(L, 6); float f = luaL_checknumber(L, 7); - nvgTransform(ctx, a, b, c, d, e, f); + nvgTransform(*ctx, a, b, c, d, e, f); return 0; } int nvgTranslate_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); nvgTranslate(*ctx, x, y); @@ -39,7 +39,7 @@ int nvgTranslate_bind(lua_State *L) int nvgRotate_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float angle = luaL_checknumber(L, 2); nvgRotate(*ctx, angle); return 0; @@ -48,7 +48,7 @@ int nvgRotate_bind(lua_State *L) int nvgSkewX_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float angle = luaL_checknumber(L, 2); nvgSkewX(*ctx, angle); return 0; @@ -57,7 +57,7 @@ int nvgSkewX_bind(lua_State *L) int nvgSkewY_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float angle = luaL_checknumber(L, 2); nvgSkewY(*ctx, angle); return 0; @@ -66,7 +66,7 @@ int nvgSkewY_bind(lua_State *L) int nvgScale_bind(lua_State *L) { - NVGcontext **ctx = lua_checkudata(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); nvgScale(*ctx, x, y); @@ -76,7 +76,7 @@ int nvgScale_bind(lua_State *L) int nvgCurrentTransform_bind(lua_State *L) { - NVGcontext **ctx = luaL_checkudate(L, 1, nvg_ctx_tname); + NVGcontext **ctx = luaL_checkudata(L, 1, nvg_ctx_tname); float xform[6]; nvgCurrentTransform(*ctx, xform); for (int i=0; i<6; i++) { diff --git a/util/generate-binding.lua b/util/generate-binding.lua new file mode 100644 index 0000000..1c5359f --- /dev/null +++ b/util/generate-binding.lua @@ -0,0 +1,62 @@ +local b = {} +setmetatable(b, {__index=G}) +setfenv(1, b) + + +--===== enums =====-- +local Enum = {} +-- create a single enum element +function Enum.new(name, id) + local self = { + name = string.upper(name), + id = id, + } + setmetatable(self, Enum) + return self +end +-- create multiple enums +function Enum.new_multi(tbl) + local enums = {} + for id, name in ipairs(tbl) do + table.insert(enums, Enum.new(name, id)) + end + return enums +end +-- make enums read-only +function Enum.__newindex(self) + error("Attempted to set index on Enum") +end +-- make enums print nicely +function Enum.__tostring(self) + return self.name +end +-- allow comparinge enums +function Enum.__eq(self, other) + return self.id == other.id +end + + +local function check_match(string, pattern, rule) + local match = string.match(string, '^' .. pattern) + if match then + return match, rule(match) + end +end + +local function append_match(string, pattern, rule, + + +local function lex(string) + for _, typename in ipairs(c_int_types) do + if (string.sub(string, 1, #typename) == typename) then + return + end +end + + +function bind(signature) + +end + + +return b diff --git a/util/test.lua b/util/test.lua new file mode 100644 index 0000000..9b9fc49 --- /dev/null +++ b/util/test.lua @@ -0,0 +1,40 @@ +function test(description, func) + io.write(description .. ': ') + local result, msg = pcall(func) + if result == true then + print("OK") + else + print("FAIL") + print(debug.traceback(msg)) + print() + end +end + + +local b = require 'generate-binding' + + +test("simplest possible binding", function() + local binding = b.bind("void some_function();") + assert(binding == [[ +int some_function_bind(lua_State *L) +{ + some_function(); + return 0; +}]]) +end) + + +test("complicated binding", function() + local binding = b.bind("const char * qqq(int a, float q, unsigned char m);") + assert(binding == [[ +int qqq_bind(lua_State *L) +{ + lua_Integer a = luaL_checkinteger(L, 1); + lua_Number q = luaL_checknumber(L, 2); + lua_Integer m = luaL_checkinteger(L, 3); + const char *result = qqq(a, q, m); + lua_pushstring(L, result); + return 1; +}]]) +end) |