我最近开始学习SFML
,我正在尝试显示图片。图像应该显示在屏幕上,并可以用箭头键和WASD移动。
然而,我得到一个空白屏幕,其中包含错误F ailed以加载图片" image.png" 。原因:无法打开文件,当我显示位图时,它可以工作。我该如何解决这个问题?
// ConsoleApplication8.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML works!", sf::Style::Resize | sf::Style::Default);
sf::CircleShape shape(100.f);
sf::Vector2i position(-9,0);
shape.setFillColor(sf::Color::Green);
sf::Texture texture;
if (!texture.loadFromFile("image.png", sf::IntRect(10, 10, 32, 32)))
{
std::cout << "Could not load image\n";
system("pause");
}
sf::Sprite Sprite;
Sprite.setTexture(texture);
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
switch (evnt.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
Sprite.move(0.0f, -1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
Sprite.move(0.0f, 1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
Sprite.move(-1.0f, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
Sprite.move(1.0f, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
Sprite.move(0.0f, -1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) {
Sprite.move(0.0f, 1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
Sprite.move(-1.0f, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
Sprite.move(1.0f, 0.0f);
}
window.setPosition(position);
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
答案 0 :(得分:0)
确保image.png位于正确的位置。 \Visual Studio 2015\Projects\*ProjectName*\*ProjectName*
如果它在那里,请尝试将其放入包含可执行文件的目录中。
\Visual Studio 2015\Projects\*ProjectName*\Debug(or release)