如何在 exe 文件夹中不包含纹理
How to not include texture in exe folder
我正在为学校做一些sfml项目,老师只想要.exe程序。我正在使用Visual Studio 2017。在这个项目中,我正在使用.jpg文件中的纹理
sf::RenderWindow window(sf::VideoMode(640, 480, 32), "Kurs SFML ");
sf::Texture texture;
texture.loadFromFile("wood.jpg");
sf::Sprite pic;
pic.setTexture(texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(pic);
window.display();
这个文件(wood.jpg(需要与项目在同一个文件夹中才能显示这种纹理,否则它只显示黑屏。当我检查另一个文件夹中的.exe程序时,它还需要此文件在此文件夹中,否则.exe显示黑屏。但是我的老师只想要.exe没有任何文件夹的文件。那么是否可以做一些事情来不包括这个文件( wood.jpg (,而是在.exe中显示纹理?
在
可执行文件中嵌入纹理。
一个简单的解决方案是编写一个小工具,用于读取文件并写出一个C++源文件,其中包含包含原始字节的constexpr std::array
。然后从链接到可执行文件的该变量(SFML 具有从内存加载资源的函数(加载纹理。
编写这样的工具不应超过 10-20 行代码。
对于特定于 SFML 的解决方案,您可以执行此操作。
sf::Image tmp;
tmp.loadFromFile("super.jpeg");
std::ofstream file;
file.open("textarray.cpp");
size_t psize = tmp.getSize().x * tmp.getSize().y * 4;
auto ptr = tmp.getPixelsPtr();
file << "sf::Uint8 imageArray[] = {" << (int)ptr[0];
for (size_t i = 1; i<psize; ++i)
file << "," << (int)ptr[i];
file << "};";
file.close();
这将创建一个文件名 textarray.cpp其中包含看起来像 sf::Uint8 imageArray[] = {...};
然后你可以像这样在程序中加载它。
sf::Uint8 imageArray[] = {...};
sf::Image img;
img.create(80, 80, imageArray); // Replace 80, 80 with width and height of your image!!!
sf::Texture texture;
texture.loadFromImage(img);
sf::Sprite sprite;
sprite.setTexture(texture);
从这里开始,只需像往常一样绘制精灵即可。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- SFML纹理像播放器
- 在混合代码库中将C转换为C++时出现许多包含错误
- OpenGL大的3D纹理(>2GB)非常慢
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 为什么您需要C++头文件的包含保护
- 如何在 exe 文件夹中不包含纹理
- 找出模型中包含非不透明纹理的Tris