Browse Source

Update to zig version 0.14.0

Daniel Sanchez 3 months ago
parent
commit
729d1f46e4
10 changed files with 40 additions and 36 deletions
  1. 1 1
      .gitignore
  2. 3 1
      README.md
  3. 27 21
      build.zig
  4. 1 1
      script.js
  5. 4 8
      src/GameState.zig
  6. 1 1
      src/enemy.zig
  7. 1 1
      src/main.zig
  8. 1 1
      src/particle.zig
  9. 1 1
      src/projectile.zig
  10. BIN
      zig-out/bin/DodgeBallz.wasm

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
-zig-cache/
+.zig-cache/
 *.o

+ 3 - 1
README.md

@@ -14,12 +14,14 @@ Among those imported functions are functions that draw things on a canvas, e.g.
 ## Build
 
 Tested in version:
+
 ```shell
 $ zig version
-0.12.0-dev.271+5cc1831ca
+0.14.0
 ```
 
 To build the wasm module, run:
+
 ```shell
 $ zig build
 

+ 27 - 21
build.zig

@@ -5,33 +5,39 @@ const std = @import("std");
 const wasm_initial_memory = 17 * std.wasm.page_size;
 const wasm_max_memory = wasm_initial_memory;
 
-pub fn build(b: *std.build.Builder) void {
-
-    const lib = b.addSharedLibrary(.{
+pub fn build(b: *std.Build) void {
+    const target = b.resolveTargetQuery(.{
+        .cpu_arch = .wasm32,
+        .os_tag = .freestanding,
+    });
+    const exe = b.addExecutable(.{
         .name = "DodgeBallz",
-        .root_source_file = .{ .path = "src/main.zig" },
-        .target = .{
-            .cpu_arch = .wasm32,
-            .os_tag = .freestanding,
-            .abi = .musl,
-        },
+        .root_source_file = b.path("src/main.zig"),
+        .target = target,
         .optimize = .ReleaseSmall,
     });
+    exe.entry = .disabled;
+    //exe.global_base = 6560;
+    exe.rdynamic = true;
+    exe.import_memory = true;
+    exe.import_symbols = true;
+    //exe.initial_memory = wasm_initial_memory; // initial size of the linear memory (1 page = 64kB)
+    //exe.max_memory = wasm_initial_memory; // maximum size of the linear memory
 
     // https://github.com/ziglang/zig/issues/8633
-    lib.global_base = 6560;
-    lib.rdynamic = true;
-    lib.import_memory = true; // import linear memory from the environment
-    lib.initial_memory = wasm_initial_memory; // initial size of the linear memory (1 page = 64kB)
-    lib.max_memory = wasm_initial_memory; // maximum size of the linear memory
+    // lib.global_base = 6560;
+    // lib.rdynamic = true;
+    // lib.import_memory = true; // import linear memory from the environment
+    // lib.initial_memory = wasm_initial_memory; // initial size of the linear memory (1 page = 64kB)
+    // lib.max_memory = wasm_initial_memory; // maximum size of the linear memory
 
     // Pass options from the build script to the files being compiled. This is awesome!
-    const lib_options = b.addOptions();
-    lib.addOptions("build_options", lib_options);
-    lib_options.addOption(usize, "memory_size", wasm_max_memory);
+    const exe_options = b.addOptions();
+    exe_options.addOption(usize, "memory_size", wasm_max_memory);
+    exe.root_module.addOptions("build_options", exe_options);
 
-     // This declares intent for the library to be installed into the standard
-     // location when the user invokes the "install" step (the default step when
-     // running `zig build`).
-     b.installArtifact(lib);
+    // This declares intent for the library to be installed into the standard
+    // location when the user invokes the "install" step (the default step when
+    // running `zig build`).
+    b.installArtifact(exe);
 }

+ 1 - 1
script.js

@@ -92,7 +92,7 @@ const wasm = {
 };
 
 function loadGame() {
-    WebAssembly.instantiateStreaming(fetch("zig-out/lib/DodgeBallz.wasm"), wasm.imports).then((result) => {
+    WebAssembly.instantiateStreaming(fetch("zig-out/bin/DodgeBallz.wasm"), wasm.imports).then((result) => {
         wasm.exports = result.instance.exports;
         window.addEventListener("keydown", (event) => {
             const key = event.key;

+ 4 - 8
src/GameState.zig

@@ -27,17 +27,13 @@ const Self = @This();
 
 pub fn init(allocator: Allocator, board_width: f32, board_height: f32) Self {
     var board = Board.init(board_width, board_height);
-    var player = Player.init(board.center());
-    var enemies = EnemyArrayList.init();
-    var projectiles = ProjectileArrayList.init();
-    var particles = ParticleArrayList.init();
     return .{
         .allocator = allocator,
         .board = board,
-        .player = player,
-        .enemies = enemies,
-        .projectiles = projectiles,
-        .particles = particles,
+        .player = Player.init(board.center()),
+        .enemies = EnemyArrayList.init(),
+        .projectiles = ProjectileArrayList.init(),
+        .particles = ParticleArrayList.init(),
     };
 }
 

+ 1 - 1
src/enemy.zig

@@ -94,7 +94,7 @@ pub const EnemyArrayList = struct {
 
         var i: usize = 0;
         while (i < self.count()) {
-            var enemy = &items[i];
+            const enemy = &items[i];
             const is_out = board.isOutOfBoundary(enemy.ball);
             if (is_out or enemy.ball.radius < min_radius) {
                 if (!is_out) {

+ 1 - 1
src/main.zig

@@ -14,7 +14,7 @@ export fn game_init(board_width: f32, board_height: f32) void {
     // To use as an slice, we take the address of the first element and cast it into a slice of bytes.
     // We pass the memory size using build options (see build.zing).
     var memory_buffer_ptr: [*]u8 = @ptrCast(&memory);
-    var memory_buffer = memory_buffer_ptr[0..build_options.memory_size];
+    const memory_buffer = memory_buffer_ptr[0..build_options.memory_size];
 
     // When the upper bound of memory can be established, FixedBufferAllocator is a great choice.
     fixed_buffer = std.heap.FixedBufferAllocator.init(memory_buffer);

+ 1 - 1
src/particle.zig

@@ -90,7 +90,7 @@ pub const ParticleArrayList = struct {
 
         var i: usize = 0;
         while (i < self.count()) {
-            var particle = &items[i];
+            const particle = &items[i];
             if (particle.ball.color.a <= 0.0 or board.isOutOfBoundary(particle.ball)) {
                 // Don't update the index if we remove an item from the list, it still valid.
                 self.delete(i);

+ 1 - 1
src/projectile.zig

@@ -83,7 +83,7 @@ pub const ProjectileArrayList = struct {
 
         var i: usize = 0;
         while (i < self.count()) {
-            var projectile = &items[i];
+            const projectile = &items[i];
             if (board.isOutOfBoundary(projectile.ball)) {
                 // Don't update the index if we remove an item from the list, it still valid.
                 self.delete(i);

BIN
zig-out/bin/DodgeBallz.wasm