简体   繁体   English

当窗口在类中时,为什么SFML窗口不显示?

[英]Why does SFML window not show when window is in a class?

I am trying to create a Screen class for SFML, however for some reason, the application works when using the Xcode example but as soon as I put the window into it's own class, it does not work. 我正在尝试为SFML创建一个Screen类,但是由于某些原因,该应用程序在使用Xcode示例时可以工作,但是一旦我将窗口放入它自己的类中,它就无法工作。 Why is this and how would I fix it? 为什么会这样,我将如何解决?

Here is my code (adapted form the example): 这是我的代码(从示例中改编而成):

Edit: 编辑:

After reading the comments, I have changed to the following code. 阅读注释后,我已更改为以下代码。 This still does not show a screen and the program still quits. 这仍然不显示屏幕,并且程序仍然退出。

#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;
}

You have a logic error in your constructor. 您的构造函数中存在逻辑错误。 The way you created it, you leave Screen::window uninitialized, and create another RenderWindow object which becomes inaccessible as soon as execution leaves constructor. 创建方式,将Screen::window保留为未初始化状态,并创建另一个RenderWindow对象,该对象在执行离开构造函数后立即变得不可访问。

Instead, you should do 相反,你应该做

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

Everything else should work as expected, unless it does not have any other bugs. 除非没有其他错误,否则其他所有内容均应按预期工作。 If you're curious about the syntax I've used, you can visit this link . 如果您对我使用的语法感到好奇,可以访问此链接

Hard to say. 很难说。 Maybe your accessing a memory address that isn't part of your program? 也许您访问的内存地址不属于您的程序? Try using unique pointers and make_unique to define some of your pointers. 尝试使用唯一指针和make_unique定义一些指针。

For example: 例如:

std::unique_ptr<sf::RenderWindow> window;

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

I did make a project using sfml before, so feel free to check out my code here --> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier 我之前确实使用sfml创建了一个项目,所以请随时在这里查看我的代码-> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM