build maps on 'zig build'

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-07-01 14:23:39 -04:00
parent 061d352140
commit 20a215518c
5 changed files with 79 additions and 66 deletions

View File

@ -1,69 +1,29 @@
const std = @import("std");
// Returns true if the version includes https://github.com/ziglang/zig/pull/10572/commits.
// When this is false, trying to place the stack first will result in data corruption.
fn version_supports_stack_first(zig_version: std.SemanticVersion) !bool {
if (zig_version.order(try std.SemanticVersion.parse("0.10.0")).compare(.gte)) {
// Merged here: https://github.com/ziglang/zig/pull/10572
return true;
}
if (zig_version.major == 0 and zig_version.minor == 10) {
// Check for 0.10.0-dev.258+. Conservatively check the prefix of the tag
// in case zig uses other prefixes that don't respect semver ordering.
if (zig_version.pre) |pre| {
// Merged here: https://github.com/ziglang/zig/pull/10572
return std.mem.startsWith(u8, pre, "dev.") and zig_version.order(try std.SemanticVersion.parse("0.10.0-dev.258")).compare(.gte);
}
}
// Backported here: https://github.com/ziglang/zig/commit/6f49233ac6a6569b909b689f22fc260dc8c19234
return zig_version.order(try std.SemanticVersion.parse("0.9.1")).compare(.gte);
}
pub fn mapgen(b: *std.build.Step) !void {
_ = b;
test "stack version check" {
const expect = std.testing.expect;
const parse = std.SemanticVersion.parse;
try expect(!try version_supports_stack_first(try parse("0.8.0")));
try expect(!try version_supports_stack_first(try parse("0.9.0")));
try expect(!try version_supports_stack_first(try parse("0.9.1-dev.259")));
try expect(try version_supports_stack_first(try parse("0.9.1")));
// Conservatively don't recognize tags other than 'dev'.
try expect(!try version_supports_stack_first(try parse("0.10.0-aev.259")));
try expect(!try version_supports_stack_first(try parse("0.10.0-zev.259")));
try expect(!try version_supports_stack_first(try parse("0.10.0-dev.257")));
try expect(try version_supports_stack_first(try parse("0.10.0-dev.258")));
try expect(try version_supports_stack_first(try parse("0.10.0-dev.259")));
try expect(try version_supports_stack_first(try parse("0.10.0")));
try expect(try version_supports_stack_first(try parse("0.10.1-dev.100")));
try expect(try version_supports_stack_first(try parse("0.10.1-dev.300")));
try expect(try version_supports_stack_first(try parse("0.10.1")));
try expect(try version_supports_stack_first(try parse("1.0.0")));
var alloc = std.heap.page_allocator;
var deno = std.ChildProcess.init(&.{"deno", "run", "-A", "./src/tools/genmap.js", "./maps/TestMap.json", "./src/maps/testmap.zig"}, alloc);
try deno.spawn();
_ = try deno.wait();
}
pub fn build(b: *std.build.Builder) !void {
const zig_version = @import("builtin").zig_version;
const map_step = b.step("mapgen", "generate maps");
map_step.makeFn = mapgen;
const mode = b.standardReleaseOptions();
const lib = b.addSharedLibrary("cart", "src/main.zig", .unversioned);
lib.step.dependOn(map_step);
lib.setBuildMode(mode);
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
lib.import_memory = true;
lib.initial_memory = 65536;
lib.max_memory = 65536;
if (try version_supports_stack_first(zig_version)) {
lib.stack_size = 14752;
} else {
// `--stack-first` option have been reenabled on wasm targets with https://github.com/ziglang/zig/pull/10572
std.log.warn("Update to Zig >=0.9.1 (or >=0.10.0-dev.258 for nightly) to detect stack overflows at runtime.", .{});
lib.global_base = 6560;
lib.stack_size = 8192;
}
// Workaround https://github.com/ziglang/zig/issues/2910, preventing
// functions from compiler_rt getting incorrectly marked as exported, which
// prevents them from being removed even if unused.
lib.stack_size = 14752;
lib.export_symbol_names = &[_][]const u8{ "start", "update" };
lib.install();
}

View File

@ -14,6 +14,8 @@ var mara_box: Rect = Rect{.base = Point{.x = 20, .y = 40}, .width = 16, .height
var mara_speed: i32 = 0;
var screen = Rect{.base = Point{.x = 0, .y = 0}, .width = 160, .height = 160};
var sound_timer: u8 = 0;
var textBuf: [160]u8 = undefined;
export fn start() void {
@ -22,6 +24,32 @@ export fn start() void {
const map = @import("./maps/testmap.zig");
fn bonk() void {
if (sound_timer != 0) {
return;
}
w4.tone(
w4.Tone.Frequency{.start = 220, .end = 40},
w4.Tone.Duration{
.attack = 0,
.decay = 0,
.sustain = 6,
.release = 6,
},
w4.Tone.Volume{
.sustain = 100,
.attack = 100,
},
w4.Tone.Flags{
.channel = .triangle,
.pulse_duty = .@"1/2",
},
);
sound_timer = 12;
}
fn drawMap() !void {
w4.m.colors.* = .{
._0 = .p0,
@ -48,17 +76,7 @@ fn drawMap() !void {
}
}
export fn update() void {
// w4.m.colors._0 = .p3;
// w4.m.colors._1 = .p0;
// w4.text("Mara 2:\nTamamo's Fury", 10, 10);
drawMap() catch unreachable;
const gamepad = w4.m.gamepads[0];
var old_speed = mara_speed;
fn move_mara(gamepad: w4.GamePad) void {
mara_speed -= 1;
if (mara_speed < 0) {
mara_speed = 0;
@ -91,11 +109,41 @@ export fn update() void {
mara_box.base.x += mara_speed;
},
}
}
export fn update() void {
if (sound_timer != 0) {
sound_timer -= 1;
}
drawMap() catch unreachable;
// w4.m.colors._0 = .p3;
// w4.m.colors._1 = .p0;
// w4.text("Mara 2:\nTamamo's Fury", 10, 10);
const gamepad = w4.m.gamepads[0];
const old_speed = mara_speed;
move_mara(gamepad);
for (map.coll) |box| {
if (mara_box.collides(box)) {
w4.trace("boncc");
switch (mara_direction) {
.Up => {
mara_box.base.y += mara_speed;
},
.Down => {
mara_box.base.y -= mara_speed;
},
.Left => {
mara_box.base.x += mara_speed;
},
.Right => {
mara_box.base.x -= mara_speed;
},
}
mara_speed = 0;
bonk();
}
}

View File

@ -1,3 +1,5 @@
// Generated by genmap.js, DO NOT EDIT
const sh0rk = @import("../sh0rk.zig");
const Point = sh0rk.Point;
const Rect = sh0rk.Rect;

View File

@ -11,6 +11,7 @@ var out = "";
const data = await Deno.readTextFile(mapFile);
const map = JSON.parse(data);
out += `// Generated by genmap.js, DO NOT EDIT\n\n`;
out += `const sh0rk = @import("../sh0rk.zig");\n`;

View File

@ -183,7 +183,7 @@ pub extern fn hline(x: u32, y: u32, len: u32) void;
/// volume: Volume of the sustain and attack durations, between 0 and 100.
pub fn tone(frequency: Tone.Frequency, duration: Tone.Duration, volume: Tone.Volume, flags: Tone.Flags) void {
std.debug.assert(volume.is_valid());
tone(@bitCast(u32, frequency), @bitCast(u32, duration), @bitCast(u32, volume), @bitCast(u32, flags));
raw_api.tone(@bitCast(u32, frequency), @bitCast(u32, duration), @bitCast(u32, volume), @bitCast(u32, flags));
}
pub const Tone = struct {
@ -219,6 +219,7 @@ pub const Tone = struct {
pub const Volume = packed struct {
sustain : u8,
attack : u8 = 100,
_reserved: u16 = 0,
pub fn is_valid(volume: @This()) bool {
return (0 <= volume.sustain) and (volume.sustain <= 100)
@ -230,6 +231,7 @@ pub const Tone = struct {
channel: Channel,
pulse_duty: DutyCycle = 2,
pan: Pan = .both,
_reserved: u26 = 0,
};
pub const Channel = enum(u2) {