From 042ace2f2f1ac67bd1bc9109595d5e9e9959e526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Catarino=20Fran=C3=A7a?= Date: Sun, 14 Jan 2024 11:01:37 -0300 Subject: [PATCH] new example: user-data --- README.md | 26 ++++++++------- build.zig | 20 +++++++----- src/examples/user-data.d | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 src/examples/user-data.d diff --git a/README.md b/README.md index a3209a1..7b87688 100644 --- a/README.md +++ b/README.md @@ -24,21 +24,23 @@ zig build -Doptimize=ReleaseFast zig build -Doptimize=ReleaseFast -Dshared # Run Examples -zig build run-blend -Doptimize=ReleaseFast # run, but no anims (fixme) -zig build run-clear -Doptimize=ReleaseFast # works -zig build run-cube -Doptimize=ReleaseFast # run, but no anims (fixme) -zig build run-debugtext-print -Doptimize=ReleaseFast # works -zig build run-mrt -Doptimize=ReleaseFast # run, but no anims (fixme) -zig build run-saudio -Doptimize=ReleaseFast # run (fixme) -zig build run-sgl-context -Doptimize=ReleaseFast # run, but no anims (fixme) -zig build run-triangle -Doptimize=ReleaseFast # run, but no anims (fixme) +zig build run-blend -Doptimize=ReleaseFast +zig build run-clear -Doptimize=ReleaseFast +zig build run-cube -Doptimize=ReleaseFast +zig build run-debugtext-print -Doptimize=ReleaseFast +zig build run-mrt -Doptimize=ReleaseFast +zig build run-saudio -Doptimize=ReleaseFast +zig build run-sgl-context -Doptimize=ReleaseFast +zig build run-user-data -Doptimize=ReleaseFast +zig build run-triangle -Doptimize=ReleaseFast zig build --help # Project-Specific Options: -# -Dgl=[bool] Force GL backend -# -Dwayland=[bool] Compile with wayland-support (default: false) -# -Dx11=[bool] Compile with x11-support (default: true) -# -Degl=[bool] Use EGL instead of GLX if possible (default: false) +# -Dgl=[bool] Force OpenGL (default: false) +# -Dwgpu=[bool] Force WebGPU (default: false, web only) +# -Dx11=[bool] Force X11 (default: true, Linux only) +# -Dwayland=[bool] Force Wayland (default: false, Linux only, not supported in main-line headers) +# -Degl=[bool] Force EGL (default: false, Linux only) # -Dtarget=[string] The CPU architecture, OS, and ABI to build for # -Dcpu=[string] Target CPU features to add or subtract # -Doptimize=[enum] Prioritize performance, safety, or binary size (-O flag) diff --git a/build.zig b/build.zig index a3dc815..28208d7 100644 --- a/build.zig +++ b/build.zig @@ -195,8 +195,9 @@ pub fn build(b: *Build) !void { const enable_betterC = b.option(bool, "betterC", "Omit generating some runtime information and helper functions. (default: false)") orelse false; const enable_zigcc = b.option(bool, "zigCC", "Use zig cc as compiler and linker. (default: false)") orelse false; - if (enable_zigcc) + if (enable_zigcc) { buildZigCC(b); + } // WiP: build examples const examples = .{ @@ -219,6 +220,7 @@ pub fn build(b: *Build) !void { "debugtext-print", // "debugtext-userfont", // "shapes", + "user-data", }; b.getInstallStep().name = "sokol library"; inline for (examples) |example| { @@ -392,10 +394,7 @@ fn DCompileStep(b: *Build, lib_sokol: *CompileStep, options: LDCOptions) !*RunSt try cmds.append("-i"); // sokol D files and include path - try cmds.append(b.fmt("-I{s}", .{b.pathJoin(&.{ - rootPath(), - "src", - })})); + try cmds.append(b.fmt("-I{s}", .{b.pathJoin(&.{ rootPath(), "src" })})); // example D file for (options.sources) |src| { @@ -431,13 +430,18 @@ fn DCompileStep(b: *Build, lib_sokol: *CompileStep, options: LDCOptions) !*RunSt // link flags // GNU LD - if (options.target.result.os.tag == .linux and !options.zig_cc) + if (options.target.result.os.tag == .linux and !options.zig_cc) { try cmds.append("-L--no-as-needed"); + } // LLD (not working in zld) - if (options.target.result.isDarwin() and !options.zig_cc) + if (options.target.result.isDarwin() and !options.zig_cc) { // https://github.com/ldc-developers/ldc/issues/4501 - try cmds.append("-L-w"); // resolve linker warnings + try cmds.append("-L-w"); // hide linker warnings + if (lib_sokol.dead_strip_dylibs) { + try cmds.append("-L=-dead_strip"); + } + } // Darwin frameworks if (options.target.result.isDarwin()) { var it = lib_sokol.root_module.frameworks.iterator(); diff --git a/src/examples/user-data.d b/src/examples/user-data.d new file mode 100644 index 0000000..c2ca2a9 --- /dev/null +++ b/src/examples/user-data.d @@ -0,0 +1,69 @@ +module examples.user_data; + +import sg = sokol.gfx; +import sapp = sokol.app; +import log = sokol.log; +import sgapp = sokol.glue; + +import std.stdio; + +extern (C): + +struct ExampleUserData { + ubyte data; + int[ubyte] map; +} + +void init() @safe +{ + sg.Desc gfx = { + context: sgapp.context(), + logger: {func: &log.slog_func} + }; + sg.setup(gfx); +} + +void frame_userdata(scope void* userdata) @trusted +{ + auto state = cast(ExampleUserData*) userdata; + + state.data++; + if (state.data % 13 == 0) { + state.map[state.data] = state.data * 13 / 3; + } + if (state.data % 12 == 0 && state.data % 15 == 0) { + state.map.clear(); + } + + writeln(*state); + + sg.PassAction pass_action = {}; + sg.beginDefaultPass(pass_action, sapp.width(), sapp.height()); + sg.endPass(); + sg.commit(); +} + +void cleanup() @safe +{ + sg.shutdown(); +} + +void main() +{ + auto userData = ExampleUserData(0, null); + + sapp.Desc runner = { + window_title: "user-data.d", + init_cb: &init, + frame_userdata_cb: &frame_userdata, + cleanup_cb: &cleanup, + user_data: &userData, + width: 640, + height: 480, + sample_count: 4, + win32_console_attach: true, + icon: {sokol_default: true}, + logger: {func: &log.func} + }; + sapp.run(runner); +}