fix: remove slight offset when cards were rendered

Fixed by updating version of seizer to one that doesn't offset the canvas
projection.
dev
LeRoyce Pearson 2024-02-28 14:47:35 -07:00
parent 84d8766124
commit 807712af79
4 changed files with 40 additions and 8 deletions

View File

@ -44,6 +44,7 @@ pub fn build(b: *std.Build) void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe_unit_tests.root_module.addImport("seizer", seizer.module("seizer"));
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

View File

@ -16,8 +16,8 @@
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
.seizer = .{ .seizer = .{
.url = "https://github.com/leroycep/seizer/archive/dd1f9f6c94e91edfd96f0075c5f5d33aa30758f4.tar.gz", .url = "https://github.com/leroycep/seizer/archive/8fdc6335641614c1cd844d1ecd5ad937584a443e.tar.gz",
.hash = "122009caed9e40d713c847b24c2abf8540299e32fb5e77f73ab2acdf5c27bc3e4c90", .hash = "1220b6f9d0aba788b55a3e26a34ca9793495fbdb062c463cef1433ff937b96aa77d6",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -48,9 +48,7 @@ pub const TileSheet = struct {
this.texture.deinit(); this.texture.deinit();
} }
pub fn renderTile(this: @This(), canvas: *seizer.Canvas, tile_id: u32, pos: [2]f32, options: struct { pub fn uvCoordinatesFromTileId(this: @This(), tile_id: u32) seizer.geometry.AABB(f32) {
size: ?[2]f32 = null,
}) void {
const texture_sizef = [2]f32{ const texture_sizef = [2]f32{
@floatFromInt(this.texture.size[0]), @floatFromInt(this.texture.size[0]),
@floatFromInt(this.texture.size[1]), @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[0] * tile_stridef[0] + tile_offsetf[0],
pos_in_tilesf[1] * tile_stridef[1] + tile_offsetf[1], pos_in_tilesf[1] * tile_stridef[1] + tile_offsetf[1],
}; };
const uv = seizer.geometry.AABB(f32){ return seizer.geometry.AABB(f32){
.min = .{ .min = .{
pixel_pos[0] / texture_sizef[0], pixel_pos[0] / texture_sizef[0],
pixel_pos[1] / texture_sizef[1], pixel_pos[1] / texture_sizef[1],
@ -95,14 +93,43 @@ pub const TileSheet = struct {
(pixel_pos[1] + tile_sizef[1]) / texture_sizef[1], (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, .texture = this.texture.glTexture,
.uv = uv, .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 /// A texture with a regular grid of sprites
pub const DeckSprites = struct { pub const DeckSprites = struct {
tilesheet: TileSheet, tilesheet: TileSheet,

View File

@ -905,7 +905,7 @@ pub const CardElement = struct {
.card => |card| render_resources.deck.tilesheet.renderTile( .card => |card| render_resources.deck.tilesheet.renderTile(
canvas, canvas,
render_resources.deck.getTileForCard(card), 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 DeckSprites = assets.DeckSprites;
const Card = assets.Card; const Card = assets.Card;