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.