Mafi:

https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/

What do you think? Any remarks?

The SDL_Event is a union. Accessing it is inherently unsafe for type consistency and memory safety. The SDL library mitigates this problem by adding a tag (the member type) which encodes which union-member is to be used.<

In Rust you use the built-in tagged unions and call it a day...



switch(polledEvent.type) {
    mixin(caseOnEvent!("SDL_QUIT", "quit"));
    mixin(caseOnEvent!("SDL_ACTIVEEVEENT", "active"));
    mixin(caseOnEvent!("SDL_KEYDOWN", "eventKeyDown")); // (*)
    mixin(caseOnEvent!("SDL_KEYUP", "eventKeyUp")); // (*)
    mixin(caseOnEvent!("SDL_MOUSEMOTION", "motion"));
mixin(caseOnEvent!("SDL_MOUSEBUTTONUP", "eventMouseButtonUp")); // (*) mixin(caseOnEvent!("SDL_MOUSEBUTTONDOWN", "eventMouseButtonDown")); // (*)
    mixin(caseOnEvent!("SDL_JOYAXISMOTION", "jaxis"));
    mixin(caseOnEvent!("SDL_JOYBALLMOTION", "jball"));
    mixin(caseOnEvent!("SDL_JOYHATMOTION", "jhat"));
mixin(caseOnEvent!("SDL_JOYBUTTONDOWN", "eventJoyButtonDown")); // (*) mixin(caseOnEvent!("SDL_JOYBUTTONUP", "eventJoyButtonUp")); // (*)
    mixin(caseOnEvent!("SDL_USEREVENT", "user"));
    mixin(caseOnEvent!("SDL_SYSWMEVENT", "syswm"));
default: //has to be there even if empty
    static if(is(typeof(that.onOther(Event.init)))) {
        that.onOther(polledEvent); break;
    }
}

The default should be aligned just like the other cases. Often is a good idea to use "final switch" with enumerations. Probably there are ways to make that code more dry, using a TypeTuple of pairs like ("SDL_QUIT", "quit").

Bye,
bearophile

Reply via email to