SFML 自定义 Class 未关闭
SFML Custom Class Not Closing
我有一个名为 "Game"
的自定义 class
#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() {
}
还有一个执行程序的 main class
#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();
}
}
window 显示在屏幕上,但是,当我尝试关闭 window 时,它会冻结并且不会关闭。我是 SFML 的新手,所以我想知道我这样做的方式是否正确,我的 class 应该是这样的。除此之外,其他一切似乎都有效。为什么不关闭?谢谢
你的标志 game.isOpen
保持为真,所以 while 循环继续执行,但是你的 RenderWindow
关闭了。
像这样更新 Game::close
方法:
void Game::close() {
window->close();
isOpen = false;
}
我有一个名为 "Game"
的自定义 class#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() {
}
还有一个执行程序的 main class
#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();
}
}
window 显示在屏幕上,但是,当我尝试关闭 window 时,它会冻结并且不会关闭。我是 SFML 的新手,所以我想知道我这样做的方式是否正确,我的 class 应该是这样的。除此之外,其他一切似乎都有效。为什么不关闭?谢谢
你的标志 game.isOpen
保持为真,所以 while 循环继续执行,但是你的 RenderWindow
关闭了。
像这样更新 Game::close
方法:
void Game::close() {
window->close();
isOpen = false;
}