SFML 只允许创建一个矩形(四四方方)形状的窗口,所有其他操作都在其中完成。我正在制作一个大富翁游戏,我基本上希望当用户单击可执行文件时大富翁 Logo 在屏幕上闪烁,并且它不必位于任何窗口内(只需具有透明背景的 Logo )。 Logo 之后,矩形窗口然后出现。有什么办法吗?
最佳答案
SFML 没有任何使窗口背景透明的集成功能。
为了实现这一点,您应该使用一些 OS specific functions .但这也行不通。如果您将窗口属性更改为具有透明窗口,SFML 将正确呈现,但您将看不到任何东西,因为所有内容,背景和前景,都是透明的。
那有什么解决办法呢?最简单的方法是没有任何背景,使您的 Logo 完全适合您的窗口。然后你只需要删除标题栏和边框,使用 sf::Style::None
。
我是这样实现的:
int main()
{
// First, I load the logo and create an sprite
sf::Texture logo;
if (!logo.loadFromFile("monopoly.png")){
exit(1);
}
sf::Sprite sp;
sp.setTexture(logo);
sp.scale(0.2, 0.2); // My logo is quite big in fact (1st google result btw)
int logoWidth = sp.getGlobalBounds().width;
int logoHeight = sp.getGlobalBounds().height;
// With the logo loaded, I know its size, so i create the window accordingly
sf::RenderWindow window(sf::VideoMode(logoWidth, logoHeight), "SFML", sf::Style::None); // <- Important!! Style=None removes title
// To close splash window by timeout, I just suppose you want something like this, don't you?
sf::Clock timer;
sf::Time time = timer.restart();
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
// event loop is always needed
}
// Window closed by time
time += timer.restart();
if (time >= sf::seconds(2.f)){
window.close();
}
window.clear();
window.draw(sp);
window.display();
}
// Then, reuse the window and do things again
window.create(sf::VideoMode(600, 400), "SFML");
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
}
window.clear();
window.draw(sp);
window.display();
}
return 0;
}
请注意,您只需使用 create
方法重新创建即可再次使用您的窗口。
结果:
然后是其他窗口:
关于c++ - SFML 飞溅 Logo 窗口透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213415/