1

I am currently working on images and procedurally generate images then convert them into dds or rtc1. Due to constrains of my program, I cannot open external files so I want them embedded with the code.

I found out GIMP has a "c header" exporter but i'd like to do that myself for my generated textures.

Using void* would allow this to be used for any types of data.

UPDATE: To clarify things, I was thinking about outputting as unsigned int like this:

void exportToHeader(const void* data, const uin32_t size, const char* name)
{
    uint32_t* pData = (uint32_t*)data;

    printf("void* %s = {\n");

    // TODO: handle last case
    for (int i = 0; i < size; ++i) {
        printf("%s, ", pData[i]);
    }

    printf("};\n");            
}

Will the compiler be able to understand this ?

4

2 回答 2

1

如果我理解您更正您想要做的是将二进制图像导入您的 C 代码。

所以在这种情况下我要做的是,编写一个简单的程序,将文件作为字节列表转储并编写如下输出:

char mydata[] = { 122, 334, 45, ... };

然后你可以简单地包含这个文件并像任何其他 C 数组一样引用你的纹理。

转储文件相当简单(在伪代码中)。

file = open(file);
printf("char mydata[] = {\n");
while(file != eof)
{
    c = getc(file);
    printf("%d, ", c);
}
close(file);
printf("\n\;\n");

有了这个,您可以在 C 代码中包含任何二进制文件。

于 2013-05-21T09:33:08.783 回答
1

我最终这样做了:

        uint32_t* intData = (uint32_t*)data; 
        for (int i = 0; i < size; ++i) {
            if (i == size -1) {
                fprintf(fp, "%uu };", intData[i]);
            } else {
                fprintf(fp, "%uu, ", intData[i]);
            }
        }

当然,您必须确保对齐 4 个字节才能转换为 uint32_t*

于 2013-05-23T13:20:42.493 回答