started sounds!

bottom-bar
Ivory 2023-01-11 02:54:14 -05:00
parent 3d0754f060
commit 807e6a2a49
34 changed files with 156 additions and 2 deletions

View File

@ -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.

View File

@ -0,0 +1 @@
https://opengameart.org/content/interface-sounds-starter-pack

View File

@ -1,6 +1,7 @@
package xyz.valnet.engine; package xyz.valnet.engine;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
import org.lwjgl.openal.*;
import org.lwjgl.opengl.*; import org.lwjgl.opengl.*;
import org.lwjgl.system.*; 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.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*; import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.openal.ALC10.*;
public class App { public class App {
// The window handle // The window handle
@ -22,6 +25,9 @@ public class App {
private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100); private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100);
public static int mouseX, mouseY; public static int mouseX, mouseY;
public static long audioContext;
public static long audioDevice;
@Deprecated @Deprecated
public static boolean mouseLeft, mouseMiddle, mouseRight; public static boolean mouseLeft, mouseMiddle, mouseRight;
@ -37,6 +43,9 @@ public class App {
glfwFreeCallbacks(window); glfwFreeCallbacks(window);
glfwDestroyWindow(window); glfwDestroyWindow(window);
alcDestroyContext(audioContext);
alcCloseDevice(audioDevice);
// Terminate GLFW and free the error callback // Terminate GLFW and free the error callback
glfwTerminate(); glfwTerminate();
glfwSetErrorCallback(null).free(); glfwSetErrorCallback(null).free();
@ -122,6 +131,20 @@ public class App {
// Enable v-sync // Enable v-sync
glfwSwapInterval(1); 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 // Make the window visible
glfwShowWindow(window); glfwShowWindow(window);

View File

@ -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;
// }
}

View File

@ -9,6 +9,7 @@ import xyz.valnet.engine.scenegraph.ITransient;
import xyz.valnet.hadean.gameobjects.Camera; import xyz.valnet.hadean.gameobjects.Camera;
import xyz.valnet.hadean.interfaces.BuildableMetadata; import xyz.valnet.hadean.interfaces.BuildableMetadata;
import xyz.valnet.hadean.interfaces.IBuildLayerListener; import xyz.valnet.hadean.interfaces.IBuildLayerListener;
import xyz.valnet.hadean.util.Assets;
import xyz.valnet.hadean.util.Layers; import xyz.valnet.hadean.util.Layers;
public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransient { public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransient {
@ -84,6 +85,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransi
@Override @Override
public void mouseDown(int button) { public void mouseDown(int button) {
if(button == 1 && active && hovered) { if(button == 1 && active && hovered) {
Assets.sndCancel.play();
listener.cancel(); listener.cancel();
} else if(button == 0 && active && hovered) { } else if(button == 0 && active && hovered) {
// TODO this conversion in negative numbers definitely works wrong. // TODO this conversion in negative numbers definitely works wrong.

View File

@ -132,6 +132,10 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr
} }
private void broadcastSelectionChanged() { private void broadcastSelectionChanged() {
Assets.sndSelectionChanged.play();
// if(selected.size() > 0) Assets.sndBubble.play();
// if(selected.size() == 0) Assets.sndCancel.play();
for(ISelectionChangeListener listener : listeners) { for(ISelectionChangeListener listener : listeners) {
listener.selectionChanged(selected); listener.selectionChanged(selected);
} }

View File

@ -121,6 +121,7 @@ public class Button extends GameObject implements IMouseCaptureArea, ITransient
if(state == IDLE) { if(state == IDLE) {
if(hovered) { if(hovered) {
Assets.sndGlassTap.play();
state = HOVER; state = HOVER;
} else if (mouseDown) { } else if (mouseDown) {
state = INACTIVE; state = INACTIVE;
@ -139,8 +140,13 @@ public class Button extends GameObject implements IMouseCaptureArea, ITransient
if(!hovered) { if(!hovered) {
state = ACTIVE_NO_HOVER; state = ACTIVE_NO_HOVER;
} else if(!mouseDown) { } else if(!mouseDown) {
state = IDLE; if(!hovered) {
state = IDLE;
} else {
state = HOVER;
}
listener.click(this); listener.click(this);
Assets.sndBubble.play();
} }
} else if (state == ACTIVE_NO_HOVER) { } else if (state == ACTIVE_NO_HOVER) {
if(hovered) { if(hovered) {

View File

@ -10,9 +10,15 @@ import xyz.valnet.engine.graphics.Tile16;
import xyz.valnet.engine.graphics.Tile9; import xyz.valnet.engine.graphics.Tile9;
import xyz.valnet.engine.math.Vector4i; import xyz.valnet.engine.math.Vector4i;
import xyz.valnet.engine.shaders.SimpleShader; import xyz.valnet.engine.shaders.SimpleShader;
import xyz.valnet.engine.sound.Sound;
public class Assets { 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 Texture atlas;
public static final Font font; public static final Font font;
public static final Font miniFont; public static final Font miniFont;
@ -308,6 +314,12 @@ public class Assets {
wall = new Tile16(atlas, new Vector4i(0, 17 * 8, 32, 32)); 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"); System.out.println("END ASSETS");
} }
} }