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;
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;
}
}

View File

@ -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);
}
}
}
}

View File

@ -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

View File

@ -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();
}
}
}

View File

@ -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:

View File

@ -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;

View File

@ -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
);
}
}
}