simplifying splash state

This commit is contained in:
yugecin 2017-02-06 10:38:35 +01:00
parent 80e1d6eb12
commit f2799a2500

View File

@ -20,17 +20,13 @@ package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage; import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.Options; import itdelatrisu.opsu.Options;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.audio.SoundController; import itdelatrisu.opsu.audio.SoundController;
import itdelatrisu.opsu.beatmap.BeatmapParser; import itdelatrisu.opsu.beatmap.BeatmapParser;
import itdelatrisu.opsu.beatmap.BeatmapSetList; import itdelatrisu.opsu.beatmap.BeatmapSetList;
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
import itdelatrisu.opsu.beatmap.OszUnpacker; import itdelatrisu.opsu.beatmap.OszUnpacker;
import itdelatrisu.opsu.replay.ReplayImporter; import itdelatrisu.opsu.replay.ReplayImporter;
import itdelatrisu.opsu.ui.UI; import itdelatrisu.opsu.ui.UI;
import itdelatrisu.opsu.ui.animations.AnimatedValue;
import itdelatrisu.opsu.ui.animations.AnimationEquation;
import java.io.File; import java.io.File;
@ -39,7 +35,6 @@ import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input; import org.newdawn.slick.Input;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.inject.Inject; import yugecin.opsudance.core.inject.Inject;
import yugecin.opsudance.core.inject.InstanceContainer;
import yugecin.opsudance.core.state.BaseOpsuState; import yugecin.opsudance.core.state.BaseOpsuState;
/** /**
@ -50,90 +45,32 @@ import yugecin.opsudance.core.state.BaseOpsuState;
public class Splash extends BaseOpsuState { public class Splash extends BaseOpsuState {
@Inject @Inject
private InstanceContainer instanceContainer; private SongMenu songMenu;
/** Minimum time, in milliseconds, to display the splash screen (and fade in the logo). */
private static final int MIN_SPLASH_TIME = 400;
/** Whether or not loading has completed. */ /** Whether or not loading has completed. */
private boolean finished = false; private boolean finished;
/** Loading thread. */ /** Loading thread. */
private Thread thread; private Thread thread;
/** Number of times the 'Esc' key has been pressed. */ /** Number of times the escape key has been pressed, to exit. */
private int escapeCount = 0; private int escapeCount = 0;
/** Whether the skin being loaded is a new skin (for program restarts). */ /** Wether the loading progress was inited. */
private boolean newSkin = false; private boolean inited;
/** Whether the watch service is newly enabled (for program restarts). */
private boolean watchServiceChange = false;
/** Logo alpha level. */
private AnimatedValue logoAlpha;
// game-related variables
private boolean init = false;
@Override @Override
protected void revalidate() { protected void revalidate() {
super.revalidate(); super.revalidate();
// TODO d check if below is needed
// check if skin changed
if (Options.getSkin() != null)
this.newSkin = (Options.getSkin().getDirectory() != Options.getSkinDir());
// check if watch service newly enabled
this.watchServiceChange = Options.isWatchServiceEnabled() && BeatmapWatchService.get() == null;
// fade in logo
this.logoAlpha = new AnimatedValue(MIN_SPLASH_TIME, 0f, 1f, AnimationEquation.LINEAR);
GameImage.MENU_LOGO.getImage().setAlpha(0f);
// pre-revalidate some states to reduce lag between switching // pre-revalidate some states to reduce lag between switching
instanceContainer.provide(SongMenu.class).revalidate(); songMenu.revalidate();
if (inited) {
return;
} }
@Override inited = true;
public void render(Graphics g) {
g.setBackground(Color.black);
GameImage.MENU_LOGO.getImage().drawCentered(displayContainer.width / 2, displayContainer.height / 2);
UI.drawLoadingProgress(g);
}
@Override
public void preRenderUpdate() {
if (!init) {
init = true;
// resources already loaded (from application restart)
if (BeatmapSetList.get() != null) {
if (newSkin || watchServiceChange) { // need to reload resources
thread = new Thread() {
@Override
public void run() {
// reload beatmaps if watch service newly enabled
if (watchServiceChange)
BeatmapParser.parseAllFiles(Options.getBeatmapDir());
// reload sounds if skin changed
// TODO: only reload each sound if actually needed?
if (newSkin)
SoundController.init();
finished = true;
thread = null;
}
};
thread.start();
} else // don't reload anything
finished = true;
}
// load all resources in a new thread
else {
thread = new Thread() { thread = new Thread() {
@Override @Override
public void run() { public void run() {
@ -157,39 +94,50 @@ public class Splash extends BaseOpsuState {
}; };
thread.start(); thread.start();
} }
@Override
public void preRenderUpdate() {
// change states when loading complete
if (!finished) {
return;
} }
// fade in logo
if (logoAlpha.update(displayContainer.renderDelta))
GameImage.MENU_LOGO.getImage().setAlpha(logoAlpha.getValue());
// change states when loading complete
if (finished && logoAlpha.getValue() >= 1f) {
// initialize song list // initialize song list
if (BeatmapSetList.get().size() > 0) { if (BeatmapSetList.get().size() == 0) {
MusicController.playThemeSong();
displayContainer.switchStateInstantly(MainMenu.class);
return;
}
BeatmapSetList.get().init(); BeatmapSetList.get().init();
if (Options.isThemeSongEnabled()) { if (Options.isThemeSongEnabled()) {
MusicController.playThemeSong(); MusicController.playThemeSong();
} else { } else {
instanceContainer.provide(SongMenu.class).setFocus(BeatmapSetList.get().getRandomNode(), -1, true, true); songMenu.setFocus(BeatmapSetList.get().getRandomNode(), -1, true, true);
}
} else {
MusicController.playThemeSong();
}
displayContainer.switchState(MainMenu.class);
} }
displayContainer.switchStateInstantly(MainMenu.class);
} }
@Override
public void render(Graphics g) {
g.setBackground(Color.black);
GameImage.MENU_LOGO.getImage().drawCentered(displayContainer.width / 2, displayContainer.height / 2);
UI.drawLoadingProgress(g);
}
@Override @Override
public boolean onCloseRequest() { public boolean onCloseRequest() {
if (thread != null && thread.isAlive()) { if (thread == null || !thread.isAlive()) {
return true;
}
thread.interrupt(); thread.interrupt();
try { try {
thread.join(); thread.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.warn("InterruptedException while waiting for splash thread to die", e); Log.warn("InterruptedException while waiting for splash thread to die", e);
} }
}
return true; return true;
} }
@ -205,4 +153,5 @@ public class Splash extends BaseOpsuState {
} }
return true; return true;
} }
} }