簡體   English   中英

如何為我的所有類函數聲明一個 SFML 窗口?

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

這可能是一個菜鳥問題,但我試圖讓一個簡單的玩家在 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
            }
        }
    }
};

嘗試使窗口成為類成員:

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

對於窗口,您可以做的一件簡單的事情是創建一個類並在其中添加窗口,然后在類所在的位置包含 .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 窗口”標題的文件都可以使用該窗口,因為它不是私有的. 我想你知道,但無論如何要添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM