Skip to content

Commit

Permalink
Add test music to check audio on itch.io
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 10, 2023
1 parent 8a951ff commit 0335cdd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
13 changes: 8 additions & 5 deletions src/AssetManager.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub const AssetManager = struct {

pub const Musics = enum {
Unknown,
test_,
};

pub const Fonts = enum {
Expand All @@ -33,7 +34,7 @@ pub const AssetManager = struct {
return AssetManagerErrors.NotFound;
},
.Meteor => {
return RawAsset.init(".png", @embedFile("./Textures/Yellow Meteor.png"));
return RawAsset.init("./Textures/Yellow Meteor.png");
},
}
}
Expand All @@ -51,6 +52,9 @@ pub const AssetManager = struct {
Musics.Unknown => {
return AssetManagerErrors.NotFound;
},
.test_ => {
return RawAsset.init("./Music/test.ogg");
},
}
}

Expand Down Expand Up @@ -168,12 +172,11 @@ pub const AssetManager = struct {
Bytes: [:0]const u8,

pub fn init(
fileType: [:0]const u8,
bytes: [:0]const u8,
comptime file: [:0]const u8,
) RawAsset {
return RawAsset{
.FileType = fileType,
.Bytes = bytes,
.FileType = file[file.len - 4 .. file.len],
.Bytes = @embedFile(file),
};
}
};
Expand Down
Binary file added src/Music/test.ogg
Binary file not shown.
2 changes: 2 additions & 0 deletions src/Views/MenuView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const vm: type = MenuViewModel.GetVM();
pub fn DrawFunction() Shared.View.Views {
raylib.clearBackground(Shared.Color.Gray.Base);

Shared.Music.Play(.test_);

const locale = Shared.Locale.GetLocale().?;
const font = Shared.Font.Get(.Unknown);

Expand Down
8 changes: 6 additions & 2 deletions src/asteroids-website/src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Module implements ICustomModule {
}

public print(t: string): void {
console.log(t);
globalThis.console.log(t);
};

public printErr(text: string): void {
Expand All @@ -84,7 +84,11 @@ export class Module implements ICustomModule {

public canvas: HTMLCanvasElement = (() => {
const c = document.createElement('canvas');
c.classList.add("rounded-lg");
setTimeout(() => {
c.addEventListener("contextmenu", (e) => e.preventDefault());
c.classList.add("rounded-lg");
});

return c;
})();

Expand Down
78 changes: 46 additions & 32 deletions src/asteroids-website/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import GameController from './controls.svelte';
import StatusContainer from './status-container.svelte';
import { Button } from '$lib/gameController';
import { _, isLoading, waitLocale } from 'svelte-i18n';
import { _, isLoading, time, waitLocale } from 'svelte-i18n';
import { Localizer } from '$lib/localizer';
import emscriptenModuleFactory from '../import/emscripten';
import { Settings } from '$lib/settings';
Expand Down Expand Up @@ -109,36 +109,48 @@
setTimeout(() => handleButtonReleased(Button.Start), 100);
}
function unlockAudio() {
// Unlock audio
['touchstart', 'touchend', 'mousedown', 'keydown'].forEach((e) =>
document.body.addEventListener(
e,
() => {
window.miniaudio?.unlock();
},
{
once: true
}
)
);
// Remove miniaudio object from window after unlocked
window.miniaudio?.devices.forEach((d, i) =>
d.webaudio.addEventListener(
'statechange',
(e) => {
if (d.webaudio.state === 'running') window.miniaudio?.untrack_device_by_index(i);
if (window.miniaudio?.devices.length == 0) {
muted = false;
delete window.miniaudio;
}
},
{
once: true
}
)
);
muted = true;
function unlockAudio(timeout: number = 100) {
const func = () => {
const runningState = 'running';
muted = window.miniaudio?.devices.some((d) => d.webaudio.state != runningState) ?? false;
if (muted) {
// Unlock audio
['touchstart', 'touchend', 'mousedown', 'keydown'].forEach((e) =>
document.body.addEventListener(
e,
() => {
window.miniaudio?.unlock();
},
{
once: true
}
)
);
// Remove miniaudio object from window after unlocked
window.miniaudio?.devices.forEach((d, i) =>
d.webaudio.addEventListener(
'statechange',
(e) => {
if (d.webaudio.state === runningState) window.miniaudio?.untrack_device_by_index(i);
if (window.miniaudio?.devices.length == 0) {
unlockAudio(0);
}
},
{
once: true
}
)
);
} else {
muted = false;
delete window.miniaudio;
}
};
if (timeout <= 0) {
func();
}
setTimeout(func, timeout);
}
const isMobile: boolean = (() => {
Expand All @@ -163,7 +175,9 @@
} else {
await waitLocale();
const module = await Module.Init($_('page.Downloading'));
emscripten = await (<EmscriptenModuleFactory<CustomEmscriptenModule>>emscriptenModuleFactory)(module);
emscripten = await (<EmscriptenModuleFactory<CustomEmscriptenModule>>emscriptenModuleFactory)(
module
);
await tick();
unlockAudio();
UpdateSize(null);
Expand Down

0 comments on commit 0335cdd

Please sign in to comment.