Async asset loading

This commit is contained in:
duck
2025-08-30 23:52:54 +05:00
parent 9fdd997be6
commit 3f1c0aa2f8
6 changed files with 309 additions and 78 deletions

34
src/assets/texture.zig Normal file
View File

@@ -0,0 +1,34 @@
const std = @import("std");
const sdl = @import("sdl");
const err = @import("../error.zig");
const c = @import("../c.zig");
const Assets = @import("../assets.zig");
const Graphics = @import("../graphics.zig");
texture: *sdl.GPUTexture,
sampler: *sdl.GPUSampler,
pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() {
_ = alloc;
var file = Assets.load(.file, path);
defer Assets.free(file);
const data = (try file.getSync()).bytes;
var x: i32 = undefined;
var y: i32 = undefined;
var z: i32 = undefined;
const image = c.stbi_load_from_memory(@ptrCast(data), @intCast(data.len), &x, &y, &z, 4);
if (image == null) err.stbi();
const image_slice = image[0..@intCast(x * y * z)];
const texture, const sampler = Graphics.loadTexture(@intCast(x), @intCast(y), image_slice);
c.stbi_image_free(image);
return .{
.texture = texture,
.sampler = sampler,
};
}
pub fn unload(self: @This(), alloc: std.mem.Allocator) void {
_ = alloc;
Graphics.unloadTexture(self.texture, self.sampler);
}