diff --git a/.gitignore b/.gitignore index d35ea3c..50f2d96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .vscode zig-cache zig-out -allegro.path **/*~ diff --git a/README.md b/README.md index 9ed5140..d5f9985 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ For building you need: - A recent (0.11.0-dev) Zig compiler. - Allegro 5.2.8 development libraries. -You can use `allegro.path` if your Allegro development libraries can't be natively found. In this file you can put the path to a directory that contains the Allegro `lib`, `bin` and `include` folders. +You can use `allegro-dir` build option if your Allegro development libraries can't be natively found. In this file you can put the path to a directory that contains the Allegro `lib`, `bin` and `include` folders. Build: `zig build` diff --git a/allegro/.gitignore b/allegro/.gitignore new file mode 100644 index 0000000..50f2d96 --- /dev/null +++ b/allegro/.gitignore @@ -0,0 +1,4 @@ +.vscode +zig-cache +zig-out +**/*~ diff --git a/allegro/build.zig b/allegro/build.zig index 75acdb7..1caf4ac 100644 --- a/allegro/build.zig +++ b/allegro/build.zig @@ -8,17 +8,88 @@ const cwd = std.fs.path.dirname(current_file()).?; const path_separator = std.fs.path.sep_str; /// add this package to exe -pub fn addTo(comptime allegro_dir: []const u8, exe: *std.build.LibExeObjStep) void { - exe.addAnonymousModule("allegro", .{ .source_file = .{ .path = cwd ++ path_separator ++ "allegro.zig" } }); - if (allegro_dir.len > 0) { - exe.addIncludePath(allegro_dir ++ path_separator ++ "include"); - exe.addLibraryPath(allegro_dir ++ path_separator ++ "lib"); - } - exe.linkLibC(); - - exe.linkSystemLibrary("allegro"); - exe.linkSystemLibrary("allegro_font"); - exe.linkSystemLibrary("allegro_image"); - exe.linkSystemLibrary("allegro_memfile"); - exe.linkSystemLibrary("allegro_ttf"); +pub fn addModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void { + // _ = b.addStaticLibrary(.{ + // .name = "allegro", + // .root_source_file = .{ .path = cwd ++ path_separator ++ "allegro.zig" }, + // .target = target, + // .optimize = optimize, + // }); + _ = b.addModule("allegro", .{ + .root_source_file = .{ .path = cwd ++ path_separator ++ "allegro.zig" }, + .target = target, + .optimize = optimize, + }); +} + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const static = false; + + // Expose this as a module that others can import + var module = b.addModule("allegro", .{ + .root_source_file = .{ .path = "src/allegro.zig" }, + .target = target, + .optimize = optimize, + }); + + const allegro_dir = b.option([]const u8, "allegro-dir", "path to a directory that contains the Allegro `lib`, `bin` and `include` folders") orelse ""; + if (allegro_dir.len != 0) { + module.addIncludePath(.{ .path = b.pathJoin(&[_][]const u8{ allegro_dir, "include" }) }); + module.addLibraryPath(.{ .path = b.pathJoin(&[_][]const u8{ allegro_dir, "lib" }) }); + } + + module.link_libc = true; + + if (static) { + module.linkSystemLibrary("allegro-static", .{}); + module.linkSystemLibrary("allegro_font-static", .{}); + module.linkSystemLibrary("allegro_image-static", .{}); + module.linkSystemLibrary("allegro_memfile-static", .{}); + module.linkSystemLibrary("allegro_ttf-static", .{}); + module.linkSystemLibrary("dumb", .{}); + module.linkSystemLibrary("FLAC", .{}); + module.linkSystemLibrary("freetype", .{}); + module.linkSystemLibrary("jpeg", .{}); + module.linkSystemLibrary("ogg", .{}); + module.linkSystemLibrary("opus", .{}); + module.linkSystemLibrary("opusfile", .{}); + module.linkSystemLibrary("physfs", .{}); + module.linkSystemLibrary("png16", .{}); + // module.linkSystemLibrary("theoradec", .{}); + module.linkSystemLibrary("vorbis", .{}); + module.linkSystemLibrary("vorbisfile", .{}); + module.linkSystemLibrary("webp", .{}); + module.linkSystemLibrary("webpdecoder", .{}); + module.linkSystemLibrary("webpdemux", .{}); + module.linkSystemLibrary("zlib", .{}); + module.linkSystemLibrary("opengl32", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + if (target.result.os.tag == .windows) { + module.linkSystemLibrary("gdi32", .{}); + module.linkSystemLibrary("ole32", .{}); + module.linkSystemLibrary("shlwapi", .{}); + module.linkSystemLibrary("winmm", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + // module.linkSystemLibrary("", .{}); + if (target.result.isGnu()) { + // module.linkSystemLibrary("stdc++", .{}); + } + } + } else { + module.linkSystemLibrary("allegro.dll", .{}); + module.linkSystemLibrary("allegro_font.dll", .{}); + module.linkSystemLibrary("allegro_image.dll", .{}); + module.linkSystemLibrary("allegro_memfile.dll", .{}); + module.linkSystemLibrary("allegro_ttf.dll", .{}); + } } diff --git a/allegro/build.zig.zon b/allegro/build.zig.zon new file mode 100644 index 0000000..7258a02 --- /dev/null +++ b/allegro/build.zig.zon @@ -0,0 +1,9 @@ +.{ + .name = "allegro", + .version = "0.1.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, +} diff --git a/allegro/allegro.zig b/allegro/src/allegro.zig similarity index 87% rename from allegro/allegro.zig rename to allegro/src/allegro.zig index 9cc6f6f..11b3a37 100644 --- a/allegro/allegro.zig +++ b/allegro/src/allegro.zig @@ -14,22 +14,22 @@ pub const Bitmap = struct { } pub fn drawCentered(self: Bitmap, x: f32, y: f32) void { - self.draw(x - 0.5 * @intToFloat(f32, self.width()), y - 0.5 * @intToFloat(f32, self.height()), DrawFlags{}); + self.draw(x - 0.5 * self.widthF(), y - 0.5 * self.heightF(), DrawFlags{}); } pub fn drawCenteredScaledUniform(self: Bitmap, x: f32, y: f32, s: f32) void { - const w = s * @intToFloat(f32, self.width()); - const h = s * @intToFloat(f32, self.height()); + const w = s * self.widthF(); + const h = s * self.heightF(); self.drawScaled(x - 0.5 * w, y - 0.5 * h, w, h); } pub fn drawScaled(self: Bitmap, x: f32, y: f32, w: f32, h: f32) void { - drawScaledBitmap(self, 0, 0, @intToFloat(f32, self.width()), @intToFloat(f32, self.height()), x, y, w, h, DrawFlags{}); + drawScaledBitmap(self, 0, 0, self.widthF(), self.heightF(), x, y, w, h, DrawFlags{}); } pub fn drawScaledUniform(self: Bitmap, x: f32, y: f32, s: f32) void { - const sourceW = @intToFloat(f32, self.width()); - const sourceH = @intToFloat(f32, self.height()); + const sourceW = self.widthF(); + const sourceH = self.heightF(); const scaledW = s * sourceW; const scaledH = s * sourceH; drawScaledBitmap(self, 0, 0, sourceW, sourceH, x, y, scaledW, scaledH, DrawFlags{}); @@ -40,22 +40,22 @@ pub const Bitmap = struct { } pub fn drawTintedCentered(self: Bitmap, tint: Color, x: f32, y: f32) void { - self.drawTinted(tint, x - 0.5 * @intToFloat(f32, self.width()), y - 0.5 * @intToFloat(f32, self.height()), DrawFlags{}); + self.drawTinted(tint, x - 0.5 * self.widthF(), y - 0.5 * self.heightF(), DrawFlags{}); } pub fn drawTintedCenteredScaled(self: Bitmap, tint: Color, x: f32, y: f32, s: f32) void { - const w = s * @intToFloat(f32, self.width()); - const h = s * @intToFloat(f32, self.height()); + const w = s * self.widthF(); + const h = s * self.heightF(); self.drawTintedScaled(tint, x - 0.5 * w, y - 0.5 * h, w, h); } pub fn drawTintedScaled(self: Bitmap, tint: Color, x: f32, y: f32, w: f32, h: f32) void { - drawTintedScaledBitmap(self, tint, 0, 0, @intToFloat(f32, self.width()), @intToFloat(f32, self.height()), x, y, w, h, DrawFlags{}); + drawTintedScaledBitmap(self, tint, 0, 0, self.widthF(), self.heightF(), x, y, w, h, DrawFlags{}); } pub fn drawTintedScaledUniform(self: Bitmap, tint: Color, x: f32, y: f32, s: f32) void { - const sourceW = @intToFloat(f32, self.width()); - const sourceH = @intToFloat(f32, self.height()); + const sourceW = self.widthF(); + const sourceH = self.heightF(); const scaledW = s * sourceW; const scaledH = s * sourceH; drawTintedScaledBitmap(self, tint, 0, 0, sourceW, sourceH, x, y, scaledW, scaledH, DrawFlags{}); @@ -66,7 +66,7 @@ pub const Bitmap = struct { } pub fn heightF(self: Bitmap) f32 { - return @intToFloat(f32, self.height()); + return @as(f32, @floatFromInt(self.height())); } pub fn sub(self: Bitmap, x: i32, y: i32, w: i32, h: i32) !Bitmap { @@ -78,7 +78,7 @@ pub const Bitmap = struct { } pub fn widthF(self: Bitmap) f32 { - return @intToFloat(f32, self.width()); + return @as(f32, @floatFromInt(self.width())); } }; @@ -87,9 +87,9 @@ pub const Color = struct { fn hexToU4(hex: u8) u4 { return switch (hex) { - '0'...'9' => @intCast(u4, hex - '0'), - 'a'...'f' => @intCast(u4, hex - 'a') + 10, - 'A'...'F' => @intCast(u4, hex - 'A') + 10, + '0'...'9' => @as(u4, @intCast(hex - '0')), + 'a'...'f' => @as(u4, @intCast(hex - 'a')) + 10, + 'A'...'F' => @as(u4, @intCast(hex - 'A')) + 10, else => unreachable, }; } @@ -101,13 +101,13 @@ pub const Color = struct { if (hex.len < left + 6) unreachable; - const r = (@intCast(u8, hexToU4(hex[left])) << 4) + @intCast(u8, hexToU4(hex[left + 1])); - const g = (@intCast(u8, hexToU4(hex[left + 2])) << 4) + @intCast(u8, hexToU4(hex[left + 3])); - const b = (@intCast(u8, hexToU4(hex[left + 4])) << 4) + @intCast(u8, hexToU4(hex[left + 5])); + const r = (@as(u8, @intCast(hexToU4(hex[left]))) << 4) + @as(u8, @intCast(hexToU4(hex[left + 1]))); + const g = (@as(u8, @intCast(hexToU4(hex[left + 2]))) << 4) + @as(u8, @intCast(hexToU4(hex[left + 3]))); + const b = (@as(u8, @intCast(hexToU4(hex[left + 4]))) << 4) + @as(u8, @intCast(hexToU4(hex[left + 5]))); if (hex.len < left + 8) { return mapRgb(r, g, b); } - const a = (@intCast(u8, hexToU4(hex[left + 6])) << 4) + @intCast(u8, hexToU4(hex[left + 7])); + const a = (@as(u8, @intCast(hexToU4(hex[left + 6]))) << 4) + @as(u8, @intCast(hexToU4(hex[left + 7]))); return mapRgba(r, g, b, a); } }; @@ -128,7 +128,7 @@ pub const Display = struct { } pub fn heightF(self: Display) f32 { - return @intToFloat(f32, self.height()); + return @as(f32, @floatFromInt(self.height())); } pub fn setTitle(self: Display, title: []const u8) void { @@ -140,7 +140,7 @@ pub const Display = struct { } pub fn widthF(self: Display) f32 { - return @intToFloat(f32, self.width()); + return @as(f32, @floatFromInt(self.width())); } }; @@ -473,23 +473,23 @@ pub fn destroyTimer(timer: Timer) void { } pub fn drawBitmap(bitmap: Bitmap, dx: f32, dy: f32, flags: DrawFlags) void { - c.al_draw_bitmap(bitmap.native, dx, dy, @bitCast(c_int, flags)); + c.al_draw_bitmap(bitmap.native, dx, dy, @as(c_int, @bitCast(flags))); } pub fn drawScaledBitmap(bitmap: Bitmap, sx: f32, sy: f32, sw: f32, sh: f32, dx: f32, dy: f32, dw: f32, dh: f32, flags: DrawFlags) void { - c.al_draw_scaled_bitmap(bitmap.native, sx, sy, sw, sh, dx, dy, dw, dh, @bitCast(c_int, flags)); + c.al_draw_scaled_bitmap(bitmap.native, sx, sy, sw, sh, dx, dy, dw, dh, @as(c_int, @bitCast(flags))); } pub fn drawText(font: Font, color: Color, x: f32, y: f32, flags: DrawTextFlags, text: [*:0]const u8) void { - c.al_draw_text(font.native, color.native, x, y, @enumToInt(flags), text); + c.al_draw_text(font.native, color.native, x, y, @intFromEnum(flags), text); } pub fn drawTintedBitmap(bitmap: Bitmap, tint: Color, dx: f32, dy: f32, flags: DrawFlags) void { - c.al_draw_tinted_bitmap(bitmap.native, tint.native, dx, dy, @bitCast(c_int, flags)); + c.al_draw_tinted_bitmap(bitmap.native, tint.native, dx, dy, @as(c_int, @bitCast(flags))); } pub fn drawTintedScaledBitmap(bitmap: Bitmap, tint: Color, sx: f32, sy: f32, sw: f32, sh: f32, dx: f32, dy: f32, dw: f32, dh: f32, flags: DrawFlags) void { - c.al_draw_tinted_scaled_bitmap(bitmap.native, tint.native, sx, sy, sw, sh, dx, dy, dw, dh, @bitCast(c_int, flags)); + c.al_draw_tinted_scaled_bitmap(bitmap.native, tint.native, sx, sy, sw, sh, dx, dy, dw, dh, @as(c_int, @bitCast(flags))); } pub fn dropNextEvent(queue: EventQueue) bool { @@ -541,7 +541,7 @@ pub fn getDisplayEventSource(display: Display) EventSource { } pub fn getDisplayFlags(display: Display) NewDisplayFlags { - return @bitCast(NewDisplayFlags, c.al_get_display_flags(display.native)); + return @as(NewDisplayFlags, @bitCast(c.al_get_display_flags(display.native))); } pub fn getDisplayHeight(display: Display) i32 { @@ -573,7 +573,7 @@ pub fn getNewBitmapFlags() NewBitmapFlags { } pub fn getNewDisplayOption(option: NewDisplayOption, importance: *OptionImportance) c_int { - return c.al_get_new_display_option(@enumToInt(option), @ptrCast(*c_int, importance)); + return c.al_get_new_display_option(@intFromEnum(option), @as(*c_int, @ptrCast(importance))); } pub fn getNewBitmapFormat() PixelFormat { @@ -661,7 +661,7 @@ pub fn loadFont(path: [*:0]const u8, size: i32, flags: c_int) !Font { } pub fn loadTtfFont(path: [*:0]const u8, size: i32, flags: LoadTtfFontFlags) !Font { - const font = c.al_load_ttf_font(path, size, @bitCast(c_int, flags)); + const font = c.al_load_ttf_font(path, size, @as(c_int, @bitCast(flags))); if (font) |native| { return Font{ .native = native }; } @@ -685,7 +685,7 @@ pub fn mapRgbF(r: f32, g: f32, b: f32) Color { } pub fn openMemfile(data: []u8, mode: []const u8) !File { - const file = c.al_open_memfile(&data[0], @intCast(i64, data.len), &mode[0]); + const file = c.al_open_memfile(&data[0], @as(i64, @intCast(data.len)), &mode[0]); if (file) |native| { return File{ .native = native }; } @@ -709,11 +709,11 @@ pub fn saveBitmapF(file: File, ident: []const u8, bitmap: Bitmap) bool { } pub fn setDisplayFlag(display: Display, flag: NewDisplayFlags, on: bool) bool { - return c.al_set_display_flag(display.native, @bitCast(c_int, flag), on); + return c.al_set_display_flag(display.native, @as(c_int, @bitCast(flag)), on); } pub fn setNewBitmapFlags(flags: NewBitmapFlags) void { - c.al_set_new_bitmap_flags(@bitCast(c_int, flags)); + c.al_set_new_bitmap_flags(@as(c_int, @bitCast(flags))); } pub fn setNewBitmapFormat(format: PixelFormat) void { @@ -721,11 +721,11 @@ pub fn setNewBitmapFormat(format: PixelFormat) void { } pub fn setNewDisplayFlags(flags: NewDisplayFlags) void { - c.al_set_new_display_flags(@bitCast(c_int, flags)); + c.al_set_new_display_flags(@as(c_int, @bitCast(flags))); } pub fn setNewDisplayOption(option: NewDisplayOption, value: i32, importance: OptionImportance) void { - c.al_set_new_display_option(@enumToInt(option), value, @enumToInt(importance)); + c.al_set_new_display_option(@intFromEnum(option), value, @intFromEnum(importance)); } pub fn setNewWindowTitle(title: []const u8) void { diff --git a/allegro/c.zig b/allegro/src/c.zig similarity index 100% rename from allegro/c.zig rename to allegro/src/c.zig diff --git a/build.zig b/build.zig index 57eb45f..ff33c28 100644 --- a/build.zig +++ b/build.zig @@ -1,7 +1,4 @@ const std = @import("std"); -const allegro = @import("allegro/build.zig"); - -const allegroPath = @embedFile("allegro.path"); // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external @@ -27,8 +24,6 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - allegro.addTo(allegroPath, exe); - // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). @@ -45,6 +40,18 @@ pub fn build(b: *std.Build) void { // files, this ensures they will be present and in the expected location. run_cmd.step.dependOn(b.getInstallStep()); + const allegro_dep = b.dependency("allegro", .{ + .target = target, + .optimize = optimize, + }); + const allegro_mod = allegro_dep.module("allegro"); + const allegro_dir = b.option([]const u8, "allegro-dir", "path to a directory that contains the Allegro `lib`, `bin` and `include` folders") orelse ""; + if (allegro_dir.len != 0) { + allegro_mod.addIncludePath(.{ .path = b.pathJoin(&[_][]const u8{ allegro_dir, "include" }) }); + allegro_mod.addLibraryPath(.{ .path = b.pathJoin(&[_][]const u8{ allegro_dir, "lib" }) }); + } + exe.root_module.addImport("allegro", allegro_mod); + // This allows the user to pass arguments to the application in the build // command itself, like this: `zig build run -- arg1 arg2 etc` if (b.args) |args| { diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..fd31ff0 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,15 @@ +.{ + .name = "tins2023", + .version = "0.1.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "README.md", + "src", + }, + .dependencies = .{ + .allegro = .{ + .path = "allegro", + }, + }, +} diff --git a/src/engine/assets.zig b/src/engine/assets.zig index 6e69092..172446d 100644 --- a/src/engine/assets.zig +++ b/src/engine/assets.zig @@ -109,13 +109,13 @@ pub const Sprite = struct { const horizontal = @divTrunc(bitmapWidth, width); const vertical = @divTrunc(bitmapHeight, height); - const n = @intCast(usize, horizontal * vertical); + const n = @as(usize, @intCast(horizontal * vertical)); const frames = try allocator.alloc(allegro.Bitmap, n); var i: i32 = 0; while (i < n) : (i += 1) { const left = @mod(i, horizontal) * width; const top = @divTrunc(i, horizontal) * height; - frames[@intCast(usize, i)] = try bitmap.sub(left, top, width, height); + frames[@as(usize, @intCast(i))] = try bitmap.sub(left, top, width, height); } return Sprite{ diff --git a/src/engine/fps.zig b/src/engine/fps.zig index 8e5b5ef..3500d34 100644 --- a/src/engine/fps.zig +++ b/src/engine/fps.zig @@ -22,6 +22,6 @@ pub const FPS = struct { pub fn fps(self: FPS) usize { if (self.framesCounted == 0) return 0; - return @floatToInt(usize, @intToFloat(f32, self.framesCounted) / self.total); + return @as(usize, @intFromFloat(@as(f32, @floatFromInt(self.framesCounted)) / self.total)); } }; diff --git a/src/engine/opaque_ptr.zig b/src/engine/opaque_ptr.zig index 3920574..5248fa7 100644 --- a/src/engine/opaque_ptr.zig +++ b/src/engine/opaque_ptr.zig @@ -21,11 +21,11 @@ pub const OpaquePtr = struct { const ptr = try allocator.create(Type); const opaq = OpaquePtr{ .allocator = allocator, - .object = @ptrToInt(ptr), + .object = @intFromPtr(ptr), .virtualTable = .{ .destroy = struct { fn destroy(self: OpaquePtr) void { - const object = @intToPtr(*Type, self.object); + const object = @as(*Type, @ptrFromInt(self.object)); self.allocator.destroy(object); } }.destroy, diff --git a/src/engine/point.zig b/src/engine/point.zig index f9a62f2..49552be 100644 --- a/src/engine/point.zig +++ b/src/engine/point.zig @@ -13,7 +13,7 @@ pub const Point = struct { } pub fn initUsize(x: usize, y: usize) Point { - return Point.init(@intCast(i64, x), @intCast(i64, y)); + return Point.init(@as(i64, @intCast(x)), @as(i64, @intCast(y))); } pub fn add(self: Point, other: Point) Point { @@ -31,7 +31,7 @@ pub const Point = struct { } pub fn float(self: Point) PointF { - return PointF{ .x = @intToFloat(f32, self.x), .y = @intToFloat(f32, self.y) }; + return PointF{ .x = @as(f32, @floatFromInt(self.x)), .y = @as(f32, @floatFromInt(self.y)) }; } pub fn multiply(self: Point, factor: i64) Point { diff --git a/src/engine/point_f.zig b/src/engine/point_f.zig index bb81350..3da9d8d 100644 --- a/src/engine/point_f.zig +++ b/src/engine/point_f.zig @@ -37,7 +37,7 @@ pub const PointF = struct { } pub fn integer(self: PointF) Point { - return Point{ .x = @floatToInt(i64, self.x), .y = @floatToInt(i64, self.y) }; + return Point{ .x = @as(i64, @intFromFloat(self.x)), .y = @as(i64, @intFromFloat(self.y)) }; } pub fn multiply(self: PointF, factor: f32) PointF { diff --git a/src/engine/scene.zig b/src/engine/scene.zig index 7dcb8b8..5728b20 100644 --- a/src/engine/scene.zig +++ b/src/engine/scene.zig @@ -38,34 +38,34 @@ pub fn Scene(comptime Context: type, comptime Event: type) type { } pub fn makeOpaque(comptime SceneType: type, object: *SceneType) Self { - return Self{ .object = @ptrToInt(object), .virtualTable = .{ + return Self{ .object = @intFromPtr(object), .virtualTable = .{ .enter = struct { fn enter(self: Self, ctx: *Context) void { - const scene = @intToPtr(*SceneType, self.object); + const scene = @as(*SceneType, @ptrFromInt(self.object)); scene.enter(ctx); } }.enter, .exit = struct { fn exit(self: Self, ctx: *Context) void { - const scene = @intToPtr(*SceneType, self.object); + const scene = @as(*SceneType, @ptrFromInt(self.object)); scene.exit(ctx); } }.exit, .handle = struct { fn handle(self: Self, ctx: *Context, event: Event) !void { - const scene = @intToPtr(*SceneType, self.object); + const scene = @as(*SceneType, @ptrFromInt(self.object)); try scene.handle(ctx, event); } }.handle, .tick = struct { fn tick(self: Self, ctx: *Context, t: f32, dt: f32) void { - const scene = @intToPtr(*SceneType, self.object); + const scene = @as(*SceneType, @ptrFromInt(self.object)); scene.tick(ctx, t, dt); } }.tick, .render = struct { fn render(self: Self, ctx: *Context) void { - const scene = @intToPtr(*SceneType, self.object); + const scene = @as(*SceneType, @ptrFromInt(self.object)); scene.render(ctx); } }.render, @@ -74,7 +74,7 @@ pub fn Scene(comptime Context: type, comptime Event: type) type { pub fn init(comptime SceneType: type, allocator: std.mem.Allocator, build: ?*const fn (*SceneType) void) !Self { const object = try OpaquePtr.create(SceneType, allocator); - var scene = Self.makeOpaque(SceneType, object.ptr); + const scene = Self.makeOpaque(SceneType, object.ptr); if (build) |b| { b(object.ptr); } diff --git a/src/engine/viewport.zig b/src/engine/viewport.zig index cafda5a..dff140b 100644 --- a/src/engine/viewport.zig +++ b/src/engine/viewport.zig @@ -22,7 +22,7 @@ pub const Viewport = struct { .actualHeight = height, .scaledWidth = width, .scaledHeight = height, - .bounds = RectangleF.initRelative(0, 0, @intToFloat(f32, width), @intToFloat(f32, height)), + .bounds = RectangleF.initRelative(0, 0, @as(f32, @floatFromInt(width)), @as(f32, @floatFromInt(height))), .scale = 1.0, }; } @@ -32,7 +32,7 @@ pub const Viewport = struct { } pub fn scaledInteger(self: Viewport, i: i32) i32 { - return @floatToInt(i32, std.math.round(@intToFloat(f32, i) * self.scale)); + return @as(i32, @intFromFloat(std.math.round(@as(f32, @floatFromInt(i)) * self.scale))); } pub fn viewToScreen(self: Viewport, x: f32, y: f32) PointF { @@ -49,20 +49,25 @@ pub const Viewport = struct { self.actualWidth = width; self.actualHeight = height; - const horizontalRatio = @intToFloat(f32, width) / @intToFloat(f32, self.unscaledWidth); - const verticalRatio = @intToFloat(f32, height) / @intToFloat(f32, self.unscaledHeight); + const widthF = @as(f32, @floatFromInt(width)); + const heightF = @as(f32, @floatFromInt(height)); + const unscaledWidthF = @as(f32, @floatFromInt(self.unscaledWidth)); + const unscaledHeightF = @as(f32, @floatFromInt(self.unscaledHeight)); + + const horizontalRatio = widthF / unscaledWidthF; + const verticalRatio = heightF / unscaledHeightF; if (horizontalRatio < verticalRatio) { self.scaledWidth = width; - self.scaledHeight = @floatToInt(i32, horizontalRatio * @intToFloat(f32, self.unscaledHeight)); + self.scaledHeight = @as(i32, @intFromFloat(horizontalRatio * unscaledHeightF)); const top = @divFloor(height - self.scaledHeight, 2); - self.bounds = RectangleF.initRelative(0, @intToFloat(f32, top), @intToFloat(f32, width), @intToFloat(f32, self.scaledHeight)); + self.bounds = RectangleF.initRelative(0, @as(f32, @floatFromInt(top)), widthF, @as(f32, @floatFromInt(self.scaledHeight))); self.scale = horizontalRatio; } else { - self.scaledWidth = @floatToInt(i32, verticalRatio * @intToFloat(f32, self.unscaledWidth)); + self.scaledWidth = @as(i32, @intFromFloat(verticalRatio * unscaledWidthF)); self.scaledHeight = height; const left = @divFloor(width - self.scaledWidth, 2); - self.bounds = RectangleF.initRelative(@intToFloat(f32, left), 0, @intToFloat(f32, self.scaledWidth), @intToFloat(f32, height)); + self.bounds = RectangleF.initRelative(@as(f32, @floatFromInt(left)), 0, @as(f32, @floatFromInt(self.scaledWidth)), heightF); self.scale = verticalRatio; } } diff --git a/src/game/animation.zig b/src/game/animation.zig index e75bf6d..fae8535 100644 --- a/src/game/animation.zig +++ b/src/game/animation.zig @@ -44,7 +44,7 @@ pub const Animation = struct { pub fn tick(self: *Animation, t: f32) void { const delta = t - self.lastUpdate; - const skip = @floatToInt(usize, @divFloor(delta, self.interval)); + const skip = @as(usize, @intFromFloat(@divFloor(delta, self.interval))); self.lastUpdate = t - @rem(delta, self.interval); self.current = self.currentOffset(skip); diff --git a/src/game/game.zig b/src/game/game.zig index f3cadbe..5af12f7 100644 --- a/src/game/game.zig +++ b/src/game/game.zig @@ -111,17 +111,17 @@ pub const Game = struct { self.playerVelocity.y = 0.1; } if (self.playerVelocity.x != 0) { - const playerTileTop = @floatToInt(i64, std.math.floor(self.playerPosition.y)); - const playerTileBottom = @floatToInt(i64, std.math.floor(self.playerPosition.y + 0.99)); + const playerTileTop = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.y))); + const playerTileBottom = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.y + 0.99))); if (self.playerVelocity.x < 0) { - const playerTileLeft = @floatToInt(i64, std.math.ceil(self.playerPosition.x)) - 1; + const playerTileLeft = @as(i64, @intFromFloat(std.math.ceil(self.playerPosition.x))) - 1; if (self.level.tiles.get(playerTileLeft, playerTileTop) != null or self.level.tiles.get(playerTileLeft, playerTileBottom) != null) { self.playerPosition.x = std.math.ceil(self.playerPosition.x); self.playerVelocity.x = 0; } } if (self.playerVelocity.x > 0) { - const playerTileRight = @floatToInt(i64, std.math.floor(self.playerPosition.x)) + 1; + const playerTileRight = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.x))) + 1; if (self.level.tiles.get(playerTileRight, playerTileTop) != null or self.level.tiles.get(playerTileRight, playerTileBottom) != null) { self.playerPosition.x = std.math.floor(self.playerPosition.x); self.playerVelocity.x = 0; @@ -158,17 +158,17 @@ pub const Game = struct { } pub fn playerIsOnTile(self: Game) bool { - const playerTileHeight = @floatToInt(i64, std.math.floor(self.playerPosition.y)) + 1; - const playerTileLeft = @floatToInt(i64, std.math.floor(self.playerPosition.x + 0.5)); - const playerTileRight = @floatToInt(i64, std.math.ceil(self.playerPosition.x - 0.5)); + const playerTileHeight = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.y))) + 1; + const playerTileLeft = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.x + 0.5))); + const playerTileRight = @as(i64, @intFromFloat(std.math.ceil(self.playerPosition.x - 0.5))); return self.level.tiles.get(playerTileLeft, playerTileHeight) != null or self.level.tiles.get(playerTileRight, playerTileHeight) != null; } pub fn playerIsUnderTile(self: Game) bool { - const playerTileHeight = @floatToInt(i64, std.math.ceil(self.playerPosition.y)) - 1; - const playerTileLeft = @floatToInt(i64, std.math.floor(self.playerPosition.x + 0.5)); - const playerTileRight = @floatToInt(i64, std.math.ceil(self.playerPosition.x - 0.5)); + const playerTileHeight = @as(i64, @intFromFloat(std.math.ceil(self.playerPosition.y))) - 1; + const playerTileLeft = @as(i64, @intFromFloat(std.math.floor(self.playerPosition.x + 0.5))); + const playerTileRight = @as(i64, @intFromFloat(std.math.ceil(self.playerPosition.x - 0.5))); return self.level.tiles.get(playerTileLeft, playerTileHeight) != null or self.level.tiles.get(playerTileRight, playerTileHeight) != null; } diff --git a/src/game/level.zig b/src/game/level.zig index 20ca36e..5204a94 100644 --- a/src/game/level.zig +++ b/src/game/level.zig @@ -43,7 +43,7 @@ pub const Level = struct { defer y += 1; for (line, 0..) |tile, column| { - var x = @intCast(i64, column); + const x = @as(i64, @intCast(column)); switch (tile) { 'P' => character = engine.pt(x, y), 'S' => try collectables.set(x, y, .Star), diff --git a/src/game_scene.zig b/src/game_scene.zig index c679b22..168e229 100644 --- a/src/game_scene.zig +++ b/src/game_scene.zig @@ -56,11 +56,11 @@ pub const GameScene = struct { } if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_LEFT)) { - self.game.playerVelocity.x = std.math.max(-maxHorizontalWalkVelocity, self.game.playerVelocity.x - horizontalWalkAcceleration); + self.game.playerVelocity.x = @max(-maxHorizontalWalkVelocity, self.game.playerVelocity.x - horizontalWalkAcceleration); } else if (ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_RIGHT)) { - self.game.playerVelocity.x = std.math.min(maxHorizontalWalkVelocity, self.game.playerVelocity.x + horizontalWalkAcceleration); + self.game.playerVelocity.x = @min(maxHorizontalWalkVelocity, self.game.playerVelocity.x + horizontalWalkAcceleration); } else { - if (std.math.fabs(self.game.playerVelocity.x) > 0.2) { + if (@abs(self.game.playerVelocity.x) > 0.2) { self.game.playerVelocity.x = self.game.playerVelocity.x - std.math.sign(self.game.playerVelocity.x) * horizontalWalkAcceleration * dt; } else { self.game.playerVelocity.x = 0; @@ -70,10 +70,10 @@ pub const GameScene = struct { if (self.game.playerVelocity.y == 0 and ctx.keys.isKeyPressed(allegro.c.ALLEGRO_KEY_SPACE)) { self.game.playerVelocity.y = jumpVelocity; } else if (!self.game.playerIsOnTile()) { - self.game.playerVelocity.y = std.math.min(maxFallVelocity, self.game.playerVelocity.y + gravity * dt); + self.game.playerVelocity.y = @min(maxFallVelocity, self.game.playerVelocity.y + gravity * dt); } - // self.game.playerVelocity.y = std.math.min(maxFallVelocity, self.game.playerVelocity.y + self.game.playerAcceleration.y * dt); + // self.game.playerVelocity.y = @min(maxFallVelocity, self.game.playerVelocity.y + self.game.playerAcceleration.y * dt); if (self.game.playerVelocity.y != 0) { self.game.playerFallingAnimation.tick(t); self.game.playerIdleAnimation.reset(); @@ -166,7 +166,7 @@ pub const GameScene = struct { switch (collectable) { .Star => { const distanceToPlayer = engine.pt(x, y).float().distance(self.game.playerPosition); - self.game.drawSpriteFrame("item_star_32", self.game.starAnimation.currentOffset(@floatToInt(usize, @mod(distanceToPlayer * 0.54, 1) * 16)), x, y); + self.game.drawSpriteFrame("item_star_32", self.game.starAnimation.currentOffset(@as(usize, @intFromFloat(@mod(distanceToPlayer * 0.54, 1) * 16))), x, y); }, .Exit => { const position = self.game.tileP(engine.pt(x, y).float().subtract(engine.ptF(0.5, 1))); @@ -196,7 +196,7 @@ pub const GameScene = struct { const textColor = allegro.mapRgb(0x48, 0x91, 0x00); if (self.game.state != .InProgress) { - ctx.renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(0, 0, 0, 191), 0, 0, @intToFloat(f32, ctx.renderer.display.width()), @intToFloat(f32, ctx.renderer.display.height())); + ctx.renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(0, 0, 0, 191), 0, 0, ctx.renderer.display.widthF(), ctx.renderer.display.heightF()); if (self.game.state == .Over) { ctx.renderer.drawTextV("default", textColor, 0.5, 0.2, .Center, "Game over"); diff --git a/src/main.zig b/src/main.zig index 9bfe1d1..0d9b21d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -66,7 +66,7 @@ pub fn main() !void { try context.switchToScene(TitleScene, null); var t = allegro.getTime(); - var fpsBuffer = [_]u8{0} ** 32; + const fpsBuffer = [_]u8{0} ** 32; _ = fpsBuffer; while (!context.shouldQuit) { const scene = if (context.scene) |scene| scene else { @@ -74,10 +74,10 @@ pub fn main() !void { }; const newT = allegro.getTime(); - const deltaT = @floatCast(f32, newT - t); + const deltaT = @as(f32, @floatCast(newT - t)); t = newT; context.fps.update(deltaT); - scene.tick(&context, @floatCast(f32, t), deltaT); + scene.tick(&context, @as(f32, @floatCast(t)), deltaT); while (!context.events.isEmpty()) { var event: allegro.Event = undefined; diff --git a/src/renderer.zig b/src/renderer.zig index 4ebe783..3b4fbb9 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -131,7 +131,7 @@ pub const Renderer = struct { } pub fn toggleFullScreen(self: *Renderer) void { - var displayFlags = allegro.getDisplayFlags(self.display); + const displayFlags = allegro.getDisplayFlags(self.display); _ = allegro.setDisplayFlag(self.display, allegro.NewDisplayFlags{ .FULLSCREEN_WINDOW = true }, !displayFlags.FULLSCREEN_WINDOW); self.resized(); }