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?