Skip to content

Commit ff5dc2d

Browse files
author
Jay
committed
play_resource may request the audio data stays in memory (for UI)
1 parent 0688984 commit ff5dc2d

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .engine,
3-
.version = "0.7.3",
3+
.version = "0.7.4",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.15.2",
66
.dependencies = .{

src/engine.zig

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,26 +2393,39 @@ pub const AudioInfo = struct {
23932393
name: []const u8,
23942394
audio: []const u8,
23952395
references: i32,
2396+
resource: ?*Resource,
2397+
autorelease: Retain,
2398+
2399+
pub const empty = .{
2400+
.name = "",
2401+
.audio = "",
2402+
.references = 0,
2403+
.resource = null,
2404+
.autorelease = .autorelease,
2405+
};
23962406

23972407
pub fn create(
23982408
allocator: Allocator,
23992409
name: []const u8,
24002410
audio: []const u8,
2411+
autorelease: Retain,
24012412
) !*AudioInfo {
2413+
std.debug.assert(audio.len > 0);
24022414
const audio_info = try allocator.create(AudioInfo);
24032415
audio_info.* = .{
24042416
.name = if (name.len > 0) try allocator.dupe(u8, name) else "",
24052417
.audio = audio,
2406-
.references = 0,
2418+
.references = if (autorelease == .autorelease) 0 else 1,
2419+
.resource = null,
2420+
.autorelease = autorelease,
24072421
};
24082422
debug("loaded audio: {s}", .{name});
24092423
return audio_info;
24102424
}
24112425

24122426
pub fn destroy(self: *AudioInfo, allocator: Allocator) void {
2413-
allocator.free(self.audio);
2414-
if (self.name.len > 0)
2415-
allocator.free(self.name);
2427+
if (self.audio.len > 0) allocator.free(self.audio);
2428+
if (self.name.len > 0) allocator.free(self.name);
24162429
allocator.destroy(self);
24172430
}
24182431

@@ -2454,6 +2467,11 @@ const FontInfo = struct {
24542467
}
24552468
};
24562469

2470+
pub const Retain = enum {
2471+
autorelease,
2472+
retain,
2473+
};
2474+
24572475
pub const TextSize = enum {
24582476
small,
24592477
normal,
@@ -2830,7 +2848,7 @@ pub const Display = struct {
28302848

28312849
var a = self.audio.iterator();
28322850
while (a.next()) |x| {
2833-
if (x.value_ptr.*.references > 0) {
2851+
if (x.value_ptr.*.references > 0 and x.value_ptr.*.autorelease == .autorelease) {
28342852
warn("audio file was not deallocated. {s} has {d} references", .{
28352853
x.key_ptr.*,
28362854
x.value_ptr.*.references,
@@ -3720,8 +3738,10 @@ pub const Display = struct {
37203738
self: *Display,
37213739
gpa: Allocator,
37223740
name: []const u8,
3741+
autorelease: Retain,
3742+
volume: f32,
37233743
) (Error || Allocator.Error || Resources.Error)!?*AudioInfo {
3724-
return self.play_bundle_resource(gpa, self.resources, name);
3744+
return self.play_bundle_resource(gpa, self.resources, name, autorelease, volume);
37253745
}
37263746

37273747
/// Load an image from a specific resource bundle.
@@ -3730,6 +3750,8 @@ pub const Display = struct {
37303750
gpa: Allocator,
37313751
bundle: *Resources,
37323752
name: []const u8,
3753+
autorelease: Retain,
3754+
volume: f32,
37333755
) (Error || Allocator.Error || Resources.Error)!?*AudioInfo {
37343756
if (name.len == 0) {
37353757
err("play_bundle_resource(\"{s}\") resource name empty", .{name});
@@ -3739,6 +3761,11 @@ pub const Display = struct {
37393761
// Load audio from memory cache if possible
37403762
var item: ?*AudioInfo = null;
37413763
if (self.audio.get(name)) |i| {
3764+
if (autorelease == .retain and i.autorelease == .autorelease) {
3765+
// This audio item is converting to permanent memory
3766+
i.autorelease = .retain;
3767+
i.references += 1;
3768+
}
37423769
i.references += 1;
37433770
item = i;
37443771
} else {
@@ -3763,8 +3790,9 @@ pub const Display = struct {
37633790
end - start,
37643791
});
37653792

3766-
const ai = try AudioInfo.create(gpa, name, audio);
3793+
const ai = try AudioInfo.create(gpa, name, audio, autorelease);
37673794
ai.references += 1;
3795+
ai.resource = resource;
37683796
try self.audio.put(gpa, ai.name, ai);
37693797
item = ai;
37703798
}
@@ -3776,7 +3804,19 @@ pub const Display = struct {
37763804
return null;
37773805
}
37783806

3779-
_ = mixer.MIX_PlayAudio(self.mix, buff.?);
3807+
const track = mixer.MIX_CreateTrack(self.mix);
3808+
if (track == null) {
3809+
err("create track for \"{s}\" failed", .{name});
3810+
return null;
3811+
}
3812+
_ = mixer.MIX_SetTrackAudio(track, buff.?);
3813+
_ = mixer.MIX_SetTrackGain(track, volume);
3814+
//_ = mixer.MIX_SetTrackStoppedCallback(track, cb: ?*const fn (?*anyopaque, ?*struct_MIX_Track) void, userdata: ?*anyopaque);
3815+
_ = mixer.MIX_PlayTrack(
3816+
track,
3817+
0, // used to request looping or play starting point
3818+
);
3819+
//_ = mixer.MIX_PlayAudio(self.mix, buff.?);
37803820

37813821
return item;
37823822
}

0 commit comments

Comments
 (0)