sayon
sayon

Reputation: 43

libgdx throws error on spritebatch creation

If I change the screen like this it works.

        button.addListener(new ClickListener() {
           @Override
           public void clicked(InputEvent event, float x, float y) {
               someclass.setScreen(someclass.getGameScreen());
           }
        });

But my goal is to use a server response to change the screen like this:

Main.java

        button.addListener(new ClickListener() {
           @Override
           public void clicked(InputEvent event, float x, float y) {
               client.sendTCP(new ChangeLobbyToGameRequest());
           }
        });

and

    public void changeToGame(){
        someclass.setScreen(someclass.getGameScreen());
    }

Server listener

if (object instanceof ChangeLobbyToGameResponse){
        Main.changeToGame();
 }

but then I get the following error

E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.somegame.game, PID: 15828
java.lang.IllegalArgumentException: Error compiling shader: 
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.createDefaultShader(SpriteBatch.java:164)
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.<init>(SpriteBatch.java:127)
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.<init>(SpriteBatch.java:81)
    at com.labyrix.game.Screens.GameScreen.show(GameScreen.java:46)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at com.labyrix.game.Screens.Lobbyscreen.changeToGame(LobbyScreen.java:59)

Gamescreen show method

@Override
public void show() {
    batch = new SpriteBatch();
    isorend = new Board(batch);
    ..... 
}

Upvotes: 2

Views: 130

Answers (1)

londonBadger
londonBadger

Reputation: 711

OpenGL operations have to be carried out on the main thread. In the case of the button, the clickListener is called on the UI thread (or called main thread) so fine. However, in the case of the server response its a different thread. So you need to transpose the server callback to the main thread

public void changeToGame(){
        Gdx.app.postRunnable (new Runnable() {
           someclass.setScreen(someclass.getGameScreen());
        });
}

postRunnable is queued on the main thread satisfying openGL requirements.

Upvotes: 0

Related Questions