如何向所有类函数声明SFML窗口?

问题描述 投票:2回答:2

这可能是一个noob问题,但我试图让一个简单的玩家在SFML中的2D网格中移动。我正在创建一个while循环来渲染正在发生的事情,我可以让它工作,但我想为网格和玩家等使用类。问题是当我创建一个名为'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
            }
        }
    }
};
c++ class sfml variable-declaration
2个回答
0
投票

对于窗口来说,一件容易的事就是创建一个类并在其中添加窗口,然后包含.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窗口”标题的文件都可以使用该窗口,因为它不是私有的。我想你知道但无论如何要添加它。


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)
            }
        }
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.