Skip to content

Commit

Permalink
new example: user-data
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jan 14, 2024
1 parent 6c899ec commit 042ace2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 20 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .{
Expand All @@ -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| {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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();
Expand Down
69 changes: 69 additions & 0 deletions src/examples/user-data.d
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 042ace2

Please sign in to comment.