ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

CCTMXLayer > CCTextureAtlas IndexOutOfBoundsException #47

Open savethejets opened 12 years ago

savethejets commented 12 years ago

I'm getting an IndexOutOfBoundsException when I try and load my Tiled maps.

I'm using gzip format and all that jazz. It looks like it might have something to do with the fact that the indices var in (CCTextureAtlas line 159) is a shortbuffer array and that the (int) index is greater than Short.MAX_VALUE; ( It looks like it's flipping to the negative number.) see below.

I'm digging into this, but has anyone else run into this? (or possibly fixed this already??? :D)

public void initIndices() { for (int i = 0; i < capacity_; i++) { if (ccConfig.CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP) { indices.put((short) (i * 6 + 0), (short) (i * 4 + 0)); indices.put((short) (i * 6 + 1), (short) (i * 4 + 0)); indices.put((short) (i * 6 + 2), (short) (i * 4 + 2)); indices.put((short) (i * 6 + 3), (short) (i * 4 + 1)); indices.put((short) (i * 6 + 4), (short) (i * 4 + 3)); indices.put((short) (i * 6 + 5), (short) (i * 4 + 3)); } else { indices.put((short) (i * 6 + 0), (short) (i * 4 + 0)); <---- (i * 6 + whatever) cast to a short will sometimes give a negative number since you have an int larger than Short.MAX_VALUE; indices.put((short) (i * 6 + 1), (short) (i * 4 + 1)); indices.put((short) (i * 6 + 2), (short) (i * 4 + 2));

            // inverted index.
            indices.put((short) (i * 6 + 5), (short) (i * 4 + 1));
            indices.put((short) (i * 6 + 4), (short) (i * 4 + 2));
            indices.put((short) (i * 6 + 3), (short) (i * 4 + 3));
        }
    }
    indices.position(0);
}
savethejets commented 12 years ago

Okay I think this is what happened.

In cocos2d for the iphone indices is a Glushort. Which is an unsigned int (16 bits) . But when it was converted indices was made to be a ShortBuffer.

However in Java a short is signed 16 bits. So thats most likely was why I was seeing a negative index on large values.

I've changed indices to be a CharBuffer, since if I recall that is what an unsigned int would translate to in java, and that seems to have cleared things up. ( I can see my TMX map now! )

I'll submit a pull request in a bit.