【问题标题】:Why does SFML window not show when window is in a class?为什么当窗口在类中时不显示 SFML 窗口?
【发布时间】:2019-06-14 06:52:06
【问题描述】:

我正在尝试为 SFML 创建一个 Screen 类,但是由于某种原因,该应用程序在使用 Xcode 示例时可以正常工作,但是一旦我将窗口放入它自己的类中,它就无法正常工作。为什么会这样,我该如何解决?

这是我的代码(改编自示例):

编辑:

看完cmets,我改成下面的代码了。这仍然不显示屏幕,程序仍然退出。

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

class Screen{
public:
  sf::RenderWindow window;
  Screen(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
  }
};


int main(int, char const**)
{


  Screen* screen = new Screen();
  // Set the Icon
  sf::Image icon;
  if (!icon.loadFromFile(resourcePath() + "icon.png")) {
    return EXIT_FAILURE;
  }
  screen->window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());

  // Load a sprite to display
  sf::Texture texture;
  if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
    return EXIT_FAILURE;
  }
  sf::Sprite sprite(texture);

  // Create a graphical text to display
  sf::Font font;
  if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
    return EXIT_FAILURE;
  }
  sf::Text text("Hello SFML", font, 50);
  text.setFillColor(sf::Color::Black);

  // Load a music to play
  sf::Music music;
  if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
    return EXIT_FAILURE;
  }

  // Play the music
  music.play();

  // Start the game loop
  while (screen->window.isOpen())
  {
    // Process events
    sf::Event event;
    while (screen->window.pollEvent(event))
    {
      // Close window: exit
      if (event.type == sf::Event::Closed) {
        screen->window.close();
      }

      // Escape pressed: exit
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
        screen->window.close();
      }
    }

    // Clear screen
    screen->window.clear();

    // Draw the sprite
    screen->window.draw(sprite);

    // Draw the string
    screen->window.draw(text);

    // Update the window
    screen->window.display();
  }

  return EXIT_SUCCESS;
}

【问题讨论】:

  • sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"); _window = &amp;window; 是你的错误。构造函数完成后,window 不再存在。
  • 在您的构造函数中,您将类指针分配给 local 变量。构造函数完成后,这个对象就消失了。
  • @drescherjm 那我该如何解决这个问题?
  • Screen() : window(sf::VideoMode(800, 600), "SFML window") {}

标签: c++ class window sfml


【解决方案1】:

您的构造函数中存在逻辑错误。按照您创建它的方式,您使 Screen::window 未初始化,并创建另一个 RenderWindow 对象,一旦执行离开构造函数,该对象就无法访问。

相反,你应该这样做

class Screen{
public:
  sf::RenderWindow window;
  Screen():
  window(sf::VideoMode(800, 600), "SFML window") {}
};

其他一切都应该按预期工作,除非它没有任何其他错误。如果您对我使用的语法感到好奇,可以访问this link

【讨论】:

    【解决方案2】:

    很难说。也许您访问的内存地址不属于您的程序?尝试使用唯一指针和 make_unique 来定义一些指针。

    例如:

    std::unique_ptr<sf::RenderWindow> window;
    
    window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT), "PushBlox",sf::Style::Default);
    

    我之前确实使用 sfml 做过一个项目,所以请随时在这里查看我的代码 --> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      相关资源
      最近更新 更多