Skip to content

Commit

Permalink
sokol build
Browse files Browse the repository at this point in the history
  • Loading branch information
nurpax committed Apr 8, 2023
1 parent ce78716 commit d949c3f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub fn build(b: *Builder) void {
linkGlfw(exe, target);
linkGlad(exe, target);
}
{
const exe = exampleExe(b, "example_sokol_app", optimize, target);
linkSokol(exe, target);
}
}

fn exampleExe(b: *Builder, comptime name: []const u8, optimize: std.builtin.Mode, target: std.zig.CrossTarget) *LibExeObjStep {
Expand All @@ -32,6 +36,7 @@ fn exampleExe(b: *Builder, comptime name: []const u8, optimize: std.builtin.Mode
exe.install();

exe.addAnonymousModule("imgui", .{ .source_file = .{ .path = "zig-imgui/imgui.zig" } });
exe.addAnonymousModule("sokol", .{ .source_file = .{ .path = "sokol/sokol.zig" } });

const run_step = b.step(name, "Run " ++ name);
const run_cmd = exe.run();
Expand Down Expand Up @@ -64,3 +69,47 @@ fn linkVulkan(exe: *LibExeObjStep, target: std.zig.CrossTarget) void {
exe.linkSystemLibrary("vulkan");
}
}

fn linkSokol(exe: *LibExeObjStep, target: std.zig.CrossTarget) void {
_ = target;
const csources = [_][]const u8{
"sokol/c/sokol_app.c",
"sokol/c/sokol_gfx.c",
"sokol/c/sokol_time.c",
"sokol/c/sokol_audio.c",
"sokol/c/sokol_gl.c",
"sokol/c/sokol_debugtext.c",
"sokol/c/sokol_shape.c",
"sokol/c/sokol_log.c",
};
//const backend = if (lib.target.isDarwin()) .metal else if (lib.target.isWindows()) .d3d11 else .gl;
const backend = .d3d11;
std.debug.assert(exe.target.isWindows());
const backend_option = switch (backend) {
.d3d11 => "-DSOKOL_D3D11",
.metal => "-DSOKOL_METAL",
.gl => "-DSOKOL_GLCORE33",
.gles2 => "-DSOKOL_GLES2",
.gles3 => "-DSOKOL_GLES3",
.wgpu => "-DSOKOL_WGPU",
else => unreachable,
};

for (csources) |csrc| {
exe.addCSourceFile(csrc, &[_][]const u8{ "-DIMPL", backend_option });
}
if (exe.target.isLinux()) {
exe.linkSystemLibrary("X11");
exe.linkSystemLibrary("Xi");
exe.linkSystemLibrary("Xcursor");
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("asound");
} else if (exe.target.isWindows()) {
exe.linkSystemLibrary("kernel32");
exe.linkSystemLibrary("user32");
exe.linkSystemLibrary("gdi32");
exe.linkSystemLibrary("ole32");
exe.linkSystemLibrary("d3d11");
exe.linkSystemLibrary("dxgi");
}
}
49 changes: 49 additions & 0 deletions examples/example_sokol_app.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//------------------------------------------------------------------------------
// clear.zig
//
// Just clear the framebuffer with an animated color.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const print = @import("std").debug.print;

var pass_action: sg.PassAction = .{};

export fn init() void {
sg.setup(.{ .context = sgapp.context(), .logger = .{ .func = slog.func } });
pass_action.colors[0] = .{ .action = .CLEAR, .value = .{ .r = 1, .g = 1, .b = 0, .a = 1 } };
print("Backend: {}\n", .{sg.queryBackend()});
}

export fn frame() void {
const g = pass_action.colors[0].value.g + 0.01;
pass_action.colors[0].value.g = if (g > 1.0) 0.0 else g;
sg.beginDefaultPass(pass_action, sapp.width(), sapp.height());
sg.endPass();
sg.commit();
}

export fn cleanup() void {
sg.shutdown();
}

pub fn main() void {
sapp.run(.{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 640,
.height = 480,
.icon = .{
.sokol_default = true,
},
.window_title = "clear.zig",
.logger = .{
.func = slog.func,
},
.win32_console_attach = true,
});
}

0 comments on commit d949c3f

Please sign in to comment.