Compare commits

...

3 Commits

4 changed files with 48 additions and 47 deletions

View File

@@ -23,8 +23,9 @@ pub fn build(b: *std.Build) void {
}), }),
}); });
exe.linkLibrary(raylib_artifact);
exe.root_module.addImport("shoots", shoots.module("shoots")); exe.root_module.addImport("shoots", shoots.module("shoots"));
exe.root_module.linkLibrary(raylib_artifact);
exe.root_module.addImport("rl", raylib_dep.module("raylib")); exe.root_module.addImport("rl", raylib_dep.module("raylib"));
b.installArtifact(exe); b.installArtifact(exe);

View File

@@ -2,11 +2,11 @@
.name = .examples, .name = .examples,
.version = "0.0.0", .version = "0.0.0",
.fingerprint = 0x7bd0ad45c350042a, .fingerprint = 0x7bd0ad45c350042a,
.minimum_zig_version = "0.15.1", .minimum_zig_version = "0.16.0",
.dependencies = .{ .dependencies = .{
.raylib_zig = .{ .raylib_zig = .{
.url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#a4d18b2d1cf8fdddec68b5b084535fca0475f466", .url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#58f3c0fa328fc9ff48d17c2c3771ab9c5114aaa6",
.hash = "raylib_zig-5.6.0-dev-KE8REL5MBQAf3p497t52Xw9P7ojndIkVOWPXnLiLLw2P", .hash = "raylib_zig-6.0.0-KE8REMNkBQCpxwqT9ubVNf5aEOcWRUVIaH2sgt_sDDoZ",
}, },
.shoots = .{ .shoots = .{
.path = "../../", .path = "../../",

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();
}, },
} }

View File

@@ -22,18 +22,20 @@ const Spaceing = 4;
pub fn TextType( pub fn TextType(
comptime FontType: type, comptime FontType: type,
comptime getTextWidthFunc: fn (string: []const u8, FontType) Real, comptime getTextSizeFunc: fn (string: []const u8, FontType, max_width: ?Real) Rect,
comptime getTextHeightFunc: fn (string: []const u8, FontType) Real,
) type { ) type {
return struct { return struct {
const Optional = struct { // const Optional = struct {
font: FontType, // font: FontType,
}; // };
const Self = @This(); const Self = @This();
string: []const u8, string: []const u8,
colour: Colour, colour: Colour,
background: Colour, background: Colour,
font: FontType, font: FontType,
/// this lib just cares about rendering some text
/// text wrapping is your job SDL3 ttf does it :D
max_width: ?Real = null,
pub fn init(string: []const u8, font: FontType, colour: Colour) Self { pub fn init(string: []const u8, font: FontType, colour: Colour) Self {
return Self{ return Self{
@@ -55,12 +57,8 @@ pub fn TextType(
return s; return s;
} }
pub fn getTextWidth(self: Self) Real { pub fn getTextSize(self: Self) Rect {
return getTextWidthFunc(self.string, self.font); return getTextSizeFunc(self.string, self.font, self.max_width);
}
pub fn getTextHeight(self: Self) Real {
return getTextHeightFunc(self.string, self.font);
} }
}; };
} }
@@ -419,9 +417,11 @@ pub fn Shoots(
} }
}, },
.text => { .text => {
parent.element.rect.h += node.text.getTextHeight(); const text_size: Rect = node.text.getTextSize();
parent.element.rect.h += text_size.h;
// std.debug.print("parent.element.rect.h : {}\n", .{parent.element.rect.h}); // std.debug.print("parent.element.rect.h : {}\n", .{parent.element.rect.h});
parent.element.rect.w += node.text.getTextWidth(); parent.element.rect.w += text_size.w;
// std.debug.print("parent.element.rect.w : {}\n", .{parent.element.rect.w}); // std.debug.print("parent.element.rect.w : {}\n", .{parent.element.rect.w});
}, },
.texture => { .texture => {