开发者

Integrating Box2D with an existing game class structure in SDL?

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 ch开发者_如何学Cecking, 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜