From fc91b57f339d1a4de3f250e87e2f6b856dad207c Mon Sep 17 00:00:00 2001 From: nothke Date: Sun, 16 Jun 2024 14:50:36 +0200 Subject: [PATCH] Events. Quit on mouse move or press like a screensaver --- build.zig | 3 +++ src/main.zig | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 0230034..e899bc6 100644 --- a/build.zig +++ b/build.zig @@ -15,6 +15,9 @@ pub fn build(b: *std.Build) void { exe.root_module.addImport("sokol", dep_sokol.module("sokol")); + // Set this to hide console + //exe.subsystem = .Windows; + b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/src/main.zig b/src/main.zig index 86ccd7f..09cec01 100644 --- a/src/main.zig +++ b/src/main.zig @@ -104,7 +104,7 @@ export fn init() void { pass_action.colors[0] = .{ .load_action = .CLEAR, - .clear_value = .{ .r = 0, .g = 1, .b = 0, .a = 1 }, + .clear_value = .{}, }; std.log.info("Backend: {}\n", .{sg.queryBackend()}); @@ -133,13 +133,29 @@ export fn frame() void { sg.commit(); } -export fn cleanup() void {} +export fn cleanup() void { + std.log.info("Ended", .{}); +} + +const Event = sokol.app.Event; + +export fn event(eptr: [*c]const Event) void { + const e: *const Event = @ptrCast(eptr); + + const buttonPressed = e.type == .MOUSE_DOWN or e.type == .KEY_DOWN; + const mouseMoved = e.type == .MOUSE_MOVE and (@abs(e.mouse_dx) > 2 or @abs(e.mouse_dy) > 2); + + if (buttonPressed or mouseMoved) { + sokol.app.quit(); + } +} pub fn main() !void { sokol.app.run(.{ .init_cb = init, .frame_cb = frame, .cleanup_cb = cleanup, + .event_cb = event, .width = 800, .height = 600, .fullscreen = true,