gpt4 book ai didi

c++ - 在 sfml 中管理多个 RenderWindow

转载 作者:行者123 更新时间:2023-11-28 06:32:03 25 4
gpt4 key购买 nike

在 SFML C++ 中管理多个窗口时,我似乎遇到了一些麻烦。当我尝试管理多个窗口时,它们都可以正确打开并且我可以在较大的窗口中进行交互,但是较小的窗口在创建时与较大的窗口重叠,在我将大窗口移开之前我无法与之交互。下面的图片有助于视觉效果。下面还有我的代码的主循环。

To help the visual, "SFML - F1 Menu" is the window not responding to interaction.

代码的主循环如下:

while (MainWin.isOpen() || F1Menu.isOpen())
{
sf::Event Event;
if (MainWin.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
break;

case sf::Event::Resized:
MainView.reset(sf::FloatRect(0.f, 0.f, (MainWin.getSize().x*0.9f), (MainWin.getSize().y*0.9)));
MainWin.setView(MainView);
break;

case sf::Event::KeyPressed:
if (Event.key.code == sf::Keyboard::F1)
F1Menu.create(sf::VideoMode(200, 500), "SFML 2D - F1 Menu");
else if (Event.key.code == sf::Keyboard::Escape)
{
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
}
break;
}
}

if (F1Menu.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
F1Menu.close();
break;

case sf::Event::MouseButtonReleased:
if (Event.mouseButton.button == sf::Mouse::Left)
if (LMButton.mouseIn(F1Menu))
LoadMap("MapA.dat");
break;

case sf::Event::MouseMoved:
if (LMButton.mouseIn(F1Menu))
LMButton.setColor(sf::Color::Yellow);
else
LMButton.setColor(sf::Color::White);
break;
}
}

moveClock.restart();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
player.move(0, -4 * time);
player.setDirection(sfm::Direction::Up);
}

else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
player.move(0, 4 * time);
player.setDirection(sfm::Direction::Down);
}

else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
player.move(-4 * time, 0);
player.setDirection(sfm::Direction::Left);
}

else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
player.move(4 * time, 0);
player.setDirection(sfm::Direction::Right);
}

if (F1Menu.isOpen())
{
F1Menu.clear();
F1Menu.draw(LMButton);
F1Menu.display();
}

if (MainWin.isOpen())
{
MainWin.clear();
if (SplashScreen.didAnimate)
SplashScreen.Animate(MainWin, sfg::Animation::FadeIn);
if (inMenu)
{

}
if (isPlaying)
{
DrawMap(MainWin);
MainWin.draw(player);
}
MainWin.display();
}
}

最佳答案

据我所知,您无法使用一个 sf::Event 对象轮询两个单独的窗口。这是因为在轮询事件时,您实际上是从事件堆栈中弹出 并处理每个事件。 pollEvent的签名是

bool pollEvent(sf::Event &event);

请注意,这里没有 const 限定符,因为每个处理过的事件都会从事件堆栈中弹出。当您完成对主窗口的轮询时,其他窗口就没有事件了。这可能是您的窗口无法聚焦的原因。你的第二个窗口应该使用它自己单独的 sf::Event

附带说明一下,我强烈建议将您的数据封装在类中。你可以找到一个很好的引用 here关于如何着手这样做。这是很好的编码实践,有助于在查找错误时最大程度地减少混淆

关于c++ - 在 sfml 中管理多个 RenderWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350714/

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