Events. Quit on mouse move or press like a screensaver

This commit is contained in:
nothke 2024-06-16 14:50:36 +02:00
parent 1c49ffa07f
commit fc91b57f33
2 changed files with 21 additions and 2 deletions

View File

@ -15,6 +15,9 @@ pub fn build(b: *std.Build) void {
exe.root_module.addImport("sokol", dep_sokol.module("sokol")); exe.root_module.addImport("sokol", dep_sokol.module("sokol"));
// Set this to hide console
//exe.subsystem = .Windows;
b.installArtifact(exe); b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);

View File

@ -104,7 +104,7 @@ export fn init() void {
pass_action.colors[0] = .{ pass_action.colors[0] = .{
.load_action = .CLEAR, .load_action = .CLEAR,
.clear_value = .{ .r = 0, .g = 1, .b = 0, .a = 1 }, .clear_value = .{},
}; };
std.log.info("Backend: {}\n", .{sg.queryBackend()}); std.log.info("Backend: {}\n", .{sg.queryBackend()});
@ -133,13 +133,29 @@ export fn frame() void {
sg.commit(); 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 { pub fn main() !void {
sokol.app.run(.{ sokol.app.run(.{
.init_cb = init, .init_cb = init,
.frame_cb = frame, .frame_cb = frame,
.cleanup_cb = cleanup, .cleanup_cb = cleanup,
.event_cb = event,
.width = 800, .width = 800,
.height = 600, .height = 600,
.fullscreen = true, .fullscreen = true,