0
\$\begingroup\$

I'm making a game with LibGDX but I ran into a really odd problem.

When I was working on the loading screen I delayed the loading process with an image of ~87MB. When the loading is finished, it should continue to the next state with the main menu.

The odd thing is that when the big-image isn't loaded the screen blacks out after displaying the main menu state for less than a second. When I let it load the big-image it doesn't black out.

Main game loop:

public class MGame extends ApplicationAdapter {
    private SpriteBatch batch;
    private AssetManager assetmanager;

    @Override
    public void create() {
        batch = new SpriteBatch();
        assetmanager = new AssetManager();

        assetmanager.load("logo.png", Texture.class);
        assetmanager.load("loading.png", Texture.class);
        assetmanager.load("loading.mp3", Music.class);
        assetmanager.finishLoading();
        assetmanager.load("title_bg.jpg", Texture.class);
        assetmanager.load("menu.mp3", Music.class);
        //assetmanager.load("bigfile.png", Texture.class); //so now it blacks out

        GameStateManager.pushOnTop(new StateLoadingScreen());
    }

    @Override
    public void dispose() {
        batch.dispose();
        assetmanager.dispose();
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        GameStateManager.updateCurrent(assetmanager, batch);
        batch.end();
    }
}

StateLoadingScreen:

public class StateLoadingScreen extends AState {
    public enum ANI_STATE {
        LOGO_INC, LOGO_OPAQUE__LOADING_INC, LOGO_OPAQUE__LOADING_DEC, DONE_DECREASE_ALL, DONE_NEXT_STATE
    }

    private float alphaLogo = 0, alphaLoading = 0;
    /** 0 = logo increasing alpha; 1 = logo opaque, loading increasing; 2 = logo opaque, loading decreasing; 3 = done->decreasing; 4 = done->next state */
    private ANI_STATE state = ANI_STATE.LOGO_INC;

    @Override
    public void update(float dt, AssetManager assetmanager) {
        if (!assetmanager.get("loading.mp3", Music.class).isLooping()) {
            assetmanager.get("loading.mp3", Music.class).setLooping(true);
            assetmanager.get("loading.mp3", Music.class).play();
        }

        if (assetmanager.update()) if (state != ANI_STATE.LOGO_INC && state != ANI_STATE.DONE_NEXT_STATE) state = ANI_STATE.DONE_DECREASE_ALL;
        switch (state) {
            case LOGO_INC:
                if (alphaLogo > 100) state = ANI_STATE.LOGO_OPAQUE__LOADING_INC;
                else alphaLogo += dt*100;
                break;
            case LOGO_OPAQUE__LOADING_INC:
                if (alphaLoading > 100) state = ANI_STATE.LOGO_OPAQUE__LOADING_DEC;
                else alphaLoading += dt*100;
                break;
            case LOGO_OPAQUE__LOADING_DEC:
                if (alphaLoading < 0) state = ANI_STATE.LOGO_OPAQUE__LOADING_INC;
                else alphaLoading -= dt*100;
                break;
            case DONE_DECREASE_ALL:
                if (alphaLogo > 0) alphaLogo -= dt*100;
                if (alphaLoading > 0) alphaLoading -= dt*100;
                if (alphaLogo <= 0 && alphaLoading <= 0) state = ANI_STATE.DONE_NEXT_STATE;
                break;
            case DONE_NEXT_STATE:
                GameStateManager.set(new StateMenu(assetmanager), assetmanager);
                break;
            default:
                throw new EnumUpdateException();
        }
    }

    @Override
    public void render(SpriteBatch sb, AssetManager assetmanager) {
        Color c = sb.getColor();
        sb.setColor(new Color(c.r, c.g, c.b, alphaLogo/100));
        Texture texLogo = assetmanager.get("logo.png", Texture.class);
        sb.draw(texLogo, (Ref.WIDTH/2)-(texLogo.getWidth()/2), (Ref.HEIGHT/2)-(texLogo.getHeight()/2));
        if (assetmanager.isLoaded("loading.png")) {
            sb.setColor(new Color(c.r, c.g, c.b, alphaLoading/100));
            Texture texLoading = assetmanager.get("loading.png", Texture.class);
            sb.draw(texLoading, (Ref.WIDTH/2)-(texLoading.getWidth()/2), (Ref.HEIGHT/2)-(texLoading.getHeight()/2)-(texLogo.getHeight()/2));
        }
    }

    @Override
    public void dispose(AssetManager assetmanager) {
        assetmanager.unload("loading.png");
        assetmanager.unload("loading.mp3");
        assetmanager.unload("logo.png");
        //if (assetmanager.isLoaded("bigfile.png")) assetmanager.unload("bigfile.png");
    }
}

StateMainMenu:

public class StateMenu extends AState {
    private boolean fadeFinished = false;
    private float a = 0;

    public StateMenu(AssetManager assetmanager) {

    }

    @Override
    public void update(float dt, AssetManager assetmanager) {
        if (!assetmanager.get("menu.mp3", Music.class).isLooping()) {
            assetmanager.get("menu.mp3", Music.class).setLooping(true);
            assetmanager.get("menu.mp3", Music.class).play();
        }

        if (a > 100) fadeFinished = true;
        if (!fadeFinished) a += 100*dt;
    }

    @Override
    public void render(SpriteBatch sb, AssetManager assetmanager) {
        sb.setColor(sb.getColor().r, sb.getColor().g, sb.getColor().b, a/100);
        sb.draw(assetmanager.get("title_bg.jpg", Texture.class), 0, 0, Ref.WIDTH, Ref.HEIGHT);
    }

    @Override
    public void dispose(AssetManager assetmanager) {
        assetmanager.unload("title_bg.jpg");
        assetmanager.unload("menu.mp3");
    }

}
\$\endgroup\$
  • 1
    \$\begingroup\$ You really should post the relevant bits to your code instead of the whole thing. It makes solving issues like this a whole lot easier, and if you for some reason remove the GitHub repo later on people with similar problems can still follow along. That being said though, try putting assetManager.finishLoading(); after loading your big image to make sure everything is loaded. \$\endgroup\$ – Charanor May 15 '16 at 19:28
  • \$\begingroup\$ You should be the one extracting the relevant code and only the relevant code for us to view, not the one helping you. As mentioned above, please post the relevant lines of code into your actual question, both for us helping you now and for the future readers looking for help. \$\endgroup\$ – Tyyppi_77 May 15 '16 at 19:33
  • \$\begingroup\$ I understand, I edited my question \$\endgroup\$ – HoldYourWaffle May 15 '16 at 19:39
0
\$\begingroup\$

So I found the solution and I find it rather odd.

Basicly I increase the overal alpha-value with 100*delta. Sometimes the result is >1, and appearently libGDX can't handle an alpha of 1.1, blacking out the screen (drawing at alpha 0.1 propably?)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.