Thread overview
[hackathon] An article about metaprogramming
Apr 29, 2015
Mafi
Apr 29, 2015
bearophile
Apr 29, 2015
Mafi
Apr 30, 2015
Idan Arye
Apr 29, 2015
weaselcat
April 29, 2015
Hello there,

I took the occasion of the D hackathon to finally write a technical article. I am programming games as a hobby and find the D programming language perfectly suited for this task. So I thought I could write an article about how I use D's capabilities to program games efficiently.

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

What do you think? Any remarks?

Mafi
April 29, 2015
On 4/29/15 8:26 AM, Mafi wrote:
> Hello there,
>
> I took the occasion of the D hackathon to finally write a technical
> article. I am programming games as a hobby and find the D programming
> language perfectly suited for this task. So I thought I could write an
> article about how I use D's capabilities to program games efficiently.
>
> So here it is:
> https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/
>
> What do you think? Any remarks?
>
> Mafi

https://www.reddit.com/r/programming/comments/34ah1q/using_d_templates_for_game_development/

https://news.ycombinator.com/newest

https://twitter.com/D_Programming/status/593450243942940672

https://www.facebook.com/dlang.org/posts/1059784987368515


Andrei

April 29, 2015
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
April 29, 2015
On Wednesday, 29 April 2015 at 16:55:58 UTC, bearophile wrote:
> 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...

Well, it is an already existing C library I am only adapting. And additionally the shonwn template encapsulates the whole outer loop as well.

>
>[...]
>
> 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

I cannot use a final switch there because it is not enumerated type. The values are just constants. Moreover I want to skip unneeded labels and instead unify them into the default branch. This is impossible with a final switch.
April 29, 2015
On Wednesday, 29 April 2015 at 15:26:58 UTC, Mafi wrote:
> Hello there,
>
> I took the occasion of the D hackathon to finally write a technical article. I am programming games as a hobby and find the D programming language perfectly suited for this task. So I thought I could write an article about how I use D's capabilities to program games efficiently.
>
> So here it is: https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/
>
> What do you think? Any remarks?
>
> Mafi

I think the article highlights the issue with the current std.variant implementation, lack of pattern matching/destructuring in D, and need for better tuple syntax.
Nice article
April 30, 2015
On Wednesday, 29 April 2015 at 18:13:10 UTC, Mafi wrote:
> On Wednesday, 29 April 2015 at 16:55:58 UTC, bearophile wrote:
>> 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...
>
> Well, it is an already existing C library I am only adapting. And additionally the shonwn template encapsulates the whole outer loop as well.
>
>>
>>[...]
>>
>> 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
>
> I cannot use a final switch there because it is not enumerated type. The values are just constants. Moreover I want to skip unneeded labels and instead unify them into the default branch. This is impossible with a final switch.

If you invest some effort in redefining the event types as D structs and the event codes as a D enum, you can get rid of that routing-list construct altogether: http://dpaste.dzfl.pl/1e992d248355