Preload game images when program initializes.

Reverted earlier change to load images immediately before a game begins, since there is a noticeable lag in some cases.

Other changes:
- Added 'preload' field to GameImage.
- Changed 'gameImage' field in GameImage to 'skinnable' (more suitable name).

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-18 17:01:13 -05:00
parent 53158fd310
commit 95775d8e73
3 changed files with 59 additions and 42 deletions

View File

@ -208,103 +208,103 @@ public enum GameImage {
SCORE_X ("score-x", "png"), SCORE_X ("score-x", "png"),
// Non-Game Components // Non-Game Components
MENU_BACK ("menu-back", "png", false) { MENU_BACK ("menu-back", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.1f) / img.getHeight()); return img.getScaledCopy((h * 0.1f) / img.getHeight());
} }
}, },
MENU_BUTTON_BG ("menu-button-background", "png", false) { MENU_BUTTON_BG ("menu-button-background", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(w / 2, h / 6); return img.getScaledCopy(w / 2, h / 6);
} }
}, },
MENU_TAB ("selection-tab", "png", false) { MENU_TAB ("selection-tab", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.033f) / img.getHeight()); return img.getScaledCopy((h * 0.033f) / img.getHeight());
} }
}, },
MENU_SEARCH ("search", "png", false) { MENU_SEARCH ("search", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight()); return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight());
} }
}, },
MENU_OPTIONS ("options", "png", false) { MENU_OPTIONS ("options", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight()); return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight());
} }
}, },
MENU_MUSICNOTE ("music-note", "png", false) { MENU_MUSICNOTE ("music-note", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight()); int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight());
return img.getScaledCopy(r, r); return img.getScaledCopy(r, r);
} }
}, },
MENU_LOADER ("loader", "png", false) { MENU_LOADER ("loader", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight()); int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight());
return img.getScaledCopy(r / 48f); return img.getScaledCopy(r / 48f);
} }
}, },
MENU_BG ("menu-background", "png|jpg", false) { MENU_BG ("menu-background", "png|jpg", false, true) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
img.setAlpha(0.9f); img.setAlpha(0.9f);
return img.getScaledCopy(w, h); return img.getScaledCopy(w, h);
} }
}, },
MENU_LOGO ("logo", "png", false) { MENU_LOGO ("logo", "png", false, true) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h / 1.2f) / img.getHeight()); return img.getScaledCopy((h / 1.2f) / img.getHeight());
} }
}, },
MENU_PlAY ("menu-play", "png", false) { MENU_PlAY ("menu-play", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(MENU_LOGO.getImage().getWidth() * 0.83f / img.getWidth()); return img.getScaledCopy(MENU_LOGO.getImage().getWidth() * 0.83f / img.getWidth());
} }
}, },
MENU_EXIT ("menu-exit", "png", false) { MENU_EXIT ("menu-exit", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(MENU_LOGO.getImage().getWidth() * 0.66f / img.getWidth()); return img.getScaledCopy(MENU_LOGO.getImage().getWidth() * 0.66f / img.getWidth());
} }
}, },
MENU_BUTTON_MID ("button-middle", "png", false) { MENU_BUTTON_MID ("button-middle", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(w / 2, img.getHeight()); return img.getScaledCopy(w / 2, img.getHeight());
} }
}, },
MENU_BUTTON_LEFT ("button-left", "png", false), MENU_BUTTON_LEFT ("button-left", "png", false, false),
MENU_BUTTON_RIGHT ("button-right", "png", false), MENU_BUTTON_RIGHT ("button-right", "png", false, false),
MUSIC_PLAY ("music-play", "png", false), MUSIC_PLAY ("music-play", "png", false, false),
MUSIC_PAUSE ("music-pause", "png", false), MUSIC_PAUSE ("music-pause", "png", false, false),
MUSIC_NEXT ("music-next", "png", false), MUSIC_NEXT ("music-next", "png", false, false),
MUSIC_PREVIOUS ("music-previous", "png", false), MUSIC_PREVIOUS ("music-previous", "png", false, false),
RANKING_RETRY ("ranking-retry", "png", false) { RANKING_RETRY ("ranking-retry", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.15f) / img.getHeight()); return img.getScaledCopy((h * 0.15f) / img.getHeight());
} }
}, },
RANKING_EXIT ("ranking-back", "png", false) { RANKING_EXIT ("ranking-back", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.15f) / img.getHeight()); return img.getScaledCopy((h * 0.15f) / img.getHeight());
} }
}, },
REPOSITORY ("repo", "png", false) { REPOSITORY ("repo", "png", false, false) {
@Override @Override
protected Image process_sub(Image img, int w, int h) { protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h / 17f) / img.getHeight()); return img.getScaledCopy((h / 17f) / img.getHeight());
@ -329,10 +329,15 @@ public enum GameImage {
private byte type; private byte type;
/** /**
* Whether or not the image is related to gameplay. * Whether or not the image is skinnable by a beatmap.
* Game images are skinnable per beatmap, while other images are not. * These images are typically related to gameplay.
*/ */
private boolean gameImage; private boolean skinnable;
/**
* Whether or not to preload the image when the program starts.
*/
private boolean preload;
/** /**
* The default image. * The default image.
@ -370,7 +375,7 @@ public enum GameImage {
public static void destroySkinImages() { public static void destroySkinImages() {
if (skinImageLoaded) { if (skinImageLoaded) {
for (GameImage img : GameImage.values()) { for (GameImage img : GameImage.values()) {
if (img.isGameImage()) if (img.isSkinnable())
img.destroySkinImage(); img.destroySkinImage();
} }
skinImageLoaded = false; skinImageLoaded = false;
@ -395,33 +400,42 @@ public enum GameImage {
} }
/** /**
* Constructor. * Constructor for game-related images (skinnable and preloaded).
* @param filename the image file name * @param filename the image file name
* @param type the file types (separated by '|') * @param type the file types (separated by '|')
*/ */
GameImage(String filename, String type) { GameImage(String filename, String type) {
this.filename = filename; this.filename = filename;
this.type = getType(type); this.type = getType(type);
this.gameImage = true; this.skinnable = true;
this.preload = true;
} }
/** /**
* Constructor. * Constructor for general images.
* @param filename the image file name * @param filename the image file name
* @param type the file types (separated by '|') * @param type the file types (separated by '|')
* @param gameImage whether or not the image is related to gameplay * @param skinnable whether or not the image is skinnable
* @param preload whether or not to preload the image
*/ */
GameImage(String filename, String type, boolean gameImage) { GameImage(String filename, String type, boolean skinnable, boolean preload) {
this.filename = filename; this.filename = filename;
this.type = getType(type); this.type = getType(type);
this.gameImage = gameImage; this.skinnable = skinnable;
this.preload = preload;
} }
/** /**
* Returns whether or not the image is related to gameplay. * Returns whether or not the image is skinnable.
* @return true if game image * @return true if skinnable
*/ */
public boolean isGameImage() { return gameImage; } public boolean isSkinnable() { return skinnable; }
/**
* Returns whether or not to preload the image when the program starts.
* @return true if preload
*/
public boolean isPreload() { return preload; }
/** /**
* Returns the image associated with this resource. * Returns the image associated with this resource.

View File

@ -188,8 +188,10 @@ public class Utils {
// initialize game images // initialize game images
GameImage.init(width, height); GameImage.init(width, height);
GameImage.MENU_LOGO.setDefaultImage(); for (GameImage img : GameImage.values()) {
GameImage.MENU_BG.setDefaultImage(); if (img.isPreload())
img.setDefaultImage();
}
// initialize game mods // initialize game mods
for (GameMod mod : GameMod.values()) for (GameMod mod : GameMod.values())

View File

@ -828,19 +828,16 @@ public class Game extends BasicGameState {
/** /**
* Loads all game images. * Loads all game images.
* @throws SlickException
*/ */
private void loadImages() throws SlickException { private void loadImages() {
int width = container.getWidth(); int width = container.getWidth();
int height = container.getHeight(); int height = container.getHeight();
// set images // set images
File parent = osu.getFile().getParentFile(); File parent = osu.getFile().getParentFile();
for (GameImage img : GameImage.values()) { for (GameImage img : GameImage.values()) {
if (img.isGameImage()) { if (img.isSkinnable())
img.setDefaultImage(); // ensure that default image has been loaded
img.setSkinImage(parent); img.setSkinImage(parent);
}
} }
// skip button // skip button
@ -852,7 +849,11 @@ public class Game extends BasicGameState {
// load other images... // load other images...
((GamePauseMenu) game.getState(Opsu.STATE_GAMEPAUSEMENU)).loadImages(); ((GamePauseMenu) game.getState(Opsu.STATE_GAMEPAUSEMENU)).loadImages();
score.loadImages(osu.getFile().getParentFile()); try {
score.loadImages(osu.getFile().getParentFile());
} catch (Exception e) {
ErrorHandler.error("Failed to load GameScore images.", e, false);
}
} }
/** /**