Here is what I just did... I wanted to write a C# extension method that could return the size, in bits, of a DXGI Format (using SlimDX's "Format" type). It would've been easy to do a case/switch style method with a big, monolithic structure. But that's a bad idea from a performance standpoint; especially if some code needs to read the size of any arbitrary format every frame. That's a bunch of wasted time. Instead, the optimal way to do it would be to create an array of sizes which can simply be indexed with the Format value casted to type int. Here's what I mean:
int size = _sizesOfAllFormat[ (int) format ];
That would be FAR faster than a huge, monolithic case/switch operation to run through every possible format.
However, this presented me with a problem. Going through, one by one, all of the DXGI Formats and hand-typing in the sizes. There's like 100 of them (including Format.Unknown), so you can imagine how much of a pain in the *** that would be, as it MUST be done in perfect order for this to work. And there was simply no way I was going to waste the better part of a day hand-typing in all of that crap when I could be working on my engine!
So I decided to let C# do the dirty work for me, once again. A quick Google search yielded a C# file from SharpDX's repository in which all DXGI Formats were listed with their sizes in bits. But they were listed like this:
Format1
Format2
Format3
16
Format4
Format5
32
So I simply used Find+Replace to turn this into one of those monolithic case/switch structures that returned the size in bits of any format passed as a parameter. The method had the following signature:
static int GetSizeInBits(Format format)
{
// The huge switch went here...
}
Now to actually generate the desired code we want: an int[] array containing all of the sizes, in order. Here's the code I wrote to do it:
[source lang="csharp"]
static string sizes = "int[] _formatSizes =\n{\n";
static void Main(string[] args)
{
Format[] allformats = (Format[])Enum.GetValues(typeof(Format));
int item = 0;
foreach (var format in allformats)
{
int size = GetSizeInBits(format);
sizes += string.Format("{0}, ", size.ToString());
if (item == 10)
{
sizes += '\n';
item = 0;
}
}
sizes += "\n};";
System.IO.File.WriteAllText(@"C:\Users\ATC\Desktop\FORMAT_ARRAY.txt", sizes);
}
[/source]
...hit "Debug", the Console flashed up and closed and my text file with my code was spit right out in a fraction of a second! It was formatted a little sloppy, but it was easy to correct within a few seconds. :-)
Just to make sure the code was generated correctly before copy+pasting it into my engine code base I ran the following test:
[source lang="csharp"]static void Main(string[] args)
{
Format[] allformats = (Format[])Enum.GetValues(typeof(Format));
foreach (var format in allformats)
{
int s1 = GetSizeInBits(format);
if (s1 != _formatSizes[(int)format])
Console.WriteLine("God damn it...");
}
}[/source]
I ran the code and the Console did not say "God damn it..." so apparently all went well lol.
![biggrin.png](http://public.gamedev.net//public/style_emoticons/default/biggrin.png)
Now, I have this handy extension method for my engine which you might even find a use for in your own SlimDX-related projects:
[source lang="csharp"]
static int[] _formatSizes =
{
-1, 128, 128, 128, 128, 96, 96, 96, 96, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 8, 8, 8, 8, 8, 8, 1, 32, 32, 32, 4, 4, 4, 8,
8, 8, 8, 8, 8, 4, 4, 4, 8, 8, 8, 16, 16, 32, 32,
32, 32, 32, 32, 32, 8, 8, 8, 8, 8, 8
};
public static int GetSize(this Format format)
{
return _formatSizes[(int)format];
}
[/source]
But I was wondering, how many of you often find yourselves using your programming skills to do your dirty work for you, be it homework, text formatting, etc? What's one of the funniest or most clever things you've done with your programming skills?
For me it would have to be using my programming skills to get one of the most beautiful girlfriends a man could ask for (now an ex-girlfriend, thank God, since she was crazy lol). I actually wrote an application with some GDI+ graphics that displayed bits of a "love letter" and eventually asked her out lol. She loved it because she said no guy had ever put that much thought towards asking her out and she thought I was like the smartest guy ever lol...