sokol-playground/src/main.zig

79 lines
1.9 KiB
Zig
Raw Normal View History

2024-06-08 22:48:23 +00:00
const std = @import("std");
2024-06-08 23:15:52 +00:00
const sokol = @import("sokol");
2024-06-11 00:19:48 +00:00
2024-06-08 23:15:52 +00:00
const sg = sokol.gfx;
2024-06-08 22:48:23 +00:00
2024-06-11 00:19:48 +00:00
const shader = @import("shaders/triangle.glsl.zig");
2024-06-09 17:28:42 +00:00
var pass_action: sg.PassAction = .{};
2024-06-08 22:48:23 +00:00
2024-06-11 00:19:48 +00:00
const state = struct {
var bind: sg.Bindings = .{};
var pip: sg.Pipeline = .{};
};
2024-06-09 17:28:42 +00:00
export fn init() void {
sg.setup(.{
.logger = .{ .func = sokol.log.func },
.environment = sokol.glue.environment(),
});
2024-06-09 17:56:09 +00:00
sokol.time.setup();
2024-06-11 00:19:48 +00:00
std.log.info("Vertex buffers len: {}", .{state.bind.vertex_buffers.len});
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32{
// positions colors
0.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0,
}),
});
var pip_desc: sg.PipelineDesc = .{
.shader = sg.makeShader(shader.triangleShaderDesc(sg.queryBackend())),
};
pip_desc.layout.attrs[0].format = .FLOAT3;
pip_desc.layout.attrs[1].format = .FLOAT4;
state.pip = sg.makePipeline(pip_desc);
2024-06-09 17:28:42 +00:00
pass_action.colors[0] = .{
.load_action = .CLEAR,
2024-06-09 17:56:09 +00:00
.clear_value = .{ .r = 0, .g = 1, .b = 0, .a = 1 },
2024-06-09 17:28:42 +00:00
};
2024-06-09 17:56:09 +00:00
std.log.info("Backend: {}\n", .{sg.queryBackend()});
}
fn timef() f32 {
2024-06-10 23:17:51 +00:00
return @floatCast(sokol.time.sec(sokol.time.now()));
2024-06-09 17:28:42 +00:00
}
export fn frame() void {
2024-06-10 23:17:51 +00:00
const col = &pass_action.colors[0].clear_value;
col.g = @abs(@sin(timef()));
col.r = @abs(@cos(timef()));
2024-06-09 17:28:42 +00:00
sg.beginPass(.{ .action = pass_action, .swapchain = sokol.glue.swapchain() });
sg.endPass();
sg.commit();
}
2024-06-08 22:48:23 +00:00
2024-06-08 23:15:52 +00:00
export fn cleanup() void {}
pub fn main() !void {
sokol.app.run(.{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 800,
.height = 600,
.icon = .{ .sokol_default = true },
.window_title = "sokol hello",
.logger = .{ .func = sokol.log.func },
});
2024-06-08 22:48:23 +00:00
}