Created
October 2, 2024 17:53
-
-
Save peterhellberg/02b9429b29ffeca68f6a481d7685d01a to your computer and use it in GitHub Desktop.
Diff of changes to have https://github.com/tsoding/zigout build with a current version of Zig ⚡
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/.gitignore b/.gitignore | |
index 4a0641e..3389c86 100644 | |
--- a/.gitignore | |
+++ b/.gitignore | |
@@ -1,2 +1,2 @@ | |
-zig-cache/ | |
-zig-out/ | |
\ No newline at end of file | |
+.zig-cache/ | |
+zig-out/ | |
diff --git a/build.zig b/build.zig | |
index c6720c8..6fe74ab 100644 | |
--- a/build.zig | |
+++ b/build.zig | |
@@ -1,36 +1,29 @@ | |
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. | |
+pub fn build(b: *std.Build) !void { | |
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(.{ | |
+ .name = "zigout", | |
+ .root_source_file = b.path("src/main.zig"), | |
+ .target = target, | |
+ .optimize = optimize, | |
+ }); | |
- const exe = b.addExecutable("zigout", "src/main.zig"); | |
- exe.setTarget(target); | |
- exe.setBuildMode(mode); | |
exe.linkSystemLibrary("SDL2"); | |
- exe.linkSystemLibrary("c"); | |
- exe.install(); | |
+ exe.linkLibC(); | |
+ | |
+ b.installArtifact(exe); | |
+ | |
+ const run_cmd = b.addRunArtifact(exe); | |
- const run_cmd = exe.run(); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
+ | |
if (b.args) |args| { | |
run_cmd.addArgs(args); | |
} | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.step); | |
- | |
- const exe_tests = b.addTest("src/main.zig"); | |
- exe_tests.setTarget(target); | |
- exe_tests.setBuildMode(mode); | |
- | |
- const test_step = b.step("test", "Run unit tests"); | |
- test_step.dependOn(&exe_tests.step); | |
} | |
diff --git a/src/main.zig b/src/main.zig | |
index 470a381..a60efbc 100644 | |
--- a/src/main.zig | |
+++ b/src/main.zig | |
@@ -1,22 +1,24 @@ | |
const std = @import("std"); | |
const math = std.math; | |
+ | |
const c = @cImport({ | |
- @cInclude("SDL2/SDL.h"); | |
+ @cInclude("SDL.h"); | |
}); | |
+ | |
const overlaps = c.SDL_HasIntersection; | |
const FPS = 60; | |
-const DELTA_TIME_SEC: f32 = 1.0/@intToFloat(f32, FPS); | |
+const DELTA_TIME_SEC: f32 = 1.0 / @as(f32, @floatFromInt(FPS)); | |
const WINDOW_WIDTH = 800; | |
const WINDOW_HEIGHT = 600; | |
const BACKGROUND_COLOR = 0xFF181818; | |
-const PROJ_SIZE: f32 = 25*0.80; | |
+const PROJ_SIZE: f32 = 25 * 0.80; | |
const PROJ_SPEED: f32 = 350; | |
const PROJ_COLOR = 0xFFFFFFFF; | |
const BAR_LEN: f32 = 100; | |
const BAR_THICCNESS: f32 = PROJ_SIZE; | |
const BAR_Y: f32 = WINDOW_HEIGHT - PROJ_SIZE - 50; | |
-const BAR_SPEED: f32 = PROJ_SPEED*1.5; | |
+const BAR_SPEED: f32 = PROJ_SPEED * 1.5; | |
const BAR_COLOR = 0xFF3030FF; | |
const TARGET_WIDTH = BAR_LEN; | |
const TARGET_HEIGHT = PROJ_SIZE; | |
@@ -24,8 +26,8 @@ const TARGET_PADDING_X = 20; | |
const TARGET_PADDING_Y = 50; | |
const TARGET_ROWS = 4; | |
const TARGET_COLS = 5; | |
-const TARGET_GRID_WIDTH = (TARGET_COLS*TARGET_WIDTH + (TARGET_COLS - 1)*TARGET_PADDING_X); | |
-const TARGET_GRID_X = WINDOW_WIDTH/2 - TARGET_GRID_WIDTH/2; | |
+const TARGET_GRID_WIDTH = (TARGET_COLS * TARGET_WIDTH + (TARGET_COLS - 1) * TARGET_PADDING_X); | |
+const TARGET_GRID_X = WINDOW_WIDTH / 2 - TARGET_GRID_WIDTH / 2; | |
const TARGET_GRID_Y = 50; | |
const TARGET_COLOR = 0xFF30FF30; | |
@@ -35,26 +37,35 @@ const Target = struct { | |
dead: bool = false, | |
}; | |
-fn init_targets() [TARGET_ROWS*TARGET_COLS]Target { | |
- var targets: [TARGET_ROWS*TARGET_COLS]Target = undefined; | |
+fn init_targets() [TARGET_ROWS * TARGET_COLS]Target { | |
+ var targets: [TARGET_ROWS * TARGET_COLS]Target = undefined; | |
var row: usize = 0; | |
+ | |
while (row < TARGET_ROWS) : (row += 1) { | |
var col: usize = 0; | |
+ | |
while (col < TARGET_COLS) : (col += 1) { | |
- targets[row*TARGET_COLS + col] = Target { | |
- .x = TARGET_GRID_X + (TARGET_WIDTH + TARGET_PADDING_X)*@intToFloat(f32, col), | |
- .y = TARGET_GRID_Y + TARGET_PADDING_Y*@intToFloat(f32, row) | |
+ targets[row * TARGET_COLS + col] = Target{ | |
+ .x = TARGET_GRID_X + (TARGET_WIDTH + TARGET_PADDING_X) * @as( | |
+ f32, | |
+ @floatFromInt(col), | |
+ ), | |
+ .y = TARGET_GRID_Y + TARGET_PADDING_Y * @as( | |
+ f32, | |
+ @floatFromInt(row), | |
+ ), | |
}; | |
} | |
} | |
+ | |
return targets; | |
} | |
var targets_pool = init_targets(); | |
-var bar_x: f32 = WINDOW_WIDTH/2 - BAR_LEN/2; | |
-var bar_dx: f32 = 0; | |
-var proj_x: f32 = WINDOW_WIDTH/2 - PROJ_SIZE/2; | |
-var proj_y: f32 = BAR_Y - BAR_THICCNESS/2 - PROJ_SIZE; | |
+var bar_x: f32 = WINDOW_WIDTH / 2 - BAR_LEN / 2; | |
+var bar_dx: f32 = 0; | |
+var proj_x: f32 = WINDOW_WIDTH / 2 - PROJ_SIZE / 2; | |
+var proj_y: f32 = BAR_Y - BAR_THICCNESS / 2 - PROJ_SIZE; | |
var proj_dx: f32 = 1; | |
var proj_dy: f32 = 1; | |
var quit = false; | |
@@ -64,64 +75,66 @@ var started = false; | |
// TODO: score | |
// TODO: victory | |
-fn make_rect(x: f32, y: f32, w: f32, h: f32) c.SDL_Rect { | |
- return c.SDL_Rect { | |
- .x = @floatToInt(i32, x), | |
- .y = @floatToInt(i32, y), | |
- .w = @floatToInt(i32, w), | |
- .h = @floatToInt(i32, h) | |
- }; | |
+fn makeRect(x: f32, y: f32, w: f32, h: f32) c.SDL_Rect { | |
+ return c.SDL_Rect{ .x = @intFromFloat(x), .y = @intFromFloat(y), .w = @intFromFloat(w), .h = @intFromFloat(h) }; | |
} | |
-fn set_color(renderer: *c.SDL_Renderer, color: u32) void { | |
- const r = @truncate(u8, (color >> (0*8)) & 0xFF); | |
- const g = @truncate(u8, (color >> (1*8)) & 0xFF); | |
- const b = @truncate(u8, (color >> (2*8)) & 0xFF); | |
- const a = @truncate(u8, (color >> (3*8)) & 0xFF); | |
+fn setColor(renderer: *c.SDL_Renderer, color: u32) void { | |
+ const r: u8 = @truncate((color >> (0 * 8)) & 0xFF); | |
+ const g: u8 = @truncate((color >> (1 * 8)) & 0xFF); | |
+ const b: u8 = @truncate((color >> (2 * 8)) & 0xFF); | |
+ const a: u8 = @truncate((color >> (3 * 8)) & 0xFF); | |
+ | |
_ = c.SDL_SetRenderDrawColor(renderer, r, g, b, a); | |
} | |
-fn target_rect(target: Target) c.SDL_Rect { | |
- return make_rect(target.x, target.y, TARGET_WIDTH, TARGET_HEIGHT); | |
+fn targetRect(target: Target) c.SDL_Rect { | |
+ return makeRect(target.x, target.y, TARGET_WIDTH, TARGET_HEIGHT); | |
} | |
-fn proj_rect(x: f32, y: f32) c.SDL_Rect { | |
- return make_rect(x, y, PROJ_SIZE, PROJ_SIZE); | |
+fn projRect(x: f32, y: f32) c.SDL_Rect { | |
+ return makeRect(x, y, PROJ_SIZE, PROJ_SIZE); | |
} | |
-fn bar_rect(x: f32) c.SDL_Rect { | |
- return make_rect(x, BAR_Y - BAR_THICCNESS/2, BAR_LEN, BAR_THICCNESS); | |
+fn barRect(x: f32) c.SDL_Rect { | |
+ return makeRect(x, BAR_Y - BAR_THICCNESS / 2, BAR_LEN, BAR_THICCNESS); | |
} | |
-fn horz_collision(dt: f32) void { | |
- var proj_nx: f32 = proj_x + proj_dx*PROJ_SPEED*dt; | |
- if (proj_nx < 0 or proj_nx + PROJ_SIZE > WINDOW_WIDTH or overlaps(&proj_rect(proj_nx, proj_y), &bar_rect(bar_x)) != 0) { | |
+fn horzCollision(dt: f32) void { | |
+ const proj_nx: f32 = proj_x + proj_dx * PROJ_SPEED * dt; | |
+ | |
+ if (proj_nx < 0 or proj_nx + PROJ_SIZE > WINDOW_WIDTH or overlaps(&projRect(proj_nx, proj_y), &barRect(bar_x)) != 0) { | |
proj_dx *= -1; | |
return; | |
} | |
- for (targets_pool) |*it| { | |
- if (!it.dead and overlaps(&proj_rect(proj_nx, proj_y), &target_rect(it.*)) != 0) { | |
+ | |
+ for (&targets_pool) |*it| { | |
+ if (!it.dead and overlaps(&projRect(proj_nx, proj_y), &targetRect(it.*)) != 0) { | |
it.dead = true; | |
proj_dx *= -1; | |
return; | |
} | |
} | |
+ | |
proj_x = proj_nx; | |
} | |
-fn vert_collision(dt: f32) void { | |
- var proj_ny: f32 = proj_y + proj_dy*PROJ_SPEED*dt; | |
+fn vertCollision(dt: f32) void { | |
+ const proj_ny: f32 = proj_y + proj_dy * PROJ_SPEED * dt; | |
+ | |
if (proj_ny < 0 or proj_ny + PROJ_SIZE > WINDOW_HEIGHT) { | |
proj_dy *= -1; | |
return; | |
} | |
- if (overlaps(&proj_rect(proj_x, proj_ny), &bar_rect(bar_x)) != 0) { | |
+ | |
+ if (overlaps(&projRect(proj_x, proj_ny), &barRect(bar_x)) != 0) { | |
if (bar_dx != 0) proj_dx = bar_dx; | |
proj_dy *= -1; | |
return; | |
} | |
- for (targets_pool) |*it| { | |
- if (!it.dead and overlaps(&proj_rect(proj_x, proj_ny), &target_rect(it.*)) != 0) { | |
+ | |
+ for (&targets_pool) |*it| { | |
+ if (!it.dead and overlaps(&projRect(proj_x, proj_ny), &targetRect(it.*)) != 0) { | |
it.dead = true; | |
proj_dy *= -1; | |
return; | |
@@ -130,35 +143,36 @@ fn vert_collision(dt: f32) void { | |
proj_y = proj_ny; | |
} | |
-fn bar_collision(dt: f32) void { | |
- var bar_nx : f32 = math.clamp(bar_x + bar_dx*BAR_SPEED*dt, 0, WINDOW_WIDTH - BAR_LEN); | |
- if (overlaps(&proj_rect(proj_x, proj_y), &bar_rect(bar_nx)) != 0) return; | |
+fn barCollision(dt: f32) void { | |
+ const bar_nx: f32 = math.clamp(bar_x + bar_dx * BAR_SPEED * dt, 0, WINDOW_WIDTH - BAR_LEN); | |
+ if (overlaps(&projRect(proj_x, proj_y), &barRect(bar_nx)) != 0) return; | |
bar_x = bar_nx; | |
} | |
fn update(dt: f32) void { | |
if (!pause and started) { | |
- if (overlaps(&proj_rect(proj_x, proj_y), &bar_rect(bar_x)) != 0) { | |
- proj_y = BAR_Y - BAR_THICCNESS/2 - PROJ_SIZE - 1.0; | |
+ if (overlaps(&projRect(proj_x, proj_y), &barRect(bar_x)) != 0) { | |
+ proj_y = BAR_Y - BAR_THICCNESS / 2 - PROJ_SIZE - 1.0; | |
return; | |
} | |
- bar_collision(dt); | |
- horz_collision(dt); | |
- vert_collision(dt); | |
+ | |
+ barCollision(dt); | |
+ horzCollision(dt); | |
+ vertCollision(dt); | |
} | |
} | |
fn render(renderer: *c.SDL_Renderer) void { | |
- set_color(renderer, PROJ_COLOR); | |
- _ = c.SDL_RenderFillRect(renderer, &proj_rect(proj_x, proj_y)); | |
+ setColor(renderer, PROJ_COLOR); | |
+ _ = c.SDL_RenderFillRect(renderer, &projRect(proj_x, proj_y)); | |
- set_color(renderer, BAR_COLOR); | |
- _ = c.SDL_RenderFillRect(renderer, &bar_rect(bar_x)); | |
+ setColor(renderer, BAR_COLOR); | |
+ _ = c.SDL_RenderFillRect(renderer, &barRect(bar_x)); | |
- set_color(renderer, TARGET_COLOR); | |
+ setColor(renderer, TARGET_COLOR); | |
for (targets_pool) |target| { | |
if (!target.dead) { | |
- _ = c.SDL_RenderFillRect(renderer, &target_rect(target)); | |
+ _ = c.SDL_RenderFillRect(renderer, &targetRect(target)); | |
} | |
} | |
} | |
@@ -170,13 +184,24 @@ pub fn main() !void { | |
} | |
defer c.SDL_Quit(); | |
- const window = c.SDL_CreateWindow("Zigout", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0) orelse { | |
+ const window = c.SDL_CreateWindow( | |
+ "Zigout", | |
+ 0, | |
+ 0, | |
+ WINDOW_WIDTH, | |
+ WINDOW_HEIGHT, | |
+ c.SDL_WINDOW_VULKAN | c.SDL_WINDOW_BORDERLESS, | |
+ ) orelse { | |
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
}; | |
defer c.SDL_DestroyWindow(window); | |
- const renderer = c.SDL_CreateRenderer(window, -1, c.SDL_RENDERER_ACCELERATED) orelse { | |
+ const renderer = c.SDL_CreateRenderer( | |
+ window, | |
+ -1, | |
+ c.SDL_RENDERER_ACCELERATED | c.SDL_RENDERER_PRESENTVSYNC, | |
+ ) orelse { | |
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
}; | |
@@ -186,13 +211,13 @@ pub fn main() !void { | |
while (!quit) { | |
var event: c.SDL_Event = undefined; | |
+ | |
while (c.SDL_PollEvent(&event) != 0) { | |
- switch (event.@"type") { | |
- c.SDL_QUIT => { | |
- quit = true; | |
- }, | |
+ switch (event.type) { | |
+ c.SDL_QUIT => quit = true, | |
c.SDL_KEYDOWN => switch (event.key.keysym.sym) { | |
- ' ' => { pause = !pause; }, | |
+ ' ' => pause = !pause, | |
+ 'q' => quit = true, | |
else => {}, | |
}, | |
else => {}, | |
@@ -200,6 +225,7 @@ pub fn main() !void { | |
} | |
bar_dx = 0; | |
+ | |
if (keyboard[c.SDL_SCANCODE_A] != 0) { | |
bar_dx += -1; | |
if (!started) { | |
@@ -207,6 +233,7 @@ pub fn main() !void { | |
proj_dx = -1; | |
} | |
} | |
+ | |
if (keyboard[c.SDL_SCANCODE_D] != 0) { | |
bar_dx += 1; | |
if (!started) { | |
@@ -217,13 +244,14 @@ pub fn main() !void { | |
update(DELTA_TIME_SEC); | |
- set_color(renderer, BACKGROUND_COLOR); | |
+ setColor(renderer, BACKGROUND_COLOR); | |
+ | |
_ = c.SDL_RenderClear(renderer); | |
render(renderer); | |
c.SDL_RenderPresent(renderer); | |
- c.SDL_Delay(1000/FPS); | |
+ c.SDL_Delay(1000 / FPS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment