javalibgdxpixmap

Can`t load an simple image. Libgdx


I'm making a splash screen (for my game) using LibGDX. I'm using a .png format images, and with few of them - it works (my images are properly located in the assets folder) I'm getting this error with few of my images I want to use:

    Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: devlogo.png
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
    at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
    at com.badlogic.gdx.graphics.GLTexture.createTextureData(GLTexture.java:185)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:103)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:95)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:91)
    at com.GunRun.Screens.Splash.show(Splash.java:34)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at com.mynewgame.game.GunRun.create(GunRun.java:16)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.io.IOException: Error loading pixmap: decoder init failed for stream
    at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.<init>(Gdx2DPixmap.java:57)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:138)
    ... 10 more

Code from splash screen class:

public class Splash implements Screen{

private Sprite devlogo;
private SpriteBatch batch;

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    devlogo.draw(batch);
    batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void show() {
    batch = new SpriteBatch();

    Texture texture = new Texture("devlogo.png");
    devlogo = new Sprite(texture);
    devlogo.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}



@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

}

The images with which works has 24 bit depth, and others who doesn't has 64 bit depth, can be there a problem?


Solution

  • I hit this problem recently too, so I did some digging into where that error message comes from. The error comes from this line in jpgd.cpp:

    jpeg_decoder decoder(pStream);
    if (decoder.get_error_code() != JPGD_SUCCESS) {
        err_reason = "decoder init failed for stream";
        return NULL;
    }
    

    The interesting thing there is that since these are PNGs, it's pretty suspect that it's trying to load the image as a JPG. It turns out that will happen here if LibGDX fails to load the image into memory on the first try:

    const unsigned char* pixels = stbi_load_from_memory(buffer, len, &width, &height, &format, 0);
    if (pixels == NULL) {
        pixels = jpgd_decompress_jpeg_image_from_memory(buffer, len, &width, &height, &format, 3);
    }
    

    It turned out that I was hitting this because I had a massive memory leak in part of my game, so LibGDX was simply failing to allocate memory to load the image. Fixing the memory leak corrected the problem for me.

    Since this is happening on your splash screen, I'm guessing a memory leak isn't the cause of your issue, but it might be worth checking that you have enough memory allocated for the application.