如何为所有 class 函数声明 SFML window?

How can I declare a SFML window to all my class functions?

这可能是一个菜鸟问题,但我正试图让一个简单的玩家在 SFML 的二维网格中移动。我正在创建一个 while 循环来渲染正在发生的事情,我可以让它工作,但我想对网格和玩家等使用 classes。问题是当我创建一个 window 称为 'window',我不知道如何实现 classes,因为它们不知道 'window' 是什么。我希望我已经充分描述了我的问题,如果我的方法已经很糟糕并且应该更改为另一种方法,我希望对如何进行这项工作有任何回应。这是 class 和未声明的 window 错误的代码片段。

class myEvents {
public:
    //Variables
    int tSize = 40;
    int tileCount = 20;
    int width = tileCount * tSize;
    int height = tileCount * tSize;

    //Function to create a grid with RectangleShapes
    void grid() {
        for (int i = 0; i < tileCount; i++) 
        {
            for (int j = 0; j < tileCount; j++) 
            {
                sf::RectangleShape tile(sf::Vector2f(40, 40));
                tile.setFillColor(sf::Color::Magenta);
                tile.setPosition(i*tSize, j*tSize);
                window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
                                    //window.draw(tile); must execute in the loop to render a full grid
            }
        }
    }

    //Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
    //So I need the window to work with my classes.
    void loop() {
        sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
                                                                        //somewhere so that my funcions know where it comes from?
        while (window.isOpen) {

            sf::Event event;
            while (window.pollEvent(e))
            {
                //to be further developed
            }
        }
    }
};

尝试让 window 成为 class 成员:

class myEvents {
public:
    //Variables
    int tSize = 40;
    int tileCount = 20;
    int width = tileCount * tSize;
    int height = tileCount * tSize;
    sf::RenderWindow window{sf::VideoMode(width, height), "Game"};

    void grid() {
        for (int i = 0; i < tileCount; i++) 
        {
            for (int j = 0; j < tileCount; j++) 
            {
                sf::RectangleShape tile(sf::Vector2f(40, 40));
                tile.setFillColor(sf::Color::Magenta);
                tile.setPosition(i*tSize, j*tSize);
                window.draw(tile); 
            }
        }
    }

    void loop() {
        while (window.isOpen) {

            sf::Event event;
            while (window.pollEvent(e))
            {
                //to be further developed
            }
        }
    }
};

或者,将 window 传递给:

class myEvents {
public:
    //Variables
    int tSize = 40;
    int tileCount = 20;
    int width = tileCount * tSize;
    int height = tileCount * tSize;

    void grid(sf::RenderWindow& window) {
        for (int i = 0; i < tileCount; i++) 
        {
            for (int j = 0; j < tileCount; j++) 
            {
                sf::RectangleShape tile(sf::Vector2f(40, 40));
                tile.setFillColor(sf::Color::Magenta);
                tile.setPosition(i*tSize, j*tSize);
                window.draw(tile); 
            }
        }
    }

    void loop() {
        sf::RenderWindow window{sf::VideoMode(width, height), "Game"};

        while (window.isOpen) {

            sf::Event event;
            while (window.pollEvent(e))
            {
                //to be further developed
                // call grid(window)
            }
        }
    }
};

好吧,对于 window 来说,您可以做的一件简单的事情就是创建一个 class 并且 在其中添加 window,然后包含 .h,其中 class 与您在 public 部分中设置的 sf::window 相同。现在我将向您展示如何使用它

我们有一个名为 window.h 的 .h 文件(您可以随意命名)

#include <SFML/Grpahics.hpp>
class window
{
public:
  sf::RenderWindow window;
}

并且您有 main.cpp(或您想要的任何文件)

#include "window.h"
window w;

int main()
{
  w.window.create(sf::VideoMode(800,600),"Title");
}

int main() 可以是你想要的任何函数,它必须是 尽快打电话,这样 window 就会显示。 我希望这可以帮助您解决问题 =) [EDID]: 由于 "sf::RenderWindow window" 是 public,每个文件头带有 public "sf::RenderWindow window" 的文件都可以使用 window,因为它不是私有的。我想你知道,但无论如何要添加它。