CCImage improvements

I would love to see a method like bool CCImage::initWithMemoryData(void* pData, int width, int height, bool alpha), but it doesn’t exist yet.

Would it be a viable option to implement something like that? What’s in the plans for allowing Cocos2d-x app developers to manipulate CCImage’s?
(afaik it’s not a part of cocos2d, so it should be possible, me thinks :slight_smile: )

Are you talking about CCImage::initWithImageData(void* pData, int nDataLen, EImageFormat eFmt) ? This method is already implemented in CCImage.

@Strawberry Milkshake,
There is an interface named initWithImageData in class CCImage. But it’s parameters not as same as you wished.

The data must be read from files.
And you must specify the image file format. Because of the engine only support .png & .jpg now.

Yeah, I know about initWithImageData, but I’d like to add raw data from memory instead of from a file and initWithImageData does not allow me to do that because it expects the data to be formatted as png or jpeg :slight_smile:

I’d like to do something like this:

// Generate a nice image
unsigned char* pData = new unsigned char[width * height * bytesPerComponent];
for (int x=0; xinitWithMemoryData(pData, width, height, (bytesPerComponent == 4) ? true : false);

// Add to CCTextureCache and start using it as a texture
CCTexture2D* tex = CCTextureCache::sharedTextureCache()->addUIImage(img, "key_name");

// Yeay, we've got a blue texture now, show it on a sprite
CCSprite* sprite = CCSprite::spriteWithTexture(tex, size_argument);

(using int bytesPerComponent instead of a bool alpha as argument for initWithMemoryData might be nicer, but you get the point)

Many libraries can do that and it’s a requirement if you’re generating textures dynamically instead of loading them from files.

Another question related to the subject: How can access raw data CCImage from CCSprite ?

@Valentin Vit
It’s another topic. CCSprite relates to CCTexture2D, you can call CCSprite::getTexture to get the CCTexture2D object. And CCTexture2D loads data from CCImage, instead of holding the CCImage object.
You can read the source code of
bool CCTexture2D::initWithImage(CCImage * uiImage)
and
bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int POTWide, unsigned int POTHigh)
these 2 functions in CCTexture2D.cpp

+1

I would also like this, it would be very nice. Procedural textures are for, as they say, the win.

Hi… i try use this example:

   // Generate a nice image
    unsigned char* pData = new unsigned char[width * height * bytesPerComponent];
    for (int x=0; xinitWithMemoryData(pData, width, height, (bytesPerComponent == 4) ? true : false);

    // Add to CCTextureCache and start using it as a texture
    CCTexture2D* tex = CCTextureCache::sharedTextureCache()->addUIImage(img, "key_name");

    // Yeay, we've got a blue texture now, show it on a sprite
    CCSprite* sprite = CCSprite::spriteWithTexture(tex, size_argument);

But, the method initWithMemoryData dont exist :S ….

My idea is to send a byte array from java to cocos2d-x and converts it into an image to be loaded by this CCSprite. The two days I’m trying to do this but can not find solution.

I also tried in java, save the image on the SD Card and the cocos2d (c++) to load the image, but without success. This is all because I can not work with CCImage.

Please tell me if you can do what I’m trying to do, if you can upload an image sent by JNI or charge via SD card, without the assets.

Thank you. (Sorry for my bad English)

Oh, Francisco, initWithMemoryData is a method assumed by Strawberry.
This function in code is CCImage::initWithImageData(void* pData, int nDataLen, EImageFormat eFmt, int nWidth, int nHeight, int nBitsPerComponent)
Have a try :slight_smile:

Walzer Wang wrote:

Oh, Francisco, initWithMemoryData is a method assumed by Strawberry.
This function in code is CCImage::initWithImageData(void* pData, int nDataLen, EImageFormat eFmt, int nWidth, int nHeight, int nBitsPerComponent)
Have a try :slight_smile:

I used the methed recently,the “pData” is from java,not the local resource but from the web,and the question is after game puase,the sprite inited from the ccimg
turns to black ,could you tell me why please,here is the code:

CCImage ccimg;
if(!ccimg.initWithImageData((void**)iconbuffer,iconbufferlan,CCImage::kFmtPng))return;
CCTexture2D** texture = new CCTexture2D();
texture->initWithImage(&ccimg, kCCResolutionUnknown);
CCSprite* tempsssss = CCSprite::create(texture);

Thanks