diff options
-rw-r--r-- | demo/vector/common.lua | 80 | ||||
-rw-r--r-- | demo/vector/main.lua | 50 | ||||
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/vector/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/vector/vector.c | 155 |
5 files changed, 289 insertions, 8 deletions
diff --git a/demo/vector/common.lua b/demo/vector/common.lua new file mode 100644 index 0000000..7ddd487 --- /dev/null +++ b/demo/vector/common.lua @@ -0,0 +1,80 @@ +honey.run = function() + local gl = honey.gl + local window = honey.window + + -- initialize opengl + gl.Init() + window.setHint(window.hintType.contextVersionMajor, 3) + window.setHint(window.hintType.contextVersionMinor, 3) + + local win = window.create(640, 480, 'first person demo') + honey.window.win = win + window.makeContextCurrent(win) + gl.InitGlad() + gl.Enable(gl.DEPTH_TEST) + + if honey.init then + honey.init() + end + + window.setFramebufferSizeCallback(win, function(_, width, height) + if honey.windowSizeCallback then + honey.windowSizeCallback(width, height) + end + end) + + local time = 0 + drawTime = 1/60 + while not window.shouldClose(win) do + local t = window.getTime() + local dt = t-time + time = t + + honey.update(dt) + window.pollEvents() + + if time > drawTime then + if honey.clearColor then + gl.ClearColor( + honey.clearColor.r, + honey.clearColor.g, + honey.clearColor.b, + 1.0 + ) + else + gl.ClearColor(0.2, 0.3, 0.3, 1.0) + end + gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) + honey.draw() + window.swapBuffers(win) + drawTime = drawTime + 1/60 + end + end + + window.destroy(win) + gl.Terminate() +end + + +local set = honey.glm.vec3_set +local get = honey.glm.vec3_get +function honey.glm.setVector(vec, x, y, z) + set(vec, 0, x) + set(vec, 1, y) + set(vec, 2, z) +end + + +function honey.glm.makeVector(x, y, z) + local v = honey.glm.vec3() + honey.glm.setVector(v, x, y, z) + return v +end + + +function honey.glm.vec3_tostring(v) + return string.format( + '[%0.2f, %0.2f, %0.2f]', + get(v, 0), get(v, 1), get(v, 2) + ) +end diff --git a/demo/vector/main.lua b/demo/vector/main.lua new file mode 100644 index 0000000..595131b --- /dev/null +++ b/demo/vector/main.lua @@ -0,0 +1,50 @@ +require 'common' + +local window = honey.window +local nvg = honey.nvg + + +local vg + +function honey.init() + vg = nvg.Context() +end + + +local time = 0 +local frames = 0 +function honey.update(dt) + if window.getKey(window.win, window.KEY_ESCAPE) == window.PRESS then + window.setShouldClose(honey.window.win, true) + end + + time = time + dt + frames = frames + 1 + if time > 5 then + print('fps:', frames/5) + frames = 0 + time = time - 5 + end +end + + +function honey.draw() + nvg.BeginFrame(vg, 640, 480, 1.0) + nvg.StrokeWidth(vg, 20) + nvg.StrokeColor(vg, 1, 0, 0, 1) + + local w = 640 + local h = 480 + + nvg.BeginPath(vg) + nvg.MoveTo(vg, w*0.2, h*.5) + nvg.LineTo(vg, w*0.4, h * (0.5 + 0.5*math.sin(math.pi * time))); + nvg.LineTo(vg, w*0.6, h * (0.5 - 0.5*math.sin(math.pi * time))); + nvg.LineTo(vg, w*0.8, h*.5) + nvg.Stroke(vg) + + nvg.EndFrame(vg) +end + + +honey.run() @@ -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"); |