From 53158fd3104da2a96620a7c160027a37b1eeb064 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Sun, 18 Jan 2015 15:26:00 -0500 Subject: [PATCH] Interrupt OsuParser by pressing 'ESC' in Splash state. Allows parsing only a subset of OsuFiles, in case parsing takes too long. Other changes: - Press 'ESC' three times to exit from the Splash state. - Override Container.exit(). - Reset MusicController in closeRequested() to prevent an OpenAL error. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/Container.java | 8 ++++ src/itdelatrisu/opsu/Opsu.java | 12 ++++-- src/itdelatrisu/opsu/OsuParser.java | 4 ++ src/itdelatrisu/opsu/states/MainMenu.java | 5 +-- src/itdelatrisu/opsu/states/MainMenuExit.java | 8 +--- src/itdelatrisu/opsu/states/Options.java | 2 +- src/itdelatrisu/opsu/states/Splash.java | 38 +++++++++++++------ 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/itdelatrisu/opsu/Container.java b/src/itdelatrisu/opsu/Container.java index 4080a313..f22bdd3d 100644 --- a/src/itdelatrisu/opsu/Container.java +++ b/src/itdelatrisu/opsu/Container.java @@ -19,6 +19,7 @@ package itdelatrisu.opsu; import itdelatrisu.opsu.audio.MusicController; +import itdelatrisu.opsu.states.Options; import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.Game; @@ -85,4 +86,11 @@ public class Container extends AppGameContainer { throw e; } } + + @Override + public void exit() { + Options.saveOptions(); + Opsu.closeSocket(); + running = false; + } } diff --git a/src/itdelatrisu/opsu/Opsu.java b/src/itdelatrisu/opsu/Opsu.java index 8b796340..e0043af3 100644 --- a/src/itdelatrisu/opsu/Opsu.java +++ b/src/itdelatrisu/opsu/Opsu.java @@ -18,6 +18,7 @@ package itdelatrisu.opsu; +import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.states.Game; import itdelatrisu.opsu.states.GamePauseMenu; import itdelatrisu.opsu.states.GameRanking; @@ -171,6 +172,7 @@ public class Opsu extends StateBasedGame { return false; } + MusicController.reset(); Options.saveOptions(); ((Container) this.getContainer()).destroy(); closeSocket(); @@ -181,10 +183,12 @@ public class Opsu extends StateBasedGame { * Closes the server socket. */ public static void closeSocket() { - try { - SERVER_SOCKET.close(); - } catch (IOException e) { - ErrorHandler.error("Failed to close server socket.", e, false); + if (SERVER_SOCKET != null) { + try { + SERVER_SOCKET.close(); + } catch (IOException e) { + ErrorHandler.error("Failed to close server socket.", e, false); + } } } } diff --git a/src/itdelatrisu/opsu/OsuParser.java b/src/itdelatrisu/opsu/OsuParser.java index 66d664e9..ce7ab7e1 100644 --- a/src/itdelatrisu/opsu/OsuParser.java +++ b/src/itdelatrisu/opsu/OsuParser.java @@ -102,6 +102,10 @@ public class OsuParser { Collections.sort(osuFiles); OsuGroupList.get().addSongGroup(osuFiles); } + + // stop parsing files (interrupt sent by Splash) + if (Thread.interrupted()) + break; } // clear string DB diff --git a/src/itdelatrisu/opsu/states/MainMenu.java b/src/itdelatrisu/opsu/states/MainMenu.java index e301765d..8c3d5228 100644 --- a/src/itdelatrisu/opsu/states/MainMenu.java +++ b/src/itdelatrisu/opsu/states/MainMenu.java @@ -397,11 +397,8 @@ public class MainMenu extends BasicGameState { if (logo.contains(x, y) || playButton.contains(x, y)) { SoundController.playSound(SoundEffect.MENUHIT); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); - } else if (exitButton.contains(x, y)) { - Options.saveOptions(); - Opsu.closeSocket(); + } else if (exitButton.contains(x, y)) container.exit(); - } } } diff --git a/src/itdelatrisu/opsu/states/MainMenuExit.java b/src/itdelatrisu/opsu/states/MainMenuExit.java index a927cf4a..7323f21a 100644 --- a/src/itdelatrisu/opsu/states/MainMenuExit.java +++ b/src/itdelatrisu/opsu/states/MainMenuExit.java @@ -134,11 +134,9 @@ public class MainMenuExit extends BasicGameState { if (button != Input.MOUSE_LEFT_BUTTON) return; - if (yesButton.contains(x, y)) { - Options.saveOptions(); - Opsu.closeSocket(); + if (yesButton.contains(x, y)) container.exit(); - } else if (noButton.contains(x, y)) + else if (noButton.contains(x, y)) game.enterState(Opsu.STATE_MAINMENU, new EmptyTransition(), new FadeInTransition(Color.black)); } @@ -146,8 +144,6 @@ public class MainMenuExit extends BasicGameState { public void keyPressed(int key, char c) { switch (key) { case Input.KEY_1: - Options.saveOptions(); - Opsu.closeSocket(); container.exit(); break; case Input.KEY_2: diff --git a/src/itdelatrisu/opsu/states/Options.java b/src/itdelatrisu/opsu/states/Options.java index 7fc96109..65b5df1a 100644 --- a/src/itdelatrisu/opsu/states/Options.java +++ b/src/itdelatrisu/opsu/states/Options.java @@ -1086,7 +1086,7 @@ public class Options extends BasicGameState { public static void setDisplayMode(Container app) throws SlickException { int screenWidth = app.getScreenWidth(); int screenHeight = app.getScreenHeight(); - + // check for larger-than-screen dimensions if (screenWidth < resolution.getWidth() || screenHeight < resolution.getHeight()) resolution = Resolution.RES_800_600; diff --git a/src/itdelatrisu/opsu/states/Splash.java b/src/itdelatrisu/opsu/states/Splash.java index 9c44adbb..5be4f3c9 100644 --- a/src/itdelatrisu/opsu/states/Splash.java +++ b/src/itdelatrisu/opsu/states/Splash.java @@ -49,6 +49,16 @@ public class Splash extends BasicGameState { */ private boolean finished = false; + /** + * Loading thread. + */ + private Thread thread; + + /** + * Number of times the ESC key has been pressed. + */ + private int escapeCount = 0; + // game-related variables private int state; private GameContainer container; @@ -106,7 +116,7 @@ public class Splash extends BasicGameState { // load other resources in a new thread final int width = container.getWidth(); final int height = container.getHeight(); - new Thread() { + thread = new Thread() { @Override public void run() { File beatmapDir = Options.getBeatmapDir(); @@ -122,7 +132,8 @@ public class Splash extends BasicGameState { finished = true; } - }.start(); + }; + thread.start(); } // fade in logo @@ -150,11 +161,13 @@ public class Splash extends BasicGameState { @Override public void keyPressed(int key, char c) { - if (key == Input.KEY_ESCAPE) { - Options.saveOptions(); - Opsu.closeSocket(); + // close program + if (++escapeCount >= 3) container.exit(); - } + + // stop parsing OsuFiles by sending interrupt to OsuParser + else if (key == Input.KEY_ESCAPE && thread != null) + thread.interrupt(); } /** @@ -165,17 +178,20 @@ public class Splash extends BasicGameState { * @param file the file being loaded */ private void drawLoadProgress(Graphics g, int progress, String text, String file) { - int lineY = container.getHeight() - 25; + float marginX = container.getWidth() * 0.02f, marginY = container.getHeight() * 0.02f; + float lineY = container.getHeight() - marginY; int lineOffsetY = Utils.FONT_MEDIUM.getLineHeight(); if (Options.isLoadVerbose()) { Utils.FONT_MEDIUM.drawString( - 25, lineY - (lineOffsetY * 2), + marginX, lineY - (lineOffsetY * 2), String.format("%s (%d%%)", text, progress), Color.white); - Utils.FONT_MEDIUM.drawString(25, lineY - lineOffsetY, file, Color.white); + Utils.FONT_MEDIUM.drawString(marginX, lineY - lineOffsetY, file, Color.white); } else { - Utils.FONT_MEDIUM.drawString(25, lineY - (lineOffsetY * 2), text, Color.white); + Utils.FONT_MEDIUM.drawString(marginX, lineY - (lineOffsetY * 2), text, Color.white); g.setColor(Color.white); - g.fillRect(25, lineY - (lineOffsetY / 2f), (container.getWidth() - 50) * progress / 100f, lineOffsetY / 4f); + g.fillRoundRect(marginX, lineY - (lineOffsetY / 2f), + (container.getWidth() - (marginX * 2f)) * progress / 100f, lineOffsetY / 4f, 4 + ); } } }