From e5fdb3a9280de1570e20bc2672198227c97f7986 Mon Sep 17 00:00:00 2001 From: RadsammyT Date: Fri, 19 Jun 2026 01:19:07 -0400 Subject: [PATCH] new primitive: `ly.putRect()` --- res/example.lua | 12 +++++++----- src/animations/Lua.zig | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/res/example.lua b/res/example.lua index 8b34321..7309da6 100644 --- a/res/example.lua +++ b/res/example.lua @@ -22,6 +22,12 @@ -- -- For the byte argument, you may use string.byte to fill this argument. -- +-- putCell(byte, fg, bg, x, y, w, h) -- Draw a rectangle. +-- Arguments are the same as putCell except for w and h, which are also +-- unsigned integers. The rectangle will be drawn from the top-left, with +-- argument w extending it to the right and argument h extending downwards. +-- +-- -- putLabel(str, fg, bg, x, y) -- Draw text in argument str. See putCell() -- for info on the rest of the arguments. -- @@ -103,11 +109,7 @@ function draw() for i, v in ipairs(squares) do - for x = v.x, v.x + SQUARE_WIDTH do - for y = v.y, v.y + SQUARE_HEIGHT do - ly.putCell(string.byte(' '), 0, v.color, x, y) - end - end + ly.putRect(string.byte(' '), 0, v.color, v.x, v.y, SQUARE_WIDTH, SQUARE_HEIGHT) end local new_perf = ly.clock() diff --git a/src/animations/Lua.zig b/src/animations/Lua.zig index 5c71115..d212e03 100644 --- a/src/animations/Lua.zig +++ b/src/animations/Lua.zig @@ -101,6 +101,9 @@ pub fn init( _ = self.lua.pushString("putLabel"); self.lua.pushFunction(luaPutLabel); self.lua.setTable(-3); + _ = self.lua.pushString("putRect"); + self.lua.pushFunction(luaPutRect); + self.lua.setTable(-3); self.lua.setGlobal("ly"); self.lua.doFile(zf) catch { @@ -231,6 +234,46 @@ fn luaPutCell(state: ?*zlua.LuaState) callconv(.c) c_int { return 0; } +fn luaPutRect(state: ?*zlua.LuaState) callconv(.c) c_int { + const lua: *zlua.Lua = @ptrCast(@alignCast(state orelse unreachable)); + const MSG = "ly.putRect: cannot convert %s-typed "; + const byte = lua.toNumeric(u32, 1) catch { + const t = lua.typeName(lua.typeOf(1)); + lua.raiseErrorStr(MSG ++ "byte to u32", .{t.ptr}); + }; + const fg = lua.toNumeric(u32, 2) catch { + const t = lua.typeName(lua.typeOf(2)); + lua.raiseErrorStr(MSG ++ "fg to u32", .{t.ptr}); + }; + const bg = lua.toNumeric(u32, 3) catch { + const t = lua.typeName(lua.typeOf(3)); + lua.raiseErrorStr(MSG ++ "bg to u32", .{t.ptr}); + }; + const x = lua.toNumeric(usize, 4) catch { + const t = lua.typeName(lua.typeOf(4)); + lua.raiseErrorStr(MSG ++ "x to usize", .{t.ptr}); + }; + const y = lua.toNumeric(usize, 5) catch { + const t = lua.typeName(lua.typeOf(5)); + lua.raiseErrorStr(MSG ++ "y to usize", .{t.ptr}); + }; + const w = lua.toNumeric(usize, 6) catch { + const t = lua.typeName(lua.typeOf(5)); + lua.raiseErrorStr(MSG ++ "w to usize", .{t.ptr}); + }; + const h = lua.toNumeric(usize, 7) catch { + const t = lua.typeName(lua.typeOf(5)); + lua.raiseErrorStr(MSG ++ "h to usize", .{t.ptr}); + }; + for (0..w) |wx| for (0..h) |hy| + TerminalBuffer.setCell(x + wx, y + hy, .{ + .fg = fg, + .bg = bg, + .ch = byte, + }) catch {}; + return 0; +} + fn luaPutLabel(state: ?*zlua.LuaState) callconv(.c) c_int { const lua: *zlua.Lua = @ptrCast(@alignCast(state orelse unreachable)); const MSG = "ly.putLabel: cannot convert %s-typed ";