Window 无法在 SFML 中打开
Window not opening in SFML
基本上,我正在用 c++ 和 sfml 制作一个 pong 克隆,并且我正在使用 类,对此我知之甚少。问题是,我首先尝试打开 window 并将其清除为黑色。文件编译没有错误, 运行 没有错误,但是 window 没有出现。
我相信它与构造函数有关,但我也不确定。我查看了所有其他问题,看看是否有回答我的问题的,其中 none 给了我答案。忽略其他头文件,它们目前没有做任何事情。
game.hpp
class Game
{
public:
Game();
void run();
public:
sf::RenderWindow window;
private:
void processEvents();
void update();
void draw();
};
pong.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "game.hpp"
#include "players.hpp"
#include "ball.hpp"
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
void Game::draw() {
window.clear(sf::Color::Black);
window.display();
}
void Game::run() {
while (window.isOpen()) {
processEvents();
draw();
}
}
int main(int argc, char const *argv[]) {
Game game;
game.run();
return 0;
}
window本来是开黑的,但是当程序是运行的时候运行没问题,但是window不弹出向上。我已经看了几个小时了,在 discord 服务器上问过一些人,但找不到答案。
在您的 Game
构造函数中,您正在创建一个本地 window 对象,该对象会在构造函数结束时立即销毁。
而不是这个:
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
这样做:
Game::Game() : window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default)
{
window.setFramerateLimit(60);
}
为了使用非默认初始化来初始化 window
数据成员。
基本上,我正在用 c++ 和 sfml 制作一个 pong 克隆,并且我正在使用 类,对此我知之甚少。问题是,我首先尝试打开 window 并将其清除为黑色。文件编译没有错误, 运行 没有错误,但是 window 没有出现。
我相信它与构造函数有关,但我也不确定。我查看了所有其他问题,看看是否有回答我的问题的,其中 none 给了我答案。忽略其他头文件,它们目前没有做任何事情。
game.hpp
class Game
{
public:
Game();
void run();
public:
sf::RenderWindow window;
private:
void processEvents();
void update();
void draw();
};
pong.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "game.hpp"
#include "players.hpp"
#include "ball.hpp"
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
void Game::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
void Game::draw() {
window.clear(sf::Color::Black);
window.display();
}
void Game::run() {
while (window.isOpen()) {
processEvents();
draw();
}
}
int main(int argc, char const *argv[]) {
Game game;
game.run();
return 0;
}
window本来是开黑的,但是当程序是运行的时候运行没问题,但是window不弹出向上。我已经看了几个小时了,在 discord 服务器上问过一些人,但找不到答案。
在您的 Game
构造函数中,您正在创建一个本地 window 对象,该对象会在构造函数结束时立即销毁。
而不是这个:
Game::Game() {
sf::RenderWindow window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default);
window.setFramerateLimit(60);
}
这样做:
Game::Game() : window(sf::VideoMode(640, 480), "Game Window", sf::Style::Default)
{
window.setFramerateLimit(60);
}
为了使用非默认初始化来初始化 window
数据成员。