Skip to content

Commit 5284ab1

Browse files
committed
Added Music
1 parent a2f0644 commit 5284ab1

File tree

9 files changed

+164
-29
lines changed

9 files changed

+164
-29
lines changed

.idea/workspace.xml

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/Game/Zombie.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ObsidianEngine.render.Texture;
66
import ObsidianEngine.utils.FileUtils;
77
import ObsidianEngine.utils.MathUtils;
8+
import org.joml.Vector2f;
89
import org.joml.Vector3f;
910

1011
import java.util.ArrayList;
@@ -22,11 +23,8 @@ public Zombie(Vector3f StartPosiition, float Radius, int Wave){
2223
this.mesh = Zombie;
2324
this.WaveSpawnedOn = Wave;
2425

25-
System.out.println("Spawning Zombies @ " + StartPosiition);
26-
27-
float X = (StartPosiition.x) + (MathUtils.getRandomPosNegHalf() * (Radius));
28-
float Y = (StartPosiition.y) + (MathUtils.getRandomPosNegHalf() * (Radius));
29-
Zombie.setPosition(X,0,Y);
26+
Vector2f RandomPoint = MathUtils.getRandomPointOnRadius(StartPosiition.x,StartPosiition.z,Radius);
27+
Zombie.setPosition(RandomPoint.x,0,RandomPoint.y);
3028
Dead = false;
3129
}
3230

src/main/java/ObsidianEngine/entity/Mesh.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.lwjgl.opengl.GL15.*;
2323
import static org.lwjgl.opengl.GL30.*;
2424

25-
public class Mesh {
25+
public class Mesh implements Cloneable {
2626
private Vector3f[] vertices;
2727
private int[] indices;
2828
private float[] UVs;
@@ -268,4 +268,9 @@ public boolean CloseEnough(Vector3f otherPosition){
268268
float Dist = Position.distance(otherPosition);
269269
return Dist < 50;
270270
}
271+
272+
public Object clone() throws CloneNotSupportedException
273+
{
274+
return super.clone();
275+
}
271276
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ObsidianEngine.io;
2+
3+
4+
import org.lwjgl.openal.AL;
5+
import org.lwjgl.openal.ALC;
6+
import org.lwjgl.openal.ALCCapabilities;
7+
import org.lwjgl.openal.ALCapabilities;
8+
9+
import static org.lwjgl.openal.ALC10.*;
10+
import static org.lwjgl.openal.ALC11.*;
11+
import static org.lwjgl.openal.ALC.*;
12+
13+
public class Audio {
14+
public static Audio AudioManager;
15+
private long AudioContext;
16+
private long AudioDevice;
17+
18+
public Audio(){
19+
String defaultDeviceName = alcGetString(0,ALC_DEVICE_SPECIFIER);
20+
AudioDevice = alcOpenDevice(defaultDeviceName);
21+
AudioContext = alcCreateContext(AudioDevice,new int[] {0});
22+
alcMakeContextCurrent(AudioContext);
23+
24+
ALCCapabilities capabilitiesA = createCapabilities(AudioDevice);
25+
ALCapabilities capabilitiesB = AL.createCapabilities(capabilitiesA);
26+
27+
if(!AL.getCapabilities().OpenAL10) assert false : "Audio Library not supported.";
28+
}
29+
30+
public static Audio getAudioManager(){
31+
if(AudioManager == null) AudioManager = new Audio();
32+
return AudioManager;
33+
}
34+
35+
public void Destroy(){
36+
alcDestroyContext(AudioContext);
37+
alcCloseDevice(AudioDevice);
38+
}
39+
}

src/main/java/ObsidianEngine/io/Window.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
import ObsidianEngine.entity.Mesh;
88
import ObsidianEngine.render.Camera;
99
import ObsidianEngine.render.Shader;
10-
import ObsidianEngine.render.Texture;
1110
import ObsidianEngine.ui.UIRenderer;
12-
import ObsidianEngine.utils.ColorUtils;
13-
import ObsidianEngine.utils.FileUtils;
14-
import ObsidianEngine.utils.MouseUtils;
15-
import ObsidianEngine.utils.TimeUtils;
11+
import ObsidianEngine.utils.*;
1612
import org.joml.Vector3f;
1713
import org.lwjgl.BufferUtils;
1814
import org.lwjgl.Version;
@@ -26,7 +22,6 @@
2622

2723
import java.nio.IntBuffer;
2824
import java.util.ArrayList;
29-
import java.util.concurrent.TimeUnit;
3025

3126
public class Window {
3227
private int width, height;
@@ -35,6 +30,7 @@ public class Window {
3530
private long GLFWWindow;
3631
private TimeUtils time;
3732
private Input inputSystem;
33+
private Sound mainMusic;
3834

3935
private static Window window = null;
4036
private Camera cam;
@@ -110,6 +106,9 @@ private void init(){
110106
//Display Window
111107
GLFW.glfwShowWindow(GLFWWindow);
112108

109+
//INIT AUDIO LIBRARY
110+
Audio.getAudioManager();
111+
113112
//LWJGL -> Binding To OPENGL & GLFW
114113
GL.createCapabilities();
115114
glMatrixMode(GL_PROJECTION);
@@ -124,6 +123,11 @@ private void init(){
124123
//Post Window Init CoreSystems
125124
inputSystem = new Input(GLFWWindow);
126125

126+
//Create Audio Context + Load Audio File
127+
Audio.getAudioManager();
128+
mainMusic = new Sound(FileUtils.getJarLoc() + "/sounds/MainTheme.ogg",true);
129+
mainMusic.play();
130+
127131
//Post OpenGL Init
128132
//Init Default Shader
129133
Shader.defaultShader = new Shader("/shaders/mainVertex.glsl","/shaders/mainFragment.glsl");
@@ -134,7 +138,7 @@ private void init(){
134138

135139
//Starting Meshes & Models
136140
//Load Map [MULTI-SPRTIE]
137-
Map.getMap(550,50,MapPieces,cam,GLFWWindow,(title + " " + versionNumber));
141+
Map.getMap(800,50,MapPieces,cam,GLFWWindow,(title + " " + versionNumber));
138142

139143
/* SINGLE COLOR MAP
140144
Mesh map = new Plane(15000,15000,new Vector3f(0,0,0));
@@ -200,7 +204,7 @@ private void updateZombies(float Delta){
200204
//All Zombies Dead / New Wave
201205
Wave += 1;
202206
for(int i = 0; i < Math.pow(Wave,2); i++){
203-
Zombies.add(new Zombie(cam.getPosition(),350,Wave));
207+
Zombies.add(new Zombie(new Vector3f(-30,0,0),350 * ((Wave/2) <= 1 ? 1 : Wave),Wave));
204208
}
205209
}
206210
}
@@ -248,12 +252,10 @@ private void loop() {
248252
float deltaTime = getDelta();
249253
float pDelta = (deltaTime/500);
250254

251-
/*
252255
if(Input.getKeyDown(GLFW_KEY_W)) Player.setPosition(0,0,-(0.05f)*pDelta);
253256
if(Input.getKeyDown(GLFW_KEY_S)) Player.setPosition(0,0,(0.05f)*pDelta);
254257
if(Input.getKeyDown(GLFW_KEY_A)) Player.setPosition(-(0.05f)*pDelta,0,0);
255258
if(Input.getKeyDown(GLFW_KEY_D)) Player.setPosition((0.05f)*pDelta,0,0);
256-
*/
257259

258260
float BulletDelay = (System.currentTimeMillis() - LastTimeShot);
259261

@@ -284,6 +286,7 @@ private void loop() {
284286

285287
//Cleanup
286288
inputSystem.destroy(GLFWWindow);
289+
Audio.getAudioManager().Destroy();
287290
GLFW.glfwDestroyWindow(GLFWWindow);
288291
}
289292
}

src/main/java/ObsidianEngine/utils/MathUtils.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ObsidianEngine.utils;
22

33
import org.joml.Matrix4f;
4+
import org.joml.Vector2f;
45
import org.joml.Vector3f;
56
import org.joml.Vector3fc;
67

@@ -23,10 +24,11 @@ public static float getRandomNumber(){
2324
return (float)(random.nextDouble() * 2 - 1);
2425
}
2526

26-
public static float getRandomPosNegHalf(){
27-
float PN = getRandomNumber();
28-
if(PN > 0.5) return 1;
29-
if(PN < -0.5) return -1;
30-
else return 0.5f;
27+
public static Vector2f getRandomPointOnRadius(float centerX,float centerY,float radius){
28+
Random random = new Random();
29+
double angle = random.nextDouble() * 2 * Math.PI;
30+
double x = centerX + radius * Math.cos(angle);
31+
double y = centerY + radius * Math.sin(angle);
32+
return new Vector2f((float) x,(float) y);
3133
}
3234
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package ObsidianEngine.utils;
2+
3+
import java.nio.IntBuffer;
4+
import java.nio.ShortBuffer;
5+
6+
import static org.lwjgl.openal.AL10.*;
7+
import static org.lwjgl.stb.STBVorbis.stb_vorbis_decode_filename;
8+
import static org.lwjgl.system.MemoryStack.*;
9+
import static org.lwjgl.system.libc.LibCStdlib.free;
10+
11+
public class Sound {
12+
private int bufferId;
13+
private int sourceId;
14+
private String filePath;
15+
private boolean isPlaying = false;
16+
17+
public Sound(String filePath, boolean loops){
18+
this.filePath = filePath;
19+
20+
stackPush();
21+
IntBuffer channelsB = stackMallocInt(1);
22+
stackPush();
23+
IntBuffer sampleRateB = stackMallocInt(1);
24+
25+
ShortBuffer rawAudio = stb_vorbis_decode_filename(filePath,channelsB,sampleRateB);
26+
27+
if(rawAudio == null){
28+
System.out.println("[SOUND NOT FOUND] " + filePath);
29+
stackPop();
30+
stackPop();
31+
return;
32+
}
33+
34+
int channels = channelsB.get();
35+
int sampleRate = sampleRateB.get();
36+
37+
stackPop();
38+
stackPop();
39+
40+
int format = -1;
41+
if(channels == 1){
42+
format = AL_FORMAT_MONO16;
43+
}
44+
else if(channels == 2){
45+
format = AL_FORMAT_STEREO16;
46+
}
47+
48+
bufferId = alGenBuffers();
49+
alBufferData(bufferId,format,rawAudio,sampleRate);
50+
51+
sourceId = alGenSources();
52+
alSourcei(sourceId,AL_BUFFER,bufferId);
53+
alSourcei(sourceId,AL_LOOPING,loops ? 1 : 0);
54+
alSourcei(sourceId,AL_POSITION,0);
55+
alSourcef(sourceId,AL_GAIN,0.3f);
56+
57+
free(rawAudio);
58+
}
59+
60+
public void delete(){
61+
alDeleteBuffers(bufferId);
62+
alDeleteSources(sourceId);
63+
}
64+
65+
public void play(){
66+
int state = alGetSourcei(sourceId,AL_SOURCE_STATE);
67+
if(state == AL_STOPPED){
68+
isPlaying = false;
69+
alSourcei(sourceId,AL_POSITION,0);
70+
}
71+
72+
if(!isPlaying){
73+
alSourcePlay(sourceId);
74+
isPlaying = true;
75+
}
76+
}
77+
78+
public void stop(){
79+
if(isPlaying){
80+
alSourceStop(sourceId);
81+
isPlaying = false;
82+
}
83+
}
84+
}
380 KB
Binary file not shown.
9.38 MB
Binary file not shown.

0 commit comments

Comments
 (0)