0

I am trying to write sf::mouse::getPosition(const Window &Relative To) in a function but I can't write the correct argument in it. Why is that?

Here is my Game.cpp File: EDIT: I pasted the current code:

#include "Game.h"

Game::Game() : m_window("Multiplayer Space Shooter Game", sf::Vector2u(800, 600)) {
    // Setting up class members.
    m_shipText.loadFromFile("C:\\Users\\AliTeo\\Desktop\\Piksel çalışmaları\\ship_pixel2.png");
    m_ship.setTexture(m_shipText);
    m_ship.setOrigin(m_shipText.getSize().x / 2, m_shipText.getSize().y / 2);
    m_ship.setPosition(320, 240);
}
Game::~Game() {}
void Game::Update() {
    m_window.Update(); // Update window events.
    m_ship.move(m_speedX, m_speedY);
    m_ship.setRotation(90 + m_angle);
    HandleInput();
}
void Game::HandleInput() {
    sf::Mouse m_mouse;
    m_angle = atan2(m_mouse.getPosition(&m_window).y - m_ship.getPosition().y, m_mouse.getPosition(&m_window).x - m_ship.getPosition().x);
    m_angle *= 180 / m_PI;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_speedX <= m_speedLimit) {
        m_speedX += m_acceleration;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_speedX >= -1 * m_speedLimit) {
        m_speedX -= m_acceleration;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && m_speedY <= m_speedLimit) {
        m_speedY += m_acceleration;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && abs(m_speedY) >= -1 * m_speedLimit) {
        m_speedY -= m_acceleration;
    }
}
void Game::Render() {
    m_window.BeginDraw(); // Clear.
    m_window.Draw(m_ship);
    m_window.EndDraw(); // Display.
}
Window* Game::GetWindow() { return &m_window; }

m_window is a member of Game.h

no instance of overloaded function "sf::Mouse::getPosition" matches the argument list   

#include "Window.h"
class Game {
public:
    Game();
    ~Game();
    void HandleInput();
    void Update();
    void Render();
    Window* GetWindow();
private:
    sf::Sprite m_ship;
    sf::Texture m_shipText;
    //sf::Vector2i mousePos = sf::Mouse::getPosition();
    Window m_window;
    const float m_PI = 3.1415f;
    float m_speedX = 0;
    float m_speedY = 0;
    float m_acceleration = 0.001f;
    //float m_friction = 0.001f;
    float m_speedLimit = 0.5f;
    float m_angle;
};
AliTeo
  • 35
  • 1
  • 7

3 Answers3

1

The getPosition method inside of the sf::Mouse class is actually static. You'll need to use it like this inside of your game loop that preforms updates every frame:

sf::Vector2i mousePos = sf::Mouse::getPosition(m_window);
nitomoe
  • 26
  • 1
  • Hi, I tried to write this inside main.cpp then Game::Update but I am still getting the same error. Where should I write it exactly? – AliTeo Mar 01 '18 at 05:21
0

You forgot to pass m_window to the second Mouse::getPosition

Bálint
  • 14,887
  • 2
  • 34
  • 55
0

You're trying to pass a pointer to m_window but the function just wants a reference.

m_mouse.getPosition(m_window) should do the trick as opposed to your current m_mouse.getPosition(&m_window).