Fixed bug in looping, some refactoring

master
Ondřej Hruška 11 years ago
parent 1a5ee30a06
commit 3b0713ff3a
  1. 77
      src/mightypork/gamecore/backends/lwjgl/audio/SlickSound.java
  2. 4
      src/mightypork/gamecore/backends/lwjgl/graphics/LwjglGraphicsModule.java

@ -18,18 +18,18 @@ import org.newdawn.slick.openal.SoundStore;
* @author Ondřej Hruška (MightyPork) * @author Ondřej Hruška (MightyPork)
*/ */
public class SlickSound extends DeferredAudio { public class SlickSound extends DeferredAudio {
private double pauseLoopPosition = 0; private double pauseLoopPosition = 0;
private boolean looping = false; private boolean looping = false;
private boolean paused = false; private boolean paused = false;
private double lastPlayPitch = 1; private double lastPlayPitch = 1;
private double lastPlayGain = 1; private double lastPlayGain = 1;
/** Audio resource */ /** Audio resource */
private Audio backingAudio = null; private Audio backingAudio = null;
private int sourceID; private int sourceID;
/** /**
* Slick-util based sound resource * Slick-util based sound resource
* *
@ -39,113 +39,122 @@ public class SlickSound extends DeferredAudio {
{ {
super(resourceName); super(resourceName);
} }
@Override @Override
protected void loadResource(String resource) throws IOException protected void loadResource(String resource) throws IOException
{ {
final String ext = FileUtil.getExtension(resource); final String ext = FileUtil.getExtension(resource);
try(final InputStream stream = FileUtil.getResource(resource)) { try(final InputStream stream = FileUtil.getResource(resource)) {
if (ext.equalsIgnoreCase("ogg")) { if (ext.equalsIgnoreCase("ogg")) {
backingAudio = SoundStore.get().getOgg(resource, stream); backingAudio = SoundStore.get().getOgg(resource, stream);
} else if (ext.equalsIgnoreCase("wav")) { } else if (ext.equalsIgnoreCase("wav")) {
backingAudio = SoundStore.get().getWAV(resource, stream); backingAudio = SoundStore.get().getWAV(resource, stream);
} else if (ext.equalsIgnoreCase("aif")) { } else if (ext.equalsIgnoreCase("aif")) {
backingAudio = SoundStore.get().getAIF(resource, stream); backingAudio = SoundStore.get().getAIF(resource, stream);
} else if (ext.equalsIgnoreCase("mod")) { } else if (ext.equalsIgnoreCase("mod")) {
backingAudio = SoundStore.get().getMOD(resource, stream); backingAudio = SoundStore.get().getMOD(resource, stream);
} else { } else {
throw new RuntimeException("Invalid audio file extension."); throw new RuntimeException("Invalid audio file extension.");
} }
} }
} }
@Override @Override
public void pauseLoop() public void pauseLoop()
{ {
if (!ensureLoaded()) return; if (!ensureLoaded()) return;
if (isPlaying() && looping) { if (isPlaying() && looping) {
pauseLoopPosition = backingAudio.getPosition(); pauseLoopPosition = backingAudio.getPosition();
stop(); stop();
paused = true; paused = true;
} }
} }
@Override @Override
public void resumeLoop() public void resumeLoop()
{ {
if (!ensureLoaded()) return; if (!ensureLoaded()) return;
if (looping && paused) { if (looping && paused) {
sourceID = backingAudio.playAsSoundEffect((float) lastPlayPitch, (float) lastPlayGain, true); sourceID = backingAudio.playAsSoundEffect((float) lastPlayPitch, (float) lastPlayGain, true);
backingAudio.setPosition((float) pauseLoopPosition); backingAudio.setPosition((float) pauseLoopPosition);
paused = false; paused = false;
} }
} }
@Override @Override
public void adjustGain(double gain) public void adjustGain(double gain)
{ {
AL10.alSourcef(sourceID, AL10.AL_GAIN, (float) gain); AL10.alSourcef(sourceID, AL10.AL_GAIN, (float) gain);
} }
@Override @Override
public void stop() public void stop()
{ {
if (!isLoaded()) return; if (!isLoaded()) return;
backingAudio.stop(); backingAudio.stop();
paused = false; paused = false;
} }
@Override @Override
public boolean isPlaying() public boolean isPlaying()
{ {
if (!isLoaded()) return false; if (!isLoaded()) return false;
return backingAudio.isPlaying(); return backingAudio.isPlaying();
} }
@Override @Override
public boolean isPaused() public boolean isPaused()
{ {
if (!isLoaded()) return false; if (!isLoaded()) return false;
return backingAudio.isPaused(); return backingAudio.isPaused();
} }
@Override
public boolean isActive()
{
if (!isLoaded()) return false;
return sourceID != -1;
}
@Override @Override
public void play(double pitch, double gain, boolean loop, double x, double y, double z) public void play(double pitch, double gain, boolean loop, double x, double y, double z)
{ {
if (!ensureLoaded()) return; if (!ensureLoaded()) return;
this.lastPlayPitch = pitch; this.lastPlayPitch = pitch;
this.lastPlayGain = gain; this.lastPlayGain = gain;
looping = loop; looping = loop;
sourceID = backingAudio.playAsSoundEffect((float) pitch, (float) gain, loop, (float) x, (float) y, (float) z); sourceID = backingAudio.playAsSoundEffect((float) pitch, (float) gain, loop, (float) x, (float) y, (float) z);
} }
@Override @Override
public void destroy() public void destroy()
{ {
if (!isLoaded() || backingAudio == null) return; if (!isLoaded() || backingAudio == null) return;
backingAudio.release(); backingAudio.release();
backingAudio = null; backingAudio = null;
} }

@ -416,14 +416,14 @@ public class LwjglGraphicsModule extends GraphicsModule {
@Override @Override
public DeferredTexture createDeferredTexture(String path) public DeferredTexture createTextureResource(String path)
{ {
return new SlickTexture(path); return new SlickTexture(path);
} }
@Override @Override
public DeferredFont createDeferredFont(String path) public DeferredFont createFontResource(String path)
{ {
return new LwjglFont(path); return new LwjglFont(path);
} }

Loading…
Cancel
Save