SoundManager update

This commit is contained in:
Antoine 2021-05-29 02:49:33 +02:00
parent a9bbde6c46
commit 4f8a32d5a4
2 changed files with 94 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package engine;
import engine.math.*;
import engine.object.*;
import engine.sound.*;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode;
@ -158,7 +159,27 @@ public class Engine {
}
};
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
/*
OpenAl TEST
*/
SoundManager soundManager = new SoundManager();
soundManager.init();
SoundListener soundListener = new SoundListener();
soundManager.setListener(soundListener);
// SoundBuffer jumpSoundBuffer = new SoundBuffer("sound/jump.ogg"); EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff893802818, pid=1788, tid=4548
// SoundSource soundSource = new SoundSource(true, false);
// soundSource.setBuffer(jumpSoundBuffer.getBufferId());
//
// soundManager.addSoundSource("jump", soundSource);
// soundManager.playSoundSource("jump");
/*
Engine Init
*/
Engine engine = new Engine(1280, 720, 9.0f / 16.0f);
int speed = 10; //vitesse déplacement Object
engine.init();
@ -219,5 +240,8 @@ public class Engine {
nextFrame = false;
if (engine.shouldClose()) engine.setRunning(false);
}
soundManager.cleanup();
}
}

View File

@ -1,6 +1,9 @@
// Source : https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter28/src/main/java/org/lwjglb/engine/sound/SoundManager.java
package engine.sound;
import engine.math.Matrix4f;
import engine.math.Vector3f;
import org.lwjgl.openal.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
@ -9,6 +12,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*;
import static org.lwjgl.system.MemoryUtil.NULL;
@ -46,4 +50,69 @@ public class SoundManager {
alcMakeContextCurrent(context);
AL.createCapabilities(deviceCaps);
}
public void addSoundSource(String name, SoundSource soundSource) {
this.soundSourceMap.put(name, soundSource);
}
public SoundSource getSoundSource(String name) {
return this.soundSourceMap.get(name);
}
public void playSoundSource(String name) {
SoundSource soundSource = this.soundSourceMap.get(name);
if (soundSource != null && !soundSource.isPlaying()) {
soundSource.play();
}
}
public void removeSoundSource(String name) {
this.soundSourceMap.remove(name);
}
public void addSoundBuffer(SoundBuffer soundBuffer) {
this.soundBufferList.add(soundBuffer);
}
public SoundListener getListener() {
return this.listener;
}
public void setListener(SoundListener listener) {
this.listener = listener;
}
// Camera Statique de notre côté pas besoin de "corriger" cette fonction
// public void updateListenerPosition(Camera camera) {
// // Update camera matrix with camera data
// Transformation.updateGenericViewMatrix(camera.getPosition(), camera.getRotation(), cameraMatrix);
//
// listener.setPosition(camera.getPosition());
// Vector3f at = new Vector3f();
// cameraMatrix.positiveZ(at).negate();
// Vector3f up = new Vector3f();
// cameraMatrix.positiveY(up);
// listener.setOrientation(at, up);
// }
public void setAttenuationModel(int model) {
alDistanceModel(model);
}
public void cleanup() {
for (SoundSource soundSource : soundSourceMap.values()) {
soundSource.cleanup();
}
soundSourceMap.clear();
for (SoundBuffer soundBuffer : soundBufferList) {
soundBuffer.cleanup();
}
soundBufferList.clear();
if (context != NULL) {
alcDestroyContext(context);
}
if (device != NULL) {
alcCloseDevice(device);
}
}
}