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 <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-18 15:26:00 -05:00
parent ee17b20b25
commit 53158fd310
7 changed files with 51 additions and 26 deletions

View File

@ -19,6 +19,7 @@
package itdelatrisu.opsu; package itdelatrisu.opsu;
import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.states.Options;
import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Game; import org.newdawn.slick.Game;
@ -85,4 +86,11 @@ public class Container extends AppGameContainer {
throw e; throw e;
} }
} }
@Override
public void exit() {
Options.saveOptions();
Opsu.closeSocket();
running = false;
}
} }

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu; package itdelatrisu.opsu;
import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.states.Game; import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.states.GamePauseMenu; import itdelatrisu.opsu.states.GamePauseMenu;
import itdelatrisu.opsu.states.GameRanking; import itdelatrisu.opsu.states.GameRanking;
@ -171,6 +172,7 @@ public class Opsu extends StateBasedGame {
return false; return false;
} }
MusicController.reset();
Options.saveOptions(); Options.saveOptions();
((Container) this.getContainer()).destroy(); ((Container) this.getContainer()).destroy();
closeSocket(); closeSocket();
@ -181,10 +183,12 @@ public class Opsu extends StateBasedGame {
* Closes the server socket. * Closes the server socket.
*/ */
public static void closeSocket() { public static void closeSocket() {
if (SERVER_SOCKET != null) {
try { try {
SERVER_SOCKET.close(); SERVER_SOCKET.close();
} catch (IOException e) { } catch (IOException e) {
ErrorHandler.error("Failed to close server socket.", e, false); ErrorHandler.error("Failed to close server socket.", e, false);
} }
} }
}
} }

View File

@ -102,6 +102,10 @@ public class OsuParser {
Collections.sort(osuFiles); Collections.sort(osuFiles);
OsuGroupList.get().addSongGroup(osuFiles); OsuGroupList.get().addSongGroup(osuFiles);
} }
// stop parsing files (interrupt sent by Splash)
if (Thread.interrupted())
break;
} }
// clear string DB // clear string DB

View File

@ -397,13 +397,10 @@ public class MainMenu extends BasicGameState {
if (logo.contains(x, y) || playButton.contains(x, y)) { if (logo.contains(x, y) || playButton.contains(x, y)) {
SoundController.playSound(SoundEffect.MENUHIT); SoundController.playSound(SoundEffect.MENUHIT);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else if (exitButton.contains(x, y)) { } else if (exitButton.contains(x, y))
Options.saveOptions();
Opsu.closeSocket();
container.exit(); container.exit();
} }
} }
}
@Override @Override
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {

View File

@ -134,11 +134,9 @@ public class MainMenuExit extends BasicGameState {
if (button != Input.MOUSE_LEFT_BUTTON) if (button != Input.MOUSE_LEFT_BUTTON)
return; return;
if (yesButton.contains(x, y)) { if (yesButton.contains(x, y))
Options.saveOptions();
Opsu.closeSocket();
container.exit(); 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)); 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) { public void keyPressed(int key, char c) {
switch (key) { switch (key) {
case Input.KEY_1: case Input.KEY_1:
Options.saveOptions();
Opsu.closeSocket();
container.exit(); container.exit();
break; break;
case Input.KEY_2: case Input.KEY_2:

View File

@ -49,6 +49,16 @@ public class Splash extends BasicGameState {
*/ */
private boolean finished = false; 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 // game-related variables
private int state; private int state;
private GameContainer container; private GameContainer container;
@ -106,7 +116,7 @@ public class Splash extends BasicGameState {
// load other resources in a new thread // load other resources in a new thread
final int width = container.getWidth(); final int width = container.getWidth();
final int height = container.getHeight(); final int height = container.getHeight();
new Thread() { thread = new Thread() {
@Override @Override
public void run() { public void run() {
File beatmapDir = Options.getBeatmapDir(); File beatmapDir = Options.getBeatmapDir();
@ -122,7 +132,8 @@ public class Splash extends BasicGameState {
finished = true; finished = true;
} }
}.start(); };
thread.start();
} }
// fade in logo // fade in logo
@ -150,11 +161,13 @@ public class Splash extends BasicGameState {
@Override @Override
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {
if (key == Input.KEY_ESCAPE) { // close program
Options.saveOptions(); if (++escapeCount >= 3)
Opsu.closeSocket();
container.exit(); 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 * @param file the file being loaded
*/ */
private void drawLoadProgress(Graphics g, int progress, String text, String file) { 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(); int lineOffsetY = Utils.FONT_MEDIUM.getLineHeight();
if (Options.isLoadVerbose()) { if (Options.isLoadVerbose()) {
Utils.FONT_MEDIUM.drawString( Utils.FONT_MEDIUM.drawString(
25, lineY - (lineOffsetY * 2), marginX, lineY - (lineOffsetY * 2),
String.format("%s (%d%%)", text, progress), Color.white); 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 { } 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.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
);
} }
} }
} }