Skip to content

Commit

Permalink
Update to Zig 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sanchez committed Sep 8, 2023
1 parent 7b7895e commit 4655b12
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
c_wefx/
zig-cache/
zig-out/
*.o
30 changes: 14 additions & 16 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
// Adds the option -Drelease=[bool] to create a release build, which by
// default we set to ReleaseSmall.
b.setPreferredReleaseMode(.ReleaseSmall);
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const wefx_module = b.addModule("WEFX", .{
.source_file = .{ .cwd_relative = "wefx/WEFX.zig" },
});

// EXAMPLES
const lib = b.addSharedLibrary("wefx", "examples/example1.zig", .unversioned);
lib.setBuildMode(mode);
lib.setTarget(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
//.abi = .musl,
const lib = b.addSharedLibrary(.{
.name = "wefx-example",
.root_source_file = .{ .path = "examples/example1.zig" },
.target = .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
},
.optimize = .ReleaseSmall,
});
lib.addPackagePath("WEFX", "./wefx/WEFX.zig");
lib.setOutputDir("./docs/");
lib.install();
lib.rdynamic = true;
lib.addModule("WEFX", wefx_module);
b.installArtifact(lib);
}
2 changes: 1 addition & 1 deletion docs/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getWASMGlobalValue(memory, global) {
}

async function bootstrap() {
const { instance } = await WebAssembly.instantiateStreaming(fetch("./wefx.wasm"));
const { instance } = await WebAssembly.instantiateStreaming(fetch("./wefx-example.wasm"));

const memory = instance.exports.memory;

Expand Down
Binary file added docs/wefx-example.wasm
Binary file not shown.
Binary file removed docs/wefx.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions examples/example1.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ fn draw(time: f32) void {
const width = wefx.width;
const height = wefx.height;

const x = @floatToInt(u32, time) % width;
wefx.point(x, (height / 2) + @floatToInt(u32, math.cos(time) * 2));
const x = @as(u32, @intFromFloat(time)) % width;
wefx.point(x, (height / 2) + @as(u32, @intFromFloat(math.cos(time) * 2)));

var i: u32 = 0;
while (i < 130) : (i += 1) {
wefx.color(0xFF, 0, 0);
wefx.point(x - i, (height / 2) + @floatToInt(u32, math.sin(time - @intToFloat(f32, i)) * 3) - 20);
wefx.point(x - i, (height / 2) + @as(u32, @intFromFloat(math.sin(time - @as(f32, @floatFromInt(i))) * 3)) - 20);

wefx.color(0, 0xFF, 0);
wefx.point(x - i, (height / 2) + @floatToInt(u32, math.cos(time - @intToFloat(f32, i)) * 2));
wefx.point(x - i, (height / 2) + @as(u32, @intFromFloat(math.cos(time - @as(f32, @floatFromInt(i))) * 2)));

wefx.color(0, 0, 0xFF);
wefx.point(x - i, (height / 2) + @floatToInt(u32, math.sin(time - @intToFloat(f32, i)) * 3) + 20);
wefx.point(x - i, (height / 2) + @as(u32, @intFromFloat(math.sin(time - @as(f32, @floatFromInt(i))) * 3)) + 20);
}

wefx.color(0xFF, 0xFF, 0xFF);
Expand Down
24 changes: 12 additions & 12 deletions wefx/WEFX.zig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub fn circle(self: *Self, x0: u32, y0: u32, r0: u32) void {
}

pub fn flush(self: *Self) void {
for (self.buffer) |val, i| {
for (self.buffer, 0..) |val, i| {
self.screen[i] = val;
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ export fn wefx_add_keyboard_event(
timestamp: f32,
key: u8,
) void {
const event_type = @intToEnum(EventType, event_type_val);
const event_type: EventType = @enumFromInt(event_type_val);
const kbd_event = .{
.key = key,
};
Expand All @@ -311,13 +311,13 @@ export fn wefx_add_mouse_event(
x: f32,
y: f32,
) void {
const event_type = @intToEnum(EventType, event_type_val);
const button = @intToEnum(MouseEvent.Button, button_val);
const event_type: EventType = @enumFromInt(event_type_val);
const button: MouseEvent.Button = @enumFromInt(button_val);

const mouse_event = .{
.button = button,
.x = @floatToInt(u32, x),
.y = @floatToInt(u32, y),
.x = @as(u32, @intFromFloat(x)), // The @as(...) here shouldn't be necessary
.y = @as(u32, @intFromFloat(y)),
};
const event = Event.initMouseEvent(timestamp, event_type, mouse_event);
// TODO: handle error on JS?
Expand All @@ -335,9 +335,9 @@ export fn wefx_add_mouse_event(
// @export(field_value, opts);
// }
// }
export var wefx_keydown = @enumToInt(EventType.keydown);
export var wefx_keypress = @enumToInt(EventType.keypress);
export var wefx_keyup = @enumToInt(EventType.keyup);
export var wefx_mousemove = @enumToInt(EventType.mousemove);
export var wefx_mousedown = @enumToInt(EventType.mousedown);
export var wefx_mouseup = @enumToInt(EventType.mouseup);
export var wefx_keydown: c_int = @intFromEnum(EventType.keydown);
export var wefx_keypress: c_int = @intFromEnum(EventType.keypress);
export var wefx_keyup: c_int = @intFromEnum(EventType.keyup);
export var wefx_mousemove: c_int = @intFromEnum(EventType.mousemove);
export var wefx_mousedown: c_int = @intFromEnum(EventType.mousedown);
export var wefx_mouseup: c_int = @intFromEnum(EventType.mouseup);

0 comments on commit 4655b12

Please sign in to comment.