Bug @ cached texture data on Android (with VolatileTexture)

I’ve found another bug :slight_smile: the thing is, VolatileTexture::reloadAllTextures() works great, but it does not reapply the GL texture options after reloading the textures!

So every time when you set the texture params in your game (using the Cocos2d-x API, not using GL), switch to the home screen or another app and back into the Cocos2d-x app, all the textures that were being repeated, linear or nearest filtered, etc. look out of place because the GL texture settings are totally gone. No more nice background patterns and the user will be afraid that something went wrong with the device :frowning:

This happens because CCTexture2D::initWithData calls setAntiAliasTexParameters. But setAntiAliasTexParameters doesn’t take into account the requested texture parameter settings. CCTexture2D::setTexParameters does not save these options either.

I’ve fixed it by saving the params inside VolatileTexture and calling setTexParameters at VolatileTexture::reloadAllTextures. Would’ve provided a patch, but I’m still not sure how to use this Git-thing the right way. Should really figure that out some day.

Anyway, it’s easy to fix :slight_smile:

Hello, Strawberry, nice work, we are in anticipation of your patch!

You could download git here: http://git-scm.com/download

And the rough process of submit a patch is as follows:
1.get an account on https://github.com/
2.fork a repository from https://github.com/cocos2d/cocos2d-x
3.clone the forked repository to your local repository
4.apply your patch above to the source codes
5.commit your patch to your local repository
6.push the patch to the your forked repository
7.submit your pull-request

Hope that it is useful for you.

oh, you fix a critical bug again, awesome!
This time you must leave your name into cocos2d-x github repository, haha. Using git & github is not too difficult, I managed the basic usage in only 1 day, git is very cool and I have no willing to go back to SVN now.

Waiting for your pull-request!

Thanks, RongHong and Walzer :slight_smile: that’s definitely helpful. I’ll look into it as soon as I can!

(could take a few days, have to finish some other code thingies (no Cocos2d-x related) first)

Is this really fixed in new version?

Cause I’m having a similar problem now.

I loaded image to the texture and in my ::draw() function I use glXX code to draw it.
It works fine but when I open a new Activity (Android) onto the glView and come back, then
I see only the blank.

For example, initially I load texture like this

    pImg = new CCImage();
    bool bimgloadok = pImg->initWithImageFile(filePath);
    m_texture->initWithImage(pImg);

And in the ::draw() function of my node

CCLayer::draw ( );
    if(m_texture==NULL) 
        return;

    glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    glBindTexture ( GL_TEXTURE_2D, m_texture->getName ( ) );


    glEnableClientState( GL_VERTEX_ARRAY );        // Enable Vertex Arrays
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );          // Enable Texture Coord Arrays


 #if CC_USES_VBO
    glBindBuffer ( GL_ARRAY_BUFFER, m_gids [ 1 ] );
    uintptr_t offset = 0; 
 #else
    uintptr_t offset = (uintptr_t) m_vertices;
 #endif



    glVertexPointer ( 2, GL_FLOAT, sizeof ( ccV2F_C4B_T2F ), (GLvoid *) ( offset + offsetof ( ccV2F_C4B_T2F, vertices ) ) );    
    glTexCoordPointer ( 2, GL_FLOAT, sizeof ( ccV2F_C4B_T2F ), (GLvoid *) ( offset + offsetof ( ccV2F_C4B_T2F, texCoords ) ) ); 
    glColorPointer ( 4, GL_UNSIGNED_BYTE, sizeof ( ccV2F_C4B_T2F ), (GLvoid *) ( offset + offsetof ( ccV2F_C4B_T2F, colors ) ) );

 #if CC_USES_VBO
    glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, m_gids [ 0 ] );
 #else
    offset = (uintptr_t) m_indices;
 #endif

    glDrawElements ( GL_TRIANGLES, 6 * m_gridSize.y * m_gridSize.x, GL_UNSIGNED_SHORT, (GLvoid *) ( offset ) );

 #if CC_USES_VBO
    glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, 0 );
    glBindBuffer ( GL_ARRAY_BUFFER, 0 );
 #endif     

    glBindTexture ( GL_TEXTURE_2D, 0 );

    glBlendFunc ( CC_BLEND_SRC, CC_BLEND_DST );

This weird problem happens only in Android and only when I popup a new Activity.

Hello,

Is anyone have similar problems or have solution for this out there???

I think your problem is different from Strawberry Milkshake.
You create texture by yourself, so you should save the texture by yourself too.
If you use CCSprite::spriteWithFile(), then engine will do it for you.

Ah, thanks Zhang :slight_smile:

I save the texture by copying the Bitmap data to the ’malloc’ed memory.
Isn’t it enough? I really don’t know how to preserve texture info. so please tell me how
can I do for it???

I did this GlDrawXXX way because I have to draw 1024 tile bitmaps at a time.
I made my own TileMap… displaying 32*32 unit TilesMap with 42*24 sized bitmaps in each.
Is there another better way to drawing those?

Thanks in advance.

Minggo Zhang wrote:

I think your problem is different from Strawberry Milkshake.
You create texture by yourself, so you should save the texture by yourself too.
If you use CCSprite::spriteWithFile(), then engine will do it for you.

You can refer CCTextrueCache::CCTextureCache::addImage(), it uses

#if CC_ENABLE_CACHE_TEXTTURE_DATA
                    // cache the texture file name
                    VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtJpg);
#endif

to cache the path of the image, and reload them after the OpenGL context is lost(refer HelloWorld/android/jni/helloworld/main.cpp).

I think you can use CCSpriteBatchNode to draw many bitmaps at a time.

Ah Thanks,

I will try your suggestion :slight_smile:

Minggo Zhang wrote:

You can refer CCTextrueCache::CCTextureCache::addImage(), it uses
[…]
to cache the path of the image, and reload them after the OpenGL context is lost(refer HelloWorld/android/jni/helloworld/main.cpp).
>
I think you can use CCSpriteBatchNode to draw many bitmaps at a time.

Thanks Minggo,
I changed to use CCSpriteBatchNode and it works great. No glDrawXXX stuffs required. :slight_smile:

(The VolatileTexture::addImageTexture approach didn’t work though. Maybe I made another mistake)

Now I think I’ve successfully migrated all of my app to cocos2d-x and if AppStore review pass,
I will let you know the URL of my app (AppStore and AndroidMarket).

Best.