c++ - 使用 SDL 打开嵌入的 BMP 图像?

标签 c++ visual-studio sdl embedded-resource sdl-image

我正在尝试将图像嵌入到 C++ 程序中,但无法使用 SDL 打开它。

我已经尝试使用 xpm、xfc,尝试使用 SDL_LoadBMP_RW(),尝试将图像转换为 C 代码...没有任何效果。

我尝试将图像添加到“资源文件”部分并进行编码:

#include <image.bmp>
(...)
const char* image_path = "image.bmp";
SDL_RWops* file = SDL_RWFromFile(image_path, "rb");
gHelloWorld = SDL_LoadBMP_RW(file, SDL_TRUE);

这是输出:

Build started...
1>------ Build started: Project: SDL Game, Configuration: Debug x64 ------
1>main.cpp
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,7): error C2018: unknown character '0x12'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,13): error C2146: syntax error: missing ';' before identifier 'Š'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,25): error C2018: unknown character '0x2'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,30): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,33): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,37): error C2018: unknown character '0x3'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,44): error C2018: unknown character '0x12'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,64): error C3872: '0x8f': this character is not allowed in an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,96): error C2018: unknown character '0x1e'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,97): error C2018: unknown character '0x15'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,98): error C2018: unknown character '0x1e'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,99): error C3873: '0x2026': this character is not allowed as a first character of an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,104): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,108): error C2018: unknown character '0x13'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,116): error C2018: unknown character '0x6'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,1): error C3873: '0xd7': this character is not allowed as a first character of an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,3): error C2018: unknown character '0x3'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,5): error C2017: illegal escape sequence
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,6): fatal error C1060: compiler is out of heap space
1>Done building project "SDL Game.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我也尝试过同样的方法,但我只是写了这个:

gHelloWorld = SDL_LoadBMP("image.bmp");

它确实编译了,但随后它在控制台中写入了以下内容:

Unable to load image image.bmp! SDL Error: Parameter 'src' is invalid
Failed to load media!

我使用的是带有 Visual Studio 的 64 位 Windows,我还安装了 SDL2 和 SDL2_image。

最佳答案

正如 drescherjm 指出的那样,image.bmp 不会是有效的 C++ 代码。

使用类似 xxd 的内容将位图数据转换为标题,例如:

输入文件(test.txt):

Hello world!

转换:

$ xxd -include test.txt | tee test.h
unsigned char test_txt[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
  0x0a
};
unsigned int test_txt_len = 13;

然后你可以使用SDL_RWFromConstMem()使用 header 中的数组和长度值来获取 SDL_LoadBMP_RW()SDL_RWops可以摄取。

关于c++ - 使用 SDL 打开嵌入的 BMP 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72634293/

相关文章:

c++ - 算法:最长近似间隔

c++ - 计算布隆过滤器的近似种群

c++ - SDL 僵硬运动

c++ - SDL_VIDEORESIZE 不起作用

c++ - 如何通过linux套接字发送图像数据

c++ - OpenGL/GLUT - 大量 undefined reference

c++ - 链接 Visual Studio 2008 Express '/Mtd' 设置时 Boost 崩溃

wcf - Jenkins 与 Visual Studio Online 集成认证

.net - 无法加载文件或程序集...或其依赖项之一。尝试加载格式不正确的程序(.resx 文件)

c - 处理 GetDIBits() 返回的像素缓冲区的正确方法是什么?