Skip to content

Commit

Permalink
user_data: use writeln and hashmap (associative arrays) on emscript…
Browse files Browse the repository at this point in the history
…en target
  • Loading branch information
kassane committed Dec 8, 2024
1 parent a19ffd3 commit 7b8fc7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn build(b: *Build) !void {
.betterC = if (std.mem.eql(u8, example, "user-data")) false else enable_betterC,
.dflags = &.{
"-w",
"-preview=all",
"-preview=rvaluerefparam",
},
// fixme: https://github.com/kassane/sokol-d/issues/1 - betterC works on darwin
.zig_cc = if (target.result.isDarwin() and !enable_betterC) false else enable_zigcc,
Expand Down
40 changes: 23 additions & 17 deletions src/examples/user_data.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,58 @@ import sglue = sokol.glue;

extern (C):

struct ExampleUserData {
struct ExampleUserData
{
ubyte data;
int[ubyte] map; // need druntime
}

void init() @safe {
void init() @safe
{
sg.Desc gfx = {
environment: sglue.environment,
logger: { func: &log.slog_func }
logger: {func: &log.slog_func}
};
sg.setup(gfx);
}

void frame_userdata(scope void* userdata) @trusted {
void frame_userdata(scope void* userdata) @trusted
{
auto state = cast(ExampleUserData*) userdata;

state.data++;
version (WebAssembly) {
// TODO support
version (D_BetterC)
{

}
else {
if (state.data % 13 == 0) {
else
{
if (state.data % 13 == 0)
{
state.map[state.data] = state.data * 13 / 3;
}
if (state.data % 12 == 0 && state.data % 15 == 0) {
if (state.data % 12 == 0 && state.data % 15 == 0)
{
state.map.clear();
}
}
debug {
import std.stdio : writeln;
try {
writeln(*state);
} catch (Exception) { }

writeln(*state);
}

sg.Pass pass = { swapchain: sglue.swapchain };
sg.Pass pass = {swapchain: sglue.swapchain};
sg.beginPass(pass);
sg.endPass();
sg.commit();
}

void cleanup() @safe {
void cleanup() @safe
{
sg.shutdown();
}

void main() {
void main()
{
auto userData = ExampleUserData(0, null);
sapp.Desc runner = {
window_title: "user-data.d",
Expand Down

0 comments on commit 7b8fc7c

Please sign in to comment.