2015-01-18 20:29:48 +01:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
2015-05-17 03:25:19 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.Beatmap;
|
|
|
|
import itdelatrisu.opsu.beatmap.BeatmapSetList;
|
2015-08-21 17:25:52 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
|
2015-02-01 08:10:17 +01:00
|
|
|
import itdelatrisu.opsu.downloads.DownloadList;
|
2015-03-07 10:17:19 +01:00
|
|
|
import itdelatrisu.opsu.downloads.Updater;
|
2015-06-24 23:55:05 +02:00
|
|
|
import itdelatrisu.opsu.render.CurveRenderState;
|
2015-05-29 07:55:57 +02:00
|
|
|
import itdelatrisu.opsu.ui.UI;
|
2015-01-18 20:29:48 +01:00
|
|
|
|
2015-03-07 01:16:43 +01:00
|
|
|
import org.lwjgl.opengl.Display;
|
2015-01-18 20:29:48 +01:00
|
|
|
import org.newdawn.slick.AppGameContainer;
|
|
|
|
import org.newdawn.slick.Game;
|
|
|
|
import org.newdawn.slick.SlickException;
|
2015-01-21 01:01:18 +01:00
|
|
|
import org.newdawn.slick.opengl.InternalTextureLoader;
|
2015-01-18 20:29:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AppGameContainer extension that sends critical errors to ErrorHandler.
|
|
|
|
*/
|
|
|
|
public class Container extends AppGameContainer {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** SlickException causing game failure. */
|
2015-01-18 20:29:48 +01:00
|
|
|
protected SlickException e = null;
|
|
|
|
|
2016-09-27 17:36:10 +02:00
|
|
|
public static Container instance;
|
|
|
|
|
2015-01-18 20:29:48 +01:00
|
|
|
/**
|
|
|
|
* Create a new container wrapping a game
|
2015-01-22 06:44:45 +01:00
|
|
|
*
|
2015-01-18 20:29:48 +01:00
|
|
|
* @param game The game to be wrapped
|
|
|
|
* @throws SlickException Indicates a failure to initialise the display
|
|
|
|
*/
|
|
|
|
public Container(Game game) throws SlickException {
|
|
|
|
super(game);
|
2016-09-27 17:36:10 +02:00
|
|
|
instance = this;
|
2016-09-27 18:36:35 +02:00
|
|
|
width = this.getWidth();
|
|
|
|
height = this.getHeight();
|
2015-01-18 20:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void start() throws SlickException {
|
|
|
|
try {
|
|
|
|
setup();
|
|
|
|
getDelta();
|
|
|
|
while (running())
|
|
|
|
gameLoop();
|
|
|
|
} finally {
|
2015-01-21 01:01:18 +01:00
|
|
|
// destroy the game container
|
|
|
|
close_sub();
|
2015-01-18 20:29:48 +01:00
|
|
|
destroy();
|
2015-01-21 01:01:18 +01:00
|
|
|
|
|
|
|
// report any critical errors
|
2015-01-18 20:29:48 +01:00
|
|
|
if (e != null) {
|
|
|
|
ErrorHandler.error(null, e, true);
|
|
|
|
e = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 10:17:19 +01:00
|
|
|
if (forceExit) {
|
|
|
|
Opsu.close();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2015-01-18 20:29:48 +01:00
|
|
|
}
|
|
|
|
|
2015-03-07 01:16:43 +01:00
|
|
|
@Override
|
|
|
|
protected void gameLoop() throws SlickException {
|
|
|
|
int delta = getDelta();
|
|
|
|
if (!Display.isVisible() && updateOnlyOnVisible) {
|
|
|
|
try { Thread.sleep(100); } catch (Exception e) {}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
updateAndRender(delta);
|
|
|
|
} catch (SlickException e) {
|
|
|
|
this.e = e; // store exception to display later
|
|
|
|
running = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateFPS();
|
|
|
|
Display.update();
|
|
|
|
if (Display.isCloseRequested()) {
|
|
|
|
if (game.closeRequested())
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 01:01:18 +01:00
|
|
|
/**
|
|
|
|
* Actions to perform before destroying the game container.
|
|
|
|
*/
|
|
|
|
private void close_sub() {
|
|
|
|
// save user options
|
|
|
|
Options.saveOptions();
|
|
|
|
|
2015-05-25 04:17:45 +02:00
|
|
|
// reset cursor
|
2015-05-29 10:48:03 +02:00
|
|
|
UI.getCursor().reset();
|
2015-05-25 04:17:45 +02:00
|
|
|
|
2015-01-21 01:01:18 +01:00
|
|
|
// destroy images
|
|
|
|
InternalTextureLoader.get().clear();
|
2015-01-21 01:24:59 +01:00
|
|
|
|
2015-01-21 01:01:18 +01:00
|
|
|
// reset image references
|
|
|
|
GameImage.clearReferences();
|
2015-01-29 01:23:02 +01:00
|
|
|
GameData.Grade.clearReferences();
|
2015-09-02 20:29:52 +02:00
|
|
|
Beatmap.clearBackgroundImageCache();
|
2015-01-21 01:24:59 +01:00
|
|
|
|
|
|
|
// prevent loading tracks from re-initializing OpenAL
|
|
|
|
MusicController.reset();
|
2015-01-29 02:57:43 +01:00
|
|
|
|
2015-05-17 03:25:19 +02:00
|
|
|
// reset BeatmapSetList data
|
|
|
|
if (BeatmapSetList.get() != null)
|
|
|
|
BeatmapSetList.get().reset();
|
2015-06-25 00:21:15 +02:00
|
|
|
|
2015-06-24 23:55:05 +02:00
|
|
|
// delete OpenGL objects involved in the Curve rendering
|
|
|
|
CurveRenderState.shutdown();
|
2015-08-21 17:25:52 +02:00
|
|
|
|
|
|
|
// destroy watch service
|
|
|
|
if (!Options.isWatchServiceEnabled())
|
|
|
|
BeatmapWatchService.destroy();
|
|
|
|
BeatmapWatchService.removeListeners();
|
2015-01-21 01:01:18 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 08:10:17 +01:00
|
|
|
@Override
|
|
|
|
public void exit() {
|
|
|
|
// show confirmation dialog if any downloads are active
|
2015-03-07 10:17:19 +01:00
|
|
|
if (forceExit) {
|
|
|
|
if (DownloadList.get().hasActiveDownloads() &&
|
|
|
|
UI.showExitConfirmation(DownloadList.EXIT_CONFIRMATION))
|
|
|
|
return;
|
2015-03-07 21:38:59 +01:00
|
|
|
if (Updater.get().getStatus() == Updater.Status.UPDATE_DOWNLOADING &&
|
2015-03-07 10:17:19 +01:00
|
|
|
UI.showExitConfirmation(Updater.EXIT_CONFIRMATION))
|
|
|
|
return;
|
|
|
|
}
|
2015-02-01 08:10:17 +01:00
|
|
|
|
|
|
|
super.exit();
|
|
|
|
}
|
2015-01-18 20:29:48 +01:00
|
|
|
}
|