From 06f8dae03713e8b91425cf92207dca0378b7cd55 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Fri, 4 Jul 2014 19:59:57 -0400 Subject: [PATCH] Reduced unnecessary reloading of default images. (follow-up 16afcaf) - GameImage now stores both a default image and skin image, returning the skin image whenever available. - Default images are loaded once on startup, instead of before every game. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/GameImage.java | 72 +++++++++++++++---- src/itdelatrisu/opsu/GameScore.java | 49 +++++++++---- src/itdelatrisu/opsu/Utils.java | 8 ++- src/itdelatrisu/opsu/states/Game.java | 55 +++++++++----- .../opsu/states/GamePauseMenu.java | 14 ++-- 5 files changed, 146 insertions(+), 52 deletions(-) diff --git a/src/itdelatrisu/opsu/GameImage.java b/src/itdelatrisu/opsu/GameImage.java index 111cf20b..68341c82 100644 --- a/src/itdelatrisu/opsu/GameImage.java +++ b/src/itdelatrisu/opsu/GameImage.java @@ -134,9 +134,19 @@ public enum GameImage { private String filename; /** - * The associated image. + * The default image. */ - private Image img; + private Image defaultImage; + + /** + * The beatmap skin image (optional, temporary). + */ + private Image skinImage; + + /** + * Whether or not the default image has been scaled. + */ + private boolean scaled; /** * Constructor. @@ -146,33 +156,67 @@ public enum GameImage { } /** - * Returns the associated image. + * Returns the image associated with this resource. + * The skin image takes priority over the default image. */ - public Image getImage() { return img; } + public Image getImage() { + return (skinImage != null) ? skinImage : defaultImage; + } /** - * Sets an image. + * Sets the image associated with this resource to another image. + * The skin image takes priority over the default image. */ - public void setImage(Image img) { this.img = img; } + public void setImage(Image img) { + if (skinImage != null) + this.skinImage = img; + else + this.defaultImage = img; + } /** - * Sets an image. - * Scans the path for the image first, then uses the default image. + * Sets the default image for this resource. */ - public void setImage(File dir) { + public void setDefaultImage() { + try { + if (defaultImage != null && !defaultImage.isDestroyed()) + defaultImage.destroy(); + + defaultImage = new Image(filename); + scaled = false; + } catch (SlickException e) { + Log.error(String.format("Failed to set default image '%s'.", filename), e); + } + } + + /** + * Sets the associated skin image. + * If the path does not contain the image, the default image is used. + */ + public void setSkinImage(File dir) { try { // destroy the existing image, if any - if (img != null && !img.isDestroyed()) - img.destroy(); + if (skinImage != null && !skinImage.isDestroyed()) + skinImage.destroy(); // set a new image File file = new File(dir, filename); if (file.isFile() && !Options.isBeatmapSkinIgnored()) - img = new Image(file.getAbsolutePath()); + skinImage = new Image(file.getAbsolutePath()); else - img = new Image(filename); + skinImage = null; } catch (SlickException e) { - Log.error(String.format("Failed to set image '%s'.", filename), e); + Log.error(String.format("Failed to set skin image '%s'.", filename), e); } } + + /** + * Returns whether or not the image has been scaled. + */ + public boolean isScaled() { return (skinImage != null) ? false : scaled; } + + /** + * Sets the scaled status of the image. + */ + public void setScaled() { if (skinImage == null) this.scaled = true; } } \ No newline at end of file diff --git a/src/itdelatrisu/opsu/GameScore.java b/src/itdelatrisu/opsu/GameScore.java index 386bc5b7..a9dc0fe0 100644 --- a/src/itdelatrisu/opsu/GameScore.java +++ b/src/itdelatrisu/opsu/GameScore.java @@ -299,11 +299,17 @@ public class GameScore { } // scorebar - Image bg = GameImage.SCOREBAR_BG.getImage(); - Image colour = GameImage.SCOREBAR_COLOUR.getImage(); int bgWidth = width / 2; - GameImage.SCOREBAR_BG.setImage(bg.getScaledCopy(bgWidth, bg.getHeight())); - GameImage.SCOREBAR_COLOUR.setImage(colour.getScaledCopy(bgWidth, colour.getHeight())); + if (!GameImage.SCOREBAR_BG.isScaled()) { + Image bg = GameImage.SCOREBAR_BG.getImage(); + GameImage.SCOREBAR_BG.setImage(bg.getScaledCopy(bgWidth, bg.getHeight())); + GameImage.SCOREBAR_BG.setScaled(); + } + if (!GameImage.SCOREBAR_COLOUR.isScaled()) { + Image colour = GameImage.SCOREBAR_COLOUR.getImage(); + GameImage.SCOREBAR_COLOUR.setImage(colour.getScaledCopy(bgWidth, colour.getHeight())); + GameImage.SCOREBAR_COLOUR.setScaled(); + } // default symbol images defaultSymbols = new Image[10]; @@ -368,16 +374,31 @@ public class GameScore { gradesSmall[GRADE_D] = GameImage.RANKING_D_SMALL.getImage(); // ranking screen elements - Image rankingPanel = GameImage.RANKING_PANEL.getImage(); - Image rankingPerfect = GameImage.RANKING_PERFECT.getImage(); - Image rankingTitle = GameImage.RANKING_TITLE.getImage(); - Image rankingMaxCombo = GameImage.RANKING_MAXCOMBO.getImage(); - Image rankingAccuracy = GameImage.RANKING_ACCURACY.getImage(); - GameImage.RANKING_PANEL.setImage(rankingPanel.getScaledCopy((height * 0.63f) / rankingPanel.getHeight())); - GameImage.RANKING_PERFECT.setImage(rankingPerfect.getScaledCopy((height * 0.16f) / rankingPerfect.getHeight())); - GameImage.RANKING_TITLE.setImage(rankingTitle.getScaledCopy((height * 0.15f) / rankingTitle.getHeight())); - GameImage.RANKING_MAXCOMBO.setImage(rankingMaxCombo.getScaledCopy((height * 0.05f) / rankingMaxCombo.getHeight())); - GameImage.RANKING_ACCURACY.setImage(rankingAccuracy.getScaledCopy((height * 0.05f) / rankingAccuracy.getHeight())); + if (!GameImage.RANKING_PANEL.isScaled()) { + Image rankingPanel = GameImage.RANKING_PANEL.getImage(); + GameImage.RANKING_PANEL.setImage(rankingPanel.getScaledCopy((height * 0.63f) / rankingPanel.getHeight())); + GameImage.RANKING_PANEL.setScaled(); + } + if (!GameImage.RANKING_PERFECT.isScaled()) { + Image rankingPerfect = GameImage.RANKING_PERFECT.getImage(); + GameImage.RANKING_PERFECT.setImage(rankingPerfect.getScaledCopy((height * 0.16f) / rankingPerfect.getHeight())); + GameImage.RANKING_PERFECT.setScaled(); + } + if (!GameImage.RANKING_TITLE.isScaled()) { + Image rankingTitle = GameImage.RANKING_TITLE.getImage(); + GameImage.RANKING_TITLE.setImage(rankingTitle.getScaledCopy((height * 0.15f) / rankingTitle.getHeight())); + GameImage.RANKING_TITLE.setScaled(); + } + if (!GameImage.RANKING_MAXCOMBO.isScaled()) { + Image rankingMaxCombo = GameImage.RANKING_MAXCOMBO.getImage(); + GameImage.RANKING_MAXCOMBO.setImage(rankingMaxCombo.getScaledCopy((height * 0.05f) / rankingMaxCombo.getHeight())); + GameImage.RANKING_MAXCOMBO.setScaled(); + } + if (!GameImage.RANKING_ACCURACY.isScaled()) { + Image rankingAccuracy = GameImage.RANKING_ACCURACY.getImage(); + GameImage.RANKING_ACCURACY.setImage(rankingAccuracy.getScaledCopy((height * 0.05f) / rankingAccuracy.getHeight())); + GameImage.RANKING_ACCURACY.setScaled(); + } } /** diff --git a/src/itdelatrisu/opsu/Utils.java b/src/itdelatrisu/opsu/Utils.java index 137a9e3d..372a0977 100644 --- a/src/itdelatrisu/opsu/Utils.java +++ b/src/itdelatrisu/opsu/Utils.java @@ -127,14 +127,14 @@ public class Utils { container.getInput().enableKeyRepeat(); container.setAlwaysRender(true); - // hide the cursor + // set the cursor try { + // hide the native cursor Cursor emptyCursor = new Cursor(1, 1, 0, 0, 1, BufferUtils.createIntBuffer(1), null); container.setMouseCursor(emptyCursor, 0, 0); } catch (LWJGLException e) { Log.error("Failed to set the cursor.", e); } - loadCursor(); // create fonts @@ -157,6 +157,10 @@ public class Utils { FONT_MEDIUM = new TrueTypeFont(font.deriveFont(fontBase * 3 / 2), false); FONT_SMALL = new TrueTypeFont(font.deriveFont(fontBase), false); + // set default game images + for (GameImage o : GameImage.values()) + o.setDefaultImage(); + // tab image tab = new Image("selection-tab.png"); float tabScale = (height * 0.033f) / tab.getHeight(); diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index 2459c6b5..bbe80432 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -754,34 +754,53 @@ public class Game extends BasicGameState { // set images File parent = osu.getFile().getParentFile(); for (GameImage o : GameImage.values()) - o.setImage(parent); + o.setSkinImage(parent); // skip button Image skip = GameImage.SKIP.getImage(); - float skipScale = (height * 0.1f) / skip.getHeight(); - skip = skip.getScaledCopy(skipScale); + if (!GameImage.SKIP.isScaled()) { + float skipScale = (height * 0.1f) / skip.getHeight(); + skip = skip.getScaledCopy(skipScale); + GameImage.SKIP.setScaled(); + } skipButton = new GUIMenuButton(skip, width - (skip.getWidth() / 2f), height - (skip.getHeight() / 2f)); // countdown - Image countdownReady = GameImage.COUNTDOWN_READY.getImage(); - Image countdown3 = GameImage.COUNTDOWN_3.getImage(); - Image countdown2 = GameImage.COUNTDOWN_2.getImage(); - Image countdown1 = GameImage.COUNTDOWN_1.getImage(); - Image countdownGo = GameImage.COUNTDOWN_GO.getImage(); float countdownHeight = height / 3f; - GameImage.COUNTDOWN_READY.setImage( - countdownReady.getScaledCopy(countdownHeight / countdownReady.getHeight())); - GameImage.COUNTDOWN_3.setImage( - countdown3.getScaledCopy(countdownHeight / countdown3.getHeight())); - GameImage.COUNTDOWN_2.setImage( - countdown2.getScaledCopy(countdownHeight / countdown2.getHeight())); - GameImage.COUNTDOWN_1.setImage( - countdown1.getScaledCopy(countdownHeight / countdown1.getHeight())); - GameImage.COUNTDOWN_GO.setImage( - countdownGo.getScaledCopy(countdownHeight / countdownGo.getHeight())); + if (!GameImage.COUNTDOWN_READY.isScaled()) { + Image countdownReady = GameImage.COUNTDOWN_READY.getImage(); + GameImage.COUNTDOWN_READY.setImage( + countdownReady.getScaledCopy(countdownHeight / countdownReady.getHeight())); + GameImage.COUNTDOWN_READY.setScaled(); + } + if (!GameImage.COUNTDOWN_3.isScaled()) { + Image countdown3 = GameImage.COUNTDOWN_3.getImage(); + GameImage.COUNTDOWN_3.setImage( + countdown3.getScaledCopy(countdownHeight / countdown3.getHeight())); + GameImage.COUNTDOWN_3.setScaled(); + } + if (!GameImage.COUNTDOWN_2.isScaled()) { + Image countdown2 = GameImage.COUNTDOWN_2.getImage(); + GameImage.COUNTDOWN_2.setImage( + countdown2.getScaledCopy(countdownHeight / countdown2.getHeight())); + GameImage.COUNTDOWN_2.setScaled(); + } + if (!GameImage.COUNTDOWN_1.isScaled()) { + Image countdown1 = GameImage.COUNTDOWN_1.getImage(); + GameImage.COUNTDOWN_1.setImage( + countdown1.getScaledCopy(countdownHeight / countdown1.getHeight())); + GameImage.COUNTDOWN_1.setScaled(); + } + if (!GameImage.COUNTDOWN_GO.isScaled()) { + Image countdownGo = GameImage.COUNTDOWN_GO.getImage(); + GameImage.COUNTDOWN_GO.setImage( + countdownGo.getScaledCopy(countdownHeight / countdownGo.getHeight())); + GameImage.COUNTDOWN_GO.setScaled(); + } + // load other images... ((GamePauseMenu) game.getState(Opsu.STATE_GAMEPAUSEMENU)).loadImages(); score.loadImages(); } diff --git a/src/itdelatrisu/opsu/states/GamePauseMenu.java b/src/itdelatrisu/opsu/states/GamePauseMenu.java index e14f343e..4a997937 100644 --- a/src/itdelatrisu/opsu/states/GamePauseMenu.java +++ b/src/itdelatrisu/opsu/states/GamePauseMenu.java @@ -182,11 +182,17 @@ public class GamePauseMenu extends BasicGameState { backButton = new GUIMenuButton(GameImage.PAUSE_BACK.getImage(), width / 2f, height * 0.75f); // pause background image - GameImage.PAUSE_OVERLAY.setImage(GameImage.PAUSE_OVERLAY.getImage().getScaledCopy(width, height)); - GameImage.PAUSE_OVERLAY.getImage().setAlpha(0.7f); + if (!GameImage.PAUSE_OVERLAY.isScaled()) { + GameImage.PAUSE_OVERLAY.setImage(GameImage.PAUSE_OVERLAY.getImage().getScaledCopy(width, height)); + GameImage.PAUSE_OVERLAY.getImage().setAlpha(0.7f); + GameImage.PAUSE_OVERLAY.setScaled(); + } // fail image - GameImage.FAIL_BACKGROUND.setImage(GameImage.FAIL_BACKGROUND.getImage().getScaledCopy(width, height)); - GameImage.FAIL_BACKGROUND.getImage().setAlpha(0.7f); + if (!GameImage.FAIL_BACKGROUND.isScaled()) { + GameImage.FAIL_BACKGROUND.setImage(GameImage.FAIL_BACKGROUND.getImage().getScaledCopy(width, height)); + GameImage.FAIL_BACKGROUND.getImage().setAlpha(0.7f); + GameImage.FAIL_BACKGROUND.setScaled(); + } } }