[SOLVED] Sprites are all black in android when using setColor()

In the game I am working on I have sprites that are greyscale, and I call setColor() on them to set them to various colors when they are created. This works great on the win32 version, and appear in the color I set them to, but when I put the game on an android device, either an emulator or actual device, the sprites are pure black. Has anyone else seen this, or know how to fix? This seems like a bug to me, but perhaps I am misconfiguring things somehow. I can comment out the setColor() call and see that the sprites are drawn correctly (that is, in greyscale).

Here is what the device reports about the GL context:

03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_OES_packed_depth_stencil: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_vertex_array_object: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_BGRA8888: false
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_ATITC: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_S3TC: false
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: cocos2d.x.version: cocos2d-x-3.13.1
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_discard_framebuffer: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: cocos2d.x.compiled_with_profiler: false
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_PVRTC: false
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: cocos2d.x.build_type: DEBUG
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.renderer: Adreno ā„¢ 418
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_OES_depth24: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_ETC1: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_OES_map_buffer: false
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: cocos2d.x.compiled_with_gl_state_cache: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.version: OpenGL ES 3.2 V@145.0 (GIT@If5818605d9)
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.supports_NPOT: true
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.max_texture_units: 96
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.vendor: Qualcomm
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: gl.max_texture_size: 16384
03-21 18:05:23.690 11000 11019 D cocos2d-x debug info: }

Here is what Iā€™m setting in the AppDeligate code relating to GL:

void AppDelegate::initGLContextAttrs()
{
     //set OpenGL context attributions,now can only set six attributions:
     //red,green,blue,alpha,depth,stencil
     GLContextAttrs glContextAttrs = { 8, 8, 8, 8, 24, 8 };

   GLView::setGLContextAttrs(glContextAttrs);
}

Here is how Iā€™m creating the sprite and setting the color:

static auto color = Color3B::YELLOW;

...

sprite = Sprite::create("dot1.png");
sprite->setPosition(x, y);
sprite->setColor(color);

Any help is much appreciated, thanks!

Try with WHITE sprite not gray, your dot1.png should be in single WHITE color.
It should work.

Thank you for the feedback, but this did not work. I set the sprites to all white (I created new sprites that were only white in color), but they still appeared all black when I call setColor() on them, regardless of the color that I used! In the windows/linux versions, this all worked correctly.

I also played with different blendFunc options. The only thing that affected the android version was to set the blendFunc on the sprite to ADDITIVE. That resulted in completely transparent sprites, regardless of the opacity that I set (or didnā€™t set). Whereas in the windows/linux versions, the ADDITIVE blending mode resulted in partially transparent sprites.

I think there is something about the way that the android devices are blending or setting the color that is not correct. Iā€™m wondering if there is a bit-depth issue with my sprites possibly? Is it possible that the output that:

gl.supports_BGRA8888: false

on the android devices implies that I need to alter the bit-depth of my sprites?

I really donā€™t know too much about this area of graphics, any ideas are appreciated. In the meantime, I will keep looking.

Iā€™m still stuck on this.

I tried changing the bit-depth of the png file, 8-bit, 24-bit and 32-bit, but that did not help. 24-bit depth resulted in no alpha information, so the dot was actually a black square instead of a circle.

My windows version also states ā€œBGRA8888: falseā€ about the gl renderer, so it is not that.

I tried playing with the parameters in:

GLContextAttrs glContextAttrs = { 8, 8, 8, 8, 24, 8 };

but not understanding what they do I havenā€™t been able to affect any change.

Googling around has returned a few instances with the Unity engine and android devices having black sprites/textrures, though it sounds like that they are either bugs in unity, or texture sizes being too large for the android device. Neither of those apply in my case.

Iā€™m becoming convinced that the problem is with how setColor() operates on android. Can anyone confirm that this method works as expected on android devices? I only have this problem when I use setColor() on the sprite. Commenting it out and everything looks fine, though obviously the color is not what I want!

Thanks!

Turns out this was a static initialization problem. I am using a static variable to store the color that I am setting on initialization, but the Color3B::[COLOR] constants are also initialized at the same time. For windows/linux, the compiler gets it ā€˜rightā€™, but for android, the compiler does not and my color variable is set to (0,0,0), i.e., BLACK which explains the behavior. The solution is to initialize my color variable during game initialization.

1 Like