4

I am writing a game for iPhone using the cocos2d for iPhone v1.0.1 library. To get my game work fine I need to check the color of a specific pixel in a CCSprite when i know the coordinates. I have been looking for the solution for two days but I did not find any working. Maybe someone did this before and knows how to do it?

Another posibility for me would be creating an UIImage from CCSprite if this is easier...

greetings, jarektb

jarektb
  • 71
  • 1
  • 3
  • Did you find a solution to your problem ? – Rahul Iyer Apr 01 '14 at 05:47
  • Hard to say. The aswer form Yannick L. was not exactly what I was looking for (problems with alpha), and when McDevon aswered I was working at something else and did not really have time to check the solution. – jarektb Apr 24 '14 at 22:56

2 Answers2

10

Appearantely the color buffer containing the sprite cannot be directly accessed. However, you can draw the sprite to a CCRenderTexture and read the pixel from there.

location = ccp(x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR());

UInt8 data[4];

CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth:sprite.boundingBox.size.width * CC_CONTENT_SCALE_FACTOR() 
                                                                 height:sprite.boundingBox.size.height * CC_CONTENT_SCALE_FACTOR() 
                                                            pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

[renderTexture begin];
[sprite draw];

glReadPixels((GLint)location.x,(GLint)location.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);

[renderTexture end];
[renderTexture release];

NSLog(@"R: %d, G: %d, B: %d, A: %d", data[0], data[1], data[2], data[3]);

If you are using a retina display, you have to take the content scale factor into account.

The solution can also easily be turned into a CCSprite category or subclass.

This seems to be an old topic, but I posted the answer here as this was the first hit on google when I was having the same dilemma just now.

McDevon
  • 161
  • 1
  • 5
4

If your sprite is visible on the screen, you can use the glReadPixels function. It should look like this (where x and y on the second line are the coordinates):

ccColor4B *buffer = malloc(sizeof(ccColor4B));
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
ccColor4B color = buffer[0];
jrtc27
  • 8,496
  • 3
  • 36
  • 68
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
  • Thank you for the answer. Your solution works, but not exactly as I need. Because you read the color of a pixel from the screen. And when the CCSprite contains some transparency I get the background color. I need to be able to get the RGBA pixel color only from the CCSprite. So when the background color is RGBA(255,255,255,255) and the CCSprite has RGBA(255,0,0,127) I should get RGBA(255,0,0,127) and your solution gives me RGBA(255,128,128,255); – jarektb Jul 30 '12 at 16:14
  • 1
    Ah ok. You can take a look to the ```CCMutableTexture2D``` here: https://github.com/manucorporat/AWTextureFilter/blob/master/AWTextureFilter/CCTexture2DMutable.h . It has a method name ```pixelAt:``` which allows you to retrieve the ```ccColor4B``` which represent the pixel at the given location. – Yannick Loriot Jul 31 '12 at 08:29
  • Unfortunately it still does not solve the problem for me, because i have some CCSprites, and i am able to get a CCTexture2D from them, but I do not see any way to convert it into a CCMutableTexture2D... – jarektb Jul 31 '12 at 09:22