This commit is contained in:
2025-11-08 18:45:12 +01:00
parent b1cf52b552
commit a956085240
3 changed files with 87 additions and 4 deletions

View File

@@ -1,8 +1,9 @@
const std = @import("std");
const cimgui = @import("cimgui");
const sokol = @import("sokol");
pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
@@ -26,8 +27,23 @@ pub fn build(b: *std.Build) void {
});
sokol_dep.artifact("sokol_clib").addIncludePath(cimgui_dep.path(cimgui_conf.include_dir));
const sokol_mod = sokol_dep.module("sokol");
const shdc_dep = sokol_dep.builder.dependency("shdc", .{});
const shader_mod = try sokol.shdc.createModule(b, "shader", sokol_mod, .{
.shdc_dep = shdc_dep,
.input = "src/shader.glsl",
.output = "shader.zig",
.slang = .{
.glsl430 = true,
.hlsl5 = true,
.metal_macos = true,
},
});
exe_mod.addImport("cimgui", cimgui_dep.module(cimgui_conf.module_name));
exe_mod.addImport("sokol", sokol_dep.module("sokol"));
exe_mod.addImport("shader", shader_mod);
exe_mod.addImport("sokol", sokol_mod);
const exe = b.addExecutable(.{
.name = "voxel-game",

View File

@@ -1,11 +1,13 @@
const sokol = @import("sokol");
const shader = @import("shader");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sglue = sokol.glue;
const simgui = sokol.imgui;
const imgui_pass_action = blk: {
const main_action = blk: {
var ret: sg.PassAction = .{};
ret.colors[0] = .{
@@ -16,6 +18,19 @@ const imgui_pass_action = blk: {
break :blk ret;
};
const imgui_action = blk: {
var ret: sg.PassAction = .{};
ret.colors[0] = .{
.load_action = .LOAD,
};
break :blk ret;
};
var bindings: sg.Bindings = .{};
var pipeline: sg.Pipeline = .{};
fn init() callconv(.c) void {
sg.setup(.{
.environment = sglue.environment(),
@@ -25,6 +40,24 @@ fn init() callconv(.c) void {
simgui.setup(.{
.logger = .{ .func = slog.func },
});
bindings.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32{
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,
}),
});
pipeline = sg.makePipeline(.{
.shader = sg.makeShader(shader.programShaderDesc(sg.queryBackend())),
.layout = blk: {
var ret: sg.VertexLayoutState = .{};
ret.attrs[shader.ATTR_program_position_CS].format = .FLOAT3;
ret.attrs[shader.ATTR_program_color].format = .FLOAT4;
break :blk ret;
},
});
}
fn deinit() callconv(.c) void {
@@ -40,10 +73,21 @@ fn frame() callconv(.c) void {
.dpi_scale = sapp.dpiScale(),
});
// --- MAIN PASS ---
sg.beginPass(.{
.action = main_action,
.swapchain = sglue.swapchain(),
});
sg.applyPipeline(pipeline);
sg.applyBindings(bindings);
sg.draw(0, 3, 1);
sg.endPass();
// --- IMGUI PASS ---
sg.beginPass(.{
.action = imgui_pass_action,
.action = imgui_action,
.swapchain = sglue.swapchain(),
});
simgui.render();

23
src/shader.glsl Normal file
View File

@@ -0,0 +1,23 @@
@vs vert
in vec4 position_CS;
in vec4 color;
out vec4 varColor;
void main() {
gl_Position = position_CS;
varColor = color;
}
@end
@fs frag
in vec4 varColor;
out vec4 fragColor;
void main() {
fragColor = varColor;
}
@end
@program program vert frag