Texture2DMutable for cocos2dx version 3.2

Hi,

Am using cocos2dx version 3.2. Am working on CCTexture2DMutable. I have created Image. From this i got texture. By using this texture am creating sprite.
I have created CCTexture2Dmutable class by refering http://www.cocos2d-x.org/attachments/664/CCTexture2DMutable.cpp this link. I have called pixelAt() function but this function returns pixel color with value 0.
Am facing problem in finding out where am doing wrong. Can anyone provide me information of updated version of texture mutable and how it works?

Please provide me link for better tutorial on this topic.

Thanks and Regards
Gurudath

Hi,

I want to get pixel color of touched position. Am using concept of ccTexture2DMutable. Am not able to find out how exactly it works. Can anyone provide better tutorial information on this topic.

Thanks

Hi,

1.Creating image:
Image* image_ = new Image();
image_->initWithImageFile(imageFile);

2.By using image am calling “initWithData” function which is in CCTexture2DMutable class. It returns texture.

  1. By using obtained texture am creating sprite and added to scene.

  2. Am calling “pixelAt” function which is in CCTexture2DMutable class by passing touch point and target color as parameter.

This is pixelAt() function:

Color4B CCTexture2DMutable::pixelAt(const Vec2& pt)
{
Color4B c = {255, 0, 0, 0};
if(!data_) return c;
if(pt.x < 0 || pt.y < 0) return c;
if(pt.x >= _contentSize.width || pt.y >= _contentSize.height) return c;

unsigned int x = pt.x, y =  _pixelsHigh - pt.y;

if(_pixelFormat == PixelFormat::RGBA8888){
	unsigned int *pixel = (unsigned int *)data_;
    CCLOG("Pixel : %u", *pixel);
	pixel = pixel + (y * _pixelsWide) + x;
	c.r = *pixel & 0xff;
	c.g = (*pixel >> 8) & 0xff;
	c.b = (*pixel >> 16) & 0xff;
	c.a = (*pixel >> 24) & 0xff;
} else if(_pixelFormat == PixelFormat::RGBA4444){
	GLushort *pixel = (GLushort *)data_;
	pixel = pixel + (y * _pixelsWide) + x;
	c.a = ((*pixel & 0xf) << 4) | (*pixel & 0xf);
	c.b = (((*pixel >> 4) & 0xf) << 4) | ((*pixel >> 4) & 0xf);
	c.g = (((*pixel >> 8) & 0xf) << 4) | ((*pixel >> 8) & 0xf);
	c.r = (((*pixel >> 12) & 0xf) << 4) | ((*pixel >> 12) & 0xf);
} else if(_pixelFormat == PixelFormat::RGB5A1){
	GLushort *pixel = (GLushort *)data_;
	pixel = pixel + (y * _pixelsWide) + x;
	c.r = ((*pixel >> 11) & 0x1f)<<3;
	c.g = ((*pixel >> 6) & 0x1f)<<3;
	c.b = ((*pixel >> 1) & 0x1f)<<3;
	c.a = (*pixel & 0x1)*255;
} else if(_pixelFormat == PixelFormat::RGB565){
	GLushort *pixel = (GLushort *)data_;
	pixel = pixel + (y * _pixelsWide) + x;
	c.b = (*pixel & 0x1f)<<3;
	c.g = ((*pixel >> 5) & 0x3f)<<2;
	c.r = ((*pixel >> 11) & 0x1f)<<3;
	c.a = 255;
} else if(_pixelFormat == PixelFormat::A8){
	GLubyte *pixel = (GLubyte *)data_;
	c.a = pixel[(y * _pixelsWide) + x];
	// Default white
	c.r = 255;
	c.g = 255;
	c.b = 255;
}

CCLOG("color : %i, %i, %i, %i", c.r, c.g, c.b, c.a);

return c;

}

Pixel at function returns null value for all points. When i look into pixelAt() function, data_ value will be like 0x11e83000 and pixelFormat is RGBA8888.
When i convert data_ value to unsigned int value, then pixel value become 0 and it returns null value.

Regards

hello I am using cocos2dx 3.16 and I am getting this error in while using Texture2DMutable can you give me a solution ?

Undefined symbols for architecture i386:
“vtable for Texture2DMutable”, referenced from:
Texture2DMutable::Texture2DMutable() in CCTexture2DMutable.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Hello,

I have exactly the same problem having just updated to cocos2d-x 3.17.

@chiragkevadiya have you found a solution ?

I have tried removing all virtual functions from CCTexture2D.h and it has not changed anything, I have also tried implementing all virtual functions even if they are not required but that did not work either.

From what I have found by incrementally updating CCTexture2D, the message appears when the following line is added to CCTexture2D.h :

namespace ui
{
    class Scale9Sprite;
} 

So I have also tried to remove all virtual functions from UIScale9Sprite.h but still no success.

I am completely stuck and out of ideas so if anyone could help it would be greatly appreciated.

no I did not find any solution.

I tried 3.16 ,3.10,3.12 but its not working

Ok thank you for the information and your reply.
I was using cocos2d-x 3.5 before and it worked with that version.

have you any solution about this ?

1 Like