0

I'm trying to write a libgdx app to perform some batch tasks that will help me automating the build of my main project.

For this, I need to create several instances of LwjglApplication with different resolutions. They have NOT to be concurrent, so I will create one, then destroy it, and then create the next, and so forth until the whole process is done.

The problem I'm facing right now is that my resolution loop is executed only once, and I don't know why. Here is my testing code so far:

package com.marzoa.ruletafree.desktop.util;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class AssetFontsGenerator {
    private static final String TAG = AssetFontsGenerator.class.getSimpleName();
    private static final int[] resolutions = new int[]{1920, 1440, 960, 720, 480, 360};
    private static final float resRatio = 1080f / 1920f;

    private static boolean appDone = false;

    public static void main(String[] args) throws InterruptedException {
        for (final int width : resolutions) {
            final LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
            config.width = width;
            config.height = (int) (width * resRatio);
            System.out.println(TAG + ": About to create an application with " + config.width + "x" + config.height + " resolution");
            appDone = false;
            new LwjglApplication(new ApplicationListener() {
                @Override
                public void create() {
                    Gdx.app.log(TAG, "@create: " + Gdx.graphics.getWidth() + "x" + Gdx.graphics.getHeight());
                }

                @Override
                public void resize(int width, int height) {
                    Gdx.app.log(TAG, "@resize: " + width + "x" + height);
                }

                @Override
                public void render() {
//                    Gdx.app.log(TAG, "@render");
                }

                @Override
                public void pause() {
                    Gdx.app.log(TAG, "@pause");
                }

                @Override
                public void resume() {
                    Gdx.app.log(TAG, "@resume");
                }

                @Override
                public void dispose() {
                    Gdx.app.log(TAG, "@dispose");
                    appDone = true;
                }
            }, config);

            while (! appDone) {
                Thread.yield();
            }
            Thread.sleep(1000); // Give it some extra time so to make sure the app instance has released all resources
        }
    }
}

And this is the output I get on the console when I execute it (please, note that this code still needs my intervention to close the app window):

AssetFontsGenerator: About to create an application with 1920x1080 resolution AssetFontsGenerator: @create: 1920x1080 AssetFontsGenerator: @resize: 1920x1080 AssetFontsGenerator: @resize: 1920x1009 AssetFontsGenerator: @pause AssetFontsGenerator: @dispose

Process finished with exit code 255

It seems to crash at Thread.sleep(1000), though it does not show any stack trace. In fact, if I comment out that line it works out to the next resolution in the for loop, but then it crashes because it seems the OpenAL context has not been fully released yet from the previous app:

AssetFontsGenerator: About to create an application with 1440x810 resolution LwjglApplication: Couldn't initialize audio, disabling audio java.lang.IllegalStateException: Only one OpenAL context may be instantiated at any one time.

Any ideas? Thanks a lot in advance.

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
  • 1
    I think you might have to run a different instance on a new process. [This](https://stackoverflow.com/a/31120304/571227) might help you. – haxpor Sep 02 '17 at 01:07
  • Thanks for your answer, I'll take a look at that ASAP. In the meantime, I'm doing it by hand each time I need it. Thankfully is not very often actually, but I'm more concerned about forgetting to do it when needed before a production release, so I'd prefer to have this automated somehow. – Fran Marzoa Sep 03 '17 at 17:57
  • Are you sure you need the whole libgdx context when generating the assets? If you don't need them, you could run a much more lightweight generator. – manabreak Sep 04 '17 at 06:36
  • It depends on how you define "need". The code uses several things from libGDX. Theoretically I could rewrite these so to make them independent of the libGDX context, but it's not worth the effort, really. If I have to spent more than a couple of hours automating this, then it isn't worth the effort. – Fran Marzoa Sep 06 '17 at 11:43

0 Answers0