diff --git a/build.zig b/build.zig index 1cb0cfd..52306c7 100644 --- a/build.zig +++ b/build.zig @@ -44,6 +44,7 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + exe_unit_tests.root_module.addImport("seizer", seizer.module("seizer")); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); diff --git a/build.zig.zon b/build.zig.zon index cddf7e0..441d4db 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -16,8 +16,8 @@ // internet connectivity. .dependencies = .{ .seizer = .{ - .url = "https://github.com/leroycep/seizer/archive/dd1f9f6c94e91edfd96f0075c5f5d33aa30758f4.tar.gz", - .hash = "122009caed9e40d713c847b24c2abf8540299e32fb5e77f73ab2acdf5c27bc3e4c90", + .url = "https://github.com/leroycep/seizer/archive/8fdc6335641614c1cd844d1ecd5ad937584a443e.tar.gz", + .hash = "1220b6f9d0aba788b55a3e26a34ca9793495fbdb062c463cef1433ff937b96aa77d6", }, }, .paths = .{ diff --git a/src/assets.zig b/src/assets.zig index c5b3a4a..00703f2 100644 --- a/src/assets.zig +++ b/src/assets.zig @@ -48,9 +48,7 @@ pub const TileSheet = struct { this.texture.deinit(); } - pub fn renderTile(this: @This(), canvas: *seizer.Canvas, tile_id: u32, pos: [2]f32, options: struct { - size: ?[2]f32 = null, - }) void { + pub fn uvCoordinatesFromTileId(this: @This(), tile_id: u32) seizer.geometry.AABB(f32) { const texture_sizef = [2]f32{ @floatFromInt(this.texture.size[0]), @floatFromInt(this.texture.size[1]), @@ -85,7 +83,7 @@ pub const TileSheet = struct { pos_in_tilesf[0] * tile_stridef[0] + tile_offsetf[0], pos_in_tilesf[1] * tile_stridef[1] + tile_offsetf[1], }; - const uv = seizer.geometry.AABB(f32){ + return seizer.geometry.AABB(f32){ .min = .{ pixel_pos[0] / texture_sizef[0], pixel_pos[1] / texture_sizef[1], @@ -95,14 +93,43 @@ pub const TileSheet = struct { (pixel_pos[1] + tile_sizef[1]) / texture_sizef[1], }, }; + } - canvas.rect(pos, options.size orelse tile_sizef, .{ + pub fn renderTile(this: @This(), canvas: *seizer.Canvas, tile_id: u32, pos: [2]f32, options: struct { + size: ?[2]f32 = null, + }) void { + const uv = this.uvCoordinatesFromTileId(tile_id); + + canvas.rect(pos, options.size orelse [2]f32{ @floatFromInt(this.tile_size[0]), @floatFromInt(this.tile_size[1]) }, .{ .texture = this.texture.glTexture, .uv = uv, }); } }; +test "tilesheet math is exact" { + const tilesheet = TileSheet{ + .texture = .{ + .glTexture = undefined, + .size = .{ 494, 329 }, + }, + .tile_offset = .{ 6, 2 }, + .tile_size = .{ 20, 29 }, + .tile_stride = .{ 33, 33 }, + }; + + try std.testing.expectEqualDeep(seizer.geometry.AABB(f32){ + .min = .{ + 468.0 / 494.0, + 35.0 / 329.0, + }, + .max = .{ + (468.0 + 20.0) / 494.0, + (35.0 + 29.0) / 329.0, + }, + }, tilesheet.uvCoordinatesFromTileId(29)); +} + /// A texture with a regular grid of sprites pub const DeckSprites = struct { tilesheet: TileSheet, diff --git a/src/main.zig b/src/main.zig index 74c4b0f..47286b2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -905,7 +905,7 @@ pub const CardElement = struct { .card => |card| render_resources.deck.tilesheet.renderTile( canvas, render_resources.deck.getTileForCard(card), - .{ min[0] + mark_offset[0], min[1] + mark_offset[1] }, + .{ @floor(min[0] + mark_offset[0]), @floor(min[1] + mark_offset[1]) }, .{}, ), } @@ -1196,6 +1196,10 @@ fn glfw_key_callback(window: ?*seizer.backend.glfw.c.GLFWwindow, key: c_int, sca } } +test { + _ = @import("./assets.zig"); +} + const DeckSprites = assets.DeckSprites; const Card = assets.Card;