gpt4 book ai didi

c++ - SFML 自定义类未关闭

转载 作者:行者123 更新时间:2023-11-28 04:42:40 24 4
gpt4 key购买 nike

我有一个名为“游戏”的自定义类

#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 的新手,所以我想知道我是否以正确的方式这样做,我的类(class)应该是怎样的。除此之外,其他一切似乎都有效。为什么不关闭?谢谢。

最佳答案

您的标志 game.isOpen 保持为真,因此 while 循环继续执行,但您的 RenderWindow 已关闭。

像这样更新 Game::close 方法:

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

关于c++ - SFML 自定义类未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com