Skip to content

Commit

Permalink
Update to Zig master
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioarnold committed Jul 3, 2023
1 parent 2d89fa5 commit 7211752
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 68 deletions.
11 changes: 6 additions & 5 deletions ProcessAssetsStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ fn loadJson(comptime T: type, path: []const u8, allocator: std.mem.Allocator) !T
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
const file_contents = try file.readToEndAlloc(allocator, max_file_size);
defer allocator.free(file_contents);
return std.json.parseFromSlice(T, allocator, file_contents, .{
return (try std.json.parseFromSlice(T, allocator, file_contents, .{
.ignore_unknown_fields = true,
});
})).value;
}

fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
Expand All @@ -110,12 +109,14 @@ fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
try writer.print("pub const {s} = Stage{{\n", .{stage.name});
try writer.writeAll(" .rooms = &[_]Room{\n");

std.debug.print("Processing stage {s}\n", .{stage.path});
const world = try loadJson(WorldJson, stage.path, allocator);
for (world.maps) |world_map| {
const map_path = if (std.fs.path.dirname(stage.path)) |path|
try std.fs.path.join(allocator, &.{ path, world_map.fileName })
else
world_map.fileName;
std.debug.print("Processing map {s}\n", .{map_path});
const map = try loadJson(MapJson, map_path, allocator);

try writer.writeAll(" Room{\n");
Expand All @@ -131,7 +132,7 @@ fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
if (layer.data) |data| {
try writer.writeAll(" .data = &[_]u8{");
for (data, 0..) |d, i| {
if (i % @intCast(usize, map.width) == 0) {
if (i % @as(usize, @intCast(map.width)) == 0) {
try writer.writeAll("\n ");
}
try writer.print("{d}, ", .{d - 1});
Expand Down Expand Up @@ -179,7 +180,7 @@ fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
}
try writer.writeAll(" .attribs = &[_]Tile.Attrib{");
for (attribs, 0..) |a, i| {
if (i % @intCast(usize, tileset.columns) == 0) {
if (i % @as(usize, @intCast(tileset.columns)) == 0) {
try writer.writeAll("\n ");
}
try writer.print(".{s}, ", .{a});
Expand Down
2 changes: 1 addition & 1 deletion src/Enemy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const GopherState = enum(u8) {

fn tickGopher(self: *Self, r: std.rand.Random, game: *GameData, attribs: []const Attrib) void {
const room = game.getCurrentRoom();
const state = @ptrCast(*GopherState, &self.state);
const state: *GopherState = @ptrCast(&self.state);
switch (state.*) {
.idle => {
self.frame = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Entity.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pub const Class = enum {
};

class: Class,
box: Box,
box: Box,
4 changes: 2 additions & 2 deletions src/Player.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ pub fn draw(self: *Player) void {
else => unreachable,
};
src_rect.w = if (use_joys_sprite) 24 else 32;
src_rect.x = 48 + @intCast(i32, frame) * src_rect.w;
src_rect.x = 48 + @as(i32, @intCast(frame)) * src_rect.w;
},
.jumping => src_rect = if (use_joys_sprite) Rect.init(144 + @intCast(i32, (self.anim_time % 20) / 10) * 24, 0, 24, 32) else Rect.init(176, 0, 32, 32),
.jumping => src_rect = if (use_joys_sprite) Rect.init(144 + @as(i32, @intCast((self.anim_time % 20) / 10)) * 24, 0, 24, 32) else Rect.init(176, 0, 32, 32),
.climbing => {
src_rect = if (use_joys_sprite) Rect.init(216, 0, 24, 32) else Rect.init(240, 0, 16, 32);
flip_x = @mod(self.box.y, 20) < 10;
Expand Down
42 changes: 21 additions & 21 deletions src/Renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub const Texture = struct {

pub const Sprite = struct {
pub fn draw(sprite: Texture, x: i32, y: i32) void {
const src_rect = Rect.init(0, 0, @intCast(i32, sprite.width), @intCast(i32, sprite.height));
const src_rect = Rect.init(0, 0, @intCast(sprite.width), @intCast(sprite.height));
const dst_rect = Rect.init(x, y, src_rect.w, src_rect.h);
drawFromTo(sprite, src_rect, dst_rect);
}
Expand All @@ -121,10 +121,10 @@ pub const Sprite = struct {
}

pub fn drawFromTo(sprite: Texture, src_rect: Rect, dst_rect: Rect) void {
const x = @intToFloat(f32, dst_rect.x - scroll.x);
const y = @intToFloat(f32, dst_rect.y - scroll.y);
const w = @intToFloat(f32, dst_rect.w);
const h = @intToFloat(f32, dst_rect.h);
const x: f32 = @floatFromInt(dst_rect.x - scroll.x);
const y: f32 = @floatFromInt(dst_rect.y - scroll.y);
const w: f32 = @floatFromInt(dst_rect.w);
const h: f32 = @floatFromInt(dst_rect.h);
const px: f32 = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
Expand All @@ -136,13 +136,13 @@ pub const Sprite = struct {
gl.glUseProgram(blit2d.program);
gl.glUniformMatrix4fv(blit2d.mvp_loc, 1, gl.GL_FALSE, &mvp);

const sx = 1.0 / @intToFloat(f32, sprite.width);
const sy = 1.0 / @intToFloat(f32, sprite.height);
const sx = 1.0 / @as(f32, @floatFromInt(sprite.width));
const sy = 1.0 / @as(f32, @floatFromInt(sprite.height));
const texmat = [16]f32{
@intToFloat(f32, src_rect.w) * sx, 0, 0, 0,
0, @intToFloat(f32, src_rect.h) * sy, 0, 0,
0, 0, 1, 0,
@intToFloat(f32, src_rect.x) * sx, @intToFloat(f32, src_rect.y) * sy, 0, 1,
@as(f32, @floatFromInt(src_rect.w)) * sx, 0, 0, 0,
0, @as(f32, @floatFromInt(src_rect.h)) * sy, 0, 0,
0, 0, 1, 0,
@as(f32, @floatFromInt(src_rect.x)) * sx, @as(f32, @floatFromInt(src_rect.y)) * sy, 0, 1,
};
gl.glUniformMatrix4fv(blit2d.texmat_loc, 1, gl.GL_FALSE, &texmat);
gl.glBindTexture(gl.GL_TEXTURE_2D, sprite.handle);
Expand All @@ -152,10 +152,10 @@ pub const Sprite = struct {

pub const Tilemap = struct {
pub fn draw(map: Texture, tiles: Texture, rect: Rect) void {
const x = @intToFloat(f32, rect.x - scroll.x);
const y = @intToFloat(f32, rect.y - scroll.y);
const w = @intToFloat(f32, rect.w);
const h = @intToFloat(f32, rect.h);
const x: f32 = @floatFromInt(rect.x - scroll.x);
const y: f32 = @floatFromInt(rect.y - scroll.y);
const w: f32 = @floatFromInt(rect.w);
const h: f32 = @floatFromInt(rect.h);
const px = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
Expand All @@ -168,9 +168,9 @@ pub const Tilemap = struct {
gl.glUniformMatrix4fv(tiled.mvp_loc, 1, gl.GL_FALSE, &mvp);
gl.glUniformMatrix4fv(tiled.texmat_loc, 1, gl.GL_FALSE, &identity_matrix);
gl.glUniform1i(tiled.map_loc, 0);
gl.glUniform2f(tiled.map_size_loc, @intToFloat(f32, map.width), @intToFloat(f32, map.height));
gl.glUniform2f(tiled.map_size_loc, @floatFromInt(map.width), @floatFromInt(map.height));
gl.glUniform1i(tiled.tiles_loc, 1);
gl.glUniform2f(tiled.tiles_size_loc, @intToFloat(f32, tiles.width), @intToFloat(f32, tiles.height));
gl.glUniform2f(tiled.tiles_size_loc, @floatFromInt(tiles.width), @floatFromInt(tiles.height));

gl.glActiveTexture(gl.GL_TEXTURE1);
gl.glBindTexture(gl.GL_TEXTURE_2D, tiles.handle);
Expand All @@ -182,10 +182,10 @@ pub const Tilemap = struct {

pub const Debug = struct {
pub fn drawRect(rect: Rect, color: Color) void {
const x = @intToFloat(f32, rect.x - scroll.x);
const y = @intToFloat(f32, rect.y - scroll.y);
const w = @intToFloat(f32, rect.w);
const h = @intToFloat(f32, rect.h);
const x: f32 = @floatFromInt(rect.x - scroll.x);
const y: f32 = @floatFromInt(rect.y - scroll.y);
const w: f32 = @floatFromInt(rect.w);
const h: f32 = @floatFromInt(rect.h);
const px = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
Expand Down
26 changes: 13 additions & 13 deletions src/Room.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub fn clipX(self: Room, attribs: []const Tile.Attrib, mover: Box, amount: i32)
else
Box{ .x = box.x + clipped, .y = box.y, .w = -clipped, .h = box.h };

const start_x = @intCast(u16, std.math.clamp(@divTrunc(area.x, Tile.size), 0, self.width - 1));
const stop_x = @intCast(u16, std.math.clamp(@divTrunc(area.x + area.w - 1, Tile.size), 0, self.width - 1));
const start_y = @intCast(u16, std.math.clamp(@divTrunc(area.y, Tile.size), 0, self.height - 1));
const stop_y = @intCast(u16, std.math.clamp(@divTrunc(area.y + area.h - 1, Tile.size), 0, self.height - 1));
const start_x: u16 = @intCast(std.math.clamp(@divTrunc(area.x, Tile.size), 0, self.width - 1));
const stop_x: u16 = @intCast(std.math.clamp(@divTrunc(area.x + area.w - 1, Tile.size), 0, self.width - 1));
const start_y: u16 = @intCast(std.math.clamp(@divTrunc(area.y, Tile.size), 0, self.height - 1));
const stop_y: u16 = @intCast(std.math.clamp(@divTrunc(area.y + area.h - 1, Tile.size), 0, self.height - 1));

var y = start_y;
while (y <= stop_y) : (y += 1) {
Expand Down Expand Up @@ -69,10 +69,10 @@ pub fn clipY(self: Room, attribs: []const Tile.Attrib, mover: Box, amount: i32)
else
Box{ .x = box.x, .y = box.y + clipped, .w = box.w, .h = -clipped };

const start_x = @intCast(u16, std.math.clamp(@divTrunc(area.x, Tile.size), 0, self.width - 1));
const stop_x = @intCast(u16, std.math.clamp(@divTrunc(area.x + area.w - 1, Tile.size), 0, self.width - 1));
const start_y = @intCast(u16, std.math.clamp(@divTrunc(area.y, Tile.size), 0, self.height - 1));
const stop_y = @intCast(u16, std.math.clamp(@divTrunc(area.y + area.h - 1, Tile.size), 0, self.height - 1));
const start_x: u16 = @intCast(std.math.clamp(@divTrunc(area.x, Tile.size), 0, self.width - 1));
const stop_x: u16 = @intCast(std.math.clamp(@divTrunc(area.x + area.w - 1, Tile.size), 0, self.width - 1));
const start_y: u16 = @intCast(std.math.clamp(@divTrunc(area.y, Tile.size), 0, self.height - 1));
const stop_y: u16 = @intCast(std.math.clamp(@divTrunc(area.y + area.h - 1, Tile.size), 0, self.height - 1));

var y = start_y;
while (y <= stop_y) : (y += 1) {
Expand All @@ -99,10 +99,10 @@ pub fn overlap(self: Room, attribs: []const Tile.Attrib, mover: Box) bool {
// move mover into room space
const box = Box{ .x = mover.x - self.bounds.x, .y = mover.y - self.bounds.y, .w = mover.w, .h = mover.h };

const start_x = @intCast(u16, std.math.clamp(@divTrunc(box.x, Tile.size), 0, self.width - 1));
const stop_x = @intCast(u16, std.math.clamp(@divTrunc(box.x + box.w - 1, Tile.size), 0, self.width - 1));
const start_y = @intCast(u16, std.math.clamp(@divTrunc(box.y, Tile.size), 0, self.height - 1));
const stop_y = @intCast(u16, std.math.clamp(@divTrunc(box.y + box.h - 1, Tile.size), 0, self.height - 1));
const start_x: u16 = @intCast(std.math.clamp(@divTrunc(box.x, Tile.size), 0, self.width - 1));
const stop_x: u16 = @intCast(std.math.clamp(@divTrunc(box.x + box.w - 1, Tile.size), 0, self.width - 1));
const start_y: u16 = @intCast(std.math.clamp(@divTrunc(box.y, Tile.size), 0, self.height - 1));
const stop_y: u16 = @intCast(std.math.clamp(@divTrunc(box.y + box.h - 1, Tile.size), 0, self.height - 1));

var y = start_y;
while (y <= stop_y) : (y += 1) {
Expand All @@ -128,6 +128,6 @@ pub fn getTileAttribAtPixel(self: Room, attribs: []const Tile.Attrib, x: i32, y:

fn getTileAttribAtTile(self: Room, attribs: []const Tile.Attrib, tx: i32, ty: i32) Tile.Attrib {
if (tx < 0 or ty < 0 or tx >= self.width or ty >= self.height) return .none;
const ti = self.data[@intCast(usize, ty * self.width + tx)];
const ti = self.data[@as(usize, @intCast(ty * self.width + tx))];
return attribs[ti];
}
2 changes: 1 addition & 1 deletion src/Stage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const Tile = @import("Tile.zig");
const Room = @import("Room.zig");

rooms: []const Room,
attribs: []const Tile.Attrib,
attribs: []const Tile.Attrib,
2 changes: 1 addition & 1 deletion src/Tile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub const Attrib = enum(u8) {
ladder,
};

pub const size = 16;
pub const size = 16;
2 changes: 1 addition & 1 deletion src/keys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ pub const KEY_BACKQUOTE = 192;
pub const KEY_BRACKET_LEFT = 219;
pub const KEY_BACKSLASH = 220;
pub const KEY_BRAKET_RIGHT = 221;
pub const KEY_QUOTE = 22;
pub const KEY_QUOTE = 22;
26 changes: 13 additions & 13 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ pub const GameData = struct {
var fba = std.heap.FixedBufferAllocator.init(&buffer);
const allocator = fba.allocator();
const value = web.LocalStorage.getString("snapshot");
self.* = std.json.parseFromSlice(GameData, allocator, value, .{
self.* = (std.json.parseFromSlice(GameData, allocator, value, .{
.ignore_unknown_fields = true,
}) catch unreachable;
}) catch return).value;
uploadRoomTexture(&cur_room_tex, self.getCurrentRoom()); // FIXME
}

Expand Down Expand Up @@ -220,15 +220,15 @@ pub const GameData = struct {
fn doLtrDoorTransition(self: *GameData) void {
mode_frame += 1;
if (mode_frame <= door_duration) {
self.door1_h = 4 - @intCast(u8, @divTrunc(4 * mode_frame, door_duration));
self.door1_h = 4 - @as(u8, @intCast(@divTrunc(4 * mode_frame, door_duration)));
} else if (mode_frame <= door_duration + 64) {
self.player.tick();
const cur_room = cur_stage.rooms[self.cur_room_index];
// const prev_room = cur_stage.rooms[self.prev_room_index];
self.scrollr.x = cur_room.bounds.x - screen_width + @divTrunc((mode_frame - door_duration) * screen_width, 64);
self.player.box.x = cur_room.bounds.x - 2 * self.player.box.w + @divTrunc(3 * self.player.box.w * (mode_frame - door_duration), 64);
} else if (mode_frame <= door_duration + 64 + door_duration) {
self.door1_h = @intCast(u8, @divTrunc(4 * (mode_frame - 64 - door_duration), door_duration));
self.door1_h = @intCast(@divTrunc(4 * (mode_frame - 64 - door_duration), door_duration));
}
if (mode_frame == door_duration + 64 + door_duration) {
room_transition = .none;
Expand All @@ -238,15 +238,15 @@ pub const GameData = struct {
fn doRtlDoorTransition(self: *GameData) void {
mode_frame += 1;
if (mode_frame <= door_duration) {
self.door2_h = 4 - @intCast(u8, @divTrunc(4 * mode_frame, door_duration));
self.door2_h = 4 - @as(u8, @intCast(@divTrunc(4 * mode_frame, door_duration)));
} else if (mode_frame <= door_duration + 64) {
self.player.tick();
// const cur_room = cur_stage.rooms[self.cur_room_index];
const prev_room = cur_stage.rooms[self.prev_room_index];
self.scrollr.x = prev_room.bounds.x - @divTrunc((mode_frame - door_duration) * screen_width, 64);
self.player.box.x = prev_room.bounds.x + self.player.box.w - @divTrunc(3 * self.player.box.w * (mode_frame - door_duration), 64);
} else if (mode_frame <= door_duration + 64 + door_duration) {
self.door2_h = @intCast(u8, @divTrunc(4 * (mode_frame - 64 - door_duration), door_duration));
self.door2_h = @intCast(@divTrunc(4 * (mode_frame - 64 - door_duration), door_duration));
}
if (mode_frame == door_duration + 64 + door_duration) {
room_transition = .none;
Expand Down Expand Up @@ -305,7 +305,7 @@ pub const GameData = struct {
if (cur_room.door1_y != Room.no_door) {
var door_box = Box{
.x = cur_room.bounds.x,
.y = cur_room.bounds.y + @intCast(i32, cur_room.door1_y) * Tile.size,
.y = cur_room.bounds.y + @as(i32, @intCast(cur_room.door1_y)) * Tile.size,
.w = Tile.size,
.h = 4 * Tile.size,
};
Expand All @@ -323,7 +323,7 @@ pub const GameData = struct {
if (cur_room.door2_y != Room.no_door) {
var door_box = Box{
.x = cur_room.bounds.x + cur_room.bounds.w - Tile.size,
.y = cur_room.bounds.y + @intCast(i32, cur_room.door2_y) * Tile.size,
.y = cur_room.bounds.y + @as(i32, @intCast(cur_room.door2_y)) * Tile.size,
.w = Tile.size,
.h = 4 * Tile.size,
};
Expand Down Expand Up @@ -462,15 +462,15 @@ fn setNextRoom(next_room_index: u8) void {

var death_frame_counter: u32 = 0;
fn drawDeathEffect(x: i32, y: i32) void {
const frame = @intCast(i32, (death_frame_counter / 3) % 6);
const frame = @as(i32, @intCast((death_frame_counter / 3) % 6));
const src_rect = Rect.init(frame * 24, 0, 24, 24);

var i: usize = 0;
while (i < 8) : (i += 1) {
const angle: f32 = std.math.pi * @intToFloat(f32, i) / 4.0;
const r: f32 = 2 * @intToFloat(f32, death_frame_counter);
const dx = x + @floatToInt(i32, r * @cos(angle));
const dy = y + @floatToInt(i32, r * @sin(angle));
const angle: f32 = std.math.pi * @as(f32, @floatFromInt(i)) / 4.0;
const r: f32 = 2 * @as(f32, @floatFromInt(death_frame_counter));
const dx = x + @as(i32, @intFromFloat(r * @cos(angle)));
const dy = y + @as(i32, @intFromFloat(r * @sin(angle)));
Sprite.drawFrame(effects_tex, src_rect, dx, dy);
}

Expand Down
6 changes: 3 additions & 3 deletions src/web.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn log(
};
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";

const writer = std.io.Writer(void, error{}, writeLog){.context = {}};
const writer = std.io.Writer(void, error{}, writeLog){ .context = {} };
writer.print(level_txt ++ prefix ++ format ++ "\n", args) catch return;

jsLogFlush();
Expand All @@ -29,11 +29,11 @@ var sbuf: [4000]u8 = undefined; // FIXME

pub const LocalStorage = struct {
pub fn setString(key: []const u8, value: []const u8) void {
jsStorageSetString(@ptrToInt(key.ptr), key.len, @ptrToInt(value.ptr), value.len);
jsStorageSetString(@intFromPtr(key.ptr), key.len, @intFromPtr(value.ptr), value.len);
}

pub fn getString(key: []const u8) []const u8 {
const len = jsStorageGetString(@ptrToInt(key.ptr), key.len, @ptrToInt(&sbuf), sbuf.len);
const len = jsStorageGetString(@intFromPtr(key.ptr), key.len, @intFromPtr(&sbuf), sbuf.len);
return sbuf[0..len];
}
};
Expand Down
Loading

0 comments on commit 7211752

Please sign in to comment.