Call System.gc() on track changes and game resets.

This causes a noticeable drop in memory usage in most cases.

Also renamed the "crash" popup since the game doesn't necessarily crash.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-15 16:47:55 -05:00
parent 9db9c2f3ad
commit 516e6280c7
5 changed files with 48 additions and 33 deletions

View File

@ -109,7 +109,7 @@ public class Opsu extends StateBasedGame {
public void uncaughtException(Thread t, Throwable e) { public void uncaughtException(Thread t, Throwable e) {
if (!(e instanceof ThreadDeath)) { // TODO: see MusicController if (!(e instanceof ThreadDeath)) { // TODO: see MusicController
Log.error("** Uncaught Exception! **", e); Log.error("** Uncaught Exception! **", e);
openCrashPopup(); openErrorPopup();
} }
} }
}); });
@ -196,16 +196,16 @@ public class Opsu extends StateBasedGame {
} }
/** /**
* Opens the crash popup. * Opens the error popup.
*/ */
private static void openCrashPopup() { private static void openErrorPopup() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
if (Desktop.isDesktopSupported()) { if (Desktop.isDesktopSupported()) {
// try to open the log file and/or issues webpage // try to open the log file and/or issues webpage
String[] options = {"Send Report", "View Error Log", "Close"}; String[] options = {"Send Report", "View Error Log", "Close"};
int n = JOptionPane.showOptionDialog(null, int n = JOptionPane.showOptionDialog(null,
"opsu! has crashed. Please report this!", "opsu! has encountered an error.\nPlease report this!",
"Error", JOptionPane.DEFAULT_OPTION, "Error", JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE, null, options, JOptionPane.ERROR_MESSAGE, null, options,
options[2]); options[2]);
@ -218,7 +218,7 @@ public class Opsu extends StateBasedGame {
} else { } else {
// display error only // display error only
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"opsu! has crashed. Please report this!", "opsu! has encountered an error.\nPlease report this!",
"Error", JOptionPane.ERROR_MESSAGE); "Error", JOptionPane.ERROR_MESSAGE);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -95,6 +95,7 @@ public class MusicController {
// releases all sources from previous tracks // releases all sources from previous tracks
destroyOpenAL(); destroyOpenAL();
System.gc();
switch (OsuParser.getExtension(osu.audioFilename.getName())) { switch (OsuParser.getExtension(osu.audioFilename.getName())) {
case "ogg": case "ogg":

View File

@ -752,19 +752,22 @@ public class Game extends BasicGameState {
// restart the game // restart the game
if (restart != Restart.FALSE) { if (restart != Restart.FALSE) {
// new game
if (restart == Restart.NEW) { if (restart == Restart.NEW) {
// new game
loadImages(); loadImages();
setMapModifiers(); setMapModifiers();
retries = 0; retries = 0;
} else } else {
// retry
retries++; retries++;
}
// reset game data
resetGameData();
MusicController.setPosition(0);
MusicController.pause();
// initialize object maps // initialize object maps
circles = new HashMap<Integer, Circle>();
sliders = new HashMap<Integer, Slider>();
spinners = new HashMap<Integer, Spinner>();
for (int i = 0; i < osu.objects.length; i++) { for (int i = 0; i < osu.objects.length; i++) {
OsuHitObject hitObject = osu.objects[i]; OsuHitObject hitObject = osu.objects[i];
@ -783,28 +786,6 @@ public class Game extends BasicGameState {
} }
} }
// reset data
MusicController.setPosition(0);
MusicController.pause();
score.clear();
objectIndex = 0;
breakIndex = 0;
breakTime = 0;
breakSound = false;
timingPointIndex = 0;
beatLengthBase = beatLength = 1;
pauseTime = -1;
pausedMouseX = -1;
pausedMouseY = -1;
countdownReadySound = false;
countdown3Sound = false;
countdown1Sound = false;
countdown2Sound = false;
countdownGoSound = false;
checkpointLoaded = false;
deaths = 0;
deathTime = -1;
// load the first timingPoint // load the first timingPoint
if (!osu.timingPoints.isEmpty()) { if (!osu.timingPoints.isEmpty()) {
OsuTimingPoint timingPoint = osu.timingPoints.get(0); OsuTimingPoint timingPoint = osu.timingPoints.get(0);
@ -829,6 +810,35 @@ public class Game extends BasicGameState {
// container.setMouseGrabbed(false); // container.setMouseGrabbed(false);
// } // }
/**
* Resets all game data and structures.
*/
public void resetGameData() {
circles = new HashMap<Integer, Circle>();
sliders = new HashMap<Integer, Slider>();
spinners = new HashMap<Integer, Spinner>();
score.clear();
objectIndex = 0;
breakIndex = 0;
breakTime = 0;
breakSound = false;
timingPointIndex = 0;
beatLengthBase = beatLength = 1;
pauseTime = -1;
pausedMouseX = -1;
pausedMouseY = -1;
countdownReadySound = false;
countdown3Sound = false;
countdown1Sound = false;
countdown2Sound = false;
countdownGoSound = false;
checkpointLoaded = false;
deaths = 0;
deathTime = -1;
System.gc();
}
/** /**
* Skips the beginning of a track. * Skips the beginning of a track.
* @return true if skipped, false otherwise * @return true if skipped, false otherwise

View File

@ -130,6 +130,7 @@ public class GamePauseMenu extends BasicGameState {
MusicController.stop(); MusicController.stop();
MusicController.playAt(MusicController.getOsuFile().previewTime, true); MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
gameState.resetGameData();
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 { } else {
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
@ -173,6 +174,7 @@ public class GamePauseMenu extends BasicGameState {
MusicController.pause(); // lose state MusicController.pause(); // lose state
MusicController.playAt(MusicController.getOsuFile().previewTime, true); MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
gameState.resetGameData();
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));
} }
} }

View File

@ -174,11 +174,13 @@ public class GameRanking extends BasicGameState {
} else if (exitButton.contains(x, y)) { } else if (exitButton.contains(x, y)) {
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
((MainMenu) game.getState(Opsu.STATE_MAINMENU)).reset(); ((MainMenu) game.getState(Opsu.STATE_MAINMENU)).reset();
((Game) game.getState(Opsu.STATE_GAME)).resetGameData();
game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else if (Utils.getBackButton().contains(x, y)) { } else if (Utils.getBackButton().contains(x, y)) {
MusicController.pause(); MusicController.pause();
MusicController.playAt(MusicController.getOsuFile().previewTime, true); MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
((Game) game.getState(Opsu.STATE_GAME)).resetGameData();
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));
} }
} }