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;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
@@ -26,7 +27,8 @@ public class MapActor extends Actor {
}
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.effect = effect;
this.offset = offset;
@@ -34,6 +36,7 @@ public class MapActor extends Actor {
}
private final String path;
private FileHandle fileHandle;
public ParticleEffect effect;
public Vector2 offset;
public boolean overlay = true;
@@ -57,10 +60,11 @@ public class MapActor extends Actor {
}
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) {
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
if (duration != 0) {
for (ParticleEmitter emitter : effect.getEmitters()) {
@@ -159,8 +163,7 @@ public class MapActor extends Actor {
if (effect.effect.isComplete()) {
effects.removeIndex(i);
i--;
//assetmanager should handle dispose automatically
//effect.effect.dispose();
Forge.getAssets().manager().unload(effect.fileHandle.path());
}
}
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.TextureLoader.TextureParameter;
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.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
@@ -368,6 +370,34 @@ public class Assets implements Disposable {
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 {
private int currentMemory;
private Map<String, Integer> memoryPerFile;
@@ -530,8 +560,8 @@ public class Assets implements Disposable {
super.unload(fileName);
if (memoryPerFile.containsKey(fileName)) {
memoryPerFile.remove(fileName);
cardArtCache().clear();
}
cardArtCache().clear();
}
public float getMemoryInMegabytes() {

View File

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

View File

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