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

Author Topic: Issues with file paths and texture.loadFromFile()  (Read 400 times)

0 Members and 1 Guest are viewing this topic.

mightbmay

  • Newbie
  • *
  • Posts: 4
    • View Profile
Issues with file paths and texture.loadFromFile()
« on: January 27, 2025, 05:23:21 pm »
Hello,
Am new to SFML, and am slowly learning the basics of it in c++.
When trying to draw my first image to the screen, i attempt to load it to a texture from a file.
Here is my code to do so:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <filesystem>

using namespace std;

int main() {
    // Print the current working directory to debug
    cout << "Current Working Directory: " << filesystem::current_path() << endl;

    sf::RenderWindow window(sf::VideoMode({ 200, 200 }), "SFML works!");

    sf::Texture texture;// ("C:/Users/turtw/OneDrive/Desktop/Cpp/2dGame/2dGame/Resources//Image1.png", false, sf::IntRect({ 10, 10 }, { 32, 32 }));
   

    texture.loadFromFile("Resources/Gun.png");
    sf::Sprite sprite(texture);

    while (window.isOpen()) {
        while (const optional<sf::Event> event = window.pollEvent()) {
            if (event->is<sf::Event::Closed>())
                window.close();
        }

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

 

However when i run this code, i get the error:
"
Failed to load image
    Provided path: ""
    Absolute path: ""
Reason: Unable to open file
"
In the tutorial on the SFML website, it mentions stuff about double checking the relative file path to make sure your working directory is where you think it is, so I confirmed my working directory is:
"C:\\Users\\turtw\\OneDrive\\Desktop\\2dGame\\2dGame" ( I set it there in visual studio)

i can also confirm the file exists at that path, both the full path, and relative. using Filesystem.exists shows the file is in fact there. you can also see how my project directory is set up in the attatched image.

i think the issue i am having is that the texture.loadFromFile() seems to not be recieving any path at all, as the provided and absolute path are both just empty strings, but i cannot for the life of me figure out why that may be.

am i correct in this assumption, or am i misinterpereting the problem? if i am correct how do i resolve it?



mightbmay

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with file paths and texture.loadFromFile()
« Reply #1 on: January 28, 2025, 07:46:20 am »
EDIT:
After getting home today, i tried switching visual studio from debug to release mode, and it worked. this is until i switch BACK to debug mode, then afterwards every single time, it forever just gives the:
"
Failed to load image
    Provided path: ""
    Absolute path: ""
Reason: Unable to open file
"
error, as before, until i close visual studio entirely, then switch to release and then run it again.

I went and re checked, and i have the includes and lib files properly set up in visual studio, with the -d versions for debugging.

mightbmay

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with file paths and texture.loadFromFile()
« Reply #2 on: January 28, 2025, 07:48:31 am »
Solved??

Right after posting my last reply, in visual studio, i right clicked my solution in the explorer, then clicked rebuild solution, and now it works perfectly fine. I'm not super familliar with visual studio but this seems to have solved the issue.

EDIT*
It partially solved it.
If i rebuild the solution on Release mode, everything works perfectly fine, even if i switch to debug mode after.
HOWEVER if i change literally anything in debug mode (or rebuild in debug mode) the same error occours.
For now i will just work in release mode, but this is obviously not the ideal solution. Please let me know if you've figured out potential causes for this.
« Last Edit: January 28, 2025, 07:52:22 am by mightbmay »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11110
    • View Profile
    • development blog
    • Email
Re: Issues with file paths and texture.loadFromFile()
« Reply #3 on: January 28, 2025, 08:26:43 am »
Sounds like you aren't linking the debug version of SFML in debug mode. Check your project settings and that you're referencing the SFML files that end in -d (e.g. sfml-system-d.lib).
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mightbmay

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with file paths and texture.loadFromFile()
« Reply #4 on: January 28, 2025, 08:33:23 am »
in my additional dependencies for the linker like so?

At least the way i have in the image, the issue still occurs.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11110
    • View Profile
    • development blog
    • Email
Re: Issues with file paths and texture.loadFromFile()
« Reply #5 on: January 28, 2025, 09:30:00 am »
You can't set the libraries in the "All configuration". They libraries differ between debug and release.

Remove them from the "All configuration".
Switch to release and add all the SFML libs without -d suffix.
Switch to debug and all the SFML libs with -d suffix.
You only need one set per configuration
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything