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

Author Topic: Failed to load image  (Read 219 times)

0 Members and 1 Guest are viewing this topic.

dimoooooch

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Failed to load image
« on: March 11, 2024, 07:09:30 am »
hello guys, i have another problem. here's my code:

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;

int main()
{
    RenderWindow window(VideoMode::getFullscreenModes()[0], "Chess Fantasy", Style::Fullscreen);
    Texture board, wr, wp;
    if (!board.loadFromFile("board.png") ||
        !wr.loadFromFile("wr.png") ||
        !wp.loadFromFile("wp.png"));
    {
        cerr << "Failed to load textures." << endl;
        return 1;
    }

    float centerX = window.getSize().x / 2.0f;
    float centerY = window.getSize().y / 2.0f;

    RectangleShape board1(Vector2f(800, 800));
    board1.setTexture(&board);
    RectangleShape wr1(Vector2f(100, 100));
    wr1.setTexture(&wr);
    board1.setPosition(centerX - board1.getLocalBounds().width / 2, centerY - 400);
    wr1.setPosition(centerX - board1.getLocalBounds().width / 2, centerY + 300);
    RectangleShape wp1(Vector2f(100, 100));
    wp1.setTexture(&wp);
    wp1.setPosition(centerX - board1.getLocalBounds().width / 2, centerY + 200);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(board1);
        window.draw(wr1);
        window.draw(wp1);
        window.display();
    }

}

in my project's folder and debug folder i have board.png, wr.png, wp.png. but without any reason, it says "Failed to load textures." Other people have a reason, but i don't. also, i tried to insert the path to the file, but then it said "Failed to load textures. Reason: Unable to open file."

so, how do i fix that?

Garwin

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: Failed to load image
« Reply #1 on: March 11, 2024, 07:21:02 am »
Do you run the program directly or through IDE? Usually, IDE has working space and could be different than where you have exe files.

dimoooooch

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Failed to load image
« Reply #2 on: March 11, 2024, 07:23:52 am »
Do you run the program directly or through IDE? Usually, IDE has working space and could be different than where you have exe files.

i run it using Visual Studio 2019 debug

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Failed to load image
« Reply #3 on: March 11, 2024, 08:33:09 am »
The default when running from visual studio is to make the working directory where the .vcxproj file is. But this can be changed in the project settings (Change $(ProjectDir) to $(TargetDir) in the working directory to look where the .exe is instead of the .vcxproj)