v3.2.1: adjusted sound asset loading to use Sample.update
This commit is contained in:
@@ -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,30 +61,7 @@ public enum Sample {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LinkedList<String> loadingQueue = new LinkedList<>();
|
public synchronized void load( final String asset){
|
||||||
|
|
||||||
public synchronized void load( final String... assets ) {
|
|
||||||
|
|
||||||
for (String asset : assets){
|
|
||||||
if (!ids.containsKey(asset) && !loadingQueue.contains(asset)){
|
|
||||||
loadingQueue.add(asset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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) {
|
if (asset != null) {
|
||||||
try {
|
try {
|
||||||
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset));
|
Sound newSound = Gdx.audio.newSound(Gdx.files.internal(asset));
|
||||||
@@ -94,12 +70,21 @@ public enum Sample {
|
|||||||
Game.reportException(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 ) {
|
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])) {
|
||||||
|
|||||||
Reference in New Issue
Block a user