c++ - std::unique_ptr 干扰某些 sf::RenderWindow 函数?

标签 c++ pointers sfml smart-pointers unique-ptr

sf::RenderWindow::getPosition() 当我从创建窗口的相同范围调用它时,总是返回 0, 0 而不是正确的位置.

这是重现问题的示例代码:

#include <iostream>
#include <memory>
#include <SFML/Graphics.hpp>

int main() {
  std::unique_ptr<sf::RenderWindow> window_;

  window_ = std::unique_ptr<sf::RenderWindow>(
    new sf::RenderWindow(sf::VideoMode(800, 600), "asd", sf::Style::Default)
  );
  window_->setFramerateLimit(30);
  window_->setVerticalSyncEnabled(false);

  while (window_->isOpen()) {
    sf::Event evt;
    while (window_->pollEvent(evt)) {
      if (evt.type == sf::Event::Closed) { window_->close(); }
    }
    window_->clear();
    window_->display();
  }

  const sf::Vector2i wpos = window_->getPosition();
  std::cout << "window position: " << wpos.x << "/" << wpos.y <<std::endl;

  return 0;
}

事情是:例如,我有一个 MainMenu 类,我将 sf::RenderWindow * 传递给它(使用 window_.get())。

当我从 MainMenu 调用 window_->getPosition() 时,它会报告正确的位置,因此 getPosition() 本身没有错误。只有当我在同一范围内(在 main 中)调用它时才会出错。

顺便说一下,我正在使用 SFML 2.3.1

最佳答案

您在 while (window_->isOpen()) 循环后打印窗口位置。这意味着窗口已关闭,即当您调用 getPosition() 时它不存在。因此,窗口位置未“正确”报告也就不足为奇了,因为窗口已经关闭(永久关闭,因此它没有位置)。

关于c++ - std::unique_ptr 干扰某些 sf::RenderWindow 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889868/

相关文章:

c++ - 将任意整数转换为 void*

c++ - 在一行上声明多个对象指针会导致编译器错误

c++ - SFML Project microsoft visual express 2010 到 2012 兼容性问题

c++ - SFML Sprite std::list

c++ - 在 C++ 中通过引用返回堆 block

c++ - boost::details::pool::pthread_mutex 和 boost::details::pool::null_mutex

c++ - Qt 4.8.2 与 GCC 4.7.0.1 不断崩溃

c - C中重叠对象的语义是什么?

c++ - 我们可以确定指向的数据是否是手动分配的吗?

c++ - 使用 boost::mpl 的类型列表的排列