You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
3.8 KiB
189 lines
3.8 KiB
11 years ago
|
package junk;
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
import java.lang.Thread.UncaughtExceptionHandler;
|
||
11 years ago
|
|
||
11 years ago
|
import mightypork.gamecore.WorkDir;
|
||
|
import mightypork.gamecore.WorkDir.RouteSetup;
|
||
11 years ago
|
import mightypork.gamecore.backend.Backend;
|
||
11 years ago
|
import mightypork.gamecore.config.Config;
|
||
|
import mightypork.gamecore.config.ConfigSetup;
|
||
|
import mightypork.gamecore.config.KeySetup;
|
||
|
import mightypork.gamecore.core.App;
|
||
|
import mightypork.gamecore.core.MainLoop;
|
||
11 years ago
|
import mightypork.gamecore.gui.screens.ScreenRegistry;
|
||
11 years ago
|
import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay;
|
||
11 years ago
|
import mightypork.gamecore.input.InputSystem;
|
||
11 years ago
|
import mightypork.gamecore.resources.Res;
|
||
|
import mightypork.gamecore.resources.ResourceSetup;
|
||
11 years ago
|
import mightypork.gamecore.resources.audio.SoundSystem;
|
||
11 years ago
|
import mightypork.utils.logging.Log;
|
||
11 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Basic screen-based game with subsystems.<br>
|
||
|
* This class takes care of the initialization sequence.
|
||
|
*
|
||
11 years ago
|
* @author Ondřej Hruška (MightyPork)
|
||
11 years ago
|
*/
|
||
11 years ago
|
public abstract class BaseApp extends App implements UncaughtExceptionHandler {
|
||
11 years ago
|
|
||
|
// modules
|
||
11 years ago
|
private MainLoop gameLoop;
|
||
11 years ago
|
private ScreenRegistry screenRegistry;
|
||
11 years ago
|
|
||
11 years ago
|
private boolean started = false;
|
||
11 years ago
|
private boolean lockObtained = false;
|
||
11 years ago
|
|
||
11 years ago
|
// init opt holder
|
||
|
private final AppInitOptions opt = new AppInitOptions();
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get init options
|
||
|
*
|
||
|
* @return opt holder
|
||
|
*/
|
||
11 years ago
|
public AppInitOptions getInitOptions()
|
||
11 years ago
|
{
|
||
11 years ago
|
if (started) {
|
||
|
throw new IllegalStateException("Cannot alter init options after starting the App.");
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return opt;
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
11 years ago
|
public BaseApp(Backend backend) {
|
||
11 years ago
|
super(backend);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Start the application
|
||
|
*/
|
||
11 years ago
|
@Override
|
||
11 years ago
|
public final void start()
|
||
11 years ago
|
{
|
||
|
initialize();
|
||
|
|
||
|
Log.i("Starting main loop...");
|
||
|
|
||
11 years ago
|
// open first screen !!!
|
||
11 years ago
|
started = true;
|
||
11 years ago
|
gameLoop.start();
|
||
|
}
|
||
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Init the app
|
||
|
*/
|
||
11 years ago
|
protected void initialize()
|
||
|
{
|
||
11 years ago
|
WorkDir.init(opt.workdir);
|
||
|
|
||
11 years ago
|
if (opt.sigleInstance) initLock();
|
||
|
lockObtained = true;
|
||
11 years ago
|
|
||
|
for (final RouteSetup rs : opt.routeLists) {
|
||
|
WorkDir.registerRoutes(rs);
|
||
11 years ago
|
}
|
||
11 years ago
|
WorkDir.addPath("_screenshot_dir", opt.screenshotDir);
|
||
11 years ago
|
|
||
|
// apply configurations
|
||
|
Config.init(WorkDir.getFile(opt.configFile), opt.configComment);
|
||
11 years ago
|
|
||
|
// add keys to config
|
||
11 years ago
|
for (final KeySetup l : opt.keyLists) {
|
||
11 years ago
|
Config.registerKeys(l);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// add options to config
|
||
|
for (final ConfigSetup c : opt.configLists) {
|
||
|
Config.registerOptions(c);
|
||
11 years ago
|
}
|
||
|
Config.load();
|
||
|
|
||
11 years ago
|
/*
|
||
|
* Display
|
||
|
*/
|
||
|
Log.f2("Initializing Display System...");
|
||
11 years ago
|
initDisplay(gfx());
|
||
11 years ago
|
|
||
|
/*
|
||
|
* Audio
|
||
|
*/
|
||
|
Log.f2("Initializing Sound System...");
|
||
|
soundSystem = new SoundSystem(this);
|
||
|
initSoundSystem(soundSystem);
|
||
|
|
||
|
/*
|
||
|
* Input
|
||
|
*/
|
||
|
Log.f2("Initializing Input System...");
|
||
|
inputSystem = new InputSystem(this);
|
||
11 years ago
|
initInputSystem(inputSystem);
|
||
11 years ago
|
|
||
|
/*
|
||
|
* Prepare main loop
|
||
|
*/
|
||
|
Log.f1("Creating Screen Registry and Game Loop...");
|
||
|
screenRegistry = new ScreenRegistry(this);
|
||
11 years ago
|
gameLoop = createMainLoop();
|
||
11 years ago
|
gameLoop.setRootRenderable(screenRegistry);
|
||
|
|
||
|
/*
|
||
|
* Load resources
|
||
|
*
|
||
11 years ago
|
* Resources should be registered to registries, and AsyncResourceLoader will load them.
|
||
11 years ago
|
*/
|
||
|
Log.f1("Loading resources...");
|
||
11 years ago
|
if (opt.resourceLoader != null) {
|
||
|
opt.resourceLoader.init(this);
|
||
|
}
|
||
|
|
||
11 years ago
|
Res.init(this);
|
||
11 years ago
|
|
||
|
for (final ResourceSetup rl : opt.resourceLists) {
|
||
11 years ago
|
Res.load(rl);
|
||
|
}
|
||
11 years ago
|
|
||
|
/*
|
||
|
* Screen registry
|
||
|
*
|
||
|
* Must be after resources, because screens can request them during instantiation.
|
||
|
*/
|
||
|
Log.f2("Registering screens...");
|
||
|
initScreens(screenRegistry);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Register game screens to the registry.
|
||
|
*
|
||
|
* @param screens
|
||
|
*/
|
||
11 years ago
|
protected void initScreens(ScreenRegistry screens)
|
||
|
{
|
||
11 years ago
|
screens.addOverlay(new CrossfadeOverlay(this));
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Create game loop instance
|
||
|
*
|
||
|
* @return the game loop.
|
||
|
*/
|
||
11 years ago
|
protected MainLoop createMainLoop()
|
||
|
{
|
||
|
return new MainLoop(this);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
protected void beforeShutdown()
|
||
|
{
|
||
11 years ago
|
// ???
|
||
11 years ago
|
if (lockObtained) Config.save();
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|