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

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

Loading…
Cancel
Save