1

Currently I use bmp files for an SDL app, but I want to hide them to distribute my exe. I thought moving them as raw bytes into header files was a good way, since the BMP are very simple Black&White patterns.

Am not sure if this is possible by using SDL only, but so far I fail to load a simple pattern of bits.

// data.h    
const unsigned char rawPixels[] =
{
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF,
};


// main.cpp
...
SDL_RWops *pixelsWop = SDL_RWFromConstMem((const unsigned char *)rawPixels, sizeof(rawPixels));
SDL_Surface *pixelsSurface = SDL_LoadBMP_RW(pixelsWop, 1);
SDL_BlitSurface(pixelsSurface, NULL, NULL, NULL);
...

I only get an empty surface from the SDL_LoadBMP_RW call, maybe the array should contain proper BMP header, etc. Could someone point out if that's the problem? Is there another way of loading this?

4

1 回答 1

3

这不起作用的原因是因为您的数据不代表位图,它是原始像素数据。如果您想查看原始位图的外观,只需在绘画中创建一个并在十六进制编辑器中打开它,您将看到标题,然后是实际的像素数据。

我建议只需SDL_Surface使用您想要的尺寸创建一个,然后从该页面修改像素访问方法以获取原始数据或调用putpixel您的每个像素(如果这仅用于测试目的)。

根据几个因素,您的所有像素都不会是内存中的连续值数组,您需要考虑SDL_Surface'spitchbpp(每像素字节数)。在您的情况下,我假设它是 8 位(1 字节)像素?在这种情况下,可以很容易地pixels逐行填充数据,pitch每次移动行指针。

您可能也会发现此文档很有趣。

于 2013-05-18T00:22:24.687 回答