From d7102e5b33b0dbd709a62ebcf9b237914a304daa Mon Sep 17 00:00:00 2001 From: Axel Meyer Date: Sun, 17 May 2026 19:58:37 +0200 Subject: [PATCH] feat: add render + load_rig/load_animation convenience wrappers Render: each bone drawn as a rotated rect of fixed length+thickness (16x4 px) centered at the bone's world-position with its current angle. Sprite-based rendering deferred to Phase 2 when tilesets exist. Load helpers: engine.asset.load_json(path) + build_rig/build_animation. The engine provides a single-call JSON loader (reads file + parses JSON -> Lua table) via engine.asset.load_json; there is no separate cjson.decode API. Confirmed by engine_lua_asset.c and maps lib usage. v0.1 complete: skeleton + bones + tracks + rest + keyframe + procedural + look-at + render + load. Footplant deferred. --- init.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/init.lua b/init.lua index 2847e6e..4be5fe2 100644 --- a/init.lua +++ b/init.lua @@ -417,4 +417,44 @@ function M.update(dt) end end +-- ==================================================================== +-- Rendering (engine.render.* -- only callable in render-phase) +-- ==================================================================== +function M.render(handle) + local p = all_puppets[handle] + if p == nil then return end + + -- Draw each bone as a rotated rect centered at the bone's world-pos. + -- v0.1 simplification: bone's world-pos = puppet origin + bone.rest offset. + -- Bone "length" is approximated by a small constant; later versions will + -- derive from rig data. + local BONE_LENGTH = 16 + local BONE_THICKNESS = 4 + + for _, b_src in ipairs(p.rig.bones) do + local b = p.rig.bones_by_id[b_src.id] + local x = p.x + b.rest.x + local y = p.y + b.rest.y + local angle = p.bone_state[b.id].angle + local c = b.color + local color = engine.render.rgb(c[1], c[2], c[3]) + engine.render.draw_rect_rotated(x, y, BONE_LENGTH, BONE_THICKNESS, angle, color) + end +end + +-- ==================================================================== +-- Asset loading (convenience wrappers around build_rig/build_animation) +-- ==================================================================== +-- engine.asset.load_json(path) reads the file and parses JSON in one +-- call, returning a Lua table directly. No separate decode step needed. +function M.load_rig(path) + local rig_table = engine.asset.load_json(path) + return M.build_rig(rig_table) +end + +function M.load_animation(path, rig) + local anim_table = engine.asset.load_json(path) + return M.build_animation(anim_table, rig) +end + return M