将 Box2D 与 SDL 中现有的游戏类结构集成?

发布于 2024-11-07 15:34:45 字数 1512 浏览 3 评论 0原文

抱歉,标题相当模糊,但这似乎适合一个类似模糊的问题,尽管希望足够简洁,以便有人能够回答它。我花了相当多的时间在 SDL 中开发 2D 横向卷轴游戏的框架,现在我对这个库很满意并且有了一个工作原型,我希望将 Box2D 物理库集成到我的游戏中。现在,我不只是一头扎进去,实际上还花时间在我的系统上编译库(显然),研究 Testbed 应用程序,创建一些我自己的应用程序来掌握这一切。然而,我仍然无法弄清楚如何将其准确地集成到我现有的游戏结构中。如果我可以这么大胆的话,下面是我的游戏“引擎”源代码示例:

CMain.cpp

// Constructor. Initialize the screen display surface and 
// let the application know that we are running.
CMain::CMain(){
    displaySurface = NULL;
    isRunning = true;
}

// Init function, hooks SDL libraries, configures the display
// surface, window and all pre-runtime environment parts.
int CMain::OnExecute(){


    if(OnInit() == false){
        return -1;
    }

    // Event checking, game loop and render loop. 
    SDL_Event Event;
    while(isRunning){
        while(SDL_PollEvent(&Event)){
            OnEvent(&Event);
        }
        OnLoop();
        OnRender();
    }

    // Cleanup, called when the application is no longer running,
    // handles the removal of all loaded resources from memory.
    OnCleanup();
    return 0;
}

int main(int argc, char* argv[]){
    CMain digIt;
    return digIt.OnExecute();
}

如您所见,它负责关键的游戏循环功能。我认为各种辅助函数的完整代码对于问题的上下文来说不是必需的,但如果有帮助,请直接说出来,我很乐意发布它。所以,我的问题是这样的:

熟悉 Box2D 世界的结构以及它是如何初始化、步进以及一切的,我现在如何将其集成到现有的游戏结构中?我是否希望我的整个 CMain 类继承自 b2World? (即类 CMain : b2World{})是否有其他方法来创建世界,以便可以在全球范围内访问它,而不会犯下良好编码实践的主要错误?例如,我尝试在 CMain 标头中声明一个 b2World* 指针,然后将其传递给后续函数以执行它们必须执行的操作,但这会导致大量空指针异常。您可以提供的任何建议或示例代码(如果可能的话)将非常感激!

Sorry for the rather vague title, but it seemed befitting of a similarly vague question, though hopefully succinct enough that somebody will be able to answer it. I have spent a decent amount of time working on the framework for a 2D side-scrolling game in SDL, and now that I am comfortable with the library and have a working prototype, I'm looking to integrate the Box2D physics library into my game. Now, I didn't just dive in head first and actually took the time to compile the library on my system (obviously), study the Testbed application, create a few of my own to get the hang of it all. And yet, I'm still unable to figure out how exactly to integrate it into my existing game structure. Below is a sample of my source for the game "engine," if I may be so bold:

CMain.cpp

// Constructor. Initialize the screen display surface and 
// let the application know that we are running.
CMain::CMain(){
    displaySurface = NULL;
    isRunning = true;
}

// Init function, hooks SDL libraries, configures the display
// surface, window and all pre-runtime environment parts.
int CMain::OnExecute(){


    if(OnInit() == false){
        return -1;
    }

    // Event checking, game loop and render loop. 
    SDL_Event Event;
    while(isRunning){
        while(SDL_PollEvent(&Event)){
            OnEvent(&Event);
        }
        OnLoop();
        OnRender();
    }

    // Cleanup, called when the application is no longer running,
    // handles the removal of all loaded resources from memory.
    OnCleanup();
    return 0;
}

int main(int argc, char* argv[]){
    CMain digIt;
    return digIt.OnExecute();
}

As you can see this takes care of the crucial game loop functions. I didn't think the full code of the various helper functions was necessary for the context of the question, but if it would be helpful just say so and I'd be glad to post it. So, my question is this:

Being familiar with the structure of the Box2D world and how it is initialized, stepped and everything, how do I now integrate that into this existing game structure? Do I want my entire CMain class to inherit from b2World? (i.e. class CMain : b2World{}) is there some other way to create the world so that it is globally accessible without committing a cardinal sin of good coding practice? For instance, I tried to declare a b2World* pointer in the CMain header and then pass that around to the subsequent functions to do with it what they had to do but that lead to a great many null pointer exceptions. Any advice you could give, or sample code if at all possible, will be incredibly appreciated!

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

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

发布评论

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

评论(1

很酷不放纵 2024-11-14 15:34:45

我不建议您的 CMain 类派生自 b2world,因为它绝对不是 is-a 关系,而是一个 有一个。我无法判断这是否是完美的解决方案,因为我不知道您的整体架构,但它似乎是 单例模式。

我建议您创建一个实现单例模式的类,该类包含 b2World 的中心实例,例如 CPhysicsService。以迈向世界和创建新机构为例。由于单例,可以在任何需要的地方检索该类的实例。

我在为 Gamebryo 引擎进行扩展时也遇到了这个问题。但无论如何,该引擎很大程度上是建立在服务之上的,因此创建 Box2D 服务是显而易见的选择。

我希望这有点用处。

I would not suggest that your CMain class derives from the b2world, since it definitely is not a is-a relation but rather a has-a. I cannot tell if it is the perfect solution, since I do not know your overall architecture, but it seems like a good place fot the Singleton pattern.

I suggest that you create a class implementing the singleton pattern which holds the central instance of b2World, like CPhysicsService. The instance is taked with stepping the world and for creation of new bodies. Because of the singleton, the instance of the class can be retrieved anywhere you need it.

I also had this issue while making an extension for the Gamebryo engine. But the engine was heavily built on services anyway, so the creation of a Box2D service was the obvious way to go.

I hope this was somehow useful.

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