views:

218

answers:

2

I'm making a 2D engine in C++, and I want to be able to supply a .dll and a .lib so that games can just include those and everything is fine and dandy.

I've looked at how Ogre does it, and it results in ugliness like this:

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
    // Create application object
    ShadowsApplication app;

    try {
        app.go();
    } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        std::cerr << "An exception has occured: " <<
        e.getFullDescription().c_str() << std::endl;
#endif
    }

    return 0;
}

#ifdef __cplusplus
}
#endif

While my engine currently does this:

MAIN
{
    Game* game = Game::Create();

    Engine::Init();
    game->Init();

    while (Engine::running)
    {
        if (Engine::PreRender())
        {
            game->Update();

            Engine::Render();
        }

        Engine::ShutDown();

        return 0;
    }
}

Which means a clean game looks like this:

class BouncingBalls : public Game
{

public:

    BouncingBalls() { }
    void Init();
    void Update();

};

Game* Game::Create() { return (new BouncingBalls()); }

Now, one major drawback: because I'm defining main in the engine instead of the game, I can't put all the necessary libraries in the engine. Every game has to load in a specific set of libraries, which is going to be hell when that list changes.

Is there a way to keep the syntax like this while also loading in the .lib's in the engine instead of the game?

Thanks in advance.

EDIT: It seems things were not clear. My eventual goal is to have a single Visual Studio project that contains all of the engine functions, which compiles to a .lib or a .dll (not a .exe). If someone wants to make a game, he can simply include Engine.lib in his project and Engine.dll in his project folder and get going.

It should be as easy as possible to start up a new project, without having to worry about low-level C++.

+4  A: 

Why not just rename your MAIN to something like Engine::MainLoop and have the client app call that from their main.

jon hanson
Ha! The best solutions are the simplest.
knight666
yes this exact same issue has been a classic flaw with the GLUT library for years. Libraries that implement main are bad!
sean riley
A: 

You don't have to use OGRE like that - the example is from their ExampleFramework and is kind of ugly.

Basically it's just a matter of doing something along these lines:

#include ogre.h

int main()
{
    SetupGame();
    SetupOGRE();

    while(true)
    {
        UpdateGame();
        GetOgreSingleton().RenderOneFrame();
    }

}

Of course, it isn't hard to abstract Ogre to a level that it works just like any other "game system", meaning you can put it inside the UpdateGame() function, making your main function even nicer.

Srekel