簡體   English   中英

SFML自定義類未關閉

[英]SFML Custom Class Not Closing

我有一個名為“游戲”的自定義類

#include "Game.h"
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
using namespace sf;

Game::Game(float length, float height, std::string title) {
    this->length = length;
    this->height = height;
    this->window = new RenderWindow(VideoMode(this->length, this->height), title);
    this->isOpen = true;
    display();
}

bool Game::pollEvent() {
    return window->pollEvent(e);
}

void Game::close() {
    window->close();
}

void Game::display() {
    window->display();
}

void Game::clear() {
    window->clear(Color::White);
}

void Game::paint(Drawable component) {
    window->draw(component);
}

void Game::sleep(long millis) {
    sf::sleep(milliseconds(millis));
}


Game::~Game() {
}

還有一個執行程序的主類

#include <cstdlib>
#include "Game.h"

using namespace sf;

int main(int argc, char** argv) {
    Game game(1000, 1000, "My Class Works!");
    while (game.isOpen) {
        while (game.pollEvent()) {
            if (game.e.type == Event::Closed) {
                game.close();
            }
        }
        game.clear();
        game.sleep(1000/60);
        game.display();
    }
}

該窗口顯示在屏幕上,但是,當我嘗試關閉該窗口時,它凍結並且沒有關閉。 我是SFML的新手,所以我想知道我是否以正確的方式進行操作,而我的課堂是應該的。 除此之外,其他一切似乎都起作用。 為什么不關門? 謝謝。

您的標志game.isOpen保持為true,因此while循環繼續執行,但您的RenderWindow已關閉。

像這樣更新Game::close方法:

void Game::close() {
    window->close();
    isOpen = false;
}

暫無
暫無

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

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