Search in sources :

Example 1 with TerasologyEngineBuilder

use of org.terasology.engine.core.TerasologyEngineBuilder in project Terasology by MovingBlocks.

the class Terasology method call.

@Override
public Integer call() throws IOException {
    handleLaunchArguments();
    setupLogging();
    SplashScreen splashScreen;
    if (splashEnabled) {
        CountDownLatch splashInitLatch = new CountDownLatch(1);
        GLFWSplashScreen glfwSplash = new GLFWSplashScreen(splashInitLatch);
        Thread thread = new Thread(glfwSplash, "splashscreen-loop");
        thread.setDaemon(true);
        thread.start();
        try {
            // wait splash initialize... we will lose some post messages otherwise.
            // noinspection ResultOfMethodCallIgnored
            splashInitLatch.await(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
        // ignore
        }
        splashScreen = glfwSplash;
    } else {
        splashScreen = SplashScreenBuilder.createStub();
    }
    splashScreen.post("Java Runtime " + System.getProperty("java.version") + " loaded");
    try {
        TerasologyEngineBuilder builder = new TerasologyEngineBuilder();
        populateSubsystems(builder);
        TerasologyEngine engine = builder.build();
        engine.subscribe(newStatus -> {
            if (newStatus == StandardGameStatus.RUNNING) {
                splashScreen.close();
            } else {
                splashScreen.post(newStatus.getDescription());
            }
        });
        if (isHeadless) {
            engine.subscribeToStateChange(new HeadlessStateChangeListener(engine));
            engine.run(new StateHeadlessSetup());
        } else if (loadLastGame) {
            // initialize the managers first
            engine.initialize();
            GameScheduler.scheduleParallel("loadGame", () -> {
                GameManifest gameManifest = getLatestGameManifest();
                if (gameManifest != null) {
                    engine.changeState(new StateLoading(gameManifest, NetworkMode.NONE));
                }
            });
        } else {
            if (createLastGame) {
                engine.initialize();
                GameScheduler.scheduleParallel("createLastGame", () -> {
                    GameManifest gameManifest = getLatestGameManifest();
                    if (gameManifest != null) {
                        String title = gameManifest.getTitle();
                        if (!title.startsWith("New Created")) {
                            // if first time run
                            gameManifest.setTitle("New Created " + title + " 1");
                        } else {
                            // if not first time run
                            gameManifest.setTitle(getNewTitle(title));
                        }
                        engine.changeState(new StateLoading(gameManifest, NetworkMode.NONE));
                    }
                });
            }
            engine.run(new StateMainMenu());
        }
    } catch (Throwable e) {
        // also catch Errors such as UnsatisfiedLink, NoSuchMethodError, etc.
        splashScreen.close();
        reportException(e);
    }
    return 0;
}
Also used : TerasologyEngineBuilder(org.terasology.engine.core.TerasologyEngineBuilder) StateLoading(org.terasology.engine.core.modes.StateLoading) CountDownLatch(java.util.concurrent.CountDownLatch) StateHeadlessSetup(org.terasology.engine.core.subsystem.headless.mode.StateHeadlessSetup) TerasologyEngine(org.terasology.engine.core.TerasologyEngine) GameManifest(org.terasology.engine.game.GameManifest) HeadlessStateChangeListener(org.terasology.engine.core.subsystem.headless.mode.HeadlessStateChangeListener) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) SplashScreen(org.terasology.splash.SplashScreen)

Example 2 with TerasologyEngineBuilder

use of org.terasology.engine.core.TerasologyEngineBuilder in project Terasology by MovingBlocks.

the class TeraEd method run.

public void run() {
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    try {
        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, you can set the GUI to another look and feel.
        logger.warn("Failed to set look and feel to Nimbus", e);
    }
    try {
        LwjglPortlet portlet = new LwjglPortlet();
        PathManager.getInstance().useDefaultHomePath();
        engine = new TerasologyEngineBuilder().add(new LwjglTimer()).add(new LwjglAudio()).add(new AwtInput()).add(new BindsSubsystem()).add(portlet).build();
        if (!GLFW.glfwInit()) {
            throw new RuntimeException("Failed to initialize GLFW");
        }
        sceneProperties = new SceneProperties(engine);
        mainWindow = new MainWindow(this, engine);
        portlet.createCanvas();
        AWTGLCanvas canvas = portlet.getCanvas();
        engine.subscribeToStateChange(mainWindow);
        engine.initializeRun(new StateMainMenu());
        mainWindow.getViewport().setTerasology(canvas);
        portlet.initInputs();
        Runnable renderLoop = new Runnable() {

            public void run() {
                if (canvas.isValid()) {
                    canvas.render();
                }
                SwingUtilities.invokeLater(this);
            }
        };
        // Setup swing thread as game thread
        PerformanceMonitor.startActivity("Other");
        SwingUtilities.invokeAndWait(portlet::setupThreads);
        SwingUtilities.invokeLater(renderLoop);
        PerformanceMonitor.endActivity();
    } catch (Throwable t) {
        logger.error("Uncaught Exception", t);
    }
}
Also used : LwjglPortlet(org.terasology.editor.subsystem.LwjglPortlet) LwjglAudio(org.terasology.engine.core.subsystem.lwjgl.LwjglAudio) TerasologyEngineBuilder(org.terasology.engine.core.TerasologyEngineBuilder) UIManager(javax.swing.UIManager) AwtInput(org.terasology.editor.subsystem.AwtInput) BindsSubsystem(org.terasology.engine.core.subsystem.config.BindsSubsystem) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) SceneProperties(org.terasology.editor.properties.SceneProperties) MainWindow(org.terasology.editor.ui.MainWindow) LwjglTimer(org.terasology.engine.core.subsystem.lwjgl.LwjglTimer) AWTGLCanvas(org.lwjgl.opengl.awt.AWTGLCanvas)

Aggregations

TerasologyEngineBuilder (org.terasology.engine.core.TerasologyEngineBuilder)2 StateMainMenu (org.terasology.engine.core.modes.StateMainMenu)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 UIManager (javax.swing.UIManager)1 AWTGLCanvas (org.lwjgl.opengl.awt.AWTGLCanvas)1 SceneProperties (org.terasology.editor.properties.SceneProperties)1 AwtInput (org.terasology.editor.subsystem.AwtInput)1 LwjglPortlet (org.terasology.editor.subsystem.LwjglPortlet)1 MainWindow (org.terasology.editor.ui.MainWindow)1 TerasologyEngine (org.terasology.engine.core.TerasologyEngine)1 StateLoading (org.terasology.engine.core.modes.StateLoading)1 BindsSubsystem (org.terasology.engine.core.subsystem.config.BindsSubsystem)1 HeadlessStateChangeListener (org.terasology.engine.core.subsystem.headless.mode.HeadlessStateChangeListener)1 StateHeadlessSetup (org.terasology.engine.core.subsystem.headless.mode.StateHeadlessSetup)1 LwjglAudio (org.terasology.engine.core.subsystem.lwjgl.LwjglAudio)1 LwjglTimer (org.terasology.engine.core.subsystem.lwjgl.LwjglTimer)1 GameManifest (org.terasology.engine.game.GameManifest)1 SplashScreen (org.terasology.splash.SplashScreen)1