Search in sources :

Example 1 with LifecycleListener

use of com.badlogic.gdx.LifecycleListener in project libgdx by libgdx.

the class LwjglApplication method mainLoop.

void mainLoop() {
    SnapshotArray<LifecycleListener> lifecycleListeners = this.lifecycleListeners;
    try {
        graphics.setupDisplay();
    } catch (LWJGLException e) {
        throw new GdxRuntimeException(e);
    }
    listener.create();
    graphics.resize = true;
    int lastWidth = graphics.getWidth();
    int lastHeight = graphics.getHeight();
    graphics.lastTime = System.nanoTime();
    boolean wasActive = true;
    while (running) {
        Display.processMessages();
        if (Display.isCloseRequested())
            exit();
        boolean isActive = Display.isActive();
        if (wasActive && !isActive) {
            // if it's just recently minimized from active state
            wasActive = false;
            synchronized (lifecycleListeners) {
                LifecycleListener[] listeners = lifecycleListeners.begin();
                for (int i = 0, n = lifecycleListeners.size; i < n; ++i) listeners[i].pause();
                lifecycleListeners.end();
            }
            listener.pause();
        }
        if (!wasActive && isActive) {
            // if it's just recently focused from minimized state
            wasActive = true;
            synchronized (lifecycleListeners) {
                LifecycleListener[] listeners = lifecycleListeners.begin();
                for (int i = 0, n = lifecycleListeners.size; i < n; ++i) listeners[i].resume();
                lifecycleListeners.end();
            }
            listener.resume();
        }
        boolean shouldRender = false;
        if (graphics.canvas != null) {
            int width = graphics.canvas.getWidth();
            int height = graphics.canvas.getHeight();
            if (lastWidth != width || lastHeight != height) {
                lastWidth = width;
                lastHeight = height;
                Gdx.gl.glViewport(0, 0, lastWidth, lastHeight);
                listener.resize(lastWidth, lastHeight);
                shouldRender = true;
            }
        } else {
            graphics.config.x = Display.getX();
            graphics.config.y = Display.getY();
            if (graphics.resize || Display.wasResized() || (int) (Display.getWidth() * Display.getPixelScaleFactor()) != graphics.config.width || (int) (Display.getHeight() * Display.getPixelScaleFactor()) != graphics.config.height) {
                graphics.resize = false;
                graphics.config.width = (int) (Display.getWidth() * Display.getPixelScaleFactor());
                graphics.config.height = (int) (Display.getHeight() * Display.getPixelScaleFactor());
                Gdx.gl.glViewport(0, 0, graphics.config.width, graphics.config.height);
                if (listener != null)
                    listener.resize(graphics.config.width, graphics.config.height);
                graphics.requestRendering();
            }
        }
        if (executeRunnables())
            shouldRender = true;
        // If one of the runnables set running to false, for example after an exit().
        if (!running)
            break;
        input.update();
        shouldRender |= graphics.shouldRender();
        input.processEvents();
        if (audio != null)
            audio.update();
        if (!isActive && graphics.config.backgroundFPS == -1)
            shouldRender = false;
        int frameRate = isActive ? graphics.config.foregroundFPS : graphics.config.backgroundFPS;
        if (shouldRender) {
            graphics.updateTime();
            graphics.frameId++;
            listener.render();
            Display.update(false);
        } else {
            // Sleeps to avoid wasting CPU in an empty loop.
            if (frameRate == -1)
                frameRate = 10;
            if (frameRate == 0)
                frameRate = graphics.config.backgroundFPS;
            if (frameRate == 0)
                frameRate = 30;
        }
        if (frameRate > 0)
            Display.sync(frameRate);
    }
    synchronized (lifecycleListeners) {
        LifecycleListener[] listeners = lifecycleListeners.begin();
        for (int i = 0, n = lifecycleListeners.size; i < n; ++i) {
            listeners[i].pause();
            listeners[i].dispose();
        }
        lifecycleListeners.end();
    }
    listener.pause();
    listener.dispose();
    Display.destroy();
    if (audio != null)
        audio.dispose();
    if (graphics.config.forceExit)
        System.exit(-1);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) LifecycleListener(com.badlogic.gdx.LifecycleListener) LWJGLException(org.lwjgl.LWJGLException)

Example 2 with LifecycleListener

use of com.badlogic.gdx.LifecycleListener in project libgdx by libgdx.

the class Lwjgl3Application method cleanupWindows.

private void cleanupWindows() {
    synchronized (lifecycleListeners) {
        for (LifecycleListener lifecycleListener : lifecycleListeners) {
            lifecycleListener.pause();
            lifecycleListener.dispose();
        }
    }
    for (Lwjgl3Window window : windows) {
        window.dispose();
    }
    windows.clear();
}
Also used : LifecycleListener(com.badlogic.gdx.LifecycleListener)

Example 3 with LifecycleListener

use of com.badlogic.gdx.LifecycleListener in project libgdx by libgdx.

the class JglfwApplication method end.

/** Called when the game loop has exited. */
protected void end() {
    synchronized (lifecycleListeners) {
        for (LifecycleListener listener : lifecycleListeners) {
            listener.pause();
            listener.dispose();
        }
    }
    listener.pause();
    listener.dispose();
    glfwTerminate();
    if (forceExit)
        System.exit(-1);
}
Also used : LifecycleListener(com.badlogic.gdx.LifecycleListener)

Example 4 with LifecycleListener

use of com.badlogic.gdx.LifecycleListener in project libgdx by libgdx.

the class AndroidDaydream method init.

private void init(ApplicationListener listener, AndroidApplicationConfiguration config, boolean isForView) {
    setApplicationLogger(new AndroidApplicationLogger());
    graphics = new AndroidGraphics(this, config, config.resolutionStrategy == null ? new FillResolutionStrategy() : config.resolutionStrategy);
    input = AndroidInputFactory.newAndroidInput(this, this, graphics.view, config);
    audio = new AndroidAudio(this, config);
    // workaround for Android bug #10515463
    this.getFilesDir();
    files = new AndroidFiles(this.getAssets(), this.getFilesDir().getAbsolutePath());
    net = new AndroidNet(this);
    this.listener = listener;
    this.handler = new Handler();
    this.clipboard = new AndroidClipboard(this);
    // Add a specialized audio lifecycle listener
    addLifecycleListener(new LifecycleListener() {

        @Override
        public void resume() {
            audio.resume();
        }

        @Override
        public void pause() {
            audio.pause();
        }

        @Override
        public void dispose() {
            audio.dispose();
            audio = null;
        }
    });
    Gdx.app = this;
    Gdx.input = this.getInput();
    Gdx.audio = this.getAudio();
    Gdx.files = this.getFiles();
    Gdx.graphics = this.getGraphics();
    Gdx.net = this.getNet();
    if (!isForView) {
        setFullscreen(true);
        setContentView(graphics.getView(), createLayoutParams());
    }
    createWakeLock(config.useWakelock);
    hideStatusBar(config);
}
Also used : FillResolutionStrategy(com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy) Handler(android.os.Handler) LifecycleListener(com.badlogic.gdx.LifecycleListener)

Example 5 with LifecycleListener

use of com.badlogic.gdx.LifecycleListener in project libgdx by libgdx.

the class IOSGraphics method resume.

public void resume() {
    if (!appPaused)
        return;
    appPaused = false;
    Array<LifecycleListener> listeners = app.lifecycleListeners;
    synchronized (listeners) {
        for (LifecycleListener listener : listeners) {
            listener.resume();
        }
    }
    app.listener.resume();
}
Also used : LifecycleListener(com.badlogic.gdx.LifecycleListener)

Aggregations

LifecycleListener (com.badlogic.gdx.LifecycleListener)17 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)5 Handler (android.os.Handler)4 FillResolutionStrategy (com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy)4 Method (java.lang.reflect.Method)3 Application (com.badlogic.gdx.Application)1 ApplicationType (com.badlogic.gdx.Application.ApplicationType)1 AndroidAudio (com.badlogic.gdx.backends.android.AndroidAudio)1 AndroidFiles (com.badlogic.gdx.backends.android.AndroidFiles)1 AndroidGraphics (com.badlogic.gdx.backends.android.AndroidGraphics)1 AndroidNet (com.badlogic.gdx.backends.android.AndroidNet)1 OpenALAudio (com.badlogic.gdx.backends.lwjgl3.audio.OpenALAudio)1 Array (com.badlogic.gdx.utils.Array)1 LWJGLException (org.lwjgl.LWJGLException)1