我有一个 256x256 的 double 组,表示我用于随机地形生成的高度图。目前,我通过为数组中的每个元素创建单个像素 Sprite 并相应地着色来以极其低效的方式显示地形。这给我留下了 256x256 的 Sprite ,我当然希望找到一种方法来渲染纹理或从这个数组创建 Sprite 或图像,而不是处理这么多微小的 Sprite 。有没有办法在Cocos2d(特别是Cocos2d-x)中实现这一点?我自己没能找到任何东西。
最佳答案
在cocos2d-iphone中,您可以使用[CCTexture2D initWithData:pixelFormat:pixelsWide:pixelsHigh:contentSize:]
来创建纹理。然后使用[CCSprite spriteWithTexture:]
创建 Sprite 。 cocos2d-x中应该有相应的方法
像这样创建数据缓冲区,对于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;
}
}
}
然后用上面提到的方法创建你的 Sprite 。
关于c++ - Cocos2d : How do I generate a texture, Sprite ,还是来自 2D 数组的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927737/