Tidy and fix up

This commit is contained in:
kayomn 2024-06-23 02:51:00 +01:00
parent ee88f58e53
commit 4125aa0ddb
4 changed files with 43 additions and 30 deletions

View File

@ -2,12 +2,12 @@ const App = @import("./App.zig");
const coral = @import("coral"); const coral = @import("coral");
const formats = @import("./gfx/formats.zig");
const device = @import("./gfx/device.zig"); const device = @import("./gfx/device.zig");
const ext = @import("./ext.zig"); const ext = @import("./ext.zig");
const formats = @import("./gfx/formats.zig");
const msg = @import("./msg.zig"); const msg = @import("./msg.zig");
const std = @import("std"); const std = @import("std");
@ -19,10 +19,10 @@ pub const Assets = struct {
pub const Format = struct { pub const Format = struct {
extension: []const u8, extension: []const u8,
open_file: *const fn (*std.heap.ArenaAllocator, []const u8) Error!Desc, file_desc: *const fn (*std.heap.ArenaAllocator, []const u8) Error!Desc,
pub const Error = std.mem.Allocator.Error || coral.files.Error || error { pub const Error = std.mem.Allocator.Error || coral.files.Error || error {
Unsupported, FormatUnsupported,
}; };
}; };
@ -40,7 +40,7 @@ pub const Assets = struct {
continue; continue;
} }
return self.context.open(try format.open_file(&self.staging_arena, path)); return self.context.open(try format.file_desc(&self.staging_arena, path));
} }
return .none; return .none;
@ -91,16 +91,12 @@ pub const Desc = union (enum) {
}; };
pub const Format = enum { pub const Format = enum {
rgba8888, rgba8,
bgra8888, bgra8,
argb8888,
rgb888,
bgr888,
pub fn byte_size(self: Format) usize { pub fn byte_size(self: Format) usize {
return switch (self) { return switch (self) {
.rgba8888, .bgra8888, .argb8888 => 4, .rgba8, .bgra8 => 4,
.rgb888, .bgr888 => 3,
}; };
} }
}; };
@ -197,7 +193,7 @@ pub const Transform2D = extern struct {
const builtin_formats = [_]Assets.Format{ const builtin_formats = [_]Assets.Format{
.{ .{
.extension = "bmp", .extension = "bmp",
.open_file = formats.load_bmp, .file_desc = formats.bmp_file_desc,
}, },
}; };

View File

@ -253,6 +253,11 @@ const Loop = struct {
break: get subimage; break: get subimage;
}, },
}, },
.pixel_format = switch (texture.format) {
.rgba8 => .RGBA8,
.bgra8 => .BGRA8,
},
}); });
errdefer sokol.gfx.destroyImage(image); errdefer sokol.gfx.destroyImage(image);

View File

@ -4,7 +4,7 @@ const gfx = @import("../gfx.zig");
const std = @import("std"); const std = @import("std");
pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Format.Error!gfx.Desc { pub fn bmp_file_desc(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Format.Error!gfx.Desc {
const header = try coral.files.bundle.read_little(path, 0, extern struct { const header = try coral.files.bundle.read_little(path, 0, extern struct {
type: [2]u8 align (1), type: [2]u8 align (1),
file_size: u32 align (1), file_size: u32 align (1),
@ -22,15 +22,15 @@ pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Fo
palette_colors_used: u32 align (1), palette_colors_used: u32 align (1),
important_colors_used: u32 align (1), important_colors_used: u32 align (1),
}) orelse { }) orelse {
return error.Unsupported; return error.FormatUnsupported;
}; };
if (!std.mem.eql(u8, &header.type, "BM")) { if (!std.mem.eql(u8, &header.type, "BM")) {
return error.Unsupported; return error.FormatUnsupported;
} }
const pixel_width = std.math.cast(u16, header.pixel_width) orelse { const pixel_width = std.math.cast(u16, header.pixel_width) orelse {
return error.Unsupported; return error.FormatUnsupported;
}; };
const pixels = try arena.allocator().alloc(coral.io.Byte, header.image_size); const pixels = try arena.allocator().alloc(coral.io.Byte, header.image_size);
@ -42,27 +42,35 @@ pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Fo
var buffer_offset: usize = 0; var buffer_offset: usize = 0;
var file_offset = @as(usize, header.image_offset); var file_offset = @as(usize, header.image_offset);
switch (header.bits_per_pixel) {
32 => {
while (buffer_offset < pixels.len) { while (buffer_offset < pixels.len) {
const line = pixels[buffer_offset .. buffer_offset + byte_stride]; const line = pixels[buffer_offset .. buffer_offset + byte_stride];
if (try coral.files.bundle.read_bytes(path, file_offset, line) != byte_stride) { if (try coral.files.bundle.read_bytes(path, file_offset, line) != byte_stride) {
return error.Unsupported; return error.FormatUnsupported;
} }
file_offset = line.len + byte_padding; for (0 .. pixel_width) |i| {
const line_offset = i * 4;
const pixel = line[line_offset .. line_offset + 4];
std.mem.swap(u8, &pixel[0], &pixel[2]);
}
file_offset += line.len + byte_padding;
buffer_offset += padded_byte_stride; buffer_offset += padded_byte_stride;
} }
},
else => return error.FormatUnsupported,
}
return .{ return .{
.texture = .{ .texture = .{
.format = switch (header.bits_per_pixel) {
24 => .bgr888,
32 => .bgra8888,
else => return error.Unsupported,
},
.width = pixel_width, .width = pixel_width,
.data = pixels, .data = pixels,
.format = .rgba8,
.access = .static, .access = .static,
} }
}; };

View File

@ -51,6 +51,10 @@ out vec4 texel;
void main() { void main() {
texel = texture(sampler2D(tex, smp), uv) * color; texel = texture(sampler2D(tex, smp), uv) * color;
if (texel.a == 0) {
discard;
}
} }
@end @end