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.Gdx;
import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.audio.Sound;
import com.watabou.noosa.Game; import com.watabou.noosa.Game;
import com.watabou.utils.Callback;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@@ -62,44 +61,30 @@ public enum Sample {
} }
} }
private static LinkedList<String> loadingQueue = new LinkedList<>(); public synchronized void load( final String asset){
if (asset != null) {
public synchronized void load( final String... assets ) { try {
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset));
for (String asset : assets){ ids.put(asset, newSound);
if (!ids.containsKey(asset) && !loadingQueue.contains(asset)){ } catch (Exception e){
loadingQueue.add(asset); 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() { private static final LinkedList<String> loadingQueue = new LinkedList<>();
@Override
public void call() { //queues multiple assets for loading, which happens in update()
synchronized (INSTANCE) { // this prevents blocking while we load many assets
String asset = loadingQueue.poll(); public void load( final String[] assets ) {
if (asset != null) { synchronized (loadingQueue) {
try { for (String asset : assets) {
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset)); if (!ids.containsKey(asset) && !loadingQueue.contains(asset)) {
ids.put(asset, newSound); loadingQueue.add(asset);
} catch (Exception e){
Game.reportException(e);
}
}
if (!loadingQueue.isEmpty()){
Game.runOnRenderThread(this);
} }
} }
} }
}; }
public synchronized void unload( Object src ) { public synchronized void unload( Object src ) {
if (ids.containsKey( src )) { if (ids.containsKey( src )) {
@@ -170,6 +155,12 @@ public enum Sample {
} }
public void update(){ public void update(){
synchronized (loadingQueue) {
if (!loadingQueue.isEmpty()) {
load(loadingQueue.poll());
}
}
synchronized (delayedSFX) { synchronized (delayedSFX) {
if (delayedSFX.isEmpty()) return; if (delayedSFX.isEmpty()) return;
for (DelayedSoundEffect sfx : delayedSFX.toArray(new DelayedSoundEffect[0])) { for (DelayedSoundEffect sfx : delayedSFX.toArray(new DelayedSoundEffect[0])) {