SFML community forums

Help => Graphics => Topic started by: ChrisPanov on January 05, 2018, 01:56:06 am

Title: Unable to open file
Post by: ChrisPanov on January 05, 2018, 01:56:06 am
Hey everyone,

I'm new to SFML, and just bought that book about game programming with SFML,
but i have some problems.

Here is the code:

#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{

        VideoMode vm(1920, 1080);

        RenderWindow window(vm, "Timber", Style::Fullscreen);

        Texture textureBackground;

        textureBackground.loadFromFile("assets/graphics/background.png");

        Sprite spriteBackground;

        spriteBackground.setTexture(textureBackground);

        spriteBackground.setPosition(0, 0);


        while (window.isOpen())
        {

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

                window.clear();

                window.draw(spriteBackground);

                window.display();

        }

        return 0;
}


 


The thing is that i cant load the background.
This is the message i get: "The requested video mode is not available, switching to a valid mode
w╠їя          8     ". Reason: Unable to open file"

Where am i wrong?

And just to note, i've tried moving the png file everywhere and still no success
Title: Re: Unable to open file
Post by: Spirro on January 05, 2018, 07:23:20 am
You didn't mention what IDE you are using to develope, but the path you have in your source may not be the path your IDE is using.  I think for VS the search path for files starts in the main directory of your project, but not 100% on that.  So your exe is looking for your lib in it's exe directory/assets/graphics/background.png.  Your IDE probably has a different start to that path.
Title: Re: Unable to open file
Post by: eXpl0it3r on January 05, 2018, 12:22:08 pm
You shouldn't be using using namespace sf as it makes your code harder to read and can quickly lead to name conflicts.

The error message doesn't seem to make that much sense, as such I wonder whether you're wrongly linking libraries. Make sure you link the debug (-d suffix) libraries in debug mode and the release (no -d suffix) libraries in release mode.

And make sure your system can handle a resolution of 1920x1080.
Title: Re: Unable to open file
Post by: ChrisPanov on January 05, 2018, 08:26:17 pm
I'm using Visual Studio 2017

Well ive done everything with the properties as it's shown in the book
The linker is okay i think

How could i change my start to the path?
What should i do?
Title: Re: Unable to open file
Post by: eXpl0it3r on January 05, 2018, 08:54:27 pm
The book might simply be wrong, make sure to follow the official tutorial for setting up Visual Studio instead: https://www.sfml-dev.org/tutorials/2.4/
Title: Re: Unable to open file
Post by: ChrisPanov on January 06, 2018, 12:52:50 am
The thing is that the sample code works perfectly
Could there still be a problem with the linker with that in mind?
Title: Re: Unable to open file
Post by: FRex on January 06, 2018, 02:03:33 pm
Use https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1VideoMode.php#a6815b9b3b35767d5b4563fbed4bfc67b and see if your desired mode is there.
Title: Re: Unable to open file
Post by: sp78 on October 15, 2024, 06:08:33 pm
I also found the same error, for future reference VS add space between ("graphics / background.png") - just put everything together ("graphics/background.png") and it works, for me at least.
Title: Re: Unable to open file
Post by: sbwtr on March 06, 2025, 04:38:16 pm
for future reference
As mentioned libraries are wrongly linked
please see attached screens
in Project settings first you must choose mode: Release or Debug
then Linker-> input add libraries
with -d in Debug mode , without -d in Release mode