new primitive: ly.putLabel()

This commit is contained in:
RadsammyT 2026-06-17 15:58:14 -04:00
commit d2d7a55e1a
No known key found for this signature in database
2 changed files with 39 additions and 0 deletions

View file

@ -22,6 +22,9 @@
--
-- For the byte argument, you may use string.byte to fill this argument.
--
-- putLabel(str, fg, bg, x, y) -- Draw text in argument str. See putCell()
-- for info on the rest of the arguments.
--
-- clock() -- The time, in microseconds.
-- }
--
@ -69,6 +72,7 @@ for i = 1, SQUARE_COUNT do
end
local timer = ly.clock()
local perf = ly.clock()
function draw()
-- Rather than progressing the animation by frame, do it based on
@ -105,4 +109,9 @@ function draw()
end
end
end
local new_perf = ly.clock()
local str = "FT: "..((new_perf - perf) / 1000).."ms"
ly.putLabel(str , 0x00FFFFFF, 0, (ly.width/2) - (string.len(str)/2), ly.height-1)
perf = new_perf
end

View file

@ -95,6 +95,9 @@ pub fn init(
_ = self.lua.pushString("putCell");
self.lua.pushFunction(luaPutCell);
self.lua.setTable(-3);
_ = self.lua.pushString("putLabel");
self.lua.pushFunction(luaPutLabel);
self.lua.setTable(-3);
self.lua.setGlobal("ly");
self.lua.doFile(zf) catch {
@ -224,3 +227,30 @@ fn luaPutCell(state: ?*zlua.LuaState) callconv(.c) c_int {
}) 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 ";
const str = lua.toString(1) catch {
const t = lua.typeName(lua.typeOf(2));
lua.raiseErrorStr(MSG ++ "str to string", .{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});
};
TerminalBuffer.drawText(str, x, y, fg, bg) catch {};
return 0;
}