Unable to load file:

// Include important C++ libraries here
#include "stdafx.h"
#include <SFML/Graphics.hpp>

// Make code easier to type with "using namespace"
using namespace sf;

int main()
{
// Create a video mode object
VideoMode vm(1920, 1080);

// Create and open a window for the game
RenderWindow window(vm, "Timber!!!", Style::Fullscreen);

// Create a texture to hold a graphic on the GPU
Texture textureBackground;

// Load a graphic into the texture
textureBackground.loadFromFile("graphics/background.png");

// Create a sprite
Sprite spriteBackground;

// Attach the texture to the sprite
spriteBackground.setTexture(textureBackground);

// Set the spriteBackground to cover the screen
spriteBackground.setPosition(0, 0);


while (window.isOpen())
{

/*
****************************************
Handle the players input
****************************************
*/

if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}

/*
****************************************
Update the scene
****************************************
*/


/*
****************************************
Draw the scene
****************************************
*/

// Clear everything from the last frame
window.clear();

// Draw our game scene here
window.draw(spriteBackground);

// Show everything we just drew
window.display();


}

return 0;
}

Can anyone give me some kind of clue to solve this problem.
I am getting this message: failed to load image "Graphics/background.png" unable to open file.

I do not quite understand this yet:
Where do you want me to add system "cd"?
Last edited on
You have to figure out what directory your program is executing from.
Usually, it's one of two places when working with an IDE
(1) Same directory as your solution or project file (.vsproj or whatever)
(2) In your project's debug/bin or release/bin directory

So you either have to set up your file system to be "[ProjectFolder]/graphics/background.png", or "[ProjectFolder]/debug/bin/graphics/background.png", depending on whether your situation is (1), or (2), respectively.

To see where your code is executing from, you can add
system("cd");.
This will print your current directory.
Last edited on
I know this book. Go back to section where you set up the project and double check your settings vs. the book. It's easy to mistype a location when setting all that information up for SFML.
If you can give me some tips on what I might not be doing RIGHT in this book
I am still stuck on this issue!

Thanks,
Chester
You haven't said anything about your setup. Are you using Visual Studio on Windows (as implied by stdafx.h)?

You spell "graphics" with a small g in the code but a capital G in the error message. I don't know if that makes a difference on Windows (I suspect not).

Do you even have the graphics directory? Does it conatin background.png? Where exactly is the graphics directory?

As for the system("cd"); command (which I'm assuming is a windows thing, since just typing cd does not print anything on Linux) just put it as the first line in main. It should print something to the terminal. And you should include <cstdlib> to use the system function.
Last edited on
tpb, true, since he mentioned Visual Studio, I didn't bother mentioning the Linux equivalent.

The Linux equivalent to system("cd"); is system("pwd"); But it sounds like OP doesn't want to take the advice anyway. Interesting point that the SFML error gives a capital G; it most likely means the directory exists, but the file inside of it does not. (It could also, on a rare occasion, be a file permissions error, but I sincerely doubt this.)
Last edited on
Topic archived. No new replies allowed.