[英]Cannot load an image file as texture using C++ and SFML 2.1
我正在尝试使用SFML 2.1,C ++和MS Visual Studio Professional 2013将图像输出到屏幕上。尝试将文件加载到纹理中时遇到意外错误。 它输出一大堆随机字符。 我确定这是我如何使用Visual Studio配置SFML库还是代码有问题。 谁能解决这个问题? 谢谢。
这是我运行程序( http://i.stack.imgur.com/uMdLT.png )时的屏幕截图:
这是我的代码:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
对于所有配置属性,它们如下所示:
链接器->常规( http://i.stack.imgur.com/NZg7P.png ):
链接器->输入( http://i.stack.imgur.com/1tPaB.png ):
**请注意,如果我没有像配置SFML库那样配置,那么我的系统将msvcr110d.dll is missing
。
好的,我设法解决了这个问题,这是您需要做的:
您确定图片在程序的执行目录中吗? 您应避免在文件名(fighter_jet.png)中使用空格。
如果不确定执行目录,请尝试使用绝对路径(以确保这是路径问题,而不是图片问题)。
希望对您有所帮助。
我已经在我的系统(Xcode-OSX)上尝试了此代码,其中包含一张图片,并且可以正常工作。 您尝试过另一张照片吗?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.