From 516e6280c7287aa3cc21bd0fe543724e4e3e1f1f Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Thu, 15 Jan 2015 16:47:55 -0500 Subject: [PATCH] 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 --- src/itdelatrisu/opsu/Opsu.java | 10 +-- .../opsu/audio/MusicController.java | 1 + src/itdelatrisu/opsu/states/Game.java | 66 +++++++++++-------- .../opsu/states/GamePauseMenu.java | 2 + src/itdelatrisu/opsu/states/GameRanking.java | 2 + 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/itdelatrisu/opsu/Opsu.java b/src/itdelatrisu/opsu/Opsu.java index 449093cd..cc755936 100644 --- a/src/itdelatrisu/opsu/Opsu.java +++ b/src/itdelatrisu/opsu/Opsu.java @@ -109,7 +109,7 @@ public class Opsu extends StateBasedGame { public void uncaughtException(Thread t, Throwable e) { if (!(e instanceof ThreadDeath)) { // TODO: see MusicController 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 { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); if (Desktop.isDesktopSupported()) { // try to open the log file and/or issues webpage String[] options = {"Send Report", "View Error Log", "Close"}; int n = JOptionPane.showOptionDialog(null, - "opsu! has crashed. Please report this!", + "opsu! has encountered an error.\nPlease report this!", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[2]); @@ -218,7 +218,7 @@ public class Opsu extends StateBasedGame { } else { // display error only JOptionPane.showMessageDialog(null, - "opsu! has crashed. Please report this!", + "opsu! has encountered an error.\nPlease report this!", "Error", JOptionPane.ERROR_MESSAGE); } } catch (Exception e) { diff --git a/src/itdelatrisu/opsu/audio/MusicController.java b/src/itdelatrisu/opsu/audio/MusicController.java index cf4d0515..e1217041 100644 --- a/src/itdelatrisu/opsu/audio/MusicController.java +++ b/src/itdelatrisu/opsu/audio/MusicController.java @@ -95,6 +95,7 @@ public class MusicController { // releases all sources from previous tracks destroyOpenAL(); + System.gc(); switch (OsuParser.getExtension(osu.audioFilename.getName())) { case "ogg": diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index dd28e173..d839098b 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -752,19 +752,22 @@ public class Game extends BasicGameState { // restart the game if (restart != Restart.FALSE) { - // new game if (restart == Restart.NEW) { + // new game loadImages(); setMapModifiers(); retries = 0; - } else + } else { + // retry retries++; + } + + // reset game data + resetGameData(); + MusicController.setPosition(0); + MusicController.pause(); // initialize object maps - circles = new HashMap(); - sliders = new HashMap(); - spinners = new HashMap(); - for (int i = 0; i < osu.objects.length; 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 if (!osu.timingPoints.isEmpty()) { OsuTimingPoint timingPoint = osu.timingPoints.get(0); @@ -829,6 +810,35 @@ public class Game extends BasicGameState { // container.setMouseGrabbed(false); // } + /** + * Resets all game data and structures. + */ + public void resetGameData() { + circles = new HashMap(); + sliders = new HashMap(); + spinners = new HashMap(); + 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. * @return true if skipped, false otherwise diff --git a/src/itdelatrisu/opsu/states/GamePauseMenu.java b/src/itdelatrisu/opsu/states/GamePauseMenu.java index 6c1ea264..7877e9dd 100644 --- a/src/itdelatrisu/opsu/states/GamePauseMenu.java +++ b/src/itdelatrisu/opsu/states/GamePauseMenu.java @@ -130,6 +130,7 @@ public class GamePauseMenu extends BasicGameState { MusicController.stop(); MusicController.playAt(MusicController.getOsuFile().previewTime, true); SoundController.playSound(SoundEffect.MENUBACK); + gameState.resetGameData(); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } else { SoundController.playSound(SoundEffect.MENUBACK); @@ -173,6 +174,7 @@ public class GamePauseMenu extends BasicGameState { MusicController.pause(); // lose state MusicController.playAt(MusicController.getOsuFile().previewTime, true); SoundController.playSound(SoundEffect.MENUBACK); + gameState.resetGameData(); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } } diff --git a/src/itdelatrisu/opsu/states/GameRanking.java b/src/itdelatrisu/opsu/states/GameRanking.java index 34cc89a8..3319562a 100644 --- a/src/itdelatrisu/opsu/states/GameRanking.java +++ b/src/itdelatrisu/opsu/states/GameRanking.java @@ -174,11 +174,13 @@ public class GameRanking extends BasicGameState { } else if (exitButton.contains(x, y)) { SoundController.playSound(SoundEffect.MENUBACK); ((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)); } else if (Utils.getBackButton().contains(x, y)) { MusicController.pause(); MusicController.playAt(MusicController.getOsuFile().previewTime, true); SoundController.playSound(SoundEffect.MENUBACK); + ((Game) game.getState(Opsu.STATE_GAME)).resetGameData(); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } }