Search in sources :

Example 6 with LifecycleListener

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

the class Controllers method initialize.

private static void initialize() {
    if (managers.containsKey(Gdx.app))
        return;
    String className = null;
    ApplicationType type = Gdx.app.getType();
    ControllerManager manager = null;
    if (type == ApplicationType.Android) {
        if (Gdx.app.getVersion() >= 12) {
            className = "com.badlogic.gdx.controllers.android.AndroidControllers";
        } else {
            Gdx.app.log(TAG, "No controller manager is available for Android versions < API level 12");
            manager = new ControllerManagerStub();
        }
    } else if (type == ApplicationType.Desktop) {
        if (Gdx.graphics.getType() == GraphicsType.LWJGL3) {
            className = "com.badlogic.gdx.controllers.lwjgl3.Lwjgl3ControllerManager";
        } else {
            className = "com.badlogic.gdx.controllers.desktop.DesktopControllerManager";
        }
    } else if (type == ApplicationType.WebGL) {
        className = "com.badlogic.gdx.controllers.gwt.GwtControllers";
    } else {
        Gdx.app.log(TAG, "No controller manager is available for: " + Gdx.app.getType());
        manager = new ControllerManagerStub();
    }
    if (manager == null) {
        try {
            Class controllerManagerClass = ClassReflection.forName(className);
            manager = (ControllerManager) ClassReflection.newInstance(controllerManagerClass);
        } catch (Throwable ex) {
            throw new GdxRuntimeException("Error creating controller manager: " + className, ex);
        }
    }
    managers.put(Gdx.app, manager);
    final Application app = Gdx.app;
    Gdx.app.addLifecycleListener(new LifecycleListener() {

        @Override
        public void resume() {
        }

        @Override
        public void pause() {
        }

        @Override
        public void dispose() {
            managers.remove(app);
            Gdx.app.log(TAG, "removed manager for application, " + managers.size + " managers active");
        }
    });
    Gdx.app.log(TAG, "added manager for application, " + managers.size + " managers active");
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ApplicationType(com.badlogic.gdx.Application.ApplicationType) LifecycleListener(com.badlogic.gdx.LifecycleListener) Application(com.badlogic.gdx.Application)

Example 7 with LifecycleListener

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

the class AndroidFragmentApplication method initializeForView.

/** This method has to be called in the
	 * {@link Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)} method. It sets up all
	 * the things necessary to get input, render via OpenGL and so on. You can configure other aspects of the application with the
	 * rest of the fields in the {@link AndroidApplicationConfiguration} instance.
	 * <p/>
	 * Note: you have to return the returned view from
	 * {@link Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)}
	 * 
	 * @param listener the {@link ApplicationListener} implementing the program logic
	 * @param config the {@link AndroidApplicationConfiguration}, defining various settings of the application (use accelerometer,
	 *           etc.).
	 * @return the GLSurfaceView of the application */
public View initializeForView(ApplicationListener listener, AndroidApplicationConfiguration config) {
    if (this.getVersion() < MINIMUM_SDK) {
        throw new GdxRuntimeException("LibGDX requires Android API Level " + MINIMUM_SDK + " or later.");
    }
    setApplicationLogger(new AndroidApplicationLogger());
    graphics = new AndroidGraphics(this, config, config.resolutionStrategy == null ? new FillResolutionStrategy() : config.resolutionStrategy);
    input = AndroidInputFactory.newAndroidInput(this, getActivity(), graphics.view, config);
    audio = new AndroidAudio(getActivity(), config);
    files = new AndroidFiles(getResources().getAssets(), getActivity().getFilesDir().getAbsolutePath());
    net = new AndroidNet(this);
    this.listener = listener;
    this.handler = new Handler();
    this.clipboard = new AndroidClipboard(getActivity());
    // 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();
        }
    });
    Gdx.app = this;
    Gdx.input = this.getInput();
    Gdx.audio = this.getAudio();
    Gdx.files = this.getFiles();
    Gdx.graphics = this.getGraphics();
    Gdx.net = this.getNet();
    createWakeLock(config.useWakelock);
    useImmersiveMode(config.useImmersiveMode);
    if (config.useImmersiveMode && getVersion() >= Build.VERSION_CODES.KITKAT) {
        try {
            Class<?> vlistener = Class.forName("com.badlogic.gdx.backends.android.AndroidVisibilityListener");
            Object o = vlistener.newInstance();
            Method method = vlistener.getDeclaredMethod("createListener", AndroidApplicationBase.class);
            method.invoke(o, this);
        } catch (Exception e) {
            log("AndroidApplication", "Failed to create AndroidVisibilityListener", e);
        }
    }
    return graphics.getView();
}
Also used : FillResolutionStrategy(com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy) Handler(android.os.Handler) LifecycleListener(com.badlogic.gdx.LifecycleListener) Method(java.lang.reflect.Method) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 8 with LifecycleListener

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

the class HeadlessApplication method mainLoop.

void mainLoop() {
    Array<LifecycleListener> lifecycleListeners = this.lifecycleListeners;
    listener.create();
    // unlike LwjglApplication, a headless application will eat up CPU in this while loop
    // it is up to the implementation to call Thread.sleep as necessary
    long t = TimeUtils.nanoTime() + renderInterval;
    if (renderInterval >= 0f) {
        while (running) {
            final long n = TimeUtils.nanoTime();
            if (t > n) {
                try {
                    Thread.sleep((t - n) / 1000000);
                } catch (InterruptedException e) {
                }
                t = TimeUtils.nanoTime() + renderInterval;
            } else
                t = n + renderInterval;
            executeRunnables();
            graphics.incrementFrameId();
            listener.render();
            graphics.updateTime();
            // If one of the runnables set running to false, for example after an exit().
            if (!running)
                break;
        }
    }
    synchronized (lifecycleListeners) {
        for (LifecycleListener listener : lifecycleListeners) {
            listener.pause();
            listener.dispose();
        }
    }
    listener.pause();
    listener.dispose();
}
Also used : LifecycleListener(com.badlogic.gdx.LifecycleListener)

Example 9 with LifecycleListener

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

the class AndroidApplication method init.

private void init(ApplicationListener listener, AndroidApplicationConfiguration config, boolean isForView) {
    if (this.getVersion() < MINIMUM_SDK) {
        throw new GdxRuntimeException("LibGDX requires Android API Level " + MINIMUM_SDK + " or later.");
    }
    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.useImmersiveMode = config.useImmersiveMode;
    this.hideStatusBar = config.hideStatusBar;
    this.clipboard = new AndroidClipboard(this);
    // Add a specialized audio lifecycle listener
    addLifecycleListener(new LifecycleListener() {

        @Override
        public void resume() {
        // No need to resume audio here
        }

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

        @Override
        public void dispose() {
            audio.dispose();
        }
    });
    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) {
        try {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        } catch (Exception ex) {
            log("AndroidApplication", "Content already displayed, cannot request FEATURE_NO_TITLE", ex);
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setContentView(graphics.getView(), createLayoutParams());
    }
    createWakeLock(config.useWakelock);
    hideStatusBar(this.hideStatusBar);
    useImmersiveMode(this.useImmersiveMode);
    if (this.useImmersiveMode && getVersion() >= Build.VERSION_CODES.KITKAT) {
        try {
            Class<?> vlistener = Class.forName("com.badlogic.gdx.backends.android.AndroidVisibilityListener");
            Object o = vlistener.newInstance();
            Method method = vlistener.getDeclaredMethod("createListener", AndroidApplicationBase.class);
            method.invoke(o, this);
        } catch (Exception e) {
            log("AndroidApplication", "Failed to create AndroidVisibilityListener", e);
        }
    }
}
Also used : FillResolutionStrategy(com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy) Handler(android.os.Handler) LifecycleListener(com.badlogic.gdx.LifecycleListener) Method(java.lang.reflect.Method) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 10 with LifecycleListener

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

the class LwjglAWTCanvas method stop.

public void stop() {
    if (!running)
        return;
    running = false;
    setGlobals();
    Array<LifecycleListener> listeners = lifecycleListeners;
    // To allow destroying of OpenGL textures during disposal.
    if (canvas.isDisplayable()) {
        makeCurrent();
    } else {
        error(logTag, "OpenGL context destroyed before application listener has had a chance to dispose of textures.");
    }
    synchronized (listeners) {
        for (LifecycleListener listener : listeners) {
            listener.pause();
            listener.dispose();
        }
    }
    listener.pause();
    listener.dispose();
    Gdx.app = null;
    Gdx.graphics = null;
    if (audio != null) {
        audio.dispose();
        Gdx.audio = null;
    }
    if (files != null)
        Gdx.files = null;
    if (net != null)
        Gdx.net = null;
    instanceCount--;
    stopped();
}
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