yszheda/cocos2d-x-irregular-button

Running with cocos2d-x 3.10 and opengl error

Opened this issue · 8 comments

Not sure if this error comes up before 3.10, but I get this... not sure if it's a real problem but:

Sprite::getTexture: 0 ms

image: game_shuffle_off.png, savedBuffWidth: 2038, savedBuffHeight: 1545

oldFBO: 1

bind texture: 0 ms

glReadPixels: 41 ms

init normalTransparent_: 37 ms

load from memory: 83 ms

OpenGL error 0x0502 in /Users/jonny/cocos2d-x-3.10bitcode/cocos/renderer/CCTexture2D.cpp initWithMipmaps 648

So some kind of OpenGL error. This is running the fork I did.

I'm doing some error tracking and this line gives the error:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, savedBufferWidth, savedBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

I'm changing the third parameter to GL_RGBA... I have no idea what I'm doing though, but the error went away.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, savedBufferWidth, savedBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

Btw I am seeing an error in which a button made by this sometimes does not get any hits at all... but it's hard to reproduce, sometimes it works and sometimes it doesn't. So I noticed this opengl error and thought it might be related. I'll run with this fix for awhile and see if things get better...

Also, in my case the source sprite comes from a sprite sheet... I use TexturePacker. It might be related, or maybe not.

The above worked for a while, the opengl error is gone anyway, but I still see some occasions where the button does not respond to touches at all. This has to be fixed sometime, as for now this class is not 100% for cocos2d-x 3.10 :-(

Hi jonnyijapan,

I'm very sorry for my late reply, but I'm too busy working on my company's project these days.

Also, in my case the source sprite comes from a sprite sheet... I use TexturePacker. It might be related, or maybe not.

Yes, it's related to your problem, because I didn't consider the case of sprite sheet when I implemented the IrregularButton. In my implementation, I treat the normal state of the button as a image file rather than a sprite frame. If a sprite sheet is used, the actual region of the sprite frame must be taken into account when loading the pixels of it. It may be more tricky if you trim the transparent pixels from sprite frames' borders when generating the sprite sheet.

As for the OpenGL error, I can't reproduce it on cocos2d-x v3.2, which is used by me now. I'm not sure whether it's related to v3.10. I'll try to figure out the problem when I'm free.

Thanks for your reply. So actually, I should try to use this class with a single sprite file rather than a sprite sheet? I might actually test that for a quick fix. My updated version ran fine for one or two weeks but all of a sudden it stopped working. Yesterday I probably updated the sprite sheet of the game screen where this button exists. I update that sprite sheet a couple of times a week. What you say about the offset region in the sprite sheet sounds like it might be true but its weird that it worked for some weeks :-)

So actually, I should try to use this class with a single sprite file rather than a sprite sheet?

This is how I use the IrregularButton now XD

My updated version ran fine for one or two weeks but all of a sudden it stopped working. What you say about the offset region in the sprite sheet sounds like it might be true but its weird that it worked for some weeks :-)

I see. In IrregularButton::loadNormalTransparentInfo, the member variable _buttonNormalRenderer of cocos2d::ui::Button class is used for obtaining its texture and loading its pixels via glReadPixels. _buttonNormalRenderer can be initialized either from a single file or from a sprite frame. Now it depends on how cocos2d-x implements getTexture. If normalRenderer->getTexture() returns only the correctly cropped texture of the sprite frame, then IrregularButton can handle the case of sprite sheets.

That gives me a hint that IrregularButton can be extended to support sprite sheets. However, that doesn't mean the current version of IrregularButton is appropriate. Because I store all the transparency information of a picture to check whether the touch is valid, that may cause large memory consumption if sprite sheets are used.

I tried using a single file instead of sprite sheet, but it didn't help. The button did not respond at all. I'm busy too so I haven't looked into the real reason. It worked well for weeks even with sprite sheets 👍 The problem is probably elsewhere.

Hi jonnyijapan,

You can try my previous implementation to see if it works: replace the line

    loadNormalTransparentInfo();

in the following function

bool IrregularButton::init(const std::string &normalImage,
                  const std::string& selectedImage ,
                  const std::string& disableImage,
                  TextureResType texType)
{
    bool ret = true;
    do {
        if (!Button::init(normalImage, selectedImage, disableImage, texType)) {
            ret = false;
            break;
        }
    } while (0);
    loadNormalTransparentInfo();
//    loadNormalTransparentInfoFromFile();
    return ret;
}

with

   loadNormalTransparentInfoFromFile();