【问题标题】:How can I declare a SFML window to all my class functions?如何为我的所有类函数声明一个 SFML 窗口?
【发布时间】:2019-07-30 21:55:13
【问题描述】:

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

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,所有类都可以访问它,但我不建议这样做。如果你有很多不同的地方试图在同一个窗口中绘制,那么追踪错误将是一个巨大的痛苦。您应该考虑使用一个单独的类来负责绘图(并且其他类可以调用)
  • 将窗口传递给您的班级。

标签: c++ class sfml variable-declaration


【解决方案1】:

尝试使窗口成为类成员:

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

【讨论】:

    【解决方案2】:

    对于窗口,您可以做的一件简单的事情是创建一个类并 在其中添加窗口,然后包含类所在的 .h ,因为您在公共部分中设置了 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() 可以是你想要的任何函数 尽快调用,以便显示窗口。 我希望这可以帮助你解决你的问题=) [EDID]: 由于“sf::RenderWindow 窗口”是公共的,因此每个具有公共“sf::RenderWindow 窗口”标题的文件都可以使用该窗口,因为它不是私有的。我想你知道,但无论如何要添加它。

    【讨论】:

    • 好的,谢谢!这为我清除了一些东西,但是当我回到电脑时必须对其进行测试。谢谢!
    • 我希望这是你需要的 =)
    猜你喜欢
    • 2019-04-20
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多