0

I am trying to load the textures in this code:

std::cout << "Current working directory: " << std::filesystem::current_path() << std::endl;


if (!texture.loadFromFile("C:/Users/User/Desktop/Project2/Project2/resources/spaceship.png"))
{
    std::cout << "ERROR: Failed to load spaceship texture!" << std::endl;
}

here is the whole code if it helps:

    #include "Spaceship.h"
    #include <cmath>
    #include <iostream> 
    #include <filesystem> 

    #ifndef M_PI
    #define M_PI 3.14159265358979323846
    #endif

Spaceship::Spaceship(float x, float y, sf::RenderWindow& window, std::vector<Projectile>& projectiles) : window(window), projectiles(projectiles)
{
    position.x = x;
    position.y = y;
    radius = 20.0f;
    shape.setPointCount(3);
    shape.setPoint(0, sf::Vector2f(0, -radius * 1.5f));
    shape.setPoint(1, sf::Vector2f(-radius * 0.5f, radius));
    shape.setPoint(2, sf::Vector2f(radius * 0.5f, radius));
    shape.setOrigin(0, -radius * 0.5f);
    shape.setPosition(position);
    shape.setFillColor(sf::Color::Green);

    
    std::cout << "Current working directory: " << std::filesystem::current_path() << std::endl;

    
    if (!texture.loadFromFile("C:/Users/User/Desktop/Project2/Project2/resources/spaceship.png"))
    {
        std::cout << "ERROR: Failed to load spaceship texture!" << std::endl;
    }
}

    void Spaceship::update()
    {
        // Handle keyboard input to set the velocity for movement
        velocity.x = 0.0f;
        velocity.y = 0.0f;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            velocity.y = -speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            velocity.y = speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x = -speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x = speed;
        }

        position += velocity;

        // Keep the spaceship inside the window
        if (position.x < radius)
            position.x = radius;
        if (position.y < radius)
            position.y = radius;
        if (position.x > window.getSize().x - radius)
            position.x = window.getSize().x - radius;
        if (position.y > window.getSize().y - radius)
            position.y = window.getSize().y - radius;

        shape.setPosition(position);

        sf::Vector2f mousePosition = sf::Vector2f(sf::Mouse::getPosition(window));
        sf::Vector2f direction = mousePosition - position;
        float angle = std::atan2(direction.y, direction.x) * 180.0f / static_cast<float>(M_PI);

        shape.setRotation(angle);

        // Handle firing projectiles with left mouse button
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && fireCooldown <= 0.0f)
        {
            sf::Vector2f direction = mousePosition - position;
            float angle = std::atan2(direction.y, direction.x) * 180.0f / static_cast<float>(M_PI);

            projectiles.emplace_back(position.x, position.y, angle);
            fireCooldown = 1.0f; // Set the cooldown to 1 second
        }

        // Update existing projectiles
        for (auto& projectile : projectiles)
        {
            projectile.update();
        }

        // Remove out-of-bounds projectiles
        projectiles.erase(std::remove_if(projectiles.begin(), projectiles.end(),
            [this](const Projectile& projectile) { return projectile.isOutOfBounds(window); }), projectiles.end());

        // Update the fire cooldown timer
        if (fireCooldown > 0.0f)
        {
            fireCooldown -= 1.0f / 60.0f; // Decrease the cooldown by the time passed since the last frame (assuming 60 FPS)
        }
    }

    void Spaceship::draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        target.draw(shape, states);

        // Draw projectiles
        for (const auto& projectile : projectiles)
        {
            target.draw(projectile, states);
        }
    }

    bool Spaceship::isColliding(const Asteroid& asteroid) const
    {
        float dx = position.x - asteroid.getPosition().x;
        float dy = position.y - asteroid.getPosition().y;
        float distance = std::sqrt(dx * dx + dy * dy);

        return distance < (radius + asteroid.getRadius());
    }

    void Spaceship::setPosition(float x, float y)
    {
        position.x = x;
        position.y = y;
        shape.setPosition(position);
    }

It shows this error when i try to run it even though the spaceship.png file is almost all the folders so i think that path is not the problem? i tried using absolute path and relative path, changing the working directory, and using different images but it still doesn't work

Current working directory: "C:\\Users\\User\\Desktop\\Project2\\Project2"
Failed to load image "". Reason: Unable to open file
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    What is `texture`? – kiner_shah Jul 31 '23 at 05:33
  • First thing to try is to view the file outside of your program. Maybe the file is corrupt somehow. – john Jul 31 '23 at 05:35
  • 1
    Are you by any chance linking to wrong libraries like Debug build but linked to release libraries? https://en.sfml-dev.org/forums/index.php?topic=18495.0 – kiner_shah Jul 31 '23 at 05:38
  • The files aren't corrupted as i can open it normally outside the program. I am also pretty sure that Debug build are properly linked with the sfml lib files but just in case, how do I confirm this? Sorry, i am pretty new in coding. – xenocian Jul 31 '23 at 05:52
  • 1
    Please remove all code not needed to reproduce the issue and fill it with code actually needed to reproduce the issue, making it a [mre]. – Ted Lyngmo Jul 31 '23 at 06:06
  • 1
    I don't think it's the code snippet you've shown us that causes the error output. – Ted Lyngmo Jul 31 '23 at 06:12
  • Most likely as I tried doing it with other classes as well in the project and all produced the same error. – xenocian Jul 31 '23 at 10:44
  • 1
    `Failed to load image "". Reason: Unable to open file` to me this message makes me think that @kiner_shah is correct and you do have a dll problem (like mixing debug and release or using the wrong dlls for your compiler) because of the lost file name. If there was not a dll problem or some type of corruption and the load failed it should have told you the exact file name of the image instead of `""` – drescherjm Jul 31 '23 at 13:19
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Aug 01 '23 at 17:09
  • 2
    you are spot on @drescherjm, the problem was i didn't include the DLLs for debug in additional dependencies (I used release) and that fixed the problem. Thank y'all, really appreciate all the help – xenocian Aug 02 '23 at 12:53

1 Answers1

0

Failed to load image "". Reason: Unable to open file

To me the "" indicates that there is likely some type of undefined behavior happening. That is assuming that the library is does not have a serious bug in the code that prints the error where somehow it lost the actual file name. For a widely used opensource library like SFML I would expect that this code is well tested and working so that the UB must be somewhere else.

From the comments and past experience one such cause of UB can be ABI incompatibility caused by using the wrong binaries. In this case for msvc dlls it's unsafe to mix debug and release dlls. The reason is in debug mode the compiler has additional fields in some structures to aide in debugging and determining of run time corruption.

Related question: Why can't we mix Debug and Release Builds with MSVC?

drescherjm
  • 10,365
  • 5
  • 44
  • 64