Skip to content

Commit

Permalink
Upgrade to Zig 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
kwakzalver committed Sep 1, 2023
1 parent 0020e1a commit 5c24643
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 122 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Any of the following commands will produce a more than decent binary.

```console
zig build -Drelease-safe
zig build -Drelease-small
zig build -Drelease-fast
zig build -Doptimize=ReleaseSafe
zig build -Doptimize=ReleaseFast
zig build -Doptimize=ReleaseSmall
```

Enjoy!
Expand Down Expand Up @@ -71,17 +71,15 @@ variant. Simply replace `./assets/font.ttf`, then recompile to change it.
## Colors

In-game you can cycle through the colorschemes by pressing **Tab** or
**Backspace**. The default colors are **habamax**, **gruvbox dark**, **gruvbox
light**, **onedark**, and catppuccin **macchiato**. These are all popular
colorschemes used by programmers.
**Backspace**. The default colors are **habamax**, **gruvbox**, **onedark**,
and some pastels. Most of these are popular colorschemes used by programmers.

## Style

The drawing style for pieces is very simple. I hope to keep it as simple as
possible, while still having some variety. That said, there is a **Style**
enumeration which could be expanded on.


# Contributions

Feel free to suggest changes.
Expand Down
79 changes: 49 additions & 30 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();

const exe = b.addExecutable("zigtris", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
const exe = b.addExecutable(.{
.name = "zigtris",
.root_source_file = .{
.path = "src/main.zig",
},
.target = target,
.optimize = optimize,
});
exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("SDL2_ttf");
// exe.linkSystemLibrary("SDL2_mixer");
exe.linkSystemLibrary("c");
exe.install();

const run_cmd = exe.run();
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
Expand All @@ -29,25 +28,45 @@ pub fn build(b: *std.build.Builder) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const main_tests = b.addTest("src/main.zig");
main_tests.setTarget(target);
main_tests.setBuildMode(mode);
const main_tests = b.addTest(.{
.name = "Main",
.root_source_file = .{
.path = "src/main.zig",
},
.target = target,
.optimize = optimize,
});

const window_tests = b.addTest("src/window.zig");
window_tests.setTarget(target);
window_tests.setBuildMode(mode);
const window_tests = b.addTest(.{
.name = "Window",
.root_source_file = .{
.path = "src/window.zig",
},
.target = target,
.optimize = optimize,
});

const game_tests = b.addTest("src/game.zig");
game_tests.setTarget(target);
game_tests.setBuildMode(mode);
const game_tests = b.addTest(.{
.name = "Game",
.root_source_file = .{
.path = "src/game.zig",
},
.target = target,
.optimize = optimize,
});

const definitions_tests = b.addTest("src/definitions.zig");
definitions_tests.setTarget(target);
definitions_tests.setBuildMode(mode);
const definitions_tests = b.addTest(.{
.name = "Definitions",
.root_source_file = .{
.path = "src/definitions.zig",
},
.target = target,
.optimize = optimize,
});

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&main_tests.step);
test_step.dependOn(&window_tests.step);
test_step.dependOn(&game_tests.step);
test_step.dependOn(&definitions_tests.step);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&b.addRunArtifact(main_tests).step);
test_step.dependOn(&b.addRunArtifact(window_tests).step);
test_step.dependOn(&b.addRunArtifact(game_tests).step);
test_step.dependOn(&b.addRunArtifact(definitions_tests).step);
}
53 changes: 25 additions & 28 deletions src/definitions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ pub const Color = struct {

pub fn from_u24(rgb: u24) Color {
return Color{
.red = @intCast(u8, (rgb >> 16) & 0xff),
.green = @intCast(u8, (rgb >> 8) & 0xff),
.blue = @intCast(u8, (rgb >> 0) & 0xff),
.red = @as(u8, @intCast((rgb >> 16) & 0xff)),
.green = @as(u8, @intCast((rgb >> 8) & 0xff)),
.blue = @as(u8, @intCast((rgb >> 0) & 0xff)),
};
}

pub fn combine(lhs: Color, rhs: Color, l: u8) Color {
const r: u8 = (128 - l);
const lr = @intCast(u16, lhs.red);
const lg = @intCast(u16, lhs.green);
const lb = @intCast(u16, lhs.blue);
const rr = @intCast(u16, rhs.red);
const rg = @intCast(u16, rhs.green);
const rb = @intCast(u16, rhs.blue);
const lr = @as(u16, @intCast(lhs.red));
const lg = @as(u16, @intCast(lhs.green));
const lb = @as(u16, @intCast(lhs.blue));
const rr = @as(u16, @intCast(rhs.red));
const rg = @as(u16, @intCast(rhs.green));
const rb = @as(u16, @intCast(rhs.blue));

return Color{
.red = @intCast(u8, @divFloor(lr * l + rr * r, 128)),
.green = @intCast(u8, @divFloor(lg * l + rg * r, 128)),
.blue = @intCast(u8, @divFloor(lb * l + rb * r, 128)),
.red = @as(u8, @intCast(@divFloor(lr * l + rr * r, 128))),
.green = @as(u8, @intCast(@divFloor(lg * l + rg * r, 128))),
.blue = @as(u8, @intCast(@divFloor(lb * l + rb * r, 128))),
};
}
};
Expand All @@ -55,15 +55,15 @@ pub const Colorname = enum(u3) {
pub const iter = iterable_enum(Self);

pub fn iter_index(s: Self) usize {
return @enumToInt(s);
return @intFromEnum(s);
}

pub fn next(s: Self) Colorname {
return Colorname.iter[(@enumToInt(s) + 1) % Colorname.iter.len];
return Colorname.iter[(@intFromEnum(s) + 1) % Colorname.iter.len];
}

pub fn previous(s: Self) Colorname {
return Colorname.iter[(@enumToInt(s) - 1) % Colorname.iter.len];
return Colorname.iter[(@intFromEnum(s) - 1) % Colorname.iter.len];
}
};

Expand Down Expand Up @@ -311,7 +311,7 @@ pub const Rotation = enum(u2) {
pub const iter = iterable_enum(Self);

pub fn iter_index(s: Self) usize {
return @enumToInt(s);
return @intFromEnum(s);
}
};

Expand Down Expand Up @@ -339,10 +339,7 @@ pub const PieceType = enum(u3) {

// IMPORTANT make sure to not use this `iter_index` when PieceType.None
pub fn iter_index(s: Self) usize {
return if (comptime s != PieceType.None)
@enumToInt(s)
else
unreachable;
return @intFromEnum(s);
}

pub fn piecetype_rotation_matrix(
Expand Down Expand Up @@ -560,8 +557,8 @@ pub const MinMaxRC = struct {
@setEvalBranchQuota(2000);
var lookup_table: [PieceType.iter.len][Rotation.iter.len]MinMaxRC =
undefined;
for (PieceType.iter) |ptype, ti| {
for (Rotation.iter) |prot, ri| {
for (PieceType.iter, 0..) |ptype, ti| {
for (Rotation.iter, 0..) |prot, ri| {
const d = PieceType.piecetype_rotation_matrix(ptype, prot);
const B = PieceType.None;

Expand Down Expand Up @@ -618,10 +615,10 @@ pub const MinMaxRC = struct {
)) : (min_col += 1) {}

lookup_table[ti][ri] = MinMaxRC{
.min_row = @intCast(i8, min_row),
.min_col = @intCast(i8, min_col),
.max_row = @intCast(i8, max_row),
.max_col = @intCast(i8, max_col),
.min_row = @as(i8, @intCast(min_row)),
.min_col = @as(i8, @intCast(min_col)),
.max_row = @as(i8, @intCast(max_row)),
.max_col = @as(i8, @intCast(max_col)),
};
}
}
Expand Down Expand Up @@ -659,8 +656,8 @@ pub const Piece = struct {
// cute little trick to generate `.iter` for enums
fn iterable_enum(comptime T: type) [@typeInfo(T).Enum.fields.len]T {
var result: [@typeInfo(T).Enum.fields.len]T = undefined;
inline for (@typeInfo(T).Enum.fields) |e, i| {
result[i] = @intToEnum(T, e.value);
inline for (@typeInfo(T).Enum.fields, 0..) |e, i| {
result[i] = @as(T, @enumFromInt(e.value));
}
return result;
}
Expand Down
46 changes: 23 additions & 23 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ fn collision() bool {
G.current_piece.type,
G.current_piece.rotation,
);
for (data) |drow, dr| {
for (drow) |e, dc| {
for (data, 0..) |drow, dr| {
for (drow, 0..) |e, dc| {
if (e != B) {
const c = @intCast(usize, col + @intCast(i8, dc));
const r = @intCast(usize, row + @intCast(i8, dr));
const c = @as(usize, @intCast(col + @as(i8, @intCast(dc))));
const r = @as(usize, @intCast(row + @as(i8, @intCast(dr))));
if (G.Grid[r][c] != B) {
return true;
}
Expand Down Expand Up @@ -128,11 +128,11 @@ fn materialize() void {
);
const col = G.current_piece.col;
const row = G.current_piece.row;
for (data) |drow, dr| {
for (drow) |e, dc| {
for (data, 0..) |drow, dr| {
for (drow, 0..) |e, dc| {
if (e != PieceType.None) {
const c = @intCast(usize, col + @intCast(i8, dc));
const r = @intCast(usize, row + @intCast(i8, dr));
const c = @as(usize, @intCast(col + @as(i8, @intCast(dc))));
const r = @as(usize, @intCast(row + @as(i8, @intCast(dr))));
G.Grid[r][c] = e;
}
}
Expand All @@ -147,11 +147,11 @@ fn push() void {
);
const col = G.current_piece.col;
const row = G.current_piece.row;
for (data) |drow, dr| {
for (drow) |e, dc| {
for (data, 0..) |drow, dr| {
for (drow, 0..) |e, dc| {
if (e != PieceType.None) {
const c = @intCast(usize, col + @intCast(i8, dc));
const r = @intCast(usize, row + @intCast(i8, dr));
const c = @as(usize, @intCast(col + @as(i8, @intCast(dc))));
const r = @as(usize, @intCast(row + @as(i8, @intCast(dr))));
G.Grid[r][c] = e;
}
}
Expand All @@ -176,11 +176,11 @@ fn pop() void {
const B = PieceType.None;
const col = piece.col;
const row = piece.row;
for (data) |drow, dr| {
for (drow) |e, dc| {
for (data, 0..) |drow, dr| {
for (drow, 0..) |e, dc| {
if (e != PieceType.None) {
const c = @intCast(usize, col + @intCast(i8, dc));
const r = @intCast(usize, row + @intCast(i8, dr));
const c = @as(usize, @intCast(col + @as(i8, @intCast(dc))));
const r = @as(usize, @intCast(row + @as(i8, @intCast(dr))));
G.Grid[r][c] = B;
}
}
Expand All @@ -207,8 +207,8 @@ fn next_piece() void {
}

fn clear_grid() void {
for (G.Grid) |Row, r| {
for (Row) |_, c| {
for (G.Grid, 0..) |Row, r| {
for (Row, 0..) |_, c| {
G.Grid[r][c] = PieceType.None;
}
}
Expand Down Expand Up @@ -409,7 +409,7 @@ fn compute_metrics(row_start: u8) Metrics {
var r: u8 = row_start;
while (r != ROWS and G.Grid[r][c] == B) : (r += 1) {
background += 1;
deepest = std.math.max(deepest, r);
deepest = @max(deepest, r);
}
while (r != ROWS) : (r += 1) {
if (G.Grid[r][c] == B) {
Expand All @@ -428,8 +428,8 @@ fn compute_metrics(row_start: u8) Metrics {
fn compute_score(placed: Piece) i32 {
const row_start = find_row_start();
const metrics = compute_metrics(row_start);
const grid_height = @intCast(i8, ROWS - row_start);
const piece_placement = @intCast(i8, ROWS) - placed.row;
const grid_height = @as(i8, @intCast(ROWS - row_start));
const piece_placement = @as(i8, @intCast(ROWS)) - placed.row;
const piece_orientation: i8 = switch (placed.rotation) {
.Right, .Left => 1,
else => 0,
Expand Down Expand Up @@ -642,9 +642,9 @@ test "clear lines" {
// for example, you just got an I, and you get all other pieces twice first
// [I] : [J L O S T Z] : [J L O S T Z] : [I]
test "piecetypes are satisfyingly random" {
G.xoshiro = std.rand.DefaultPrng.init(@intCast(
G.xoshiro = std.rand.DefaultPrng.init(@as(
u64,
std.time.milliTimestamp(),
@intCast(std.time.milliTimestamp()),
));
G.rngesus = G.xoshiro.random();
var seen: [PieceType.iter.len]u8 = .{0} ** PieceType.iter.len;
Expand Down
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const std = @import("std");
const window = @import("window.zig");

pub fn main() anyerror!void {
Expand Down
Loading

0 comments on commit 5c24643

Please sign in to comment.