[英]SFML Sprite not displaying
我最近开始学习SFML
并且正在尝试显示图像。 该图像应该显示在屏幕上,并且可以通过箭头键和WASD移动。
但是,出现黑屏,错误为未能加载图像“ 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;
}
确保image.png在正确的位置。 \\Visual Studio 2015\\Projects\\*ProjectName*\\*ProjectName*
如果在其中,请尝试将其放入包含可执行文件的目录中。
\Visual Studio 2015\Projects\*ProjectName*\Debug(or release)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.