v3.2.1: adjusted sound asset loading to use Sample.update

This commit is contained in:
Evan Debenham
2025-08-13 18:16:36 -04:00
parent c6f866ea96
commit 11373e6fdf

View File

@@ -24,7 +24,6 @@ package com.watabou.noosa.audio;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.watabou.noosa.Game;
import com.watabou.utils.Callback;
import java.util.HashMap;
import java.util.HashSet;
@@ -62,44 +61,30 @@ public enum Sample {
}
}
private static LinkedList<String> loadingQueue = new LinkedList<>();
public synchronized void load( final String... assets ) {
for (String asset : assets){
if (!ids.containsKey(asset) && !loadingQueue.contains(asset)){
loadingQueue.add(asset);
public synchronized void load( final String asset){
if (asset != null) {
try {
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset));
ids.put(asset, newSound);
} catch (Exception e){
Game.reportException(e);
}
}
//cancel if all assets are already loaded
if (loadingQueue.isEmpty()) return;
//load one at a time on the UI thread to prevent this blocking the UI
//yes this may cause hitching, but only in the first couple seconds of game runtime
Game.runOnRenderThread(loadingCallback);
}
private Callback loadingCallback = new Callback() {
@Override
public void call() {
synchronized (INSTANCE) {
String asset = loadingQueue.poll();
if (asset != null) {
try {
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset));
ids.put(asset, newSound);
} catch (Exception e){
Game.reportException(e);
}
}
if (!loadingQueue.isEmpty()){
Game.runOnRenderThread(this);
private static final LinkedList<String> loadingQueue = new LinkedList<>();
//queues multiple assets for loading, which happens in update()
// this prevents blocking while we load many assets
public void load( final String[] assets ) {
synchronized (loadingQueue) {
for (String asset : assets) {
if (!ids.containsKey(asset) && !loadingQueue.contains(asset)) {
loadingQueue.add(asset);
}
}
}
};
}
public synchronized void unload( Object src ) {
if (ids.containsKey( src )) {
@@ -170,6 +155,12 @@ public enum Sample {
}
public void update(){
synchronized (loadingQueue) {
if (!loadingQueue.isEmpty()) {
load(loadingQueue.poll());
}
}
synchronized (delayedSFX) {
if (delayedSFX.isEmpty()) return;
for (DelayedSoundEffect sfx : delayedSFX.toArray(new DelayedSoundEffect[0])) {