简体   繁体   English

SFML Sprite不显示

[英]SFML Sprite not displaying

I recently started learning SFML and I am trying to display an image. 我最近开始学习SFML并且正在尝试显示图像。 The image is supposed to be displayed on the screen and be movable with arrow keys and WASD. 该图像应该显示在屏幕上,并且可以通过箭头键和WASD移动。

However, am getting a blank screen with an error F ailed to load image "image.png" . 但是,出现黑屏,错误为未能加载图像“ image.png” Reason: Unable to open file, when I display a bitmap it works. 原因:无法打开文件,当我显示位图时它可以工作。 How can I fix this? 我怎样才能解决这个问题?

// 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;
}

Make sure that image.png is in the correct place. 确保image.png在正确的位置。 \\Visual Studio 2015\\Projects\\*ProjectName*\\*ProjectName*

If it is in there try and put it in the directory with the executable in it. 如果在其中,请尝试将其放入包含可执行文件的目录中。

\Visual Studio 2015\Projects\*ProjectName*\Debug(or release)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM