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