From 90c8c9e705d1532fd6ee07931bd059def689e0a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Wed, 3 Jun 2015 06:23:23 -0400 Subject: [PATCH] Fixed a bug where sounds weren't reloaded when changing skins. When restarting the program through the options menu, all sounds now get reloaded if the skin was changed. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/Options.java | 24 ++++++++++---- src/itdelatrisu/opsu/audio/HitSound.java | 6 ++++ .../opsu/audio/SoundController.java | 18 +++++++++-- src/itdelatrisu/opsu/states/Splash.java | 31 ++++++++++++++++--- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/itdelatrisu/opsu/Options.java b/src/itdelatrisu/opsu/Options.java index e2436cbe..292aa951 100644 --- a/src/itdelatrisu/opsu/Options.java +++ b/src/itdelatrisu/opsu/Options.java @@ -911,15 +911,12 @@ public class Options { * If the directory is invalid, the default skin will be loaded. */ public static void loadSkin() { - File root = getSkinRootDir(); - File skinDir = new File(root, skinName); - if (!skinDir.isDirectory()) { // invalid skin name + File skinDir = getSkinDir(); + if (skinDir == null) // invalid skin name skinName = Skin.DEFAULT_SKIN_NAME; - skinDir = null; - } // create available skins list - File[] dirs = SkinLoader.getSkinDirectories(root); + File[] dirs = SkinLoader.getSkinDirectories(getSkinRootDir()); skinDirs = new String[dirs.length + 1]; skinDirs[0] = Skin.DEFAULT_SKIN_NAME; for (int i = 0; i < dirs.length; i++) @@ -953,6 +950,21 @@ public class Options { */ public static Skin getSkin() { return skin; } + /** + * Returns the current skin directory. + *

+ * NOTE: This directory will differ from that of the currently loaded skin + * if {@link #loadSkin()} has not been called after a directory change. + * Use {@link Skin#getDirectory()} to get the directory of the currently + * loaded skin. + * @return the skin directory, or null for the default skin + */ + public static File getSkinDir() { + File root = getSkinRootDir(); + File dir = new File(root, skinName); + return (dir.isDirectory()) ? dir : null; + } + /** * Returns a dummy Beatmap containing the theme song. * @return the theme song beatmap diff --git a/src/itdelatrisu/opsu/audio/HitSound.java b/src/itdelatrisu/opsu/audio/HitSound.java index ab2457d5..590f3ddb 100644 --- a/src/itdelatrisu/opsu/audio/HitSound.java +++ b/src/itdelatrisu/opsu/audio/HitSound.java @@ -105,6 +105,12 @@ public enum HitSound implements SoundController.SoundComponent { return (currentSampleSet != null) ? clips.get(currentSampleSet) : null; } + /** + * Returns the Clip associated with the given sample set. + * @param s the sample set + */ + public MultiClip getClip(SampleSet s) { return clips.get(s); } + /** * Sets the hit sound Clip for the sample type. * @param s the sample set diff --git a/src/itdelatrisu/opsu/audio/SoundController.java b/src/itdelatrisu/opsu/audio/SoundController.java index fd929cc1..b91db34e 100644 --- a/src/itdelatrisu/opsu/audio/SoundController.java +++ b/src/itdelatrisu/opsu/audio/SoundController.java @@ -207,7 +207,14 @@ public class SoundController { ErrorHandler.error(String.format("Could not find sound file '%s'.", s.getFileName()), null, false); continue; } - s.setClip(loadClip(currentFileName, currentFileName.endsWith(".mp3"))); + MultiClip newClip = loadClip(currentFileName, currentFileName.endsWith(".mp3")); + if (s.getClip() != null) { // clip previously loaded (e.g. program restart) + if (newClip != null) { + s.getClip().destroy(); // destroy previous clip + s.setClip(newClip); + } + } else + s.setClip(newClip); currentFileIndex++; } @@ -219,7 +226,14 @@ public class SoundController { ErrorHandler.error(String.format("Could not find hit sound file '%s'.", filename), null, false); continue; } - s.setClip(ss, loadClip(currentFileName, false)); + MultiClip newClip = loadClip(currentFileName, false); + if (s.getClip(ss) != null) { // clip previously loaded (e.g. program restart) + if (newClip != null) { + s.getClip(ss).destroy(); // destroy previous clip + s.setClip(ss, newClip); + } + } else + s.setClip(ss, newClip); currentFileIndex++; } } diff --git a/src/itdelatrisu/opsu/states/Splash.java b/src/itdelatrisu/opsu/states/Splash.java index 7d2172c0..7b48729e 100644 --- a/src/itdelatrisu/opsu/states/Splash.java +++ b/src/itdelatrisu/opsu/states/Splash.java @@ -55,6 +55,9 @@ public class Splash extends BasicGameState { /** Number of times the 'Esc' key has been pressed. */ private int escapeCount = 0; + /** Whether the skin being loaded is a new skin (for program restarts). */ + private boolean newSkin = false; + // game-related variables private int state; private GameContainer container; @@ -69,6 +72,10 @@ public class Splash extends BasicGameState { throws SlickException { this.container = container; + // check if skin changed + if (Options.getSkin() != null) + this.newSkin = (Options.getSkin().getDirectory() != Options.getSkinDir()); + // load Utils class first (needed in other 'init' methods) Utils.init(container, game); @@ -89,11 +96,27 @@ public class Splash extends BasicGameState { if (!init) { init = true; + // resources already loaded (from application restart) if (BeatmapSetList.get() != null) { - // resources already loaded (from application restart) - finished = true; - } else { - // load resources in a new thread + // reload sounds if skin changed + if (newSkin) { + thread = new Thread() { + @Override + public void run() { + // TODO: only reload each sound if actually needed? + SoundController.init(); + + finished = true; + thread = null; + } + }; + thread.start(); + } else // don't reload anything + finished = true; + } + + // load all resources in a new thread + else { thread = new Thread() { @Override public void run() {