javaanimationlibgdxgif

java.lang.IllegalArgumentException: capacity < 0: (-1611268096 < 0)


Error:


java.lang.IllegalArgumentException: capacity < 0: (-1611268096 < 0)
    at java.base/java.nio.Buffer.createCapacityException(Buffer.java:279)
    at java.base/java.nio.Buffer.<init>(Buffer.java:242)
    at java.base/java.nio.ByteBuffer.<init>(ByteBuffer.java:288)
    at java.base/java.nio.ByteBuffer.<init>(ByteBuffer.java:296)
    at java.base/java.nio.MappedByteBuffer.<init>(MappedByteBuffer.java:112)
    at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:170)
    at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.newPixmap(Native Method)
    at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.<init>(Gdx2DPixmap.java:137)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
    at com.tds.game.GifDecoder.getAnimation(GifDecoder.java:697)
    at com.tds.game.GifDecoder.loadGIFAnimation(GifDecoder.java:731)
    at com.tds.game.AfterBossScreen.show(AfterBossScreen.java:26)
    at com.badlogic.gdx.Game.setScreen(Game.java:63)
    at com.tds.game.updateAndDrawBulletsAndBadBoys.updateAndDrawBadBoys(updateAndDrawBulletsAndBadBoys.java:179)
    at com.tds.game.MainGameScreen.render(MainGameScreen.java:157)
    at com.badlogic.gdx.Game.render(Game.java:48)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:387)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
    at com.tds.game.DesktopLauncher.main(DesktopLauncher.java:14)

I tried to use other libraries for decryption, but it didn't work. I also created a gifdecoder as a class.

Changed:

Gif size is 2560x1440px, idk why do you need this information.

Gif isn't damaged, and that's what you asked:

Code around AfterBossScreen.java:26:

@Override
public void show() {
    System.out.println("AfterBossScreen show() called");
    font = new BitmapFont();
    batch = new SpriteBatch();

    // Загрузка GIF
    try {
        animation = GifDecoder.loadGIFAnimation(Animation.PlayMode.LOOP,Gdx.files.internal("begin.gif").read()); // stroke 26 
        System.out.println("AfterBossScreen: Animation loaded");
    } catch (Exception e) {
        System.out.println("Error loading GIF: " + e.getMessage());
        e.printStackTrace();
    }

    batch.begin();
    font.draw(batch, "Loading...", 100, 100);
    batch.end();
}

GifDecoder.java:697:

public Animation<TextureRegion> getAnimation(PlayMode playMode) {
        int nrFrames = getFrameCount();
        Pixmap frame = getFrame(0);
        int width = frame.getWidth();
        int height = frame.getHeight();
        int vzones = (int)Math.sqrt((double)nrFrames);
        int hzones = vzones;

        while(vzones * hzones < nrFrames) vzones++;

        int v, h;

        Pixmap target = new Pixmap(width * hzones, height * vzones, Pixmap.Format.RGBA8888); // stroke 697

        for(h = 0; h < hzones; h++) {
            for(v = 0; v < vzones; v++) {
                int frameID = v + h * vzones;
                if(frameID < nrFrames) {
                    frame = getFrame(frameID);
                    target.drawPixmap(frame, h * width, v * height);
                }
            }
        }

        Texture texture = new Texture(target);
        Array<TextureRegion> texReg = new Array<TextureRegion>();

        for(h = 0; h < hzones; h++) {
            for(v = 0; v < vzones; v++) {
                int frameID = v + h * vzones;
                if(frameID < nrFrames) {
                    TextureRegion tr = new TextureRegion(texture, h * width, v * height, width, height);
                    texReg.add(tr);
                }
            }
        }
        float frameDuration = (float)getDelay(0);
        frameDuration /= 1000; // convert milliseconds into seconds
        Animation<TextureRegion> result = new Animation<TextureRegion>(frameDuration, texReg, playMode);

        return result;
    }

updateAndDrawBulletsAndBadBoys.java:179:

if(boss != null){
    if(boss.x <= 16){
        System.out.println("}-- Boss is in the end");
        game.setScreen(new AfterBossScreen()); // stroke 179
        return;
    }
}

Solution

  • It looks like you have a numerical overflow:

    If you look at the sourcecode of com.badlogic.gdx.graphics.Pixmap.java the constructor is:

    /** Creates a new Pixmap instance with the given width, height and format.
     * @param width the width in pixels
     * @param height the height in pixels
     * @param format the {@link Format} */
    public Pixmap (int width, int height, Format format) {
    

    Your code multiples the width and height by hzones and vzones which results in values of 33,280 and 20,160 for arguments width and height respectively. Pixmap.Format.RGBA8888 is 4 bytes. This is a ByteBuffer capacity of 2,683,699,200 bytes (33,280 X 20,160 X 4)

    java.nio.ByteBuffer constructor accepts the cap (ie capacity) argument as an int. int overflows above 2,147,483,647 which is below 2,683,699,20 so it wraps around to -1,611,268,096 by my calculations. (-2147483648 + 2,683,699,20 - 2,147,483,647)

    Anyway the fact that you are multiplying width and height by hzones and vzones is the cause of the exception.