Reputation: 1328
I'm trying to create a rectangle sprite using CCTexture2D in cocos2d-x. Here is my code.
CCSprite *sprite = CCSprite::create();
GLubyte buffer[sizeof(GLubyte)*4];
buffer[0]=255;
buffer[sizeof(GLubyte)]=0;
buffer[sizeof(GLubyte)*2]=0;
buffer[sizeof(GLubyte)*3]=255;
CCTexture2D *texture = new CCTexture2D;
CCSize size = CCSize(100, 100);
texture->initWithData(buffer, kCCTexture2DPixelFormat_RGB5A1, 1, 1, size);
sprite->setTexture(texture);
sprite->setTextureRect(CCRectMake(0, 0, size.width, size.height));
this->addChild(sprite, 1);
The problem is that I'm not getting the expected color for the rectangle. With the above buffer values I am getting blue color. I am not sure about how to give values to GLubyte(also I dont no how GLubyte works).
Thanks in advance.
Upvotes: 0
Views: 961
Reputation: 64477
If you get blue instead of red, try what you get when you set index 1 or 2 to 255. Perhaps the layout of the pixel data is actually BGR. It certainly seems like it is.
If that's not the case it may be due to the texture having to have a minimum size of 2x2 or 4x4 pixels (yours is 1x1). You may want to try with a 4x4 texture and correspondingly sized data buffer.
Note that you can just write buffer[3]=255;
because the buffer is already "sized as" an array of GLubyte. There's no need to use sizeof for indexes.
Upvotes: 1