Ozzy_MetaMath.cs, or, Baby's First Metaprogram

Oswald Hurlem  —  3 months, 3 weeks ago [Edited 1 week, 4 days later]
So, one of the things I have to do with my game is change some of the types so that global positions in the game are expressed as double rather than single floating points. If I express positions only using floating points, I can maintain millimeter precision only out to 5 miles from (0,0,0). And that ain't something I'm okay with.

I didn't want to rewrite a bunch of code, I wanted to keep nice naming conventions, and I didn't want to resort to using C++ templates (I've always thought they were goofy). Casey has mentioned the advantages of metaprogramming so I decided to give it a shot.

I used C# since it's a language that comes much more naturally to me than C. C# and .NET have lots of nice string manipulation features so this was a breeze. This was my first time doing metaprogramming, and I followed Snuffleupagus-oriented programming to an extreme. The result is code that does what I need it to do. I am not sure how useful it will be to the the rest of Handmade Network or the public at large.

Because this only runs at build-time, I did not take performance into account. This code has such an absurd number of needless string allocations and initializes so many iterators, collections, functors, and other doodads through its sinister seductive syntactic sugar that I must give the more dogmatic members of the handmade community the Highest Trigger Warning Imaginable.

For me this is pretty cool. It does what I need it to do, and I can generate code that would have been insane to write by hand or via macro lunacy. Compiling the code with csc.exe takes too long, but running it using LinqPad takes 0.8 seconds, which, well, I'm OK with it. I plan to keep adding to this file as I continue development, periodically taking time to tidy up the API. I to add rectangle and matrix types, as well as some sort of thing for generating SIMD math.

You can see the vector types file I generated here:

If you believe that this sort of thing could potentially be helpful for your own projects, even with the caveat that you want it to have more features, do something differently, or be made in a different language, please let me know. As much as I can, I try to do work that will have a large positive effect.
#8780 Andrew Chronister  —  3 months, 3 weeks ago
Interesting stuff! I'm kind of a on-again off-again C# user -- the language is pretty nice, and garbage collection aside you can get fairly down and dirty with it despite its JITiness. And linux support has been pretty stellar recently, too.

I'm going to write a blog post about it soon, but when I approached metaprogramming for my twitter lib, I decided to stick with C and use printf/fprintf as my output mechanism. This, combined with the lesser known function open_memstream, makes it a bit easier to manage the string manipulation in C (open_memstream allows you to fprintf to an in-memory buffer, controlled by the same mechanism that all the C FILE* functions use internally).
Log in to comment