Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Unable to open file error on Arch Linux  (Read 1627 times)

0 Members and 1 Guest are viewing this topic.

arash28134

  • Newbie
  • *
  • Posts: 4
  • Stayin' still, eyes closed, let the world pass by
    • View Profile
    • My Github
    • Email
SFML Unable to open file error on Arch Linux
« on: August 15, 2023, 02:48:27 pm »
Hi. I'm using Arch with Nvidia's latest stable driver.
This is my code:

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

#include <iostream>

using namespace sf;

int main(){
    RenderWindow window(VideoMode(800, 600), "Flappy Bird");
    std::cout << "\nWindow created.\n";

    std::cout << "\nCreating texture...\n";
    Texture texture;
    if (!texture.loadFromFile("/assets/textures/image.jpg"))
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;
    Sprite sprite(texture);
    std::cout << "\nTexture created.\n";

    std::cout << "\nCreating font...\n";
    Font font;
    if (!font.loadFromFile("assets/fonts/arial.ttf"))
        std::cout << "\nFailed loading font.\n";
        return EXIT_FAILURE;
    Text text("Hello SFML world!", font, 50);

    std::cout << "\nFont created.\n";

    std::cout << "\nCreating music...\n";
    Music music;
    if (!music.openFromFile("assets/sounds/background.mp3"))
        std::cout << "\nFailed loading music.\n";
        return EXIT_FAILURE;
    std::cout << "\nMusic created...\n";
   
    music.play();

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case Event::Closed:
                window.close();
                break;
           
            default:
                break;
            }
        }

        window.clear();

        window.draw(sprite);

        window.draw(text);

        window.display();
    }
   
    // Exit the program successfully...
    return EXIT_SUCCESS;
}
 

When I run this, I get the following output:

Quote
Window created.
Creating texture...
Failed to load image "/assets/textures/image.jpg". Reason: Unable to open file


Failed loading texture.

Any ideas?  :-\
p.s.: This is my first time posting here, sorry if the formatting is trash.
« Last Edit: August 15, 2023, 05:30:26 pm by arash28134 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML Unable to open file error on Arch Linux
« Reply #1 on: August 15, 2023, 04:55:52 pm »
You most likely don't want to use /assets as this will look for a folder in the root directory of you system called assets. For a relative path, you can just use assets/textures/image.jpg.

Then make sure the assets directory in your working directory.

p.s.: This is my first time posting here, sorry if the formatting is trash.
You can use the [code=cpp][/code] tags for code formatting.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

arash28134

  • Newbie
  • *
  • Posts: 4
  • Stayin' still, eyes closed, let the world pass by
    • View Profile
    • My Github
    • Email
Re: SFML Unable to open file error on Arch Linux
« Reply #2 on: August 15, 2023, 05:28:09 pm »
You most likely don't want to use /assets as this will look for a folder in the root directory of you system called assets. For a relative path, you can just use assets/textures/image.jpg.

Then make sure the assets directory in your working directory.

p.s.: This is my first time posting here, sorry if the formatting is trash.
You can use the [code=cpp][/code] tags for code formatting.

Changing /assets to assets doesn't make any difference, except it now doesn't even output an error for some reason.
this is the output:

Quote
Window created.

Creating texture...
%

What does that "%" at the end even mean?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML Unable to open file error on Arch Linux
« Reply #3 on: August 15, 2023, 05:51:31 pm »
Note that this code is wrong:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;

without indentation it means:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
        std::cout << "\nFailed loading texture.\n";

    return EXIT_FAILURE;

So it would also just exit your application.
I'm personally a fan of using curly braces at all times, even or especially for single line statements:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
    {
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;
    }

Note the same issue for the music
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

arash28134

  • Newbie
  • *
  • Posts: 4
  • Stayin' still, eyes closed, let the world pass by
    • View Profile
    • My Github
    • Email
Re: SFML Unable to open file error on Arch Linux
« Reply #4 on: August 15, 2023, 06:39:59 pm »
Note that this code is wrong:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;

without indentation it means:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
        std::cout << "\nFailed loading texture.\n";

    return EXIT_FAILURE;

So it would also just exit your application.
I'm personally a fan of using curly braces at all times, even or especially for single line statements:

    if (!texture.loadFromFile("/assets/textures/image.jpg"))
    {
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;
    }

Note the same issue for the music

Yes! this fixed my issue; thanks. it's weird how I had never noticed this in C++, I guess because I also usually go with curly braces.

std::cout << "\nCreating texture...\n";
    Texture texture;
    if (!texture.loadFromFile("assets/textures/image.jpg"))
    {
        std::cout << "\nFailed loading texture.\n";
        return EXIT_FAILURE;
    }

    Sprite sprite(texture);
    std::cout << "\nTexture created.\n";

    std::cout << "\nCreating font...\n";
    Font font;
    if (!font.loadFromFile("assets/fonts/arial.ttf"))
    {
        std::cout << "\nFailed loading font.\n";
        return EXIT_FAILURE;
    }
    Text text("Hello SFML world!", font, 50);

    std::cout << "\nFont created.\n";

    std::cout << "\nCreating music...\n";
    Music music;
    if (!music.openFromFile("assets/sounds/background.mp3"))
    {
        std::cout << "\nFailed loading music.\n";
        return EXIT_FAILURE;
    }
    std::cout << "\nMusic created...\n";
 

 

anything