SFML 图像打不开

问题描述 投票:0回答:0

我正在尝试加载此代码中的纹理:

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;
}

如果有帮助的话,这是完整的代码:

    #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);
    }

当我尝试运行它时,它显示此错误,即使 spaceship.png 文件几乎是所有文件夹,所以我认为路径不是问题?我尝试使用绝对路径和相对路径,更改工作目录,并使用不同的图像,但它仍然不起作用

当前工作目录:“C:\Users\User\Desktop\Project2\Project2” 无法加载图像“”。原因:无法打开文件

c++ sfml
© www.soinside.com 2019 - 2024. All rights reserved.