88 lines
2.3 KiB
Zig
88 lines
2.3 KiB
Zig
const std = @import("std");
|
|
|
|
const tiled = @import("tiled");
|
|
|
|
const Math = @import("./engine/math.zig");
|
|
const Engine = @import("./engine/root.zig");
|
|
const STBImage = @import("stb_image");
|
|
const Gfx = Engine.Graphics;
|
|
const Audio = Engine.Audio;
|
|
|
|
const Assets = @This();
|
|
|
|
const FontName = enum {
|
|
regular,
|
|
bold,
|
|
italic,
|
|
|
|
const EnumArray = std.EnumArray(FontName, Gfx.Font.Id);
|
|
};
|
|
|
|
font_id: FontName.EnumArray,
|
|
wood01: Audio.Data.Id,
|
|
map: tiled.Tilemap,
|
|
tilesets: tiled.Tileset.List,
|
|
tileset_texture: Gfx.TextureId,
|
|
tile_size: Engine.Vec2,
|
|
|
|
pub fn init(gpa: std.mem.Allocator) !Assets {
|
|
const font_id_array: FontName.EnumArray = .init(.{
|
|
.regular = try Gfx.addFont("regular", @embedFile("assets/roboto-font/Roboto-Regular.ttf")),
|
|
.bold = try Gfx.addFont("bold", @embedFile("assets/roboto-font/Roboto-Bold.ttf")),
|
|
.italic = try Gfx.addFont("italic", @embedFile("assets/roboto-font/Roboto-Italic.ttf")),
|
|
});
|
|
|
|
const wood01 = try Audio.load(.{
|
|
.format = .vorbis,
|
|
.data = @embedFile("assets/wood01.ogg"),
|
|
});
|
|
|
|
var scratch = std.heap.ArenaAllocator.init(gpa);
|
|
defer scratch.deinit();
|
|
|
|
var xml_buffers = tiled.xml.Lexer.Buffers.init(gpa);
|
|
defer xml_buffers.deinit();
|
|
|
|
const map = try tiled.Tilemap.initFromBuffer(
|
|
gpa,
|
|
&scratch,
|
|
&xml_buffers,
|
|
@embedFile("assets/map.tmx")
|
|
);
|
|
|
|
const tileset = try tiled.Tileset.initFromBuffer(
|
|
gpa,
|
|
&scratch,
|
|
&xml_buffers,
|
|
@embedFile("assets/tileset.tsx")
|
|
);
|
|
|
|
var tilesets: tiled.Tileset.List = .empty;
|
|
try tilesets.add(gpa, "tileset.tsx", tileset);
|
|
|
|
|
|
const tileset_image = try STBImage.load(@embedFile("assets/kenney_desert-shooter-pack_1.0/PNG/Tiles/tilemap_packed.png"));
|
|
defer tileset_image.deinit();
|
|
const tileset_texture = try Gfx.addTexture(&.{
|
|
.{
|
|
.width = tileset_image.width,
|
|
.height = tileset_image.height,
|
|
.rgba = tileset_image.rgba8_pixels
|
|
}
|
|
});
|
|
|
|
return Assets{
|
|
.font_id = font_id_array,
|
|
.wood01 = wood01,
|
|
.map = map,
|
|
.tilesets = tilesets,
|
|
.tileset_texture = tileset_texture,
|
|
.tile_size = .init(16, 16)
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Assets, gpa: std.mem.Allocator) void {
|
|
self.map.deinit();
|
|
self.tilesets.deinit(gpa);
|
|
}
|