I have a 256x256 array of doubles representing a height map which I use for random terrain generation. Currently I display the terrain in a horribly inefficient manner by creating a single pixel sprite for each element in the array and coloring it accordingly. This leaves me with 256x256 sprites, I would of course like to find a way to render a texture or create a sprite or image from this array rather than having to deal with so many tiny sprites. Is there a way to accomplish this in Cocos2d (Cocos2d-x specifically)? I haven't been able to find anything myself.
In cocos2d-iphone you could use [CCTexture2D initWithData:pixelFormat:pixelsWide:pixelsHigh:contentSize:]
to create a texture. And then use [CCSprite spriteWithTexture:]
to create a sprite. There should be corresponding methods in cocos2d-x
Create your data buffer like this, for pixelFormat kCCTexture2DPixelFormat_RGBA8888
:
int width = 256;
int height = 256;
GLubyte *buffer = malloc(sizeof(GLubyte)*4*256);
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
// convert your doubles to pixels, here I just create a white pixel value
for (int i=0;i<4;i++) {
buffer[x*width*4+y*4+i]=255;
}
}
}
Then create your sprite with the methods mentioned above.