c++ - 如何为所有类函数声明一个 SFML 窗口?

标签 c++ class sfml variable-declaration

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

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

最佳答案

尝试让窗口成为​​类成员:

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

或者,传递窗口:

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

关于c++ - 如何为所有类函数声明一个 SFML 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070452/

相关文章:

c++ - 令人费解的编译器优化结果

c++ - 模板模板参数偏特化c++

假设 C++ 显式类型缺失 ('int')

python - 如何访问不同类的对象?

c++ - 函数被多次调用

c++ - 来自 VertexArray 的 SFML 绘图基元

c++ - 处理鼠标事件 SFML

C++ 分割和比较二维动态数组

c++ - Unresolved inclusion 在任何包含文件中

包含数据库查询的 PHP 构造函数来构建对象,好/坏?