Search code examples
c++sfml

Why is my sound file not being found when I run my program from the desktop?


I am making a simple snake game with SFML and am currently trying to add a sound that plays whenever the snake "eats" one of the dots. So I downloaded a sound file and put it in my project directory and added this to my code:

sf::SoundBuffer buffer;
buffer.loadFromFile("Crunch.wav")
sf::Sound crunch;
crunch.setBuffer(buffer);
if (snake.isTouching(food))
{
    crunch.play();
}

When I run the game from Visual Studio it works fine. But I like to put my finished games on my desktop, so I went into my Debug folder, found my .exe file, made a shortcut and placed it on my desktop. But whenever I try to run the program with the desktop shortcut, the message "Failed to open sound file 'Crunch.wav' (couldn't open stream)" shows up on the output window. Does anyone know what is causing this? The program runs as it's supposed to when I run it from VS. Thanks in advance.


Solution

  • loadFromFile() looks in the current directory, when you build and run in visual studio the directory is ProjectName\ProjectName by default, in this case it seems you have Crunch.wav in the ProjectName\ProjectName folder alongside the .vcxproj file, so you should make sure to keep Crunch.wav with the .exe in the same way you have it in the project folder, in this case copying Crunch.wav to the same location as the .exe