Skip to content
kweicht edited this page Jun 7, 2012 · 7 revisions

Architecture

Main Loop Possibilities

Engine Main Loop

The engine could control everything, from initialization of the application to the actual running of the main loop. The user could then just give the engine an Init, Update and Shutdown functions where they would place all their game code. These 3 functions could be written in C/C++ and/or Lua. Each of the engines subsystems would still work independently, just instead of the Game being the "glue" between them, the engine core would be.

Pros

  • Makes writing the game code much simpler and more straightforward
  • Logically makes a lot of sense for the engine to take care of the system-level stuff
  • The game code can truly be "write once, compile/run anywhere"
  • The engine would have much more control over how its run

Cons

  • Engine development will take more time. The engine will have to be updated to support any new platform it needs to support
  • Engine imposes requirements on the user about how it can be used

Example Game

static lae_world* world = NULL;
static lae_entity* player_entity = NULL;

int init(void)
{
    world = lae_engine_create_world();
    player_entity= world->spawn_entity(0,0,0);

    lae_render_params render_params("my_mesh.mesh", "my_texture.tex", "my_material.mat");
    player_entity->add_component(lae_render_component(render_params));
    player_entity->add_component(lae_physics_component());
    player_entity->add_component(lae_script("my_script.lua"));
}
int update(float dt)
{
    if(lae_engine_key_down(W))
        player_entity->translate(1.0f, 0.0f, 0.0f);
    if(lae_engine_key_down(ESCAPE))
        return 1;

    return 0;
}
void shutdown(void)
{
}

int main()
{
    lae_engine_init("user_settings.json", &init, &update, &shutdown);
    return lae_engine_run();
}

Game Main Loop

In this case, the game/application would control all the application level stuff. The game would initialize it's own application and run the main loop. The engine is then essentially the callback. The game would just call engine_frame() once per loop iteration and the engine would run what it should.

Pros

  • Engine doesn't impose anything on the user. The user can use the engine however they desire
  • Cross-platform code is written on a case-by-case basis
  • Engine development becomes slightly simpler

Cons

  • Slightly less logical. How does keyboard input get passed to the engine?
  • A lot more work is placed on the users shoulders
  • Potentially harder to debug; it could be difficult to determine if an error is an engine bug or a game bug

Example

int main()
{
    void* window = SpawnOSWindow(1024,768);

    lae_timer timer;
    lae_graphics* graphics = lae_graphics_create(window);
    lae_asset_manager* assets = lae_asset_manager_create();
    lae_scripting* scripting = lae_scripting_create();
    lae_input* input = lae_input_create();
    lae_world* world = lae_world_create(graphics, assets, scripting, input);

    lae_entity* player_entity = world->spawn_entity(0,0,0);

    lae_render_params render_params("my_mesh.mesh", "my_texture.tex", "my_material.mat");
    player_entity->add_component(lae_render_component(render_params));
    player_entity->add_component(lae_physics_component());
    player_entity->add_component(lae_script("my_script.lua"));

    lae_timer_init(&timer);
    while(running)
    {
        while(HandleOSMessage())
        {
            if(KeydownMessage)
                input->keydown(THE_KEY);
        }

        if(input->key_is_down(W))
            player_entity->translate(1.0f, 0.0f, 0.0f);
        if(input->key_is_down(ESCAPE))
            break;

        world->update(timer.get_delta_time());
        graphics->clear();
        graphics->present();
    }
    return lae_engine_run();
}