简体   繁体   English

SFML-无法加载图像-无法打开文件-LINUX

[英]SFML - failed to load image - unable to open file - LINUX


Description of problem 问题描述

I was developing my project on ubuntu (clion ide) but I changed distribution to xubuntu and cloned the same repository to it. 我当时正在ubuntu (clion ide)上开发我的项目,但是我将发行版更改为xubuntu并向其克隆了相同的存储库。 Unfortunately, each image which I include in project generate this runtime error: 不幸的是,我包含在项目中的每个图像都会生成此运行时错误:

Failed to load image "image.png". Reason: Unable to open file

SFML is installed correctly because I checked it with standard program from SFML Site . SFML已正确安装,因为我已使用SFML Site的标准程序对其进行了检查。 I didn't have that problem on ubuntu. 我在ubuntu上没有那个问题。

My try 我的尝试

I think that there is a problem with permissions of files. 我认为文件权限存在问题。 So I create sample project (as below), added player.png and I get the same error. 所以我创建了示例项目(如下所示),添加了player.png,并且得到了相同的错误。 So I use that command in terminal: 所以我在终端中使用该命令:

chmod a+w player.png

It doesn't solve a problem. 它不能解决问题。 I am new in linux so maybe there is something more to do, but I don't know. 我是Linux新手,所以也许还有更多事情要做,但我不知道。 I also tried different ways from this site and SFML forum but it doesn't helps me. 我也尝试过从本网站和SFML论坛中使用其他方法,但这对我没有帮助。
I also tried to load other images, doesn't work too. 我也尝试加载其他图像,但也无法正常工作。

Code

There is sample code which I have created to find problem. 我创建了示例代码来查找问题。 It is only modified sample from SFML Site . 它仅是SFML网站的修改样本。

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
    sf::Texture t;
    t.loadFromFile("player.png");
    sf::Sprite sprite;
    sprite.setTexture(t);

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

        window.clear();
        window.draw(sprite);
        window.display();
    }
    return 0;
}

Solution and another problem: 解决方案和另一个问题:

What is interesting, 有趣的是

    t.loadFromFile("../player.png");

solved the problem. 解决了问题。 By default, sfml expected the image to be in cmake-build-debug folder... 默认情况下,sfml希望该图像位于cmake-build-debug文件夹中。
How to add default directory to my project, not to cmake folder? 如何将默认目录添加到我的项目,而不是cmake文件夹?

You are looking for an option to change your current working directory. 您正在寻找一个更改当前工作目录的选项。 This can be done either using CLion or CMake. 可以使用CLion或CMake来完成。

Take a look at this question where you can find answers which describe both ways. 看一下这个问题 ,您可以在其中找到描述两种方式的答案。

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

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