Compare commits

..

2 Commits

Author SHA1 Message Date
nothke
2524dbf206 Slightly better time management 2024-07-04 16:02:52 +02:00
nothke
fc91b57f33 Events. Quit on mouse move or press like a screensaver 2024-06-16 14:50:36 +02:00
2 changed files with 30 additions and 4 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,20 +104,27 @@ 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()});
sokol.app.showMouse(false);
} }
fn timef() f32 { fn timef() f32 {
return @floatCast(sokol.time.sec(sokol.time.now())); return @floatCast(sokol.time.sec(sokol.time.now()));
} }
var time: f32 = 0;
export fn frame() void { export fn frame() void {
const dt: f32 = @floatCast(sokol.app.frameDuration());
time += dt;
const col = &pass_action.colors[0].clear_value; const col = &pass_action.colors[0].clear_value;
col.g = @abs(@sin(timef())); col.g = @abs(@sin(time));
col.r = @abs(@cos(timef())); col.r = @abs(@cos(time));
state.vsParams.aspectRatio = sokol.app.heightf() / sokol.app.widthf(); state.vsParams.aspectRatio = sokol.app.heightf() / sokol.app.widthf();
@ -133,13 +140,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,