Factored out BaseApp.

This commit is contained in:
Ondřej Hruška
2014-04-09 01:28:32 +02:00
parent e4e7aa7eb3
commit e03d547fab
8 changed files with 346 additions and 205 deletions
+48 -185
View File
@@ -1,19 +1,14 @@
package mightypork.rogue;
import java.io.File;
import java.util.Locale;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import mightypork.gamecore.SlickLogRedirector;
import mightypork.gamecore.audio.SoundSystem;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.BaseApp;
import mightypork.gamecore.control.GameLoop;
import mightypork.gamecore.control.bus.EventBus;
import mightypork.gamecore.control.bus.events.*;
import mightypork.gamecore.control.interf.Destroyable;
import mightypork.gamecore.control.interf.Updateable;
import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.input.KeyStroke;
@@ -24,7 +19,6 @@ import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.rogue.screens.test_bouncyboxes.ScreenTestBouncy;
import mightypork.rogue.screens.test_cat_sound.ScreenTestCat;
import mightypork.rogue.screens.test_font.ScreenTestFont;
import mightypork.utils.files.InstanceLock;
import mightypork.utils.logging.Log;
import mightypork.utils.logging.LogInstance;
@@ -34,18 +28,10 @@ import mightypork.utils.logging.LogInstance;
*
* @author MightyPork
*/
public class App implements AppAccess {
public class App extends BaseApp {
/** instance pointer */
private static App inst;
// modules
private InputSystem inputSystem;
private DisplaySystem displaySystem;
private SoundSystem soundSystem;
private EventBus eventBus;
private GameLoop mainLoop;
private ScreenRegistry screens;
private static BaseApp inst;
/**
@@ -69,20 +55,6 @@ public class App implements AppAccess {
}
/**
* Start the application
*/
private void start()
{
initialize();
Log.i("Starting main loop...");
// open first screen
mainLoop.start();
}
/**
* Handle a crash
*
@@ -102,132 +74,43 @@ public class App implements AppAccess {
@Override
public void shutdown()
{
Log.i("Shutting down subsystems...");
if (getEventBus() != null) {
getEventBus().send(new DestroyEvent());
getEventBus().destroy();
}
Log.i("Terminating...");
System.exit(0);
}
public void initialize()
protected void preInit()
{
// to get dot instead of comma in floats
Locale.setDefault(Locale.ENGLISH);
/*
* Lock working directory
*/
initLock();
/*
* Setup logging
*/
}
@Override
protected LogInstance createLog()
{
final LogInstance log = Log.create("runtime", Paths.LOGS, 10);
log.setFileLevel(Level.WARNING);
log.setSysoutLevel(Level.ALL);
log.enable(Config.LOGGING_ENABLED);
log.enableSysout(Config.LOG_TO_STDOUT);
org.newdawn.slick.util.Log.setLogSystem(new SlickLogRedirector(log));
Log.f1("Initializing subsystems...");
/*
* Event bus
*/
Log.f2("Initializing Event Bus...");
eventBus = new EventBus();
eventBus.detailedLogging = true;
initChannels();
/*
* Display
*/
Log.f2("Initializing Display System...");
displaySystem = new DisplaySystem(this);
displaySystem.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR);
displaySystem.setTargetFps(Const.FPS_RENDER);
/*
* Audio
*/
Log.f2("Initializing Sound System...");
soundSystem = new SoundSystem(this);
soundSystem.setMasterVolume(1);
/*
* Input
*/
Log.f2("Initializing Input System...");
inputSystem = new InputSystem(this);
setupGlobalKeystrokes();
/*
* Prepare main loop
*/
Log.f1("Preparing game systems...");
screens = new ScreenRegistry(this);
mainLoop = new MainLoop(this, screens);
/*
* Load resources
*/
Log.f1("Loading resources...");
Res.load(this);
/*
* Screen registry
*/
Log.f2("Initializing screens...");
initScreens();
return log;
}
private void initScreens()
@Override
protected void initDisplay(DisplaySystem display)
{
Log.f3("Registering game screens...");
screens.add(new ScreenTestBouncy(this));
screens.add(new ScreenTestCat(this));
screens.add(new ScreenTestFont(this));
screens.showScreen("test.bouncy");
display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR);
display.setTargetFps(Const.FPS_RENDER);
}
private void initChannels()
@Override
protected void initSoundSystem(SoundSystem audio)
{
Log.f3("Registering channels...");
// framework events
getEventBus().addChannel(DestroyEvent.class, Destroyable.class);
getEventBus().addChannel(UpdateEvent.class, Updateable.class);
// input events
getEventBus().addChannel(ScreenChangeEvent.class, ScreenChangeEvent.Listener.class);
getEventBus().addChannel(KeyEvent.class, KeyEvent.Listener.class);
getEventBus().addChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class);
getEventBus().addChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class);
// control events
getEventBus().addChannel(ScreenRequestEvent.class, ScreenRequestEvent.Listener.class);
getEventBus().addChannel(ResourceLoadRequest.class, ResourceLoadRequest.Listener.class);
getEventBus().addChannel(ActionRequest.class, ActionRequest.Listener.class);
getEventBus().addChannel(MainLoopTaskRequest.class, MainLoopTaskRequest.Listener.class);
audio.setMasterVolume(1);
}
private void setupGlobalKeystrokes()
@Override
protected void initKeystrokes(InputSystem input)
{
Log.f3("Setting up hot keys...");
// Go fullscreen
getInput().bindKeyStroke(new KeyStroke(Keys.KEY_F11), new Runnable() {
@@ -260,65 +143,45 @@ public class App implements AppAccess {
}
private void initLock()
@Override
protected GameLoop createLoop()
{
if (!Config.SINGLE_INSTANCE) return;
return new MainLoop(this);
}
@Override
protected void initResources()
{
Res.load(this);
}
@Override
protected void initScreens(ScreenRegistry screens)
{
screens.add(new ScreenTestBouncy(this));
screens.add(new ScreenTestCat(this));
screens.add(new ScreenTestFont(this));
if (!InstanceLock.onFile(Paths.LOCK)) {
System.err.println("Could not obtain lock.\nOnly one instance can run at a time.");
//@formatter:off
JOptionPane.showMessageDialog(
null,
"Another instance is already running.",
"Instance error",
JOptionPane.ERROR_MESSAGE
);
//@formatter:on
shutdown();
return;
}
screens.showScreen("test.bouncy");
}
/**
* @return sound system of the running instance
*/
@Override
public SoundSystem getSoundSystem()
protected File getLockFile()
{
return soundSystem;
return Paths.LOCK;
}
/**
* @return input system of the running instance
*/
@Override
public InputSystem getInput()
protected void initChannels(EventBus bus)
{
return inputSystem;
}
/**
* @return display system of the running instance
*/
@Override
public DisplaySystem getDisplay()
{
return displaySystem;
}
/**
* @return event bus
*/
@Override
public EventBus getEventBus()
{
return eventBus;
super.initChannels(bus);
// custom channels
bus.addChannel(ActionRequest.class, ActionRequest.Listener.class);
}
}
+3 -3
View File
@@ -1,8 +1,8 @@
package mightypork.rogue;
import mightypork.gamecore.control.BaseApp;
import mightypork.gamecore.control.GameLoop;
import mightypork.gamecore.gui.renderers.Renderable;
import mightypork.gamecore.input.Action;
import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType;
@@ -11,8 +11,8 @@ import mightypork.rogue.util.Utils;
public class MainLoop extends GameLoop implements ActionRequest.Listener {
public MainLoop(App app, Renderable masterRenderable) {
super(app, masterRenderable);
public MainLoop(BaseApp app) {
super(app);
}
+1 -1
View File
@@ -9,7 +9,7 @@ import mightypork.utils.files.OsUtils;
public class Paths {
private static final String APPDIR_NAME = "rogue";
public static final File WORKDIR = OsUtils.getWorkDir(APPDIR_NAME);
public static final File LOGS = OsUtils.getWorkDir(APPDIR_NAME, "logs");
public static final File SCREENSHOTS = OsUtils.getWorkDir(APPDIR_NAME, "screenshots");
+2 -4
View File
@@ -5,7 +5,7 @@ import mightypork.gamecore.audio.SoundBank;
import mightypork.gamecore.audio.players.EffectPlayer;
import mightypork.gamecore.audio.players.LoopPlayer;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.loading.AsyncResourceLoader;
import mightypork.gamecore.control.BaseApp;
import mightypork.gamecore.render.fonts.DeferredFont;
import mightypork.gamecore.render.fonts.DeferredFont.FontStyle;
import mightypork.gamecore.render.fonts.FontBank;
@@ -37,13 +37,11 @@ public class Res {
*
* @param app app access
*/
public static void load(App app)
public static void load(BaseApp app)
{
if (initialized) return;
initialized = true;
AsyncResourceLoader.launch(app);
textures = new TextureBank(app);
sounds = new SoundBank(app);
fonts = new FontBank(app);
@@ -0,0 +1,67 @@
package mightypork.rogue.util;
import mightypork.utils.logging.LogInstance;
import org.newdawn.slick.util.LogSystem;
public class SlickLogRedirector implements LogSystem {
LogInstance l;
public SlickLogRedirector(LogInstance log) {
this.l = log;
}
@Override
public void error(String msg, Throwable e)
{
l.e(msg, e);
}
@Override
public void error(Throwable e)
{
l.e(e);
}
@Override
public void error(String msg)
{
l.e(msg);
}
@Override
public void warn(String msg)
{
l.w(msg);
}
@Override
public void warn(String msg, Throwable e)
{
l.e(msg, e);
}
@Override
public void info(String msg)
{
l.i(msg);
}
@Override
public void debug(String msg)
{
l.f3(msg);
}
}