【发布时间】:2020-11-22 04:36:42
【问题描述】:
我试图在我的 sfml 应用程序中设置背景, 但出现错误 代码:
Image hero_image;
hero_image.loadFromFile("image/main.png");
Texture hero_texture;
hero_texture.loadFromImage((hero_image));
Sprite hero_sprite;
hero_sprite.setTexture(hero_texture);
hero_sprite.setPosition(50,30);
//main loop
window.draw(hero_sprite);
错误:
Failed to load image "image/main.png". Reason: Unable to open file
Failed to create texture, invalid size (0x0)
【问题讨论】:
-
好像是你的可执行文件在错误的工作目录下执行,或者镜像文件损坏,或者你没有读取文件的权限。
-
MikeCAT, soo...如何解决目录问题?...
-
使用您的文件浏览器找出图像的位置。然后阅读 IDE 上的文档,了解在调试应用程序时将哪个文件夹设置为工作目录。第二种方法是简单地在你的 c++ 代码中创建一个文件,然后使用你的文件资源管理器来放置它。确定正确的工作目录后,调整图像文件的路径。
-
您在使用 Visual Studio 吗?如果是这样,请记住,当从 Visual Studio 运行可执行文件时,您的 .exe 所在的目录不是您的活动目录。