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 )
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
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.
@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
// 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.
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
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
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: