Implemented volume-bg and master volume setting.

- All sounds are now multiplied by a master volume setting.
- Changed all default volume levels.
- Scrolling in the main menu and game states changes the master volume and displays a volume bar on the right side of the screen.
- "volume-bg.png" image by @kouyang.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-20 14:52:02 -05:00
parent f9c0794693
commit 3b13cc794b
12 changed files with 150 additions and 10 deletions

BIN
res/volume-bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

View File

@ -208,6 +208,12 @@ public enum GameImage {
SCORE_X ("score-x", "png"), SCORE_X ("score-x", "png"),
// Non-Game Components // Non-Game Components
VOLUME ("volume-bg", "png", false, false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.3f) / img.getHeight());
}
},
MENU_BACK ("menu-back", "png", false, false) { MENU_BACK ("menu-back", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {

View File

@ -43,6 +43,7 @@ import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Animation; import org.newdawn.slick.Animation;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer; import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image; import org.newdawn.slick.Image;
import org.newdawn.slick.Input; import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException; import org.newdawn.slick.SlickException;
@ -112,6 +113,16 @@ public class Utils {
cursorX = new LinkedList<Integer>(), cursorX = new LinkedList<Integer>(),
cursorY = new LinkedList<Integer>(); cursorY = new LinkedList<Integer>();
/**
* Time to show volume image, in milliseconds.
*/
private static final int VOLUME_DISPLAY_TIME = 1500;
/**
* Volume display elapsed time.
*/
private static int volumeDisplay = -1;
/** /**
* Set of all Unicode strings already loaded. * Set of all Unicode strings already loaded.
*/ */
@ -517,6 +528,63 @@ public class Utils {
); );
} }
/**
* Draws the volume bar on the middle right-hand side of the game container.
* Only draws if the volume has recently been changed using with {@link #changeVolume(int)}.
* @param g the graphics context
*/
public static void drawVolume(Graphics g) {
if (volumeDisplay == -1)
return;
int width = container.getWidth(), height = container.getHeight();
Image img = GameImage.VOLUME.getImage();
// move image in/out
float xOffset = 0;
float ratio = (float) volumeDisplay / VOLUME_DISPLAY_TIME;
if (ratio <= 0.1f)
xOffset = img.getWidth() * (1 - (ratio * 10f));
else if (ratio >= 0.9f)
xOffset = img.getWidth() * (1 - ((1 - ratio) * 10f));
img.drawCentered(width - img.getWidth() / 2f + xOffset, height / 2f);
float barHeight = img.getHeight() * 0.9f;
float volume = Options.getMasterVolume();
g.setColor(Color.white);
g.fillRoundRect(
width - (img.getWidth() * 0.368f) + xOffset,
(height / 2f) - (img.getHeight() * 0.47f) + (barHeight * (1 - volume)),
img.getWidth() * 0.15f, barHeight * volume, 3
);
}
/**
* Updates volume display by a delta interval.
* @param delta the delta interval since the last call
*/
public static void updateVolumeDisplay(int delta) {
if (volumeDisplay == -1)
return;
volumeDisplay += delta;
if (volumeDisplay > VOLUME_DISPLAY_TIME)
volumeDisplay = -1;
}
/**
* Changes the master volume by a unit (positive or negative).
* @param units the number of units
*/
public static void changeVolume(int units) {
final float UNIT_OFFSET = 0.05f;
Options.setMasterVolume(container, Utils.getBoundedValue(Options.getMasterVolume(), UNIT_OFFSET * units, 0f, 1f));
if (volumeDisplay == -1)
volumeDisplay = 0;
else if (volumeDisplay >= VOLUME_DISPLAY_TIME / 10)
volumeDisplay = VOLUME_DISPLAY_TIME / 10;
}
/** /**
* Takes a screenshot. * Takes a screenshot.
* @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots * @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots

View File

@ -133,7 +133,7 @@ public class MusicController {
*/ */
public static void playAt(final int position, final boolean loop) { public static void playAt(final int position, final boolean loop) {
if (trackExists()) { if (trackExists()) {
SoundStore.get().setMusicVolume(Options.getMusicVolume()); setVolume(Options.getMusicVolume() * Options.getMasterVolume());
player.setPosition(position / 1000f); player.setPosition(position / 1000f);
if (loop) if (loop)
player.loop(); player.loop();
@ -303,7 +303,8 @@ public class MusicController {
* Toggles the volume dim state of the current track. * Toggles the volume dim state of the current track.
*/ */
public static void toggleTrackDimmed() { public static void toggleTrackDimmed() {
setVolume((trackDimmed) ? Options.getMusicVolume() : Options.getMusicVolume() / 3f); float volume = Options.getMusicVolume() * Options.getMasterVolume();
setVolume((trackDimmed) ? volume : volume / 3f);
trackDimmed = !trackDimmed; trackDimmed = !trackDimmed;
} }

View File

@ -169,7 +169,7 @@ public class SoundController {
* @param s the sound effect * @param s the sound effect
*/ */
public static void playSound(SoundComponent s) { public static void playSound(SoundComponent s) {
playClip(s.getClip(), Options.getEffectVolume()); playClip(s.getClip(), Options.getEffectVolume() * Options.getMasterVolume());
} }
/** /**
@ -180,7 +180,7 @@ public class SoundController {
if (hitSound < 0) if (hitSound < 0)
return; return;
float volume = Options.getHitSoundVolume() * sampleVolumeMultiplier; float volume = Options.getHitSoundVolume() * sampleVolumeMultiplier * Options.getMasterVolume();
if (volume == 0f) if (volume == 0f)
return; return;
@ -202,7 +202,7 @@ public class SoundController {
* @param s the hit sound * @param s the hit sound
*/ */
public static void playHitSound(SoundComponent s) { public static void playHitSound(SoundComponent s) {
playClip(s.getClip(), Options.getHitSoundVolume() * sampleVolumeMultiplier); playClip(s.getClip(), Options.getHitSoundVolume() * sampleVolumeMultiplier * Options.getMasterVolume());
} }
/** /**

View File

@ -421,6 +421,7 @@ public class Game extends BasicGameState {
cursorCirclePulse.drawCentered(pausedMouseX, pausedMouseY); cursorCirclePulse.drawCentered(pausedMouseX, pausedMouseY);
} }
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -429,6 +430,7 @@ public class Game extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
skipButton.hoverUpdate(delta, mouseX, mouseY); skipButton.hoverUpdate(delta, mouseX, mouseY);
@ -707,6 +709,11 @@ public class Game extends BasicGameState {
hitObjects[objectIndex].mousePressed(x, y); hitObjects[objectIndex].mousePressed(x, y);
} }
@Override
public void mouseWheelMoved(int newValue) {
Utils.changeVolume((newValue < 0) ? -1 : 1);
}
@Override @Override
public void enter(GameContainer container, StateBasedGame game) public void enter(GameContainer container, StateBasedGame game)
throws SlickException { throws SlickException {

View File

@ -104,6 +104,7 @@ public class GamePauseMenu extends BasicGameState {
retryButton.draw(); retryButton.draw();
backButton.draw(); backButton.draw();
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -112,6 +113,7 @@ public class GamePauseMenu extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
continueButton.hoverUpdate(delta, mouseX, mouseY); continueButton.hoverUpdate(delta, mouseX, mouseY);
retryButton.hoverUpdate(delta, mouseX, mouseY); retryButton.hoverUpdate(delta, mouseX, mouseY);

View File

@ -129,6 +129,7 @@ public class GameRanking extends BasicGameState {
exitButton.draw(); exitButton.draw();
Utils.getBackButton().draw(); Utils.getBackButton().draw();
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -137,6 +138,7 @@ public class GameRanking extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY); Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY);
} }

View File

@ -242,6 +242,7 @@ public class MainMenu extends BasicGameState {
new SimpleDateFormat("h:mm a").format(new Date())), new SimpleDateFormat("h:mm a").format(new Date())),
marginX, height - marginY - lineHeight); marginX, height - marginY - lineHeight);
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -250,6 +251,7 @@ public class MainMenu extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
logo.hoverUpdate(delta, mouseX, mouseY); logo.hoverUpdate(delta, mouseX, mouseY);
playButton.hoverUpdate(delta, mouseX, mouseY); playButton.hoverUpdate(delta, mouseX, mouseY);
@ -402,6 +404,11 @@ public class MainMenu extends BasicGameState {
} }
} }
@Override
public void mouseWheelMoved(int newValue) {
Utils.changeVolume((newValue < 0) ? -1 : 1);
}
@Override @Override
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {
switch (key) { switch (key) {

View File

@ -107,6 +107,7 @@ public class MainMenuExit extends BasicGameState {
"2. No", Color.white "2. No", Color.white
); );
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -115,6 +116,7 @@ public class MainMenuExit extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
// move buttons to center // move buttons to center
float yesX = yesButton.getX(), noX = noButton.getX(); float yesX = yesButton.getX(), noX = noButton.getX();

View File

@ -172,14 +172,24 @@ public class Options extends BasicGameState {
container.setVSync(getTargetFPS() == 60); container.setVSync(getTargetFPS() == 60);
} }
}, },
MUSIC_VOLUME ("Music Volume", "Global music volume.") { MASTER_VOLUME ("Master Volume", "Global volume level.") {
@Override
public String getValueString() { return String.format("%d%%", masterVolume); }
@Override
public void drag(GameContainer container, int d) {
masterVolume = Utils.getBoundedValue(masterVolume, d, 0, 100);
container.setMusicVolume(getMasterVolume() * getMusicVolume());
}
},
MUSIC_VOLUME ("Music Volume", "Volume of music.") {
@Override @Override
public String getValueString() { return String.format("%d%%", musicVolume); } public String getValueString() { return String.format("%d%%", musicVolume); }
@Override @Override
public void drag(GameContainer container, int d) { public void drag(GameContainer container, int d) {
musicVolume = Utils.getBoundedValue(musicVolume, d, 0, 100); musicVolume = Utils.getBoundedValue(musicVolume, d, 0, 100);
container.setMusicVolume(getMusicVolume()); container.setMusicVolume(getMasterVolume() * getMusicVolume());
} }
}, },
EFFECT_VOLUME ("Effect Volume", "Volume of menu and game sounds.") { EFFECT_VOLUME ("Effect Volume", "Volume of menu and game sounds.") {
@ -480,6 +490,7 @@ public class Options extends BasicGameState {
* Music options. * Music options.
*/ */
private static final GameOption[] musicOptions = { private static final GameOption[] musicOptions = {
GameOption.MASTER_VOLUME,
GameOption.MUSIC_VOLUME, GameOption.MUSIC_VOLUME,
GameOption.EFFECT_VOLUME, GameOption.EFFECT_VOLUME,
GameOption.HITSOUND_VOLUME, GameOption.HITSOUND_VOLUME,
@ -612,20 +623,25 @@ public class Options extends BasicGameState {
*/ */
private static boolean showComboBursts = true; private static boolean showComboBursts = true;
/**
* Global volume level.
*/
private static int masterVolume = 35;
/** /**
* Default music volume. * Default music volume.
*/ */
private static int musicVolume = 30; private static int musicVolume = 80;
/** /**
* Default sound effect volume. * Default sound effect volume.
*/ */
private static int effectVolume = 20; private static int effectVolume = 70;
/** /**
* Default hit sound volume. * Default hit sound volume.
*/ */
private static int hitSoundVolume = 20; private static int hitSoundVolume = 70;
/** /**
* Offset time, in milliseconds, for music position-related elements. * Offset time, in milliseconds, for music position-related elements.
@ -847,6 +863,7 @@ public class Options extends BasicGameState {
); );
} }
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -855,6 +872,7 @@ public class Options extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY); Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY);
for (GameMod mod : GameMod.values()) for (GameMod mod : GameMod.values())
@ -1044,6 +1062,24 @@ public class Options extends BasicGameState {
*/ */
public static int getTargetFPS() { return targetFPS[targetFPSindex]; } public static int getTargetFPS() { return targetFPS[targetFPSindex]; }
/**
* Returns the master volume level.
* @return the volume [0, 1]
*/
public static float getMasterVolume() { return masterVolume / 100f; }
/**
* Sets the master volume level (if within valid range).
* @param container the game container
* @param volume the volume [0, 1]
*/
public static void setMasterVolume(GameContainer container, float volume) {
if (volume >= 0f && volume <= 1f) {
masterVolume = (int) (volume * 100f);
container.setMusicVolume(getMasterVolume() * getMusicVolume());
}
}
/** /**
* Returns the default music volume. * Returns the default music volume.
* @return the volume [0, 1] * @return the volume [0, 1]
@ -1417,6 +1453,11 @@ public class Options extends BasicGameState {
case "LoadVerbose": case "LoadVerbose":
loadVerbose = Boolean.parseBoolean(value); loadVerbose = Boolean.parseBoolean(value);
break; break;
case "VolumeUniversal":
i = Integer.parseInt(value);
if (i >= 0 && i <= 100)
masterVolume = i;
break;
case "VolumeMusic": case "VolumeMusic":
i = Integer.parseInt(value); i = Integer.parseInt(value);
if (i >= 0 && i <= 100) if (i >= 0 && i <= 100)
@ -1551,6 +1592,8 @@ public class Options extends BasicGameState {
writer.newLine(); writer.newLine();
writer.write(String.format("LoadVerbose = %b", loadVerbose)); writer.write(String.format("LoadVerbose = %b", loadVerbose));
writer.newLine(); writer.newLine();
writer.write(String.format("VolumeUniversal = %d", masterVolume));
writer.newLine();
writer.write(String.format("VolumeMusic = %d", musicVolume)); writer.write(String.format("VolumeMusic = %d", musicVolume));
writer.newLine(); writer.newLine();
writer.write(String.format("VolumeEffect = %d", effectVolume)); writer.write(String.format("VolumeEffect = %d", effectVolume));

View File

@ -343,6 +343,7 @@ public class SongMenu extends BasicGameState {
// back button // back button
Utils.getBackButton().draw(); Utils.getBackButton().draw();
Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
} }
@ -351,6 +352,7 @@ public class SongMenu extends BasicGameState {
public void update(GameContainer container, StateBasedGame game, int delta) public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { throws SlickException {
Utils.updateCursor(delta); Utils.updateCursor(delta);
Utils.updateVolumeDisplay(delta);
int mouseX = input.getMouseX(), mouseY = input.getMouseY(); int mouseX = input.getMouseX(), mouseY = input.getMouseY();
Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY); Utils.getBackButton().hoverUpdate(delta, mouseX, mouseY);
optionsButton.hoverUpdate(delta, mouseX, mouseY); optionsButton.hoverUpdate(delta, mouseX, mouseY);