【问题标题】:c++ sfml writes to the console: Failed to load image "". Reason: Unable to open filec++ sfml 写入控制台:无法加载图像 \"\"。原因:无法打开文件
【发布时间】:2023-01-20 02:38:47
【问题描述】:

我试图通过 sfml 可能以所有可能的方式上传图像,但我在控制台中收到错误
我的代码:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    sf::Texture texture;
    int i=0;
    if (!texture.loadFromFile("box.jpg"))
    {
        std::cout << "Error!";
        window.close();
        return 1;
    }
    
    sf::Sprite sprite;
    sprite.setTexture(texture);
    sf::Event event=sf::Event();
    while (window.isOpen())
    {   
        
        if (event.type == sf::Event::Closed)
            window.close();
        window.draw(sprite);
        window.clear();
        window.display();
    }

    return 0;
}

也许我做错了什么,我是初学者 c++ 和 sfml 开发人员。
我的设置:
https://i.stack.imgur.com/OF9FA.png
https://i.stack.imgur.com/u2ZSC.png
带有图片的文件位于从 repos 开始的所有文件夹中 \

我将文件拖到解决方案文件夹中的所有文件夹中,从第一个以 x64 文件夹结尾的文件夹开始,都没有成功,我在互联网上搜索了这个问题,我没有找到

【问题讨论】:

  • box.jpg 是否位于启动可执行文件的同一目录中?将图像文件放在源文件夹中是常见的错误。
  • 如果有疑问,只需将 loadFromFile("box.jpg") 更改为您的图像的绝对路径,看看是否可行,例如 loadFromFile("C:\\thisfolder\\thatfolder\\box.jpg")

标签: c++ sfml


【解决方案1】:

你可以从这里开始。

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    sf::Texture texture;
    int i = 0;
    if (!texture.loadFromFile("box.jpg"))
    {
        std::cout << "Error!";
        window.close();
        return 1;
    }

    sf::Sprite sprite;
    sprite.setTexture(texture);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event)) // poll event
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(); // clear first
        window.draw(sprite);
        window.display();
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多