feat: render stack of cards

Added `tile_offset` and `tile_stride` to render exactly the card without
the blank space surrounding it on the tile.
dev
LeRoyce Pearson 2024-02-24 16:27:37 -07:00
parent 755518bb5d
commit 680f56a39d
1 changed files with 40 additions and 8 deletions

View File

@ -18,12 +18,16 @@ const Card = packed struct(u6) {
/// A texture with a regular grid of sprites
const TileSheet = struct {
texture: seizer.Texture,
tile_offset: [2]u32,
tile_size: [2]u32,
tile_stride: [2]u32,
pub const InitOptions = struct {
allocator: std.mem.Allocator,
image_file_contents: []const u8,
tile_offset: [2]u32,
tile_size: [2]u32,
tile_stride: [2]u32,
texture_options: seizer.Texture.InitFromFileOptions = .{},
};
@ -35,7 +39,9 @@ const TileSheet = struct {
);
return @This(){
.texture = texture,
.tile_offset = options.tile_offset,
.tile_size = options.tile_size,
.tile_stride = options.tile_stride,
};
}
@ -50,14 +56,22 @@ const TileSheet = struct {
@floatFromInt(this.texture.size[0]),
@floatFromInt(this.texture.size[1]),
};
const tile_offsetf = [2]f32{
@floatFromInt(this.tile_offset[0]),
@floatFromInt(this.tile_offset[1]),
};
const tile_stridef = [2]f32{
@floatFromInt(this.tile_stride[0]),
@floatFromInt(this.tile_stride[1]),
};
const tile_sizef = [2]f32{
@floatFromInt(this.tile_size[0]),
@floatFromInt(this.tile_size[1]),
};
// add a half to round up
const size_in_tiles = [2]u32{
(@as(u32, @intCast(this.texture.size[0])) + (this.tile_size[0] / 2)) / this.tile_size[0],
(@as(u32, @intCast(this.texture.size[1])) + (this.tile_size[1] / 2)) / this.tile_size[1],
(@as(u32, @intCast(this.texture.size[0])) + (this.tile_stride[0] / 2)) / this.tile_stride[0],
(@as(u32, @intCast(this.texture.size[1])) + (this.tile_stride[1] / 2)) / this.tile_stride[1],
};
const pos_in_tiles = [2]u32{
tile_id % size_in_tiles[0],
@ -67,16 +81,22 @@ const TileSheet = struct {
@floatFromInt(pos_in_tiles[0]),
@floatFromInt(pos_in_tiles[1]),
};
const pixel_pos = [2]f32{
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){
.min = .{
(pos_in_tilesf[0] * tile_sizef[0]) / texture_sizef[0],
(pos_in_tilesf[1] * tile_sizef[1]) / texture_sizef[1],
pixel_pos[0] / texture_sizef[0],
pixel_pos[1] / texture_sizef[1],
},
.max = .{
((pos_in_tilesf[0] + 1) * tile_sizef[0]) / texture_sizef[0],
((pos_in_tilesf[1] + 1) * tile_sizef[1]) / texture_sizef[1],
(pixel_pos[0] + tile_sizef[0]) / texture_sizef[0],
(pixel_pos[1] + tile_sizef[1]) / texture_sizef[1],
},
};
canvas.rect(pos, options.size orelse tile_sizef, .{
.texture = this.texture.glTexture,
.uv = uv,
@ -199,6 +219,14 @@ pub fn main() !void {
@floatFromInt(5 * deck_sprites.tilesheet.tile_size[1]),
}, .{});
for (0..52) |i| {
const oy = -@as(f32, @floatFromInt(i)) * (@as(f32, @floatFromInt(deck_sprites.tilesheet.tile_size[1])) / (52.0 * 4));
deck_sprites.tilesheet.renderTile(&canvas, deck_sprites.back, .{
@floatFromInt(4 * deck_sprites.tilesheet.tile_size[0]),
@as(f32, @floatFromInt(5 * deck_sprites.tilesheet.tile_size[1])) + oy,
}, .{});
}
canvas.end();
seizer.backend.glfw.c.glfwSwapBuffers(window);
@ -209,7 +237,9 @@ fn loadMediumCards(gpa: std.mem.Allocator) !DeckSprites {
var tilesheet = try TileSheet.init(.{
.allocator = gpa,
.image_file_contents = @embedFile("./cardsMedium_tilemap.png"),
.tile_size = .{ 33, 33 },
.tile_offset = .{ 6, 2 },
.tile_size = .{ 20, 29 },
.tile_stride = .{ 33, 33 },
.texture_options = .{
.min_filter = .nearest,
.mag_filter = .nearest,
@ -265,7 +295,9 @@ fn loadLargeCards(gpa: std.mem.Allocator) !DeckSprites {
var tilesheet = try TileSheet.init(.{
.allocator = gpa,
.image_file_contents = @embedFile("./cardsLarge_tilemap.png"),
.tile_size = .{ 65, 65 },
.tile_offset = .{ 11, 2 },
.tile_size = .{ 41, 60 },
.tile_stride = .{ 65, 65 },
.texture_options = .{
.min_filter = .nearest,
.mag_filter = .nearest,