c++ - Cocos2d : How do I generate a texture, Sprite ,还是来自 2D 数组的图像?

标签 c++ ios cocos2d-iphone cocos2d-x

我有一个 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/

相关文章:

objective-c - 应用程序中的颜色不同于它们所基于的图像

c++ - 数组类节点不起作用

C++:默认参数和 vector

c++ - 有什么方法可以编写一个类,使得不能从它继承任何类?

ios - 如何使用 AspectFit 缩放模式正确检测 UIImageView 上的点击

ios - localizedString(forRegionCode :) uses a language the app does not support

iphone - 降低背景音乐的音量

ios - cocos2d setOpacity 在 CCRenderTexture 上使用时使 Sprite 变白

c# - 将结构数组从 C++ 编码到 C#?

ios - 如何从子 UIViewController 到父 UIViewController 并从那里到另一个子 UIViewController