Merge pull request #2668 from kevlahnota/newmaster2

update AudioClip, AudioMusic
This commit is contained in:
Anthony Calosa
2023-03-13 11:19:06 +08:00
committed by GitHub
4 changed files with 59 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
package forge.adventure.character; package forge.adventure.character;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
@@ -26,7 +27,8 @@ public class MapActor extends Actor {
} }
static class CurrentEffect { static class CurrentEffect {
public CurrentEffect(String path, ParticleEffect effect, Vector2 offset, boolean overlay) { public CurrentEffect(FileHandle file, String path, ParticleEffect effect, Vector2 offset, boolean overlay) {
this.fileHandle = file;
this.path = path; this.path = path;
this.effect = effect; this.effect = effect;
this.offset = offset; this.offset = offset;
@@ -34,6 +36,7 @@ public class MapActor extends Actor {
} }
private final String path; private final String path;
private FileHandle fileHandle;
public ParticleEffect effect; public ParticleEffect effect;
public Vector2 offset; public Vector2 offset;
public boolean overlay = true; public boolean overlay = true;
@@ -57,10 +60,11 @@ public class MapActor extends Actor {
} }
public void playEffect(String path, float duration, boolean overlay, Vector2 offset) { public void playEffect(String path, float duration, boolean overlay, Vector2 offset) {
ParticleEffect effect = Forge.getAssets().getEffect(Config.instance().getFile(path)); FileHandle file = Config.instance().getFile(path);
ParticleEffect effect = Forge.getAssets().getEffect(file);
if (effect != null) { if (effect != null) {
effect.setPosition(getCenter().x, getCenter().y); effect.setPosition(getCenter().x, getCenter().y);
effects.add(new CurrentEffect(path, effect, offset, overlay)); effects.add(new CurrentEffect(file, path, effect, offset, overlay));
//ParticleEffect.setDuration uses an integer for some reason //ParticleEffect.setDuration uses an integer for some reason
if (duration != 0) { if (duration != 0) {
for (ParticleEmitter emitter : effect.getEmitters()) { for (ParticleEmitter emitter : effect.getEmitters()) {
@@ -159,8 +163,7 @@ public class MapActor extends Actor {
if (effect.effect.isComplete()) { if (effect.effect.isComplete()) {
effects.removeIndex(i); effects.removeIndex(i);
i--; i--;
//assetmanager should handle dispose automatically Forge.getAssets().manager().unload(effect.fileHandle.path());
//effect.effect.dispose();
} }
} }
if (effects.size == 0 && removeIfEffectsAreFinished && getParent() != null) if (effects.size == 0 && removeIfEffectsAreFinished && getParent() != null)

View File

@@ -8,6 +8,8 @@ import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.ParticleEffectLoader; import com.badlogic.gdx.assets.loaders.ParticleEffectLoader;
import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter; import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver; import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
@@ -368,6 +370,34 @@ public class Assets implements Disposable {
return textrafonts.get(name); return textrafonts.get(name);
} }
public Music getMusic(FileHandle file) {
if (file == null || !file.exists() || !FileType.Absolute.equals(file.type())) {
System.err.println("Failed to load: " + file +"!.");
return null;
}
Music music = manager().get(file.path(), Music.class, false);
if (music == null) {
manager().load(file.path(), Music.class);
manager().finishLoadingAsset(file.path());
music = manager().get(file.path(), Music.class);
}
return music;
}
public Sound getSound(FileHandle file) {
if (file == null || !file.exists() || !FileType.Absolute.equals(file.type())) {
System.err.println("Failed to load: " + file +"!.");
return null;
}
Sound sound = manager().get(file.path(), Sound.class, false);
if (sound == null) {
manager().load(file.path(), Sound.class);
manager().finishLoadingAsset(file.path());
sound = manager().get(file.path(), Sound.class);
}
return sound;
}
public class MemoryTrackingAssetManager extends AssetManager { public class MemoryTrackingAssetManager extends AssetManager {
private int currentMemory; private int currentMemory;
private Map<String, Integer> memoryPerFile; private Map<String, Integer> memoryPerFile;
@@ -530,9 +560,9 @@ public class Assets implements Disposable {
super.unload(fileName); super.unload(fileName);
if (memoryPerFile.containsKey(fileName)) { if (memoryPerFile.containsKey(fileName)) {
memoryPerFile.remove(fileName); memoryPerFile.remove(fileName);
}
cardArtCache().clear(); cardArtCache().clear();
} }
}
public float getMemoryInMegabytes() { public float getMemoryInMegabytes() {
return (float) currentMemory / 1024f / 1024f; return (float) currentMemory / 1024f / 1024f;

View File

@@ -21,6 +21,7 @@ package forge.sound;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import forge.Forge;
public class AudioClip implements IAudioClip { public class AudioClip implements IAudioClip {
private Sound clip; private Sound clip;
@@ -33,7 +34,7 @@ public class AudioClip implements IAudioClip {
private AudioClip(final FileHandle fileHandle) { private AudioClip(final FileHandle fileHandle) {
try { try {
clip = Gdx.audio.newSound(fileHandle); clip = Forge.getAssets().getSound(fileHandle);
} }
catch (Exception ex) { catch (Exception ex) {
System.err.println("Unable to load sound file: " + fileHandle.toString()); System.err.println("Unable to load sound file: " + fileHandle.toString());
@@ -48,7 +49,7 @@ public class AudioClip implements IAudioClip {
Thread.sleep(SoundSystem.DELAY); Thread.sleep(SoundSystem.DELAY);
} }
catch (InterruptedException ex) { catch (InterruptedException ex) {
ex.printStackTrace(); //ex.printStackTrace();
} }
clip.play(value); clip.play(value);
} }
@@ -61,7 +62,7 @@ public class AudioClip implements IAudioClip {
Thread.sleep(SoundSystem.DELAY); Thread.sleep(SoundSystem.DELAY);
} }
catch (InterruptedException ex) { catch (InterruptedException ex) {
ex.printStackTrace(); //ex.printStackTrace();
} }
clip.loop(); clip.loop();
} }

View File

@@ -3,15 +3,21 @@ package forge.sound;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener; import com.badlogic.gdx.audio.Music.OnCompletionListener;
import com.badlogic.gdx.files.FileHandle;
import forge.Forge;
public class AudioMusic implements IAudioMusic { public class AudioMusic implements IAudioMusic {
private Music music; private Music music;
private FileHandle file;
public AudioMusic(String filename) { public AudioMusic(String filename) {
music = Gdx.audio.newMusic(Gdx.files.absolute(filename)); file = Gdx.files.absolute(filename);
music = Forge.getAssets().getMusic(file);
} }
public void play(final Runnable onComplete) { public void play(final Runnable onComplete) {
if (music == null)
return;
music.setOnCompletionListener(new OnCompletionListener() { music.setOnCompletionListener(new OnCompletionListener() {
@Override @Override
public void onCompletion(Music music) { public void onCompletion(Music music) {
@@ -22,27 +28,35 @@ public class AudioMusic implements IAudioMusic {
} }
public void pause() { public void pause() {
if (music == null)
return;
music.pause(); music.pause();
} }
public void resume() { public void resume() {
if (music == null)
return;
if (!music.isPlaying()) { if (!music.isPlaying()) {
music.play(); music.play();
} }
} }
public void stop() { public void stop() {
if (music == null)
return;
music.setOnCompletionListener(null); //prevent firing if stopped manually music.setOnCompletionListener(null); //prevent firing if stopped manually
music.stop(); music.stop();
} }
public void dispose() { public void dispose() {
stop(); stop();
music.dispose(); Forge.getAssets().manager().unload(file.path());
} }
@Override @Override
public void setVolume(float value) { public void setVolume(float value) {
if (music == null)
return;
music.setVolume(value); music.setVolume(value);
} }
} }