added layout time

This commit is contained in:
sirlilpanda
2026-01-15 17:41:18 +13:00
parent 6249f6784f
commit 894731c646

View File

@@ -2,7 +2,7 @@ const std = @import("std");
const rl = @import("rl"); const rl = @import("rl");
const bmb = @import("pandas_ui_lib"); const bmb = @import("pandas_ui_lib");
fn getTextWidth(string: []const u8, font: rl.Font) i32 { fn getTextWidth(string: []const u8, font: rl.Font) f32 {
var width: c_int = 0; var width: c_int = 0;
for (string) |char| { for (string) |char| {
width += width +=
@@ -11,13 +11,13 @@ fn getTextWidth(string: []const u8, font: rl.Font) i32 {
} }
const s = @as([:0]const u8, @ptrCast(string)); const s = @as([:0]const u8, @ptrCast(string));
return @as(bmb.Real, @intCast(rl.measureText(s, @as(i32, @intCast(font.baseSize))))); return @as(bmb.Real, @floatFromInt(rl.measureText(s, font.baseSize)));
} }
fn getTextheight(string: []const u8, font: rl.Font) i32 { fn getTextheight(string: []const u8, font: rl.Font) f32 {
_ = string; _ = string;
// std.debug.print("font.baseSize : {}\n", .{font.baseSize}); // std.debug.print("font.baseSize : {}\n", .{font.baseSize});
return @as(bmb.Real, @intCast(font.baseSize)); return @as(bmb.Real, @floatFromInt(font.baseSize));
} }
const TextType = bmb.TextType(rl.Font, getTextWidth, getTextheight); const TextType = bmb.TextType(rl.Font, getTextWidth, getTextheight);
@@ -44,7 +44,12 @@ fn sideBar() UI.Node {
} }
fn button() UI.Node { fn button() UI.Node {
return UI.Element(.{ return UI.ElementWborder(
.{ .bottom = 5, .left = 10, .right = 20, .top = 30 },
.{
.b = 30,
},
.{
.name = "button", .name = "button",
.rect = .{ .rect = .{
.h = 20, .h = 20,
@@ -55,14 +60,16 @@ fn button() UI.Node {
.g = 255, .g = 255,
}, },
}, },
}); },
);
} }
pub fn main() anyerror!void { pub fn main() anyerror!void {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
const screenWidth = 1920; const screenWidth = 1200;
const screenHeight = 1080; const screenHeight = 700;
var timer = try std.time.Timer.start();
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
defer rl.closeWindow(); // Close window and OpenGL context defer rl.closeWindow(); // Close window and OpenGL context
@@ -71,6 +78,7 @@ pub fn main() anyerror!void {
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
std.debug.print("bmb version :{s}\n", .{bmb.VERSON}); std.debug.print("bmb version :{s}\n", .{bmb.VERSON});
std.debug.print("raylib time : {}\n", .{@divTrunc(timer.lap(), std.time.ns_per_ms)});
const child = &[_]UI.Node{ const child = &[_]UI.Node{
sideBar(), sideBar(),
@@ -83,7 +91,7 @@ pub fn main() anyerror!void {
.padding = .{ .padding = .{
.bottom = 10, .bottom = 10,
.left = 10, .left = 10,
.right = 10, .right = 10.4,
.top = 10, .top = 10,
}, },
}, },
@@ -279,6 +287,7 @@ pub fn main() anyerror!void {
}, },
}), }),
button(), button(),
button(),
}; };
const root = UI.Element(.{ const root = UI.Element(.{
@@ -311,6 +320,7 @@ pub fn main() anyerror!void {
const sized = try UI.resolveSizing(al.allocator(), root); const sized = try UI.resolveSizing(al.allocator(), root);
// UI.printTree(sized); // UI.printTree(sized);
const commands = try UI.getRenderCommands(sized, al.allocator()); const commands = try UI.getRenderCommands(sized, al.allocator());
std.debug.print("layout time : {}\n", .{@divTrunc(timer.lap(), std.time.ns_per_ms)});
// Main game loop // Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key while (!rl.windowShouldClose()) { // Detect window close button or ESC key
@@ -331,12 +341,12 @@ pub fn main() anyerror!void {
if (r.rounding) |rounding| { if (r.rounding) |rounding| {
rl.drawRectangleRounded( rl.drawRectangleRounded(
rl.Rectangle.init( rl.Rectangle.init(
@as(f32, @floatFromInt(r.pos.x)), r.pos.x,
@as(f32, @floatFromInt(r.pos.y)), r.pos.y,
@as(f32, @floatFromInt(r.rect.w)), r.rect.w,
@as(f32, @floatFromInt(r.rect.h)), r.rect.h,
), ),
@as(f32, @floatFromInt(rounding)) / 100.0, rounding / 100.0,
10, 10,
rl.Color.init( rl.Color.init(
r.colour.r, r.colour.r,
@@ -346,7 +356,12 @@ pub fn main() anyerror!void {
), ),
); );
} else { } else {
rl.drawRectangle(r.pos.x, r.pos.y, r.rect.w, r.rect.h, rl.Color.init( rl.drawRectangleRec(rl.Rectangle.init(
r.pos.x,
r.pos.y,
r.rect.w,
r.rect.h,
), rl.Color.init(
r.colour.r, r.colour.r,
r.colour.g, r.colour.g,
r.colour.b, r.colour.b,
@@ -356,7 +371,7 @@ pub fn main() anyerror!void {
}, },
.text => |t| { .text => |t| {
const string = @as([:0]const u8, @ptrCast(t.text.string)); const string = @as([:0]const u8, @ptrCast(t.text.string));
rl.drawText(string, t.pos.x, t.pos.y, t.text.getTextHeight(), rl.Color.init( rl.drawText(string, @as(i32, @intFromFloat(t.pos.x)), @as(i32, @intFromFloat(t.pos.y)), @as(i32, @intFromFloat(t.text.getTextHeight())), rl.Color.init(
t.text.colour.r, t.text.colour.r,
t.text.colour.g, t.text.colour.g,
t.text.colour.b, t.text.colour.b,