如何在SDL中使用CImg图像

时间:2013-04-20 12:02:43

标签: sdl cimg

我正在尝试在SDL表面上显示使用CImg生成的图像。

CImg将图像数据保存为一个简单的数组(例如,绿色值之前的红色值,蓝色值之前)。

我已经知道使用RWops是可行的方法,但不知怎的,我无法弄清楚如何将图像数据转换为RWops结构。

1 个答案:

答案 0 :(得分:1)

我从未使用CImg,但基本上,您需要创建一种方法来转换CImg数据以遵循已知的图像格式,例如bitmap。< / p>

不幸的是,CImg似乎没有提供这种功能,如SourceForge所示,但有人在线程中提供代码(尽管似乎存在格式问题) )。

使用上面线程中的代码和SDL_LoadBMP_RW,您可以执行以下操作:

unsigned char *bitmapImage = NULL; //the target-buffer
bitmapImage = cimg_image.save_bmp2buffer(); //get the bmp-buffer

// the buffer size is based on the bmp format, according to save_bmp2buffer it should be something like:
// I simplified a bit his formula, some operations didn't seem necessary
// The 54 represents the size of a bitmap header, the rest is the padded pixel content size
int imgSize = 54 + (3 * cimg_image.width() + 4 - (3 * cimg_image.width()) % 4) * cimg_image.height();

SDL_RWops* rw = SDL_RWFromMem(bitmapImage, imgSize );

SDL_Surface* yourSurface = SDL_LoadBMP_RW(rw, 1); // 1 will free the rw when done

free(bitmapImage);

此代码未经过测试,但应该是一个很好的起点!