I'm using Non-continuous Rendering in my loading screen so I can update it as I INIT different parts of my code in a secondary thread. That way I can manually increase the Animation counter that I use to draw a Sprite which shows a filling hourglass in tune with the loading of my assets.
So, I load my game screen with the help of another thread (atlases, textures, init variables,etc) :
Runnable load_game_screen = new Runnable(){
public void run(){
new_game_screen.load_all();
}
}};
Thread thread_load_game = new Thread(load_game_screen);
thread_load_game.start();
In my game screen load_all function I use Gdx.app.postRunnable to run those initializations on the next render of the main UI thread so I don't get any context errors:
public void load_all(){
Gdx.app.postRunnable(new Runnable(){
@Override
public void run(){
...load stuff
loading_screen.loading_counter += 0.025f;
loading_screen.loadingTimerSprite.setRegion(loading_screen.animation.getKeyFrame(loading_screen.loading_counter, true));
}
Gdx.graphics.requestRendering();
Thread.sleep(25);
Gdx.app.postRunnable(new Runnable(){
@Override
public void run(){
...load stuff part2
loading_screen.loading_counter += 0.025f;
loading_screen.loadingTimerSprite.setRegion(loading_screen.animation.getKeyFrame(loading_screen.loading_counter, true));
}
Gdx.graphics.requestRendering();
Thread.sleep(25);
... etc ...
}
The problem is that requestRendering doesn't act immediately and sometimes the postRunnables are run one after another without immediately going into the main UI render function so the user can't see the complete animation of the loading sprite; it happens with jumps. HOW can I force Rendering in the main UI thread from the secondary thread that is loading the assets and variables of behalf of the main thread?