How are components in larger game engines implemented?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by StackMasher, Jun 8, 2017.

  1. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    102
    49
    Nov 29, 2016
    A lot of games allow you to choose multiple renderers, input libraries etc but how do they do this? I'm thinking they would have to have some sort of a standardized interface for each component, but if-else statements would probably add a lot of overhead and all the components are statically linked with the main game
     
  2. catlover007

    catlover007 GBAtemp Regular

    Member
    143
    152
    Oct 23, 2015
    Germany
    In most cases(I know of), interfaces with static or dynamic dispatch are used.
    To illustrate this, here is bit of Java/C++ mix like pseudo code

    Solution #1, using dynamic dispatch:
    Code:
    interface Renderer {
    RenderThis()
    RenderThat()
    }
    
    class RendererDirectX implements Renderer{
    ...
    }
    
    class RendererOpenGL implements Renderer{
    ...
    }
    
    if(options.renderer == "directx"){
    renderer = new RendererDirectX();
    }else{
    renderer = new RendererOpenGL();
    }
    
    Solution #2, using static dispatch(which is more performant, but the destination needs to known at compile time):
    Code:
    class Renderer {
    #ifdef PLATFORM_WII
    Renderer rendererImpl = new RendererWii();
    #else
    Renderer rendererImpl = new RendererOpenGL();
    #endif
    
    RenderThis() {
    rendererImpl.renderThis();
    }
    
    RenderThat() {
    rendererImpl.renderThat();
    }
    }
    
    
    If you are more familiar with C++, a interface is basically a class containing only pure virtual methods
     
    StackMasher likes this.
  3. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    102
    49
    Nov 29, 2016
    awesome thanks