Greetings,

I've been having an interesting problem with a game I'm developing.

The game is being built using the cocos2d-android-1 library.

All sprites are loaded from ARGB8888 files.  They are of varying size
(not powers of 2).

When I deploy a test build to my Nexus One after making any code
changes (anything that forces the device to redeploy the APK) all the
sprites show up just fine.  Alpha shows up correctly, etc.

If I rerun the application from my N1, it will be missing some or all
of the textures at random.  Occasionally this is my splash screen
sprite.  Sometimes it is sprites of individual items on the screen.
There doesn't seem to be any rhyme or reason as to what sprites are
rendered and which are not.  All the items move and behave correctly,
but the sprites have been rendered with white textures.

If I reboot my phone, the game loads fine for the initial play
through, but subsequent play throughs have the bug again.

Now I've done some research and it looks like some people had issues
using non-power of 2 images for openGL textures.  However this doesn't
explain why my non-power of 2 textures render fine on the first
passthrough, and fail on subsequent passthroughs.

Also I've noticed that this problem seems to occur more often when I
have the application loading images from the sdcard.  If I load the
images from the assets folder and use the AssetManager I don't run
into the issue as much, although I did see it very rarely.

Is there some type of file locking going on here that is preventing
subsequent playthroughs from accessing the image file?  When a bitmap
is loaded, does it prevent other applications from reading the image
file while it's in use?

I'm going to try using sprite sheets and/or switching everything to
use powers of 2, but I'm not sure if that will fix it.  I swear it is
something wrong with how the files are loaded (asset folder vs folder
on sdcard) that is prevent subsequent playthroughs from correctly
loading the texture.

This is the code I'm using to read in the files:

        private static HashMap<String, CCTexture2D> textureMappings = new
HashMap<String, CCTexture2D>();
        public static final CCTexture2D addTexture(String filePath) {
                if(filePath.contains("../")) {
                        return null;
                }
                CCTexture2D rtn = textureMappings.get(filePath);
                if (rtn == null) {
                        String path = Constants.DEFAULT_PATH  + filePath;
                        File f = new File(path);
                        if (f.exists()) {
                                Bitmap bmp = 
BitmapFactory.decodeFile(Constants.DEFAULT_PATH +
filePath);
                                rtn = new CCTexture2D(bmp);
                                textureMappings.put(filePath, rtn);
                        }
                }
                return rtn;
        }

This is the constructor for CCTexture2D:

    public CCTexture2D(Bitmap image) {

        CGSize imageSize = CGSize.make(image.getWidth(),
image.getHeight());
        CGAffineTransform transform = CGAffineTransform.identity();

        int width = toPow2((int) imageSize.width);
        int height = toPow2((int) imageSize.height);

        while (width > kMaxTextureSize || height > kMaxTextureSize) {
            width /= 2;
            height /= 2;
            transform = transform.getTransformScale(0.5f, 0.5f);
            imageSize.width *= 0.5f;
            imageSize.height *= 0.5f;
        }

        if (imageSize.width != width || imageSize.height != height) {
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    image.hasAlpha() ? Bitmap.Config.ARGB_8888 :
Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(image, 0, 0, null);
            image.recycle();
            image = bitmap;
        }

        init(image, imageSize);
    }

This is what happens in init:

    private void init(Bitmap image, CGSize imageSize) {
        mBitmap = image;

        mWidth = image.getWidth();
        mHeight = image.getHeight();
        mContentSize = imageSize;
        // _format = image.getConfig();
        _maxS = mContentSize.width / (float) mWidth;
        _maxT = mContentSize.height / (float) mHeight;
        _texParams = _gTexParams;
        ByteBuffer vfb = ByteBuffer.allocateDirect(4 * 3 * 4);
        vfb.order(ByteOrder.nativeOrder());
        mVertices = vfb.asFloatBuffer();

        ByteBuffer tfb = ByteBuffer.allocateDirect(4 * 2 * 4);
        tfb.order(ByteOrder.nativeOrder());
        mCoordinates = tfb.asFloatBuffer();

                if(mBitmap.getConfig() == Bitmap.Config.ARGB_8888)
                        premultipliedAlpha = true;
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to