如何管理 SFML 渲染、“信号”方式?

发布于 2024-10-21 23:54:38 字数 437 浏览 2 评论 0原文

我正在开发一个用 C++ 编写并使用 SFML 2D 库的小蛇游戏。问题是:为了渲染一个窗口并打印其中的任何内容,您必须通过一个

while (App->IsOpened())
{
    //Do the stuff
    App->Clear();
    App->Display();
}

但是,我想以更通用的方式构建我的程序,这将使我能够只初始化window,然后从 while 语句外部向其发送信号,例如其中的“RenderARect”,或“ClearTheWindow”。例如,它可以让我使用我的渲染类实例作为动态库,使游戏代码和渲染代码成为两个不同且独立的东西...

您对如何在我的 SFML 中实现这样的信号系统有什么建议吗程序?

PS:我听说过libsigc++,但不知道如何实现它......

谢谢!

I'm developing a little snake game written in C++ and using SFML 2D library. The problem is: in order to render a window, and to print anything in it, you have to make it through a

while (App->IsOpened())
{
    //Do the stuff
    App->Clear();
    App->Display();
}

But, I'd like to build my program in a more generic ways, which would make me able to just Init the window, and then send signals to it, such as "RenderARect" in it, or "ClearTheWindow", from outside the while statement. It would let me use my rendering class instance as a dynamic library for exemple, making the game code, and the rendering code two different and independant things...

would you have you any advices on how to implement such a signal system to my SFML program?

PS: I've eared of the libsigc++, but have no idea on how to implement it...

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2024-10-28 23:54:38

没有必要发出这样的信号。一切都应该在 while 循环(主循环)中处理,并且每个帧都应该被清除,并单独和整体绘制,而不是在区域中绘制,因此 SFML 使用 OpenGL。

将游戏逻辑与主循环分离的最明显的方法是使游戏面向对象。
当然,它只会将其分离为单独的逻辑单元,在单独的文件中,但它们将在同一个循环中“一起”运行。我认为这是期望的(但至少可以接受的)行为。

所以你将有一个 Game 类,它有一个 Update() 方法。这是游戏逻辑发生、处理事件的地方(最好在调用之前查询事件,并将其作为参数传递给 Update()),并且更新必须显示的所有内容的状态。
您应该在主循环的每次迭代中调用它。

该类还将有一个 Render() 方法,它将根据需要绘制所有内容。

所以它看起来像这样:

while (App->IsOpened())
{
    Game->Update();
    App->Clear();
    Game->Render();
    App->Display();
}

PS:抱歉我的英语不好,我希望你能理解。

There's no need to send such signals. Everything should be handled in that while loop (the main loop), and every frame should be cleared, and drawn individually and as a whole, not in regions, hence SFML uses OpenGL.

The most obvious way to separate the game logic from the main loop is making the game OOP.
Of course, it will separate it only to separate logic units, in separate files, but they will run "together", in the same loop. I think this is the desired (but at least acceptable) behaviour.

So you'll have a Game class, which will have an Update() method. This is where the game logic happens, events are handled (preferably events are queryed before the call, and are passed as a parameter to Update()), and the state of everything which has to be displayed is updated.
You should call this in every iteration of the main loop.

And that class will also have a Render() method, which will draw everything as it is needed.

So it would look like this:

while (App->IsOpened())
{
    Game->Update();
    App->Clear();
    Game->Render();
    App->Display();
}

P.S.: Sorry for my bad English, I hope you can understand it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文