Skip to content

Commit 9070063

Browse files
Add an enhanced Sound class
1 parent 47ed22b commit 9070063

File tree

5 files changed

+241
-23
lines changed

5 files changed

+241
-23
lines changed

assets/test.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ local Paths = luajava.bindClass("me.stringfromjava.funkin.util.Paths")
55
local Sprite = luajava.bindClass("com.badlogic.gdx.graphics.g2d.Sprite")
66
local Texture = luajava.bindClass("com.badlogic.gdx.graphics.Texture")
77

8+
-- TODO: make this stay persistent!
9+
local runCount = 0
10+
local hasRan = false
11+
12+
if not hasRan and runCount == 0 then
13+
hasRan = true
14+
runCount = runCount + 1
15+
end
16+
17+
print(runCount)
18+
819
local TitleScreen = Funkin.screen
920

1021
local path = Paths:image('transitionSwag/stickers-set-1/bfSticker1')
@@ -33,6 +44,13 @@ local runnable = luajava.createProxy('java.lang.Runnable', {
3344
run = onPostRender
3445
})
3546

47+
local runnableOnClose = luajava.createProxy('java.lang.Runnable', {
48+
run = function()
49+
print('the game closed lol')
50+
end
51+
})
52+
3653
Funkin.Signals.postRender:add(runnable)
54+
Funkin.Signals.postGameClose:add(runnableOnClose)
3755

3856
Funkin:playSound('shared/sounds/ANGRY_TEXT_BOX.ogg')

core/src/main/java/me/stringfromjava/funkin/Funkin.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.badlogic.gdx.Gdx;
44
import com.badlogic.gdx.audio.Music;
55
import com.badlogic.gdx.audio.Sound;
6+
import me.stringfromjava.funkin.audio.FunkinSound;
67
import me.stringfromjava.funkin.backend.display.FunkinScreen;
78
import me.stringfromjava.funkin.lua.FunkinLua;
89
import me.stringfromjava.funkin.tween.FunkinTween;
@@ -104,14 +105,14 @@ public static void setScreen(FunkinScreen screen) {
104105
* Plays a sound. (Duh.)
105106
*
106107
* @param path The path to play the sound from.
107-
* @return The sound instance itself.
108+
* @return The sound instance itself, as a {@link FunkinSound}.
108109
*/
109-
public static Sound playSound(String path) {
110-
Sound sound = Gdx.audio.newSound(Paths.asset(path));
111-
long id = sound.play();
112-
if (id != -1) { // libGDX will return -1 if the sound fails to play.
113-
soundPool.put(id, sound);
110+
public static FunkinSound playSound(String path) {
111+
FunkinSound sound = new FunkinSound(path);
112+
if (sound.ID != -1) { // libGDX will return -1 if the sound fails to play.
113+
soundPool.put(sound.ID, sound);
114114
}
115+
sound.play();
115116
return sound;
116117
}
117118

@@ -170,6 +171,10 @@ public static FunkinGame getGame() {
170171
* <p>
171172
* This includes anything from the screen being switched, the game updating every frame, and
172173
* just about everything you can think of.
174+
* <p>
175+
* IMPORTANT DETAIL!: Anything with the {@code pre} and {@code post} prefixes always mean the same thing.
176+
* If a signal has {@code pre}, then the signal gets ran BEFORE any functionality is executed, and {@code post}
177+
* means AFTER all functionality was executed.
173178
*/
174179
public static class Signals {
175180

@@ -179,6 +184,7 @@ public static class Signals {
179184
public static final FunkinSignal postScreenSwitch = new FunkinSignal();
180185
public static final FunkinSignal preGameClose = new FunkinSignal();
181186
public static final FunkinSignal postGameClose = new FunkinSignal();
187+
public static final FunkinSignal soundPlayed = new FunkinSignal();
182188

183189
private Signals() {
184190
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package me.stringfromjava.funkin.audio;
2+
3+
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.audio.Sound;
5+
import com.badlogic.gdx.files.FileHandle;
6+
import me.stringfromjava.funkin.Funkin;
7+
import me.stringfromjava.funkin.util.Paths;
8+
9+
/**
10+
* An enhanced version of libGDX's {@link Sound}.
11+
*/
12+
public class FunkinSound implements Sound {
13+
14+
/**
15+
* The ID of {@code this} specific sound.
16+
*/
17+
public final long ID;
18+
19+
private Sound thisSound;
20+
private float volume;
21+
private float pitch;
22+
private float pan;
23+
private boolean looping;
24+
private boolean isPaused;
25+
26+
public FunkinSound(String path) {
27+
this(Paths.asset(path));
28+
}
29+
30+
public FunkinSound(FileHandle path) {
31+
thisSound = Gdx.audio.newSound(path);
32+
ID = thisSound.play();
33+
volume = 1.0f;
34+
pitch = 1.0f;
35+
pan = 0.0f;
36+
looping = false;
37+
isPaused = false;
38+
thisSound.stop();
39+
}
40+
41+
@Override
42+
public long play() {
43+
return thisSound.play();
44+
}
45+
46+
@Override
47+
public long play(float volume) {
48+
return play(volume, 1.0f, 0.0f);
49+
}
50+
51+
public long play(float volume, float pitch) {
52+
return play(volume, pitch, 0.0f);
53+
}
54+
55+
@Override
56+
public long play(float volume, float pitch, float pan) {
57+
Funkin.Signals.soundPlayed.dispatch();
58+
this.volume = volume * Funkin.masterVolume;
59+
this.pitch = pitch;
60+
this.pan = pan;
61+
this.looping = false;
62+
this.isPaused = false;
63+
return thisSound.play(volume, pitch, pan);
64+
}
65+
66+
@Override
67+
public long loop() {
68+
return loop(1.0f, 1.0f, 0.0f);
69+
}
70+
71+
@Override
72+
public long loop(float volume) {
73+
return loop(volume, 1.0f, 0.0f);
74+
}
75+
76+
public long loop(float volume, float pitch) {
77+
return loop(volume, pitch, 0.0f);
78+
}
79+
80+
@Override
81+
public long loop(float volume, float pitch, float pan) {
82+
Funkin.Signals.soundPlayed.dispatch();
83+
this.volume = volume * Funkin.masterVolume;
84+
this.pitch = pitch;
85+
this.pan = pan;
86+
this.looping = true;
87+
this.isPaused = false;
88+
return thisSound.loop(volume, pitch, pan);
89+
}
90+
91+
@Override
92+
public void stop() {
93+
isPaused = false;
94+
thisSound.stop();
95+
}
96+
97+
@Override
98+
public void pause() {
99+
isPaused = true;
100+
thisSound.pause();
101+
}
102+
103+
@Override
104+
public void resume() {
105+
isPaused = false;
106+
thisSound.resume();
107+
}
108+
109+
@Override
110+
public void dispose() {
111+
volume = 1.0f;
112+
pitch = 1.0f;
113+
pan = 0.0f;
114+
looping = false;
115+
isPaused = false;
116+
thisSound.dispose();
117+
}
118+
119+
@Override
120+
public void stop(long soundId) {
121+
isPaused = false;
122+
thisSound.stop();
123+
}
124+
125+
@Override
126+
public void pause(long soundId) {
127+
isPaused = true;
128+
thisSound.pause(soundId);
129+
}
130+
131+
@Override
132+
public void resume(long soundId) {
133+
isPaused = false;
134+
thisSound.resume();
135+
}
136+
137+
@Override
138+
public void setLooping(long soundId, boolean looping) {
139+
this.looping = looping;
140+
thisSound.setLooping(soundId, looping);
141+
}
142+
143+
@Override
144+
public void setPitch(long soundId, float pitch) {
145+
this.pitch = pitch;
146+
thisSound.setPitch(soundId, pitch);
147+
}
148+
149+
@Override
150+
public void setVolume(long soundId, float volume) {
151+
float v = volume * Funkin.masterVolume;
152+
this.volume = v;
153+
thisSound.setVolume(soundId, v);
154+
}
155+
156+
public void setPan(long soundId, float pan) {
157+
setPan(soundId, pan, 1.0f);
158+
}
159+
160+
@Override
161+
public void setPan(long soundId, float pan, float volume) {
162+
thisSound.setPan(soundId, pan, volume);
163+
}
164+
165+
public float getVolume() {
166+
return volume;
167+
}
168+
169+
public float getPitch() {
170+
return pitch;
171+
}
172+
173+
public float getPan() {
174+
return pan;
175+
}
176+
177+
public boolean isLooping() {
178+
return looping;
179+
}
180+
181+
public boolean isPaused() {
182+
return isPaused;
183+
}
184+
}

core/src/main/java/me/stringfromjava/funkin/backend/display/FunkinScreen.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,31 @@
2020
*/
2121
public abstract class FunkinScreen implements Screen {
2222

23-
/**
24-
* The {@link SpriteBatch} used to render
25-
* sprites in the current screen.
26-
*/
27-
protected SpriteBatch spriteBatch;
28-
2923
// TODO: Create a way to add more than one camera!
3024
/**
3125
* The {@link OrthographicCamera} used to see the world.
3226
*/
33-
protected OrthographicCamera camera;
27+
public OrthographicCamera camera;
3428

3529
/**
3630
* The current {@link Viewport} of {@code this} current screen.
3731
*/
38-
protected Viewport viewport;
32+
public Viewport viewport;
3933

4034
/**
4135
* The background color of {@code this} current screen.
4236
*/
43-
protected Color bgColor;
37+
public Color bgColor;
38+
39+
/**
40+
* All display objects that are shown in {@code this} screen.
41+
*/
42+
public final ArrayList<Sprite> members = new ArrayList<>();
4443

45-
public final ArrayList<Sprite> sprites = new ArrayList<>();
44+
/**
45+
* The {@link SpriteBatch} used to render sprites in the current screen.
46+
*/
47+
protected SpriteBatch spriteBatch;
4648

4749
@Override
4850
public void show() {
@@ -65,10 +67,9 @@ public void render(float delta) {
6567
camera.update();
6668
spriteBatch.setProjectionMatrix(camera.combined);
6769

68-
// Draw the background color.
6970
spriteBatch.begin();
7071

71-
for (Sprite s : sprites) {
72+
for (Sprite s : members) {
7273
s.draw(spriteBatch);
7374
}
7475

@@ -92,7 +93,7 @@ public void hide() {}
9293
@Override
9394
public void dispose() {
9495
spriteBatch.dispose();
95-
for (Sprite s : sprites) {
96+
for (Sprite s : members) {
9697
Texture texture = s.getTexture();
9798
if (texture != null) {
9899
texture.dispose();
@@ -109,7 +110,7 @@ public void dispose() {
109110
*/
110111
public void add(Sprite s) {
111112
if (s != null) {
112-
sprites.add(s);
113+
members.add(s);
113114
}
114115
}
115116
}

core/src/main/java/me/stringfromjava/funkin/game/menus/TitleScreen.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
import com.badlogic.gdx.Gdx;
44
import com.badlogic.gdx.Input;
5-
import com.badlogic.gdx.graphics.Texture;
65
import com.badlogic.gdx.graphics.g2d.Sprite;
76
import me.stringfromjava.funkin.Funkin;
7+
import me.stringfromjava.funkin.audio.FunkinSound;
88
import me.stringfromjava.funkin.backend.display.FunkinScreen;
99
import me.stringfromjava.funkin.lua.FunkinLua;
10-
import me.stringfromjava.funkin.util.Paths;
1110

1211
public class TitleScreen extends FunkinScreen {
1312

1413
private Sprite logo;
14+
private FunkinSound tickleFight;
1515

1616
@Override
1717
public void show() {
1818
super.show();
19-
// Funkin.playSound("shared/sounds/tickleFight.ogg");
19+
tickleFight = Funkin.playSound("shared/sounds/tickleFight.ogg");
2020
Funkin.playMusic("preload/music/freakyMenu/freakyMenu.ogg", 0.5f);
2121
}
2222

@@ -27,5 +27,14 @@ public void render(float delta) {
2727
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
2828
FunkinLua.executeScript("test.lua");
2929
}
30+
31+
if (Gdx.input.isKeyJustPressed(Input.Keys.R)) {
32+
System.out.println(tickleFight.isPaused());
33+
if (tickleFight.isPaused()) {
34+
tickleFight.resume();
35+
} else {
36+
tickleFight.pause();
37+
}
38+
}
3039
}
3140
}

0 commit comments

Comments
 (0)