new primitive: ly.putRect()

This commit is contained in:
RadsammyT 2026-06-19 01:19:07 -04:00
commit e5fdb3a928
No known key found for this signature in database
2 changed files with 50 additions and 5 deletions

View file

@ -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()

View file

@ -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 ";