updated to 0.16.0 and new text size func

This commit is contained in:
2026-05-10 00:53:45 +12:00
parent 18fb90638f
commit 058c3c4156

View File

@@ -1,26 +1,24 @@
const std = @import("std"); const std = @import("std");
const rl = @import("rl"); const rl = @import("rl");
const shoots = @import("root.zig"); const shoots = @import("shoots");
fn getTextWidth(string: []const u8, font: rl.Font) shoots.Real { fn getTextSize(string: []const u8, font: rl.Font, max_width: ?shoots.Real) shoots.Rect {
var width: c_int = 0; _ = max_width;
for (string) |char| {
width += const text_size = rl.measureTextEx(
@as(c_int, @intFromFloat(font.recs[char - 32].width)) + font,
font.glyphs[char - 32].offsetX; @as([:0]const u8, @ptrCast(string)),
@floatFromInt(font.baseSize),
@floatFromInt(1),
);
return shoots.Rect{
.h = @as(shoots.Real, text_size.y),
.w = @as(shoots.Real, text_size.x),
};
} }
const s = @as([:0]const u8, @ptrCast(string)); const TextType = shoots.TextType(rl.Font, getTextSize);
return @as(shoots.Real, @floatFromInt(rl.measureText(s, font.baseSize)));
}
fn getTextheight(string: []const u8, font: rl.Font) shoots.Real {
_ = string;
// std.debug.print("font.baseSize : {}\n", .{font.baseSize});
return @as(shoots.Real, @floatFromInt(font.baseSize));
}
const TextType = shoots.TextType(rl.Font, getTextWidth, getTextheight);
const TextureType = shoots.TextureType(rl.Texture2D); const TextureType = shoots.TextureType(rl.Texture2D);
const UI = shoots.Shoots(TextType, TextureType); const UI = shoots.Shoots(TextType, TextureType);
@@ -99,12 +97,14 @@ inline fn button() UI.Node {
); );
} }
pub fn main() anyerror!void { pub fn main(init: std.process.Init) anyerror!void {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
const screenWidth = 1200; const screenWidth = 1200;
const screenHeight = 700; const screenHeight = 700;
var timer = try std.time.Timer.start();
const io = init.io;
const start = std.Io.Clock.awake.now(io);
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
@@ -115,7 +115,7 @@ pub fn main() anyerror!void {
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
std.debug.print("shoots version :{s}\n", .{shoots.VERSION}); std.debug.print("shoots version :{s}\n", .{shoots.VERSION});
std.debug.print("raylib time : {}\n", .{@divTrunc(timer.lap(), std.time.ns_per_ms)}); std.debug.print("raylib time : {}ms\n", .{start.untilNow(io, .awake).toMilliseconds()});
var scroll_offset: shoots.Pos = .{}; var scroll_offset: shoots.Pos = .{};
@@ -144,9 +144,12 @@ pub fn main() anyerror!void {
), ),
UI.Txt( UI.Txt(
UI.Text.init( UI.Text.init(
"hello world again", "hello world again \n this is a new line",
try rl.getFontDefault(), try rl.getFontDefault(),
.{}, .{
.b = 255,
.r = 255,
},
).setBackground(.{ ).setBackground(.{
.a = 255, .a = 255,
.g = 255, .g = 255,
@@ -358,8 +361,6 @@ pub fn main() anyerror!void {
}), }),
}; };
// root.Shoots(root.TextType(raylib.Font,(function 'getTextWidth'),(function 'getTextheight')),root.TextureType(raylib.Texture)).NodeTypes
// root.Shoots(root.TextType(raylib.Font,(function 'getTextWidth'),(function 'getTextheight')),root.TextureType(raylib.Texture)).Node
const root = UI.Element(.{ const root = UI.Element(.{
.name = "root", .name = "root",
.pos = .{ .pos = .{
@@ -402,7 +403,8 @@ pub fn main() anyerror!void {
rl.beginDrawing(); rl.beginDrawing();
defer rl.endDrawing(); defer rl.endDrawing();
// std.debug.print("starting layout\n", .{}); // std.debug.print("starting layout\n", .{});
_ = timer.lap(); var start_time = std.Io.Timestamp.now(init.io, std.Io.Clock.awake);
var sized = try UI.resolveSizing(al.allocator(), root); var sized = try UI.resolveSizing(al.allocator(), root);
const mouse_pos = rl.getMousePosition(); const mouse_pos = rl.getMousePosition();
@@ -423,9 +425,7 @@ pub fn main() anyerror!void {
const commands = try UI.getRenderCommands(sized, al.allocator()); const commands = try UI.getRenderCommands(sized, al.allocator());
// std.debug.print("{f}\n", .{sized}); // std.debug.print("{f}\n", .{sized});
std.debug.print("layout time : {}ms\n", .{ std.debug.print("layout time : {}ns\n", .{start_time.untilNow(io, .awake).toNanoseconds()});
@as(f32, @floatFromInt(timer.lap())) / @as(f32, @floatFromInt(std.time.ns_per_ms)),
});
for (commands.items) |command| { for (commands.items) |command| {
// std.debug.print("command : {any}\n", .{command}); // std.debug.print("command : {any}\n", .{command});
@@ -469,7 +469,7 @@ pub fn main() anyerror!void {
string, string,
@as(i32, @intFromFloat(t.pos.x)), @as(i32, @intFromFloat(t.pos.x)),
@as(i32, @intFromFloat(t.pos.y)), @as(i32, @intFromFloat(t.pos.y)),
@as(i32, @intFromFloat(t.text.getTextHeight())), @as(i32, t.text.font.baseSize),
rl.Color.init( rl.Color.init(
t.text.colour.r, t.text.colour.r,
t.text.colour.g, t.text.colour.g,
@@ -504,7 +504,7 @@ pub fn main() anyerror!void {
// clip.rect.h, // clip.rect.h,
// ), .blue); // ), .blue);
}, },
.clip_end => |_| { .clip_end => {
rl.endScissorMode(); rl.endScissorMode();
}, },
} }