started sounds!
parent
3d0754f060
commit
807e6a2a49
Binary file not shown.
|
|
@ -0,0 +1 @@
|
|||
https://opengameart.org/content/botton-sound-pack
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1 @@
|
|||
https://opengameart.org/content/interface-sounds-starter-pack
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package xyz.valnet.engine;
|
||||
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.openal.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
|
|
@ -14,6 +15,8 @@ import static org.lwjgl.opengl.GL11.*;
|
|||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
import static org.lwjgl.openal.ALC10.*;
|
||||
|
||||
public class App {
|
||||
|
||||
// The window handle
|
||||
|
|
@ -22,6 +25,9 @@ public class App {
|
|||
private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100);
|
||||
public static int mouseX, mouseY;
|
||||
|
||||
public static long audioContext;
|
||||
public static long audioDevice;
|
||||
|
||||
@Deprecated
|
||||
public static boolean mouseLeft, mouseMiddle, mouseRight;
|
||||
|
||||
|
|
@ -37,6 +43,9 @@ public class App {
|
|||
glfwFreeCallbacks(window);
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
alcDestroyContext(audioContext);
|
||||
alcCloseDevice(audioDevice);
|
||||
|
||||
// Terminate GLFW and free the error callback
|
||||
glfwTerminate();
|
||||
glfwSetErrorCallback(null).free();
|
||||
|
|
@ -122,6 +131,20 @@ public class App {
|
|||
// Enable v-sync
|
||||
glfwSwapInterval(1);
|
||||
|
||||
// Audio device
|
||||
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
|
||||
audioDevice = alcOpenDevice(defaultDeviceName);
|
||||
int[] attributes = {0};
|
||||
audioContext = alcCreateContext(audioDevice, attributes);
|
||||
alcMakeContextCurrent(audioContext);
|
||||
|
||||
ALCCapabilities alcCapabilities = ALC.createCapabilities(audioDevice);
|
||||
ALCapabilities alCapabilities = AL.createCapabilities(alcCapabilities);
|
||||
|
||||
if(!alCapabilities.OpenAL10) {
|
||||
System.err.println("Audio not supported?!");
|
||||
}
|
||||
|
||||
// Make the window visible
|
||||
glfwShowWindow(window);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,105 @@
|
|||
package xyz.valnet.engine.sound;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.ShortBuffer;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.PointerBuffer;
|
||||
import static org.lwjgl.openal.AL10.*;
|
||||
|
||||
import static org.lwjgl.stb.STBVorbis.*;
|
||||
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.memFree;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
public class Sound {
|
||||
|
||||
private int bufferId;
|
||||
private int sourceId;
|
||||
|
||||
private boolean loops = false;
|
||||
|
||||
// private float volume = 1.0f;
|
||||
|
||||
public Sound setVolume(float vol) {
|
||||
alSourcef(sourceId, AL_GAIN, vol);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Sound(String path) {
|
||||
|
||||
try (MemoryStack stack = stackPush()) {
|
||||
IntBuffer channelsBuffer = stack.mallocInt(1); // int*
|
||||
IntBuffer sampleRateBuffer = stack.mallocInt(1); // int*
|
||||
|
||||
ShortBuffer rawAudioBuffer = stb_vorbis_decode_filename(path, channelsBuffer, sampleRateBuffer);
|
||||
|
||||
if(rawAudioBuffer == null) {
|
||||
System.err.println("SOUND FAILED BRUV");
|
||||
return;
|
||||
}
|
||||
|
||||
int channels = channelsBuffer.get();
|
||||
int samplerate = sampleRateBuffer.get();
|
||||
|
||||
int format = -1;
|
||||
if(channels == 1) {
|
||||
format = AL_FORMAT_MONO16;
|
||||
} else if (channels == 2) {
|
||||
format = AL_FORMAT_STEREO16;
|
||||
}
|
||||
|
||||
bufferId = alGenBuffers();
|
||||
alBufferData(bufferId, format, rawAudioBuffer, samplerate);
|
||||
|
||||
sourceId = alGenSources();
|
||||
|
||||
alSourcei(sourceId, AL_BUFFER, bufferId);
|
||||
alSourcei(sourceId, AL_LOOPING, loops ? 1 : 0);
|
||||
alSourcei(sourceId, AL_POSITION, 0);
|
||||
alSourcef(sourceId, AL_GAIN, 1.0f);
|
||||
|
||||
memFree(rawAudioBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void play() {
|
||||
int state = alGetSourcei(sourceId, AL_SOURCE_STATE);
|
||||
if(state == AL_PLAYING) {
|
||||
alSourceStop(sourceId);
|
||||
alSourcei(sourceId, AL_POSITION, 0);
|
||||
}
|
||||
alSourcePlay(sourceId);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
int state = alGetSourcei(sourceId, AL_SOURCE_STATE);
|
||||
if(state == AL_PLAYING) {
|
||||
alSourceStop(sourceId);
|
||||
alSourcei(sourceId, AL_POSITION, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPlaying() {
|
||||
return alGetSourcei(sourceId, AL_SOURCE_STATE) == AL_PLAYING;
|
||||
}
|
||||
|
||||
// public static void start() {
|
||||
// FileInputStream fileStream = new FileInputStream("res/sounds/p0ss/interface-sounds/appear-online.ogg");
|
||||
// IntBuffer samples = readOffSamplesToBuffer(fileStream);
|
||||
// }
|
||||
|
||||
// private static IntBuffer readOffSamplesToBuffer(FileInputStream fileStream) {
|
||||
// // This theoretical class is what I need – something that
|
||||
// // can read the data from the OGG file.
|
||||
// STBVorbis.info
|
||||
// OggReader reader = new OggReader(fileStream);
|
||||
|
||||
// // Very simplified example
|
||||
// ShortBuffer buffer = BufferUtils.createIntBuffer(reader.getSampleCount());
|
||||
// buffer.put(reader.samplesAsShortArray());
|
||||
// return buffer;
|
||||
// }
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import xyz.valnet.engine.scenegraph.ITransient;
|
|||
import xyz.valnet.hadean.gameobjects.Camera;
|
||||
import xyz.valnet.hadean.interfaces.BuildableMetadata;
|
||||
import xyz.valnet.hadean.interfaces.IBuildLayerListener;
|
||||
import xyz.valnet.hadean.util.Assets;
|
||||
import xyz.valnet.hadean.util.Layers;
|
||||
|
||||
public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransient {
|
||||
|
|
@ -84,6 +85,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransi
|
|||
@Override
|
||||
public void mouseDown(int button) {
|
||||
if(button == 1 && active && hovered) {
|
||||
Assets.sndCancel.play();
|
||||
listener.cancel();
|
||||
} else if(button == 0 && active && hovered) {
|
||||
// TODO this conversion in negative numbers definitely works wrong.
|
||||
|
|
|
|||
|
|
@ -132,6 +132,10 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr
|
|||
}
|
||||
|
||||
private void broadcastSelectionChanged() {
|
||||
Assets.sndSelectionChanged.play();
|
||||
// if(selected.size() > 0) Assets.sndBubble.play();
|
||||
// if(selected.size() == 0) Assets.sndCancel.play();
|
||||
|
||||
for(ISelectionChangeListener listener : listeners) {
|
||||
listener.selectionChanged(selected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,6 +121,7 @@ public class Button extends GameObject implements IMouseCaptureArea, ITransient
|
|||
|
||||
if(state == IDLE) {
|
||||
if(hovered) {
|
||||
Assets.sndGlassTap.play();
|
||||
state = HOVER;
|
||||
} else if (mouseDown) {
|
||||
state = INACTIVE;
|
||||
|
|
@ -139,8 +140,13 @@ public class Button extends GameObject implements IMouseCaptureArea, ITransient
|
|||
if(!hovered) {
|
||||
state = ACTIVE_NO_HOVER;
|
||||
} else if(!mouseDown) {
|
||||
state = IDLE;
|
||||
if(!hovered) {
|
||||
state = IDLE;
|
||||
} else {
|
||||
state = HOVER;
|
||||
}
|
||||
listener.click(this);
|
||||
Assets.sndBubble.play();
|
||||
}
|
||||
} else if (state == ACTIVE_NO_HOVER) {
|
||||
if(hovered) {
|
||||
|
|
|
|||
|
|
@ -10,9 +10,15 @@ import xyz.valnet.engine.graphics.Tile16;
|
|||
import xyz.valnet.engine.graphics.Tile9;
|
||||
import xyz.valnet.engine.math.Vector4i;
|
||||
import xyz.valnet.engine.shaders.SimpleShader;
|
||||
import xyz.valnet.engine.sound.Sound;
|
||||
|
||||
public class Assets {
|
||||
|
||||
|
||||
public static final Sound sndBubble;
|
||||
public static final Sound sndCancel;
|
||||
public static final Sound sndGlassTap;
|
||||
public static final Sound sndSelectionChanged;
|
||||
|
||||
public static final Texture atlas;
|
||||
public static final Font font;
|
||||
public static final Font miniFont;
|
||||
|
|
@ -308,6 +314,12 @@ public class Assets {
|
|||
|
||||
wall = new Tile16(atlas, new Vector4i(0, 17 * 8, 32, 32));
|
||||
|
||||
sndSelectionChanged = new Sound("res/sounds/leohpaz/retro-rpg-menu-sounds/079_Buy_sell_01.ogg");
|
||||
sndBubble = new Sound("res/sounds/p0ss/interface-sounds/appear-online.ogg");
|
||||
// sndCancel = new Sound("res/sounds/Listener/botton-sound-pack/cancel.ogg").setVolume(2f);
|
||||
sndCancel = new Sound("res/sounds/leohpaz/retro-rpg-menu-sounds/098_Unpause_04.ogg").setVolume(0.8f);
|
||||
sndGlassTap = new Sound("res/sounds/p0ss/interface-sounds/click5.ogg").setVolume(0.2f);
|
||||
|
||||
System.out.println("END ASSETS");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue