|
| 1 | +/// An audio file is held in memory for the entire duration it might be needed. |
| 2 | +/// |
| 3 | +/// If an audio file is in use by more than one element, then the `references` |
| 4 | +/// counter keeps track of how many elements are currently depending on |
| 5 | +/// this audio file. |
| 6 | +name: []const u8, |
| 7 | +audio: []const u8, |
| 8 | +references: i32, |
| 9 | +resource: ?*Resource, |
| 10 | +autorelease: Retain, |
| 11 | + |
| 12 | +const Audio = @This(); |
| 13 | + |
| 14 | +pub const empty = .{ |
| 15 | + .name = "", |
| 16 | + .audio = "", |
| 17 | + .references = 0, |
| 18 | + .resource = null, |
| 19 | + .autorelease = .autorelease, |
| 20 | +}; |
| 21 | + |
| 22 | +pub fn create( |
| 23 | + allocator: Allocator, |
| 24 | + name: []const u8, |
| 25 | + audio: []const u8, |
| 26 | + autorelease: Retain, |
| 27 | +) Allocator.Error!*Audio { |
| 28 | + std.debug.assert(audio.len > 0); |
| 29 | + const audio_info = try allocator.create(Audio); |
| 30 | + audio_info.* = .{ |
| 31 | + .name = if (name.len > 0) try allocator.dupe(u8, name) else "", |
| 32 | + .audio = audio, |
| 33 | + .references = if (autorelease == .autorelease) 0 else 1, |
| 34 | + .resource = null, |
| 35 | + .autorelease = autorelease, |
| 36 | + }; |
| 37 | + debug("loaded audio: {s}", .{name}); |
| 38 | + return audio_info; |
| 39 | +} |
| 40 | + |
| 41 | +pub fn destroy(self: *Audio, allocator: Allocator) void { |
| 42 | + if (self.audio.len > 0) allocator.free(self.audio); |
| 43 | + if (self.name.len > 0) allocator.free(self.name); |
| 44 | + allocator.destroy(self); |
| 45 | +} |
| 46 | + |
| 47 | +pub fn clone(self: *Audio) *Audio { |
| 48 | + self.references += 1; |
| 49 | + return self; |
| 50 | +} |
| 51 | + |
| 52 | +const std = @import("std"); |
| 53 | +const ArrayListUnmanaged = std.ArrayListUnmanaged; |
| 54 | +const Allocator = std.mem.Allocator; |
| 55 | +const sdl = @import("sdl"); |
| 56 | +const builtin = @import("builtin"); |
| 57 | +const engine = @import("engine.zig"); |
| 58 | +const debug = engine.debug; |
| 59 | +const Retain = engine.Retain; |
| 60 | + |
| 61 | +const Resource = @import("resources").Resource; |
0 commit comments