gpt4 book ai didi

c++ - SFML sf::Mouse::getPosition 方法无法写入正确的参数

转载 作者:行者123 更新时间:2023-11-30 05:04:07 25 4
gpt4 key购买 nike

我的问题是,我已经在 Game::HandleInput() 方法中编写了这段代码,但我无法使 sf::Mouse::getPosition() 方法获取鼠标相对于窗口的坐标。没有争论,我不会得到错误。但是,船不能正常旋转。我试过 getPosition(m_window)getPosition(&m_window)。我收到此错误:

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

编辑:更新了 WINDOW.H

Window.h:

class Window{
//Constructers
public:
Window();
Window(const std::string& l_title, const sf::Vector2u& l_size);

...
private:
sf::RenderWindow m_window;
...
}

编辑:添加了 WINDOW.CPP 的完整代码:

Window.cpp:

#include "Window.h"

Window::Window() {
Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& l_title, const sf::Vector2u& l_size) {
Setup(l_title, l_size);
}
Window::~Window() {
Destroy();
}

void Window::Setup(const std::string& l_title,
const sf::Vector2u& l_size)
{
m_windowTitle = l_title;
m_windowSize = l_size;
m_isFullscreen = false;
m_isDone = false;
Create();
}
void Window::Create() {
auto style = (m_isFullscreen ? sf::Style::Fullscreen
: sf::Style::Default);
m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
m_windowTitle, style);
}
void Window::Destroy() {
m_window.close();
}
void Window::Update() {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
m_isDone = true;
}
else if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F5)
{
ToggleFullscreen();
}
}
}
void Window::ToggleFullscreen() {
m_isFullscreen = !m_isFullscreen;
Destroy();
Create();
}
void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }

bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }
sf::Vector2u Window::GetWindowSize() { return m_windowSize; }

void Window::Draw(sf::Drawable& l_drawable){
m_window.draw(l_drawable);
}

编辑:更新了游戏。H

游戏.h:

class Game{
public:
Game();
~Game();
void HandleInput();
void Update();
void Render();
Window* GetWindow();
private:
...
Window m_window;
...
}

编辑:更新游戏.CPP

游戏.cpp:

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);
}
void Game::HandleInput() {
...
//Get the angle between ship and mouse.
//Error if there is an argument in getPosition()
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y, sf::Mouse::getPosition().x - m_ship.getPosition().x); //TO DO: getPosition(&Relative To)
m_angle *= 180 / m_PI;
...
}
Window* Game::GetWindow() { return &m_window; }

编辑:添加了 MAIN.CPP

Main.cpp

int main() {
Game game;
while (!game.GetWindow()->IsDone()) {
game.HandleInput();
game.Update();
game.Render();
}
}

最佳答案

首先让我给你一个我认为是重现你的问题的最小例子:

#include <SFML/Graphics.hpp>

class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}

private:
sf::RenderWindow m_window;
};

int main() {
MyWindow window;
sf::Mouse::getPosition(window);
}

这段代码无法编译(诚然,编译后不会做任何有趣的事情,但这不是重点)。我怀疑如果您尝试编译它,它会给您带来与您当前遇到的错误相同的错误。

请注意,这是我们在谈论 MCVE 时所期望的: 这段代码简短、简单,显示了错误,如果不是因为错误,也可以编译。

此外,它使错误变得非常清楚,如果您尝试自己想出一个 MCVE,您可能已经解决了您的问题而无需在此处发布问题,这肯定会节省您的时间。


对比你的代码:

m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y
,sf::Mouse::getPosition().x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)

这段代码是合法的,但你解释说它不正确,你想把它变成类似这样的东西:

m_angle = atan2(sf::Mouse::getPosition(m_window).y - m_ship.getPosition().y
,sf::Mouse::getPosition(m_window).x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)

...无法编译。

但是,在此范围内 m_window 是一个 Window 而不是 sf::RenderWindow!

问题是您正在传递对封装 sf::RenderWindow,但不能转换为 sf::Window& 本身。因此,您不能将其传递给 sf::Mouse::getPosition它不需要任何东西或 sf::Window&,但肯定不是 Window&MyWindow&


有很多方法可以解决这个问题。下面介绍了其中两个:

#include <SFML/Graphics.hpp>

class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}

// you could add an accessor
const sf::Window& getSfmlWindow() const { return m_window; }

// you may also expose a method to get the mouse position
// relatively to this window
const sf::Vector2i getMousePosition() const {
return sf::Mouse::getPosition(m_window);
}

private:
sf::RenderWindow m_window;
};

int main() {
MyWindow window;
sf::Vector2i mouse_position;
// this won't work! window isn't convertible to sf::Window&
// mouse_position = sf::Mouse::getPosition(window);

// using the accessor
mouse_position = sf::Mouse::getPosition(window.getSfmlWindow());

// or the exposed method
mouse_position = window.getMousePosition();
}

关于c++ - SFML sf::Mouse::getPosition 方法无法写入正确的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com