diff options
| author | Steven Van Dorp <steven@vandorp.lu> | 2026-02-06 10:00:08 +0100 |
|---|---|---|
| committer | Steven Van Dorp <steven@vandorp.lu> | 2026-02-06 10:00:08 +0100 |
| commit | 0d8aeb6464103ec8e35c10c448bf08b0b9edc156 (patch) | |
| tree | 80ab47bf3e0f137856d8494753e774c8456c85c8 /src/app/app.zig | |
Diffstat (limited to 'src/app/app.zig')
| -rw-r--r-- | src/app/app.zig | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/app/app.zig b/src/app/app.zig new file mode 100644 index 0000000..1f33960 --- /dev/null +++ b/src/app/app.zig @@ -0,0 +1,126 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const mem = std.mem; +const fmt = std.fmt; +const fs = std.fs; +const io = std.io; + +const Allocator = mem.Allocator; +const Thread = std.Thread; + +const input = @import("input.zig"); +const gui = @import("ui.zig"); + +var allocator: Allocator = undefined; + +pub const Pixel = packed struct { + r: u8 = 0, + g: u8 = 0, + b: u8 = 0, + _a: u8 = undefined, +}; +var pixel_buffer: *const []Pixel = undefined; + +pub const Action = enum { + primary, + text_cursor_left, + text_cursor_right, +}; + +// DEBUG +var img: gui.Image = undefined; +var font: gui.Font = undefined; + +pub fn init() !void { + allocator = @import("root").allocator; + + img = try .fromPath("assets/img/test_image.png", allocator); + //font = try .fromPath("assets/font/OpendyslexicRegular.otf", allocator, .{.font_height = 128}); + font = try .fromPath("assets/font/OpenSans-VariableFont_wdth,wght.ttf", allocator, .{.font_height = 256}); + + try input.bindPointerButton(.primary, .left); + try input.bindKey(.text_cursor_left, .arrow_left); + try input.bindKey(.text_cursor_right, .arrow_right); + + try gui.globalInit(allocator); + ui = try gui.Context.init(.{.allocator = allocator}); + errdefer ui.deinit(); + + user_input = try ui.textBufferFromString("samplwwwawwwwwwwwwwwwwwwwwwwwwwwwwwwww"); +} + +pub fn deinit() void { + font.deinit(); + img.deinit(); + + user_input.deinit(); + ui.deinit(); + gui.globalDeinit(); +} + +var window_size: @Vector(2, u32) = undefined; +var fwindow_size: @Vector(2, f32) = undefined; +pub fn setWindowSize(ws: @Vector(2, c_uint)) void { + window_size = @intCast(ws); + fwindow_size = @floatFromInt(window_size); + pixel_buffer = @ptrCast(@import("root").pixels); +} + +var ui: gui.Context = undefined; +var user_input: gui.TextBuffer = undefined; +var dbg_poss = [3]gui.V2{.{0.1, 0.1}, .{0.2, 0.2}, .{0.3, 0.3}}; +var dbg_colors = [3]gui.Color{.hex(0xFFFF0000), .hex(0xFF00FF00), .hex(0xFF0000FF)}; +var time: f32 = 0; +pub fn loop(dt: f32) !bool { + // TODO styles or something. TBD once we have some real world tests + ui.frameStart(); + //std.debug.print("{d}\n", .{gui.mouse_delta}); + //if (@reduce(.And, gui.mouse_delta == @Vector(2, f32){0, 0})) return false; + time += dt; + try ui.textFmt("{d:.1} FPS", .{1/dt}, .{.pos = .{0, 0}, .size = .{1, 1}}, .{.font = &font, .scale = 0.1}); + //std.debug.print("{d:.1}\n", .{1/dt}); + + //const pos = gui.V2{-1+time*0.1,-1+time*0.1}; + //const color = gui.Color.float(1,1,0,0.5); + //try ui.rectangle(.{.pos = pos, .size = .{1,1}}, .{.color = color}); + try ui.rectangle(.{.pos = .{0,0}, .size = .{1,1}}, .{.color = .float(1,1,1,1), .image = img}); + + //try ui.areaBegin(.{.rect = .{.pos = .{time * 0.0, 0.0}, .size = .{0.9-0.0*time, 0.9}}, .children_offset = .{time * 0.0, -time * 0.1}}); + //for (0..1) |i| { + // try ui.textField(&user_input, @import("root").utf8_of_keys_pressed, .{.pos = .{0, 0.1 + 0.1 * @as(f32, @floatFromInt(i)) / 3}, .size = .{1, 1}}, .{ + // .text_options = .{.font = &font, .scale = 0.05}, + // .cursor_to_hovered_glpyh_if = input.ended(.primary), + // .cursor_move_left = input.ended(.text_cursor_left), + // .cursor_move_right = input.ended(.text_cursor_right), + // }); + //} + //try ui.rectangle(.{.pos = .{time * 0.0, 0}, .size = .{1,1}}, .{ + // .color = .hex(0xffff0000), + //}); + //try ui.rectangle(ui._current_area.rect, .{.color = .hex(0xff00ff00), .apply_area_transformations = false, .clip_to_area = true}); + //try ui.areaEnd(); + + //for (&dbg_poss, &dbg_colors) |*dbg_pos, dbg_color| { + // var rect_drag: ?gui.V2 = null; + // try ui.rectangle(.{.pos = dbg_pos.*, .size = .{1, 0.2}}, .{ + // .color = dbg_color, + // .drag = &rect_drag, + // .begin_drag_if_hover_and = input.started(.primary), + // .end_drag_if = input.ended(.primary), + // }); + // if (rect_drag) |drag| { + // dbg_pos.* += drag; + // dbg_pos[0] = @max(dbg_pos[0], 0); + // dbg_pos[1] = @max(dbg_pos[1], 0); + // } + //} + + gui.clear(@ptrCast(pixel_buffer.*)); + try ui.draw(.{ + .pixels = @ptrCast(pixel_buffer.*), + .bytes_per_pixel = @sizeOf(Pixel), + .pixels_per_row = window_size[0], + }); + return true; +} + |
